19 package org.turro.warnings;
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;
45 return I_.
get(
"Contacts");
53 return Collections.EMPTY_LIST;
56 private List<IWarning> createModel() {
57 List<IWarning> warnings =
new ArrayList<>();
59 checkMultipleEmails(dao, warnings);
60 checkDuplicates(dao, warnings);
61 checkInvalidEmails(dao, warnings);
62 checkUnresolvedRelations(dao, warnings);
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);
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);
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);
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);
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);
boolean isInRole(String role)
static Application getApplication()
static String get(String msg)