BrightSide Workbench Full Report + Source Code
ContactWarnings.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.warnings;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import org.turro.action.Contacts;
25 import org.turro.mail.MailValidator;
26 import org.turro.annotation.ElephantWarning;
27 import org.turro.contacts.BusinessRelation;
28 import org.turro.contacts.Connector;
29 import org.turro.contacts.Contact;
30 import org.turro.contacts.db.ContactsPU;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.db.WhereClause;
33 import org.turro.i18n.I_;
34 import org.turro.jpa.Dao;
35 
40 @ElephantWarning
41 public class ContactWarnings implements IElephantWarning {
42 
43  @Override
44  public String getHeader() {
45  return I_.get("Contacts");
46  }
47 
48  @Override
49  public List<IWarning> getWarnings() {
50  if(Application.getApplication().isInRole("contact:all")) {
51  return createModel();
52  }
53  return Collections.EMPTY_LIST;
54  }
55 
56  private List<IWarning> createModel() {
57  List<IWarning> warnings = new ArrayList<>();
58  Dao dao = new ContactsPU();
59  checkMultipleEmails(dao, warnings);
60  checkDuplicates(dao, warnings);
61  checkInvalidEmails(dao, warnings);
62  checkUnresolvedRelations(dao, warnings);
63  //checkStudents(dao, warnings);
64  return warnings;
65  }
66 
67  private void checkMultipleEmails(Dao dao, List<IWarning> warnings) {
68  WhereClause wc = new WhereClause();
69  wc.addClause("select c.contact, count(c) from Connector c");
70  wc.addClause("where c.description = 'Email'");
71  wc.addClause("group by c.contact");
72  wc.addClause("having count(c) > 1");
73  for(Object[] v : (List<Object[]>) dao.getResultList(wc)) {
74  Warning warning = new Warning();
75  warning.setEntity(v[0]);
76  warning.addMessage(I_.format("To many Email: %d", v[1]));
77  warnings.add(warning);
78  }
79  }
80 
81  private void checkDuplicates(Dao dao, List<IWarning> warnings) {
82  WhereClause wc = new WhereClause();
83  wc.addClause("select c.contact, c2.contact");
84  wc.addClause("from Connector c join Connector c2");
85  wc.addClause("on c2.description = c.description");
86  wc.addClause("and c2.value = c.value");
87  wc.addClause("where c.description = 'Email'");
88  wc.addClause("and c.value <> 'nomail'");
89  wc.addClause("and c.id <> c2.id");
90  for(Object[] v : (List<Object[]>) dao.getResultList(wc)) {
91  Warning warning = new Warning();
92  warning.setEntity(v[0]);
93  warning.addMessage(I_.format("%s has the same email", ((Contact) v[1]).getName()));
94  warnings.add(warning);
95  }
96  }
97 
98  private void checkInvalidEmails(Dao dao, List<IWarning> warnings) {
99  WhereClause wc = new WhereClause();
100  wc.addClause("select c from Connector c");
101  wc.addClause("where c.description = 'Email'");
102  wc.addClause("and c.value <> 'nomail'");
103  wc.addClause("and (c.value <> trim(c.value)");
104  wc.addClause("or c.value not like '%@%.%')");
105  for(Connector connector : dao.getResultList(Connector.class, wc)) {
106  if(!MailValidator.single().silently(connector.getValue())) {
107  Warning warning = new Warning();
108  warning.setEntity(connector.getContact());
109  warning.addMessage(I_.format("%s is not a valid email address", connector.getValue()));
110  warnings.add(warning);
111  }
112  }
113  }
114 
115  private void checkUnresolvedRelations(Dao dao, List<IWarning> warnings) {
116  WhereClause wc = new WhereClause();
117  wc.addClause("select r from BusinessRelation r");
118  wc.addClause("where r.validated = FALSE");
119  for(BusinessRelation relation : dao.getResultList(BusinessRelation.class, wc)) {
120  Warning warning = new Warning();
121  warning.setEntity(relation.getContact());
122  warning.addMessage(I_.get("Pending"));
123  warnings.add(warning);
124  }
125  }
126 
127  private void checkStudents(Dao dao, List<IWarning> warnings) {
128  Contacts.getStudents().forEach(student -> {
129  if(student.isStudent() && student.isWorker()) {
130  Warning warning = new Warning();
131  warning.setEntity(student.getContact());
132  warning.addMessage(I_.get("Student while professional"));
133  warnings.add(warning);
134  }
135  });
136  }
137 
138 }
static String get(String msg)
Definition: I_.java:41