19 package org.turro.matching;
21 import java.util.HashMap;
22 import java.util.List;
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;
44 private PathSet selectedPaths;
49 selectedPaths =
new PathSet();
55 protected void end() {
62 return selectedPaths.getPathList();
69 wc.
addIn(
"where",
"c.id", selectedPaths.getIdList());
105 return entity instanceof
Contact;
108 private static final int MAX_HOMONYMS = 100;
110 private void selectPaths() {
113 wc.
addClause(
"select c.id from Contact c");
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) {
121 selectedPaths.addAll(
"/contact/",
getDao().getResultList(String.class, wc, MAX_HOMONYMS - selectedPaths.size()));
123 UserPreferences.getSystem().put(
"lastHomonymId", selectedPaths.getMaxId());
126 private Map<String, String> zipCodes;
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");
134 zipCodes =
new HashMap<>();
135 contacts.forEach(c -> {
136 String zipCode = getZipCode(c);
137 if(!Strings.isBlank(zipCode)) {
138 ZipCodeEntry info = ZipCodeAPI.getInfo(zipCode);
140 wc.addNamedValue(
"latitude", info.getLatitude());
141 wc.addNamedValue(
"longitude", info.getLongitude());
142 wc.addNamedValue(
"id", c.getId());
145 wc.addNamedValue(
"latitude", 0.0);
146 wc.addNamedValue(
"longitude", 0.0);
147 wc.addNamedValue(
"id", c.getId());
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);
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);
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);
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);
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);
224 Threads.instance(
"Clear homonyms").start(() -> {
226 SqlClause.delete(
"HomonymsMatching")
228 SqlClause.select(
"*").from(
"Contact c")
229 .where(
"c.identifier = SUBSTRING(entityPath , 10)")
230 .and().isFalse(
"c.deactivated")
231 .and().isFalse(
"c.inactive"))
234 SqlClause.delete(
"HomonymsMatching")
236 SqlClause.select(
"*").from(
"Contact c")
237 .where(
"c.identifier = SUBSTRING(relatedPath , 10)")
238 .and().isFalse(
"c.deactivated")
239 .and().isFalse(
"c.inactive"))
void addClause(String clause)
void addIn(String operator, String field, List values)
void addNamedValue(String name, Object value)
static UserPreferences getSystem()
int executeUpdate(String query)
List< String > getSelectedPaths()
static void cleanHomonyms()
boolean itsMine(Object entity)
Stream< Contact > getEntityStream()
IMatching createMatchingInstance()