BrightSide Workbench Full Report + Source Code
Contacts.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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.action;
20 
21 import java.util.List;
22 import java.util.Optional;
23 import java.util.SortedMap;
24 import java.util.TreeMap;
25 import java.util.stream.Collectors;
26 import org.turro.mail.MailValidator;
27 import org.turro.string.Strings;
28 import org.turro.annotation.ElephantContact;
29 import org.turro.collections.KeyValueMap;
30 import org.turro.elephant.context.Application;
31 import org.turro.elephant.security.IUser;
32 import org.turro.lock.Initializer;
33 import org.turro.path.Path;
34 import org.turro.plugin.contacts.IContact;
35 import org.turro.reflection.ClassNames;
36 import org.turro.reflection.Reflections;
37 
42 public class Contacts {
43 
44  public final static String
45  LOGGED_ICONTACT = "loggedIContact",
46  BEHAVEAS_ICONTACT = "behaveAsIContact";
47 
48  public static boolean exists(String idContact) {
49  return getContactById(idContact).isValid();
50  }
51 
52  public static boolean isValid(IContact contact) {
53  return contact != null && contact.isValid();
54  }
55 
56  public static IContact getEmpty() {
57  for(Class javaClass : instance().values()) {
58  IContact contact = (IContact) Reflections.of(javaClass).instance();
59  if(contact != null) return contact;
60  }
61  return null;
62  }
63 
64  public static IContact getEmptyFor(Object object) {
65  for(Class javaClass : instance().values()) {
66  IContact contact = (IContact) Reflections.of(javaClass).instance();
67  if(contact != null && contact.accepts(object)) return contact;
68  }
69  return getEmpty();
70  }
71 
72  public static IContact getContactById(String id) {
73  if(!Strings.isBlank(id) && id.startsWith("/contact/")) {
74  id = new Path(id).getNode(1);
75  }
76  IContact iContact = getEmptyFor(id);
77  if(iContact != null && !Strings.isBlank(id)) {
78  iContact.loadById(id);
79  }
80  return iContact;
81  }
82 
83  public static IContact getContactByEmail(String email) {
84  IContact iContact = getEmpty();
85  if(iContact != null) {
86  iContact = iContact.loadByEmail(email);
87  }
88  return iContact;
89  }
90 
91  public static List<IContact> getWebUsers(List<IContact> contacts) {
92  return contacts.stream().filter((contact) -> (contact.isWebUser())).collect(Collectors.toList());
93  }
94 
95  public static boolean hasValidEmail(IContact contact) {
97  }
98 
99  public static boolean isValidEmail(String email) {
100  if(Strings.isBlank(email) || email.contains("nomail")) return false;
101  return MailValidator.single().silently(email);
102  }
103 
104  public static String getNoEmail() {
105  return "nomail@nomail.no";
106  }
107 
108  // From ID or from Contact
109  public static IContact getContact(Object object) {
110  if(object instanceof String) {
111  return getContactById((String) object);
112  } else {
113  IContact contact = getEmptyFor(object);
114  if(contact != null) {
115  contact.setContact(object);
116  return contact;
117  }
118  }
119  return null;
120  }
121 
123  IContact contact = getEmptyFor(app.getConstructor().getUser());
124  if(contact != null) {
125  contact.loadLogged(app);
126  }
127  return contact;
128  }
129 
130  public static List<IContact> getBySyndication(String syndication) {
131  return getEmpty().getBySyndication(syndication);
132  }
133 
134  public static List<IContact> getByRole(String role) {
135  return getEmpty().getByRole(role);
136  }
137 
138  public static List<IContact> getByGrouping(String grouping) {
139  return getEmpty().getByGrouping(grouping);
140  }
141 
142  public static List<IContact> getByGrouping(List<String> groupings) {
143  return getEmpty().getByGrouping(groupings);
144  }
145 
146  public static List<IContact> getCenters() {
147  return getEmpty().getCenters();
148  }
149 
150  public static List<IContact> getStudents() {
151  return getEmpty().getStudents();
152  }
153 
154  public static IContact getBehavingAs() {
156  IContact contact = (IContact) app.getHttpSession(false).getAttribute(BEHAVEAS_ICONTACT);
157  if(contact == null) {
158  contact = (IContact) app.getHttpSession(false).getAttribute(LOGGED_ICONTACT);
159  }
160  return contact;
161  }
162 
163  public static void startContactFromValues(KeyValueMap values) {
164  IContact iContact = getEmpty();
165  if(iContact != null) {
166  iContact.startContactFromValues(values);
167  }
168  }
169 
170  public static boolean isCompany(IContact contact) {
171  return Optional.ofNullable(contact).filter(c -> c.isValid() && c.isCompany()).isPresent();
172  }
173 
174  public static boolean isCenter(IContact contact) {
175  return Optional.ofNullable(contact).filter(c -> c.isValid() && c.isCenter()).isPresent();
176  }
177 
178  public static boolean isWorker(IContact contact) {
179  return Optional.ofNullable(contact).filter(c -> c.isValid() && c.isWorker()).isPresent();
180  }
181 
182  public static boolean isHHRR(IContact contact) {
183  return Optional.ofNullable(contact).filter(c -> c.isValid() && c.isHHRR()).isPresent();
184  }
185 
186  public static boolean isStudent(IContact contact) {
187  return Optional.ofNullable(contact).filter(c -> c.isValid() && c.isStudent()).isPresent();
188  }
189 
190  /* Factory */
191 
192  private static final Initializer<SortedMap<Integer, Class>> INIT = new Initializer<>();
193 
194  public static SortedMap<Integer, Class> instance() {
195  return INIT.instance(() -> getContactClasses());
196  }
197 
198  public static void reset() {
199  INIT.reset();
200  }
201 
202  private static SortedMap<Integer, Class> getContactClasses() {
203  SortedMap<Integer, Class> contactMap = new TreeMap<>();
204  List<String> contactClasses = ClassNames.cached().byAnnotation(ElephantContact.class.getName());
205  if(contactClasses != null) {
206  for(String contactClass : contactClasses) {
207  Class iContactClass = Reflections.check(contactClass);
208  if(iContactClass != null) {
209  contactMap.put(((ElephantContact) iContactClass.getAnnotation(ElephantContact.class)).priority(), iContactClass);
210  }
211  }
212  }
213  return contactMap;
214  }
215 
216  private Contacts() {
217  }
218 
219 }
static boolean isHHRR(IContact contact)
Definition: Contacts.java:182
static boolean isStudent(IContact contact)
Definition: Contacts.java:186
static IContact getContact(Object object)
Definition: Contacts.java:109
static List< IContact > getCenters()
Definition: Contacts.java:146
static IContact getEmpty()
Definition: Contacts.java:56
static List< IContact > getByGrouping(String grouping)
Definition: Contacts.java:138
static List< IContact > getByRole(String role)
Definition: Contacts.java:134
static boolean exists(String idContact)
Definition: Contacts.java:48
static boolean hasValidEmail(IContact contact)
Definition: Contacts.java:95
static IContact getEmptyFor(Object object)
Definition: Contacts.java:64
static IContact getBehavingAs()
Definition: Contacts.java:154
static boolean isCenter(IContact contact)
Definition: Contacts.java:174
static List< IContact > getByGrouping(List< String > groupings)
Definition: Contacts.java:142
static IContact getLoggedIContact(Application app)
Definition: Contacts.java:122
static boolean isWorker(IContact contact)
Definition: Contacts.java:178
static IContact getContactByEmail(String email)
Definition: Contacts.java:83
static List< IContact > getBySyndication(String syndication)
Definition: Contacts.java:130
static boolean isValidEmail(String email)
Definition: Contacts.java:99
static boolean isValid(IContact contact)
Definition: Contacts.java:52
static SortedMap< Integer, Class > instance()
Definition: Contacts.java:194
static final String LOGGED_ICONTACT
Definition: Contacts.java:45
static IContact getContactById(String id)
Definition: Contacts.java:72
static boolean isCompany(IContact contact)
Definition: Contacts.java:170
static void startContactFromValues(KeyValueMap values)
Definition: Contacts.java:163
static String getNoEmail()
Definition: Contacts.java:104
static List< IContact > getStudents()
Definition: Contacts.java:150
static List< IContact > getWebUsers(List< IContact > contacts)
Definition: Contacts.java:91
HttpSession getHttpSession(boolean create)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27
List< IContact > getBySyndication(String syndication)
Object loadLogged(Application app)
List< IContact > getByRole(String role)
boolean accepts(Object object)
List< IContact > getByGrouping(String grouping)
IContact loadByEmail(String email)
void setContact(Object contact)
void startContactFromValues(KeyValueMap values)