BrightSide Workbench Full Report + Source Code
ProcessHomonymsMatching.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.matching;
20 
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.stream.Stream;
25 import org.turro.string.Strings;
26 import org.turro.contacts.Contact;
27 import org.turro.contacts.db.ContactsPU;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.elephant.impl.security.UserPreferences;
30 import org.turro.jpa.Dao;
31 import org.turro.path.PathSet;
32 import org.turro.sql.SqlClause;
33 import org.turro.util.Threads;
34 import org.turro.zipcode.ZipCodeAPI;
35 import org.turro.zipcode.ZipCodeEntry;
36 
41 @ElephantMatching
42 public class ProcessHomonymsMatching extends ProcessMatching<Contact> {
43 
44  private PathSet selectedPaths;
45 
46  @Override
47  protected void start() {
48  super.start();
49  selectedPaths = new PathSet();
50  selectPaths();
51  checkGeolocation();
52  }
53 
54  @Override
55  protected void end() {
56  super.end();
57  selectedPaths = null;
58  }
59 
60  @Override
61  protected List<String> getSelectedPaths() {
62  return selectedPaths.getPathList();
63  }
64 
65  @Override
66  protected Stream<Contact> getEntityStream() {
67  WhereClause wc = new WhereClause();
68  wc.addClause("select c from Contact c");
69  wc.addIn("where", "c.id", selectedPaths.getIdList());
70  return createDao().stream(Contact.class, wc);
71  }
72 
73  @Override
75  return new HomonymsMatching();
76  }
77 
78  @Override
79  protected Class instanceClass() {
80  return HomonymsMatching.class;
81  }
82 
83  @Override
84  protected String indicatorsRoot() {
85  return "contact";
86  }
87 
88  @Override
89  protected String entitiesRoot() {
90  return "contact";
91  }
92 
93  @Override
94  protected String relatedRoot() {
95  return "contact";
96  }
97 
98  @Override
99  protected Dao createDao() {
100  return new ContactsPU();
101  }
102 
103  @Override
104  public boolean itsMine(Object entity) {
105  return entity instanceof Contact;
106  }
107 
108  private static final int MAX_HOMONYMS = 100;
109 
110  private void selectPaths() {
111  String lastId = (String) UserPreferences.getSystem().get("lastHomonymId");
112  WhereClause wc = new WhereClause();
113  wc.addClause("select c.id from Contact c");
114  wc.addClause("where c.id > :lastId");
115  wc.addNamedValue("lastId", lastId);
116  wc.addClause("and c.inactive = false");
117  wc.addClause("and c.deactivated = false");
118  selectedPaths.addAll("/contact/", getDao().getResultList(String.class, wc, MAX_HOMONYMS));
119  if(selectedPaths.isEmpty() || selectedPaths.size() < MAX_HOMONYMS) {
120  wc.addNamedValue("lastId", "");
121  selectedPaths.addAll("/contact/", getDao().getResultList(String.class, wc, MAX_HOMONYMS - selectedPaths.size()));
122  }
123  UserPreferences.getSystem().put("lastHomonymId", selectedPaths.getMaxId());
124  }
125 
126  private Map<String, String> zipCodes;
127 
128  private void checkGeolocation() {
129  WhereClause wc = new WhereClause();
130  wc.addClause("update Contact");
131  wc.addClause("set latitude = :latitude, longitude = :longitude");
132  wc.addClause("where id = :id");
133  try(Stream<Contact> contacts = getEntityStream()) {
134  zipCodes = new HashMap<>();
135  contacts.forEach(c -> {
136  String zipCode = getZipCode(c);
137  if(!Strings.isBlank(zipCode)) {
138  ZipCodeEntry info = ZipCodeAPI.getInfo(zipCode);
139  if(info != null) {
140  wc.addNamedValue("latitude", info.getLatitude());
141  wc.addNamedValue("longitude", info.getLongitude());
142  wc.addNamedValue("id", c.getId());
143  getDao().executeUpdate(wc);
144  } else {
145  wc.addNamedValue("latitude", 0.0);
146  wc.addNamedValue("longitude", 0.0);
147  wc.addNamedValue("id", c.getId());
148  getDao().executeUpdate(wc);
149  }
150  }
151  });
152  zipCodes = null;
153  }
154  }
155 
156  private String getZipCode(Contact contact) {
157  String zc = zipCodes.get(contact.getId());
158  if(!Strings.isBlank(zc)) return zc;
159  WhereClause wc = new WhereClause();
160  wc.addClause("select a.zipCode from JobBoard a");
161  wc.addClause("where a.contact = :contact");
162  wc.addNamedValue("contact", contact);
163  for(String zipCode : getDao().getResultList(String.class, wc)) {
164  if(!Strings.isBlank(zipCode)) {
165  zipCodes.put(contact.getId(), zipCode);
166  return zipCode;
167  }
168  }
169  wc = new WhereClause();
170  wc.addClause("select a.zipCode from Address a");
171  wc.addClause("where a.contact = :contact");
172  wc.addClause("and a.description = 'Fiscal'");
173  wc.addNamedValue("contact", contact);
174  for(String zipCode : getDao().getResultList(String.class, wc)) {
175  if(!Strings.isBlank(zipCode)) {
176  zipCodes.put(contact.getId(), zipCode);
177  return zipCode;
178  }
179  }
180  Contact business = contact.getBusiness();
181  if(business != null) {
182  zc = zipCodes.get(business.getId());
183  if(!Strings.isBlank(zc)) return zc;
184  wc = new WhereClause();
185  wc.addClause("select a.zipCode from Address a");
186  wc.addClause("where a.contact = :contact");
187  wc.addClause("and a.description = 'Fiscal'");
188  wc.addNamedValue("contact", business);
189  for(String zipCode : getDao().getResultList(String.class, wc)) {
190  if(!Strings.isBlank(zipCode)) {
191  zipCodes.put(business.getId(), zipCode);
192  return zipCode;
193  }
194  }
195  }
196  wc = new WhereClause();
197  wc.addClause("select a.zipCode from Address a");
198  wc.addClause("where a.contact = :contact");
199  wc.addClause("and a.description <> 'Fiscal'");
200  wc.addNamedValue("contact", contact);
201  for(String zipCode : getDao().getResultList(String.class, wc)) {
202  if(!Strings.isBlank(zipCode)) {
203  zipCodes.put(contact.getId(), zipCode);
204  return zipCode;
205  }
206  }
207  if(business != null) {
208  wc = new WhereClause();
209  wc.addClause("select a.zipCode from Address a");
210  wc.addClause("where a.contact = :contact");
211  wc.addClause("and a.description <> 'Fiscal'");
212  wc.addNamedValue("contact", business);
213  for(String zipCode : getDao().getResultList(String.class, wc)) {
214  if(!Strings.isBlank(zipCode)) {
215  zipCodes.put(business.getId(), zipCode);
216  return zipCode;
217  }
218  }
219  }
220  return null;
221  }
222 
223  public static void cleanHomonyms() {
224  Threads.instance("Clear homonyms").start(() -> {
225  Dao dao = new ContactsPU();
226  SqlClause.delete("HomonymsMatching")
227  .where().notExists(
228  SqlClause.select("*").from("Contact c")
229  .where("c.identifier = SUBSTRING(entityPath , 10)")
230  .and().isFalse("c.deactivated")
231  .and().isFalse("c.inactive"))
232  .dao(dao)
233  .executeNative();
234  SqlClause.delete("HomonymsMatching")
235  .where().notExists(
236  SqlClause.select("*").from("Contact c")
237  .where("c.identifier = SUBSTRING(relatedPath , 10)")
238  .and().isFalse("c.deactivated")
239  .and().isFalse("c.inactive"))
240  .dao(dao)
241  .executeNative();
242  });
243  //"where not exists (\n" +
244  //" select * from Contact c \n" +
245  //" where c.IDENTIFIER = SUBSTRING(entityPath , 10)\n" +
246  //" and c.deactivated = FALSE and c.inactive = false \n" +
247  //");"
248  //"delete FROM pool01_contacts.HomonymsMatching\n" +
249  //"where not exists (\n" +
250  //" select * from Contact c \n" +
251  //" where c.IDENTIFIER = SUBSTRING(relatedPath , 10)\n" +
252  //" and c.deactivated = FALSE and c.inactive = false \n" +
253  //");"
254  }
255 
256 }
void addIn(String operator, String field, List values)
void addNamedValue(String name, Object value)
int executeUpdate(String query)
Definition: Dao.java:463