18 package org.turro.contacts.util;
20 import java.io.Serializable;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Locale;
29 import java.util.Objects;
30 import java.util.Optional;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34 import org.turro.string.Strings;
35 import org.turro.action.Plugins;
36 import org.turro.actor.Actors;
37 import org.turro.annotation.ElephantContact;
38 import org.turro.auth.Authentication;
39 import org.turro.collections.KeyValueMap;
40 import org.turro.contacts.BusinessRelation;
41 import org.turro.contacts.Comment;
42 import org.turro.contacts.Connector;
43 import org.turro.contacts.Contact;
44 import org.turro.contacts.ContactType;
45 import org.turro.contacts.Syndication;
46 import org.turro.contacts.db.ContactsPU;
47 import org.turro.contacts.relation.FuzzyRelationTypes;
48 import org.turro.contacts.relation.Relations;
49 import org.turro.contacts.zul.menu.ContactMenu;
50 import org.turro.elephant.context.Application;
51 import org.turro.elephant.context.ElephantContext;
52 import org.turro.elephant.db.WhereClause;
53 import org.turro.elephant.security.IUser;
54 import org.turro.entities.ContactEntities;
55 import org.turro.entities.Faces;
56 import org.turro.file.util.FileAttach;
57 import org.turro.i18n.Locales;
58 import org.turro.jpa.Dao;
59 import org.turro.plugin.contacts.CompoundId;
60 import org.turro.plugin.contacts.IContact;
61 import org.turro.plugin.contacts.IProfile;
62 import org.turro.plugin.contacts.IRelations;
63 import org.turro.security.ContactPermissions;
64 import org.turro.security.FlatPermissions;
65 import org.turro.security.Permissions;
66 import org.turro.security.SocialGroups;
67 import org.turro.string.Words;
68 import org.turro.util.CompareUtil;
74 @ElephantContact(priority=1)
83 this.contact = (
Contact)
object;
87 this.contact = contact;
93 if(!Strings.isBlank(
id)) {
105 "select c from Contact as c where c.login = ? or c.id = ?",
106 new String[] { login, login }));
128 this.contact = (
Contact) contact;
130 _syndications =
null;
135 return contact !=
null ? contact.
getId() :
null;
145 return contact !=
null ? contact.
getGrouping() :
null;
151 while(business !=
null) {
162 return contact !=
null ? contact.
getLogin() :
null;
167 return contact !=
null ? contact.
getName().trim() :
"";
172 return contact !=
null ? contact.
getFriendly().trim() :
"";
177 return contact !=
null ? contact.
getFormal().trim() :
"";
182 return contact !=
null ? contact.
getFullName().trim() :
"";
187 return contact !=
null ? contact.
getTradeName().trim() :
"";
197 return contact !=
null ? contact.
getLocale() :
null;
202 if(contact !=
null) {
204 if(
id.equalsIgnoreCase(connector.getDescription())) {
205 return connector.getValue();
214 ArrayList<String> list =
new ArrayList<>();
215 if(contact !=
null) {
217 if(
id.equalsIgnoreCase(connector.getDescription())) {
218 list.add(connector.getValue());
243 List<IContact> list =
new ArrayList<>();
244 if(contact !=
null) {
246 if(
id.equalsIgnoreCase(relation.getDescription())) {
247 list.add(relation.getBusiness().getIContact());
256 List<IContact> list =
new ArrayList<>();
257 if(contact !=
null) {
259 list.add(relation.getBusiness().getIContact());
262 list.add(relation.getContact().getIContact());
270 if(contact !=
null) {
281 org.turro.plugin.contacts.ContactList list =
new org.turro.plugin.contacts.ContactList();
282 if(contact !=
null) {
284 if(!c.getId().equals(contact.
getId())) {
285 list.add(c.getIContact());
294 org.turro.plugin.contacts.ContactList list =
new org.turro.plugin.contacts.ContactList();
295 if(contact !=
null) {
297 if(!c.getId().equals(contact.
getId())) {
298 list.add(c.getIContact());
307 org.turro.plugin.contacts.ContactList list =
new org.turro.plugin.contacts.ContactList();
308 if(contact !=
null) {
310 if(!c.getId().equals(contact.
getId())) {
311 list.add(c.getIContact());
320 return getCoworkers().contains(worker);
325 if(contact !=
null) {
327 if(
id.equalsIgnoreCase(connector.getDescription())) {
328 connector.setValue(value);
344 if(contact !=
null) {
352 if(newValue !=
null && newValue.trim().length() > 0 && newValue.equals(repeatValue)) {
355 }
catch (Exception ex) {
364 new org.turro.contacts.form.ContactWrapper(contact).save();
365 }
catch (Exception ex) {
372 return contact !=
null;
379 }
catch (Exception ex) {
388 }
catch (Exception ex) {
398 if(!Strings.isBlank(c.getComment()) && c.getComment().startsWith(lang)) {
399 comment = c.getComment().substring(lang.length());
403 if(org.turro.string.Strings.isBlank(comment)) {
406 if(!Strings.isBlank(c.getComment()) && c.getComment().startsWith(lang)) {
407 comment = c.getComment().substring(lang.length());
432 return contact ==
null ? false : contact.
isWebapp();
437 return contact ==
null ? false : contact.
isUserMenu();
445 private ArrayList<String> _syndications =
null;
449 if(_syndications ==
null) {
450 _syndications =
new ArrayList<>();
452 _syndications.add(s.getName());
455 return _syndications;
462 if(_permissions ==
null) {
480 return Locales.checkLanguage(contact.
getLocale(), lang);
485 return loadByPartialName(name,
null);
490 ArrayList<IContact> list =
new ArrayList<IContact>();
491 if(name !=
null && name.trim().length() > 2) {
494 list.add(c.getIContact());
503 "select c from Contact as c where c.name = ?",
504 new String[] { name });
511 "select c.contact from Connector as c where c.description = ? and c.value = ?",
518 "select name from Contact contact " +
519 "where contact.id = ?",
538 ArrayList<IContact> list =
new ArrayList<>();
539 if(!Strings.isBlank(syndication)) {
540 if(
"hhrr".equals(syndication)) {
541 list.addAll(getHHRR());
542 }
else if(
"student".equals(syndication)) {
543 list.addAll(getStudents());
546 "select s.contact from Syndication as s where s.name = ?",
547 new String[] { syndication })) {
548 list.add(c.getIContact());
557 ArrayList<IContact> list =
new ArrayList<>();
558 if(!Strings.isBlank(role)) {
561 wc.
addClause(
"select distinct syndication.contact from Syndication as syndication");
562 wc.
addClause(
"where syndication.name in (:syndicationNames)");
565 list.add(c.getIContact());
573 return getByGrouping(Collections.singletonList(grouping));
578 ArrayList<IContact> list =
new ArrayList<>();
579 if(groupings !=
null && !groupings.isEmpty()) {
581 wc.
addClause(
"select contact from Contact as contact");
582 wc.
addClause(
"where contact.grouping in (:groupings)");
585 list.add(c.getIContact());
593 ArrayList<IContact> list =
new ArrayList<>();
595 wc.
addClause(
"select contact from Contact as contact");
596 wc.
addClause(
"where contact.type = :type");
599 list.add(c.getIContact());
606 ArrayList<IContact> list =
new ArrayList<>();
608 wc.
addClause(
"select contact from Contact as contact");
609 wc.
addClause(
"where contact.type = :type");
612 list.add(c.getIContact());
619 ArrayList<IContact> list =
new ArrayList<>();
621 wc.
addClause(
"select contact from Contact as contact");
622 wc.
addClause(
"left outer join contact.businessRelations relation");
625 list.add(c.getIContact());
632 ArrayList<IContact> list =
new ArrayList<>();
634 wc.
addClause(
"select contact from Contact as contact");
635 wc.
addClause(
"left outer join contact.businessRelations relation");
638 list.add(c.getIContact());
646 if(values.containsKey(
"name")) {
647 newContact.
setName(values.get(String.class,
"name"));
649 if(values.containsKey(
"email")) {
653 connector.
setValue(values.get(String.class,
"email"));
660 public void syndicate(String syndicationName,
boolean syndicate) {
667 setContact(dao.saveObject(contact));
674 String faceFile = fileAttach.
getPublicFile(
"/profile/face.png",
false);
675 return !Strings.isBlank(faceFile) ? faceFile :
null;
690 return getSyndications().contains(
"brightside_admin");
736 .isByCondition(contact, c -> c.isWorker(),
new Date());
742 .isByCondition(contact, c -> c.isStudent(),
new Date());
748 .isByCondition(contact, c -> c.isResponsible(),
new Date());
754 .isByCondition(contact, c -> c.isHHRR(),
new Date());
780 public boolean isActor(Object entity, String actor) {
798 if(
object instanceof String) {
801 return object instanceof
Contact;
807 if (!this.isValid()) {
811 hash = 97 * hash + Objects.hashCode(this.getId());
817 if (!this.isValid()) {
833 if (!Objects.equals(
this.getId(), other.
getId())) {
841 return CompareUtil.compare(getId(), o.
getId());
846 protected boolean superUser =
false;
849 public boolean validate(String login, String password) {
850 login = login.trim();
851 password = password.trim();
852 if(Strings.isBlank(login) || Strings.isBlank(password))
return false;
853 Map<String, Object> args =
new HashMap<>();
854 args.put(
"login", login);
855 args.put(
"password", password);
857 if(Boolean.TRUE.equals(args.get(
"su"))) {
862 if(login.contains(
"@")) {
863 ic = loadByEmail(login);
868 if(isValid() && checkPassword(password)) {
879 return checkPassword2(password2);
885 if(login.contains(
"@")) {
886 ic = loadByEmail(login);
902 return impersonate(email);
915 return !Strings.isBlank(
id) && (
id.equals(contact.
getId()) ||
id.equals(contact.
getLogin()));
920 return getConnector(key);
925 if(superUser)
return true;
926 return getPermissions().isInRole(role);
931 if(superUser)
return true;
932 return getPermissions().anyKeyMatch(Strings.csvToList(role));
static Map< String, Object > execute(String name, Map params)
static boolean isActorFor(Object entity, String actor)
static boolean isActor(String actor)
static IContact getIContact()
IConstructor getConstructor()
static Locale getUsedLocale()
static String logMsg(String msg)
static byte[] encrypt(byte[] value)
void addClause(String clause)
void addNamedValue(String name, Object value)
static Faces of(IContact contact)
String getPublicFile(String file)
Object getSingleResult(WhereClause wc)
Object getSingleResultOrNull(SqlClause sc)
void setFull(String full)
static Set< String > getSecurityGroupIdsBy(String role)
static void unsyndicate(List< Contact > contacts, Set< String > socialGroupIds, Dao dao)
static void checkInheritance(List< Contact > contacts, Dao dao)
static void syndicate(List< Contact > contacts, Set< String > socialGroupIds, Dao dao)
static final String CONNECTOR_EMAIL