BrightSide Workbench Full Report + Source Code
AllianceContact.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.alliance.contact;
20 
21 import java.io.Serializable;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Locale;
25 import java.util.Objects;
26 import java.util.Optional;
27 import org.turro.string.Strings;
28 import org.turro.alliance.db.AlliancePU;
29 import org.turro.alliance.db.entities.AxContact;
30 import org.turro.alliance.db.entities.ProcedenceId;
31 import org.turro.annotation.ElephantContact;
32 import org.turro.collections.KeyValueMap;
33 import org.turro.elephant.context.Application;
34 import org.turro.elephant.security.IUser;
35 import org.turro.entities.Faces;
36 import org.turro.plugin.contacts.IContact;
37 import org.turro.plugin.contacts.IProfile;
38 import org.turro.plugin.contacts.IRelations;
39 import org.turro.security.ContactPermissions;
40 import org.turro.security.FlatPermissions;
41 import org.turro.sql.SqlClause;
42 import org.turro.util.CompareUtil;
43 
48 @ElephantContact(priority=2)
49 public class AllianceContact implements IContact, IUser, Comparable<IContact> {
50 
51  private AxContact contact, business;
52 
53  public AllianceContact() {
54  }
55 
56  public AllianceContact(AxContact contact) {
57  init(contact);
58  }
59 
60  @Override
61  public void setContact(Object contact) {
62  if(contact instanceof AxContact) {
63  init((AxContact) contact);
64  }
65  }
66 
67  @Override
68  public Object getContact() {
69  return contact;
70  }
71 
72  @Override
73  public String getId() {
74  return contact.getContactId().stringify();
75  }
76 
77  @Override
78  public String getGlobalId() {
79  return null;
80  }
81 
82  @Override
83  public String getGrouping() {
84  return null;
85  }
86 
87  @Override
88  public String getDeepGrouping() {
89  return null;
90  }
91 
92  @Override
93  public String getLogin() {
94  return contact.getEmail();
95  }
96 
97  @Override
98  public String getName() {
99  return contact.getName();
100  }
101 
102  @Override
103  public String getFriendly() {
104  return contact.getName();
105  }
106 
107  @Override
108  public String getFormal() {
109  return contact.getName();
110  }
111 
112  @Override
113  public String getFullName() {
114  return contact.getName();
115  }
116 
117  @Override
118  public String getTradeName() {
119  return null;
120  }
121 
122  @Override
123  public String getEmail() {
124  return contact.getEmail();
125  }
126 
127  @Override
128  public Locale getLocale() {
129  return null;
130  }
131 
132  @Override
133  public String getConnector(String id) {
134  return IUser.CONNECTOR_EMAIL.equals(id) ? getEmail() : null;
135  }
136 
137  @Override
138  public void setConnector(String id, String value) {
139  // do nothing
140  }
141 
142  @Override
143  public List<String> getConnectorList(String id) {
144  return null;
145  }
146 
147  /* Relations */
148 
149  private IRelations _companies, _staff;
150 
151  @Override
153  if(_companies == null) _companies = Relations.companies(this);
154  return _companies;
155  }
156 
157  @Override
159  if(_staff == null) _staff = Relations.staff(this);
160  return _staff;
161  }
162 
163  @Override
164  public List<IContact> getRelations(String id) {
165  return null;
166  }
167 
168  @Override
169  public List<IContact> getRelations() {
170  return null;
171  }
172 
173  @Override
175  return business != null ? new AllianceContact(business) : null;
176  }
177 
178  @Override
179  public List<IContact> getBusinessList() {
180  return business != null ? List.of(getBusiness()) : null;
181  }
182 
183  @Override
184  public List<IContact> getCoworkers() {
185  return null;
186  }
187 
188  @Override
189  public List<IContact> getCoworkers(IContact business) {
190  return null;
191  }
192 
193  @Override
194  public boolean isInBusiness(IContact worker) {
195  return false;
196  }
197 
198  @Override
199  public void setName(String value) {
200  // do nothing
201  }
202 
203  @Override
204  public void setPassword(String newValue, String repeatValue) {
205  // do nothing
206  }
207 
208  @Override
209  public void applyChanges() {
210  // do nothing
211  }
212 
213  @Override
214  public boolean isValid() {
215  return contact != null;
216  }
217 
218  @Override
219  public boolean checkPassword(String password) {
220  throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
221  }
222 
223  @Override
224  public String getCommentValue(String key) {
225  return null;
226  }
227 
228  @Override
229  public Object getObject(String key) {
230  return null;
231  }
232 
233  @Override
234  public void setObject(String key, Serializable value) {
235  // do nothing
236  }
237 
238  @Override
239  public boolean isWebUser() {
240  return true;
241  }
242 
243  @Override
244  public boolean isWebapp() {
245  return false;
246  }
247 
248  @Override
249  public boolean isUserMenu() {
250  return false;
251  }
252 
253  @Override
254  public boolean isDeactivated() {
255  return false;
256  }
257 
258  @Override
259  public List<String> getSyndications() {
260  return Collections.EMPTY_LIST;
261  }
262 
263  private FlatPermissions _permissions = null;
264 
265  @Override
267  if(_permissions == null) {
268  _permissions = new ContactPermissions(this);
269  }
270  return _permissions;
271  }
272 
273  @Override
274  public void resetPermissions() {
275  // do nothing
276  }
277 
278  @Override
279  public IUser getAsUser() {
280  return this;
281  }
282 
283  @Override
284  public boolean acceptsLang(String lang) {
285  return true;
286  }
287 
288  @Override
289  public Object loadById(String id) {
290  ProcedenceId contactId = ProcedenceId.from(id);
291  if(contactId != null) {
292  AxContact c = new AlliancePU().find(AxContact.class, contactId);
293  if(c != null) {
294  init(c);
295  }
296  }
297  return this;
298  }
299 
300  @Override
301  public Object loadByLogin(String login) {
302  init(null);
303  return null;
304  }
305 
306  @Override
307  public Object loadLogged(Application app) {
308  IUser user = app.getConstructor().getUser();
309  init((AxContact) ((IContact) user).getContact());
310  return user;
311  }
312 
313  @Override
314  public List<IContact> loadByPartialName(String name) {
315  return null;
316  }
317 
318  @Override
319  public List<IContact> loadByPartialName(String name, KeyValueMap kvm) {
320  return null;
321  }
322 
323  @Override
324  public IContact loadByName(String name) {
325  return null;
326  }
327 
328  @Override
329  public IContact loadByEmail(String email) {
330  return null;
331  }
332 
333  @Override
334  public void setAttribute(String key, Object value) {
335  // do nothing
336  }
337 
338  @Override
339  public Object getAttribute(String key) {
340  return null;
341  }
342 
343  @Override
344  public List<IContact> getBySyndication(String syndication) {
345  return null;
346  }
347 
348  @Override
349  public List<IContact> getByRole(String role) {
350  return null;
351  }
352 
353  @Override
354  public List<IContact> getByGrouping(String grouping) {
355  return null;
356  }
357 
358  @Override
359  public List<IContact> getByGrouping(List<String> groupings) {
360  return null;
361  }
362 
363  @Override
364  public List<IContact> getCompanies() {
365  return SqlClause.select("distinct c.contactId.memberId, c.company, c.companyFace").from("AxContact c")
366  .dao(new AlliancePU())
367  .resultList(Object[].class)
368  .stream()
369  .map(c -> (IContact) new AllianceContact(createBusiness((long) c[0], (String) c[1], (String) c[2])))
370  .toList();
371  }
372 
373  @Override
374  public List<IContact> getCenters() {
375  return Collections.EMPTY_LIST;
376  }
377 
378  @Override
379  public List<IContact> getHHRR() {
380  return Collections.EMPTY_LIST;
381  }
382 
383  @Override
384  public List<IContact> getStudents() {
385  return Collections.EMPTY_LIST;
386  }
387 
388  @Override
389  public void startContactFromValues(KeyValueMap values) {
390  // do nothing
391  }
392 
393  @Override
394  public void syndicate(String syndicationName, boolean syndicate) {
395  // do nothing
396  }
397 
398  @Override
399  public String getFace() {
400  return contact.getFace();
401  }
402 
403  @Override
404  public Faces getFaces() {
405  return null;
406  }
407 
408  @Override
409  public IProfile getProfile() {
410  return new Profile(contact);
411  }
412 
413  @Override
414  public boolean isAdmin() {
415  return false;
416  }
417 
418  @Override
419  public boolean isUser() {
420  return true;
421  }
422 
423  @Override
424  public boolean isCompany() {
425  return false;
426  }
427 
428  @Override
429  public boolean isCenter() {
430  return false;
431  }
432 
433  @Override
434  public boolean isInternal() {
435  return false;
436  }
437 
438  @Override
439  public boolean isWorker() {
440  return false;
441  }
442 
443  @Override
444  public boolean isStudent() {
445  return false;
446  }
447 
448  @Override
449  public boolean isResponsible() {
450  return false;
451  }
452 
453  @Override
454  public boolean isHHRR() {
455  return false;
456  }
457 
458  @Override
459  public boolean isWorker(IContact business) {
460  return false;
461  }
462 
463  @Override
464  public boolean isStudent(IContact business) {
465  return false;
466  }
467 
468  @Override
469  public boolean isResponsible(IContact business) {
470  return false;
471  }
472 
473  @Override
474  public boolean isHHRR(IContact business) {
475  return false;
476  }
477 
478  @Override
479  public boolean isInNetworking() {
480  return false;
481  }
482 
483  @Override
484  public boolean isInPremiumNetworking() {
485  return false;
486  }
487 
488  @Override
489  public String getLangTranslator() {
490  return null;
491  }
492 
493  @Override
494  public IContact getLogged() {
495  return null;
496  }
497 
498  @Override
499  public boolean validate(String login, String password) {
500  return false;
501  }
502 
503  @Override
504  public boolean validate2(String password2) {
505  return false;
506  }
507 
508  @Override
509  public boolean impersonate(String login) {
510  return false;
511  }
512 
513  @Override
514  public boolean impersonateByEmail(String email) {
515  return impersonate(email);
516  }
517 
518  @Override
519  public void reload() {
520  loadById(getId());
521  if(isValid()) {
522  resetPermissions();
523  }
524  }
525 
526  @Override
527  public boolean isThisUser(String id) {
528  return contact.getContactId().getEntityId().equals(id);
529  }
530 
531  @Override
532  public String getProperty(String key) {
533  return null;
534  }
535 
536  @Override
537  public boolean isInRole(String role) {
538  return false;
539  }
540 
541  @Override
542  public boolean hasAnyRoleKey(String role) {
543  return false;
544  }
545 
546  @Override
547  public boolean isOutsider() {
548  return true;
549  }
550 
551  @Override
552  public boolean accepts(Object object) {
553  if(object instanceof String) {
554  return ProcedenceId.from((String) object) != null;
555  } else {
556  return object instanceof AxContact;
557  }
558  }
559 
560  @Override
561  public int compareTo(IContact o) {
562  return CompareUtil.compare(getId(), o.getId());
563  }
564 
565  private void init(AxContact contact) {
566  this.contact = contact;
567  this.business = createBusiness(contact);
568  }
569 
570  private AxContact createBusiness(AxContact contact) {
571  return contact == null ? null : createBusiness(contact.getContactId().getMemberId(), contact.getCompany(), contact.getCompanyFace());
572  }
573 
574  private AxContact createBusiness(long memberId, String name, String face) {
575  if(!Strings.isBlank(name)) {
576  AxContact b = new AxContact();
577  ProcedenceId id = new ProcedenceId();
578  id.setEntityId(name + "#c");
579  id.setMemberId(memberId);
580  b.setContactId(id);
581  b.setName(name);
582  b.setFace(face);
583  return b;
584  }
585  return null;
586  }
587 
588  @Override
589  public int hashCode() {
590  int hash = 5;
591  hash = 13 * hash + Objects.hashCode(this.contact);
592  return hash;
593  }
594 
595  @Override
596  public boolean equals(Object obj) {
597  if (this == obj) {
598  return true;
599  }
600  if (obj == null) {
601  return false;
602  }
603  if (getClass() != obj.getClass()) {
604  return false;
605  }
606  final AllianceContact other = (AllianceContact) obj;
607  return Objects.equals(this.contact, other.contact);
608  }
609 
610 }
List< IContact > getCoworkers(IContact business)
boolean validate(String login, String password)
List< IContact > loadByPartialName(String name, KeyValueMap kvm)
List< IContact > getByGrouping(String grouping)
void syndicate(String syndicationName, boolean syndicate)
List< IContact > loadByPartialName(String name)
void setObject(String key, Serializable value)
void setPassword(String newValue, String repeatValue)
void setConnector(String id, String value)
List< IContact > getByGrouping(List< String > groupings)
void setAttribute(String key, Object value)
List< IContact > getByRole(String role)
List< IContact > getBySyndication(String syndication)
static ProcedenceId from(String id)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27