BrightSide Workbench Full Report + Source Code
BrightSide/contacts/src/main/java/org/turro/contacts/profile/Profile.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.contacts.profile;
20 
21 import java.util.List;
22 import java.util.Set;
23 import java.util.function.Predicate;
24 import java.util.stream.Collectors;
25 import org.turro.string.Strings;
26 import org.turro.action.Actions;
27 import org.turro.action.Contacts;
28 import org.turro.assistant.ParticipationInfo;
29 import org.turro.auth.Authentication;
30 import org.turro.contacts.BusinessRelation;
31 import org.turro.contacts.Contact;
32 import org.turro.contacts.ContactService;
33 import org.turro.contacts.JobBoard;
34 import org.turro.contacts.db.ContactsPU;
35 import org.turro.contacts.relation.Business;
36 import org.turro.contacts.relation.Relations;
37 import org.turro.elephant.context.Application;
38 import org.turro.elephant.context.IConstructor;
39 import org.turro.elephant.entities.db.Skill;
40 import org.turro.elephant.entities.db.SkillType;
41 import org.turro.elephant.impl.repository.RepositoryFile;
42 import org.turro.elephant.impl.util.Files;
43 import org.turro.elephant.security.IUser;
44 import org.turro.elephant.util.Images;
45 import org.turro.entities.Controls;
46 import org.turro.entities.Entities;
47 import org.turro.entities.IElephantEntity;
48 import org.turro.file.util.FileAttach;
49 import org.turro.participation.ParticipationReason;
50 import org.turro.plugin.contacts.IContact;
51 import org.turro.plugin.contacts.IContactRelation;
52 import org.turro.plugin.contacts.IProfile;
53 import org.turro.skills.Skills;
54 
59 public class Profile implements IProfile {
60 
61  public final static String PHONE = "PhoneProfile",
62  WEB = "Web",
63  NETWORK = "NetworkProfile";
64 
65  private final IContact contact;
66  private final Business business;
67  private final List<ProfileRelation> companies, learningCenters;
68  private final ProfilePolicies policies;
69  private final PublishPolicy policy;
70  private final List<Skill> knowledge, functional, attitude, sector, specialty;
71 
72  private boolean edit = false;
73 
74  public Profile(Contact contact) {
75  this(contact, null);
76  }
77 
78  public Profile(Contact contact, boolean edit) {
79  this(contact, null);
80  this.edit = edit;
81  }
82 
83  public Profile(Contact contact, PublishPolicy policy) {
84  this.contact = Contacts.getContact(contact);
85  this.policies = new ProfilePolicies(contact);
86  this.policy = policy == null ? takeRequiredFromLogged() : policy;
87  this.business = contact.getBusinessSet();
88  this.companies = loadCompanies();
89  this.learningCenters = loadLearningCenters();
90  this.knowledge = loadKnowledge();
91  this.functional = loadFunctional();
92  this.attitude = loadAttitude();
93  this.sector = loadSector();
94  this.specialty = loadSpecialty();
95  }
96 
97  @Override
98  public IContact getContact() {
99  return contact;
100  }
101 
102  @Override
103  public boolean getHasImage() {
104  return Files.exists(contact.getFace());
105  }
106 
107  @Override
108  public String getImage() {
109  return Images.getImageOrNull(contact.getFace());
110  }
111 
112  @Override
113  public String getImageOrDefault() {
114  return Images.getImageOrDefault(contact.getFace());
115  }
116 
117  @Override
118  public String getEntityPath() {
119  return ContactsPU.getObjectPath(contact.getContact());
120  }
121 
122  @Override
123  public boolean getHasName() {
124  return !Strings.isBlank(getFinalName());
125  }
126 
127  @Override
128  public String getName() {
129  return contact.getName();
130  }
131 
132  @Override
133  public String getTradeName() {
134  return contact.getConnector(IUser.CONNECTOR_TRADE);
135  }
136 
137  @Override
138  public String getFinalName() {
139  return Strings.isBlank(getTradeName(), getName());
140  }
141 
142  @Override
143  public boolean getHasEmail() {
144  return !Strings.isBlank(getEmail()) && (isBusiness() || policies.getPolicy(null, "email").allows(policy));
145  }
146 
147  @Override
148  public String getEmail() {
149  return contact.getEmail();
150  }
151 
152  @Override
153  public boolean getHasWeb() {
154  return !Strings.isBlank(getWeb()) && (isBusiness() || ProfileContext.getWebOnUsers());
155  }
156 
157  @Override
158  public String getWeb() {
159  return contact.getConnector(WEB);
160  }
161 
162  @Override
163  public boolean getHasPhone() {
164  return !Strings.isBlank(getPhone()) && (isBusiness() || policies.getPolicy(null, "phone").allows(policy));
165  }
166 
167  @Override
168  public String getPhone() {
169  return contact.getConnector(PHONE);
170  }
171 
172  @Override
173  public boolean getHasNetwork() {
174  return !Strings.isBlank(getNetwork());
175  }
176 
177  @Override
178  public String getNetwork() {
179  return contact.getConnector(NETWORK);
180  }
181 
182  @Override
183  public boolean getHasAbout() {
184  return !Strings.isBlank(getAbout());
185  }
186 
187  @Override
188  public String getAbout() {
189  return contact.getCommentValue("#cv");
190  }
191 
192  private RepositoryFile publicDocs;
193 
194  public boolean getHasDocuments() {
195  if(publicDocs == null) {
197  FileAttach files = (FileAttach) iee.getControl(Controls.CTRL_FILES, iee.getPath(), contact);
198  if(files != null) publicDocs = files.getPublishableRepository(Application.getApplication().getConstructor()).getRoot("/public");
199  }
200  return publicDocs != null && publicDocs.hasContent();
201  }
202 
204  return getHasDocuments() ? publicDocs : null;
205  }
206 
207  public boolean getHasServices() {
208  return !getServices().isEmpty();
209  }
210 
211  public Set<ContactService> getServices() {
212  return ((Contact) contact.getContact()).getServices();
213  }
214 
215  public List<ProfileRelation> getCurrentCompanies() {
216  return getCompanies().stream().filter(r -> r.isInRange()).collect(Collectors.toList());
217  }
218 
219  public List<ProfileRelation> getPastCompanies() {
220  return getCompanies().stream().filter(r -> !r.isInRange()).collect(Collectors.toList());
221  }
222 
223  public List<ProfileRelation> getCompanies() {
224  return isEdit() ? companies : companies.stream().filter(r -> r.isValidated()).collect(Collectors.toList());
225  }
226 
227  public List<ProfileRelation> getCurrentCenters() {
228  return getLearningCenters().stream().filter(r -> r.isInRange()).collect(Collectors.toList());
229  }
230 
231  public List<ProfileRelation> getPastCenters() {
232  return getLearningCenters().stream().filter(r -> !r.isInRange()).collect(Collectors.toList());
233  }
234 
235  public List<ProfileRelation> getLearningCenters() {
236  return isEdit() ? learningCenters : learningCenters.stream().filter(r -> r.isValidated()).collect(Collectors.toList());
237  }
238 
239  public boolean getHasSkills() {
240  return isNatural() &&
241  (!knowledge.isEmpty() || !functional.isEmpty() || !attitude.isEmpty()) &&
242  policies.getPolicy(null, "skills").allows(policy);
243  }
244 
245  public List<Skill> getKnowledge() {
246  return knowledge;
247  }
248 
249  public List<Skill> getAttitude() {
250  return attitude;
251  }
252 
253  public List<Skill> getFunctional() {
254  return functional;
255  }
256 
257  public boolean getHasCSkills() {
258  return isJuridical() && (!sector.isEmpty() || !specialty.isEmpty());
259  }
260 
261  public List<Skill> getSector() {
262  return sector;
263  }
264 
265  public List<Skill> getSpecialty() {
266  return specialty;
267  }
268 
269  public boolean getHasJobBoard() {
270  Contact c = (Contact) contact.getContact();
271  return c.getJobBoard() != null && isStudent();
272  }
273 
275  Contact c = (Contact) contact.getContact();
276  return c.getJobBoard();
277  }
278 
279  public PublishPolicy getPolicy(Object entity, String element) {
280  return policies.getPolicy(entity, element);
281  }
282 
283  @Override
284  public boolean isEdit() {
285  return edit;
286  }
287 
288  @Override
289  public boolean canBeProfessional() {
290  return ProfileContext.hasCompanies() && !hasCurrentRelations();
291  }
292 
293  @Override
294  public boolean canBeStudent() {
295  return ProfileContext.hasCenters() && !hasCurrentRelations();
296  }
297 
298  @Override
299  public boolean canFollow() {
300  return !Authentication.isLogged(contact);
301  }
302 
303  public String getFollowCtrl(IConstructor constructor) {
304  return Entities.getController(contact.getContact()).parseFollowCtrl(constructor);
305  }
306 
307  public boolean canEdit() {
308  return isLoggedSelf() || isLogged(w -> w.isResponsible()) ||
309  (isJuridical() && isLoggedAdmin());
310  }
311 
312  public String linkEdit() {
313  return contact.isUser() ? "/user/profile" :
314  "/user/profile/editc?" + Actions.createRightNowAction("contact=" + contact.getId());
315  }
316 
317  @Override
318  public boolean isInNetworking() {
320  }
321 
322  @Override
323  public boolean isInPremiumNetworking() {
325  }
326 
327  @Override
328  public boolean isWorker() {
329  return !getCurrentCompanies().isEmpty();
330  }
331 
332  @Override
333  public boolean isStudent() {
334  return !getCurrentCenters().isEmpty();
335  }
336 
337  @Override
338  public boolean isResponsible() {
339  return getCurrentCenters().stream().anyMatch(r -> r.getRelation().isResponsible());
340  }
341 
342  @Override
343  public boolean isCompanyHHRR() {
344  return getCurrentCompanies().stream().anyMatch(r -> r.getRelation().isHHRR());
345  }
346 
347  @Override
348  public boolean isCenterHHRR() {
349  return getCurrentCenters().stream().anyMatch(r -> r.getRelation().isHHRR());
350  }
351 
352  @Override
353  public boolean isHHRR() {
354  return isCompanyHHRR() || isCenterHHRR();
355  }
356 
357  public boolean isNatural() {
358  return !isJuridical();
359  }
360 
361  public boolean isJuridical() {
362  return contact.isCompany() || contact.isCenter();
363  }
364 
365  @Override
366  public boolean isCompany() {
367  return contact.isCompany();
368  }
369 
370  @Override
371  public boolean isCenter() {
372  return contact.isCenter();
373  }
374 
375  @Override
376  public boolean isSingleton() {
377  return getCurrentCompanies().isEmpty() && getCurrentCenters().isEmpty();
378  }
379 
380  @Override
381  public boolean isTranslator() {
382  return !Strings.isBlank(contact.getLangTranslator());
383  }
384 
385  public boolean isLoggedSelf() {
386  return Authentication.isLogged(contact);
387  }
388 
389  public boolean isLogged(Predicate<BusinessRelation> predicate) {
390  return Authentication.hasContact() &&
391  ((Contact) contact.getContact()).getWorkerSet()
392  .isByCondition((Contact) Authentication.getIContact().getContact(), predicate);
393  }
394 
395  @Override
396  public boolean isShowCompletion() {
398  }
399 
401  return new ProfileCompletion(this);
402  }
403 
404  private boolean hasCurrentRelations() {
405  return !(getCurrentCompanies().isEmpty() && getCurrentCenters().isEmpty());
406  }
407 
408  private List<ProfileRelation> loadCompanies() {
409  return business.stream().filter(r -> r.isWorker())
410  .map(r -> new ProfileRelation(r, policies, policy)).collect(Collectors.toList());
411  }
412 
413  private List<ProfileRelation> loadLearningCenters() {
414  return business.stream().filter(r -> r.isStudent())
415  .map(r -> new ProfileRelation(r, policies, policy)).collect(Collectors.toList());
416  }
417 
418  private List<Skill> loadKnowledge() {
419  return Skills.getSkillsFrom(getEntityPath(), SkillType.SKILL_KNOWLEDGE);
420  }
421 
422  private List<Skill> loadAttitude() {
423  return Skills.getSkillsFrom(getEntityPath(), SkillType.SKILL_ATTITUDE);
424  }
425 
426  private List<Skill> loadFunctional() {
427  return Skills.getSkillsFrom(getEntityPath(), SkillType.SKILL_FUNCTIONAL);
428  }
429 
430  private List<Skill> loadSector() {
431  return Skills.getSkillsFrom(getEntityPath(), SkillType.SKILL_SECTOR);
432  }
433 
434  private List<Skill> loadSpecialty() {
435  return Skills.getSkillsFrom(getEntityPath(), SkillType.SKILL_SPECIALTY);
436  }
437 
438  private PublishPolicy takeRequiredFromLogged() {
439  IContact logged = Authentication.getIContact();
440  if(logged == null || !logged.isValid()) {
441  return PublishPolicy.PUBLISH_ALL;
442  } else if(logged.equals(contact)) {
443  return PublishPolicy.PUBLISH_NONE;
444  } else if(new ParticipationInfo(contact, logged, ParticipationReason.REASON_FOLLOW).isChecked()) {
445  return PublishPolicy.PUBLISH_FOLLOWING;
446  } else {
447  return PublishPolicy.PUBLISH_USERS;
448  }
449  }
450 
451  private boolean isBusiness() {
452  return contact.isCompany() || contact.isCenter();
453  }
454 
455  private boolean isLoggedAdmin() {
456  IContact logged = Authentication.getIContact();
457  return logged != null && logged.isValid() && logged.isAdmin();
458  }
459 
460  @Override
462  return Relations.companies(contact).getCompanyRelation();
463  }
464 
465 }
static String createRightNowAction(String values)
Definition: Actions.java:312
static IContact getContact(Object object)
Definition: Contacts.java:109
static boolean isLogged(IContact contact)
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
PublishPolicy getPolicy(Object obj, String element)
static boolean exists(String path)
Definition: Files.java:80
static String getImageOrNull(String image)
Definition: Images.java:60
static String getImageOrDefault(String image)
Definition: Images.java:52
static IElephantEntity getController(String path)
Definition: Entities.java:78
Repository getPublishableRepository(IConstructor constructor)
Definition: FileAttach.java:47
boolean allows(PublishPolicy policy)
static final String CONNECTOR_TRADE
Definition: IUser.java:28
String parseFollowCtrl(IConstructor constructor)
Object getControl(String name, IConstructor constructor, IContact contact)
String getCommentValue(String key)