BrightSide Workbench Full Report + Source Code
DuplicatesModel.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.duplicates;
20 
21 import java.util.List;
22 import java.util.TreeSet;
23 import org.turro.string.Phrases;
24 import org.turro.contacts.Address;
25 import org.turro.contacts.Connector;
26 import org.turro.contacts.Contact;
27 import org.turro.contacts.db.ContactsPU;
28 import org.turro.i18n.I_;
29 import org.turro.jpa.Dao;
30 import org.turro.plugin.contacts.IContact;
31 import org.turro.sql.SqlClause;
32 import org.turro.string.Strings;
33 import org.turro.util.Cached;
34 
39 public class DuplicatesModel extends TreeSet<Duplicated> {
40 
41  private final Contact contact;
42 
43  public DuplicatesModel(Contact contact) {
44  this.contact = contact;
45  if(contact != null) performChecks();
46  }
47 
48  public boolean hasStoppers() {
49  return stream().anyMatch(dup -> dup.getType().equals(Duplicated.DuplicatedType.STOPPER));
50  }
51 
52  private void performChecks() {
53  checkGlobalID();
54  checkName();
55  checkConnectors();
56  checkAddreses();
57  }
58 
59  private void checkGlobalID() {
60  if(Strings.isBlank(contact.getGlobalIdentifier())) return;
61  List<String> contacts = SqlClause.select("c.name").from("Contact c")
62  .where().notEqual("c.globalIdentifier", null)
63  .and().equal("c.globalIdentifier", contact.getGlobalIdentifier())
64  .startIf(!contact.isNew())
65  .and().notEqual("c", contact)
66  .endIf()
67  .dao(dao.get())
68  .resultList(String.class);
69  if(!contacts.isEmpty()) {
70  Duplicated duplicated = new Duplicated(Duplicated.DuplicatedType.STOPPER,
71  I_.get("Global identifier"), contact.getGlobalIdentifier());
72  duplicated.setContacts(contacts);
73  add(duplicated);
74  }
75  }
76 
77  private void checkName() {
78  List<String> contacts = SqlClause.select("c.name").from("Contact c")
79  .where().equal("c.name", contact.getName())
80  .startIf(!contact.isNew())
81  .and().notEqual("c", contact)
82  .endIf()
83  .dao(dao.get())
84  .resultList(String.class);
85  if(!contacts.isEmpty()) {
86  Duplicated duplicated = new Duplicated(Duplicated.DuplicatedType.WARNING,
87  I_.get("Name"), contact.getName());
88  duplicated.setConsequence(I_.get("These users will be difficult to spot"));
89  duplicated.setContacts(contacts);
90  add(duplicated);
91  }
92  }
93 
94  private void checkConnectors() {
95  for(Connector connector : contact.getConnectors()) {
96  if(connector != null) {
97  List<String> contacts = SqlClause.select("c.contact.name").from("Connector c")
98  .where().equal("c.description", connector.getDescription())
99  .and().equal("c.value", connector.getValue())
100  .startIf(!contact.isNew())
101  .and().notEqual("c.contact", contact)
102  .endIf()
103  .dao(dao.get())
104  .resultList(String.class);
105  if(!contacts.isEmpty()) {
106  if(connector.getDescription().equals(IContact.CONNECTOR_EMAIL)) {
107  Duplicated duplicated = new Duplicated(Duplicated.DuplicatedType.WARNING,
108  Phrases.start(I_.get("Connector"), ":", connector.getDescription()).toString(),
109  connector.getValue());
110  duplicated.setConsequence(I_.get("These users wont be able to authenticate"));
111  duplicated.setContacts(contacts);
112  add(duplicated);
113  } else if(connector.getDescription().equals(IContact.CONNECTOR_TRADE)) {
114  Duplicated duplicated = new Duplicated(Duplicated.DuplicatedType.WARNING,
115  Phrases.start(I_.get("Connector"), ":", connector.getDescription()).toString(),
116  connector.getValue());
117  duplicated.setConsequence(I_.get("These users have the same trademark"));
118  duplicated.setContacts(contacts);
119  add(duplicated);
120  } else if(connector.getDescription().equals(IContact.CONNECTOR_FISCAL)) {
121  Duplicated duplicated = new Duplicated(Duplicated.DuplicatedType.WARNING,
122  Phrases.start(I_.get("Connector"), ":", connector.getDescription()).toString(),
123  connector.getValue());
124  duplicated.setConsequence(I_.get("These users have the same fiscal name"));
125  duplicated.setContacts(contacts);
126  add(duplicated);
127  } else {
128  Duplicated duplicated = new Duplicated(Duplicated.DuplicatedType.SAFE,
129  Phrases.start(I_.get("Connector"), ":", connector.getDescription()).toString(),
130  connector.getValue());
131  duplicated.setContacts(contacts);
132  add(duplicated);
133  }
134  }
135  }
136  }
137  }
138 
139  private void checkAddreses() {
140  for(Address address : contact.getAddresses()) {
141  if(address != null) {
142  List<String> contacts = SqlClause.select("c.contact.name").from("Address c")
143  .where().equal("c.street", address.getStreet())
144  .and().equal("c.zipCode", address.getZipCode())
145  .and().equal("c.city", address.getCity())
146  .startIf(!contact.isNew())
147  .and().notEqual("c.contact", contact)
148  .endIf()
149  .dao(dao.get())
150  .resultList(String.class);
151  if(!contacts.isEmpty()) {
152  if(address.getDescription().equals(IContact.ADDRESS_FISCAL)) {
153  Duplicated duplicated = new Duplicated(Duplicated.DuplicatedType.WARNING,
154  Phrases.start(I_.get("Address"), ":", address.getDescription()).toString(),
155  address.getAddressString());
156  duplicated.setConsequence(I_.get("These users have the same fiscal address"));
157  duplicated.setContacts(contacts);
158  add(duplicated);
159  } else {
160  Duplicated duplicated = new Duplicated(Duplicated.DuplicatedType.SAFE,
161  Phrases.start(I_.get("Address"), ":", address.getDescription()).toString(),
162  address.getAddressString());
163  duplicated.setContacts(contacts);
164  add(duplicated);
165  }
166  }
167  }
168  }
169  }
170 
171  private final Cached<Dao> dao = Cached.instance(() -> new ContactsPU());
172 
173 }
Set< Address > getAddresses()
Definition: Contact.java:355
Set< Connector > getConnectors()
Definition: Contact.java:367
static String get(String msg)
Definition: I_.java:41