BrightSide Workbench Full Report + Source Code
ContactIndicator.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.indicator;
20 
21 import java.util.List;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24 import org.amic.util.date.CheckDate;
25 import org.turro.annotation.ElephantIndicator;
26 import org.turro.contacts.Contact;
27 import org.turro.contacts.ContactType;
28 import org.turro.contacts.db.ContactsPU;
29 import org.turro.contacts.relation.FuzzyRelationTypes;
30 import org.turro.elephant.db.WhereClause;
31 import org.turro.entities.Entities;
32 import org.turro.entities.IElephantEntity;
33 import org.turro.jpa.Dao;
34 import org.turro.log.LogCount;
35 import org.turro.log.Logs;
36 import org.turro.matching.GenericMatching;
37 import org.turro.matching.IMatching;
38 import org.turro.math.Zero;
39 import org.turro.ranking.GenericRanking;
40 import org.turro.ranking.IRanking;
41 import org.turro.sql.SqlClause;
42 import org.turro.zipcode.ZipCodeAPI;
43 
49 public class ContactIndicator extends AbstractIndicator {
50 
51  public ContactIndicator() {
52  root = "contact";
53  }
54 
55  @Override
56  protected void initVariables() {
57  addVariable("available", true, VariableType.RANKING_VARIABLE);
59  addVariable("distance", VariableType.MATCHING_VARIABLE, true);
60  }
61 
62  @Override
63  protected void initExternalVariables() {
64  addExternalVariable("dossier", "participation");
65  addExternalVariable("dossier", "categoryParticipation");
66  addExternalVariable("dossier", "issueResponsible");
67  addExternalVariable("dossier", "issueReporter");
68  addExternalVariable("dossier", "issueQA");
69  addExternalVariable("dossier", "issueAssistant");
70  addExternalVariable("student", "challenges");
71  addExternalVariable("student", "responses");
72  addExternalVariable("student", "responsesLike");
73  addExternalVariable("forum", "topics");
74  addExternalVariable("forum", "posts");
75  addExternalVariable("commons", "following");
76  addExternalVariable("commons", "followed");
77  addExternalVariable("commons", "seen");
78  addExternalVariable("commons", "like");
79  }
80 
81  @Override
82  public void generateSiblings() {
83  List<LogCount> genact = Logs.getGeneratorActivityCount(new CheckDate().addYears(-2).getDate());
84  SqlClause.update("Contact").set("inactive", true)
85  .dao(getDao()).execute();
86  try(Stream<Contact> stream = createDao().stream(Contact.class, "select c from Contact c")) {
87  stream.forEach(contact -> {
88  if(genact.contains(new LogCount("/contact/" + contact.getId(), 0))) {
89  SqlClause.update("Contact").set("inactive", false)
90  .where().equal("id", contact.getId())
91  .dao(getDao()).execute();
92  }
93  });
94  }
95  try(Stream<Contact> stream = SqlClause.select("distinct c").from("Contact c")
96  .where().in("c.type", List.copyOf(ContactType.juridicals()))
97  .and().exists(SqlClause.select("r").from("BusinessRelation r")
98  .where("r.business.id = c.id")
99  .and().equal("r.contact.inactive", false))
100  .dao(createDao()).stream(Contact.class)) {
101  stream.forEach(contact -> {
102  SqlClause.update("Contact").set("inactive", false)
103  .where().equal("id", contact.getId())
104  .dao(getDao()).execute();
105  });
106  }
107  }
108 
109  @Override
110  protected Dao createDao() {
111  return new ContactsPU();
112  }
113 
114  @Override
115  public double getValue(IndicatorVariable variable, Object entity) {
116  if((entity instanceof Contact) && itsMine(variable)) {
117  Contact contact = (Contact) entity;
118  switch(variable.getName()) {
119  case "profile":
120  return contact.getProfile().getCompletion().getPercent();
121  }
122  }
123  return 0.0;
124  }
125 
126  @Override
127  public double getValue(IndicatorVariable variable, Object entity, String relatedPath) {
128  if((entity instanceof Contact) && itsMine(variable)) {
129  Contact contact = (Contact) entity;
130  IElephantEntity related = Entities.getController(relatedPath);
131  if("contact".equals(related.getRoot())) {
132  switch(variable.getName()) {
133  case "distance":
134  if(contact.hasLocation()) {
135  Contact crelated = (Contact) related.getEntity();
136  if(crelated.hasLocation()) {
137  return ZipCodeAPI.distanceOrZero(contact.getLatitude(), crelated.getLatitude(),
138  contact.getLongitude(), crelated.getLongitude());
139  }
140  }
141  }
142  }
143  }
144  return 0.0;
145  }
146 
147  @Override
148  protected void processRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot) {
149  WhereClause wc = null;
150  if("contact".equals(entityRoot)) {
151  switch(variable.getName()) {
152  case "available":
153  wc = PreprocessClause.load("JobBoard")
154  .addCondition("and available = TRUE")
155  .setRankingRoot(entityRoot)
156  .setRankingField("concat('/contact/',contact.id)")
157  .setVariable(variable)
158  .setAggregate("count(id)")
159  .getClause();
160  break;
161  }
162  }
163  if(wc != null) {
164  try(Stream<GenericRanking> stream = dao.stream(GenericRanking.class, wc, 1000)) {
165  stream.forEach(r -> {
166  preprocessor.poolInstance(r.copyTo((IRanking) preprocessor.createInstance()));
167  });
168  preprocessor.finishPreprocessor();
169  }
170  }
171  }
172 
173  @Override
174  protected void processMatchingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot, String relatedRoot) {
175  WhereClause wc = null;
176  if("contact".equals(entityRoot)) {
177  if("contact".equals(relatedRoot)) {
178  switch(variable.getName()) {
179  }
180  }
181  }
182  if(wc != null) {
183  try(Stream<GenericMatching> stream = dao.stream(GenericMatching.class, wc, 1000)) {
184  stream.forEach(r -> {
185  preprocessor.poolInstance(r.copyTo((IMatching) preprocessor.createInstance()));
186  });
187  preprocessor.finishPreprocessor();
188  }
189  }
190  }
191 
192  @Override
193  protected void postprocessRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot) {
194  if("contact".equals(entityRoot)) {
195  try(Stream<Contact> stream = getJuridicals()) {
196  stream.forEach(juridical -> {
197  List<String> springPaths = getSpringPaths(juridical);
198  if(!springPaths.isEmpty()) {
199  WhereClause wc = PostprocessClause.load(preprocessor.tableName())
201  .setSpringPaths(springPaths)
202  .getRankingClause();
203  if(wc != null) {
204  try(Stream<GenericRanking> istream = dao.stream(GenericRanking.class, wc, 1000)) {
205  istream.forEach(r -> {
206  if(r != null && !Zero.near(r.getRanking(), 2)) {
207  preprocessor.poolInstance(r.copyTo((IRanking) preprocessor.createInstance()));
208  }
209  });
210  }
211  }
212  }
213  });
214  preprocessor.finishPreprocessor();
215  }
216  }
217  }
218 
219  private Stream<Contact> getJuridicals() {
220  WhereClause wc = new WhereClause();
221  wc.addClause("select contact from Contact contact");
222  FuzzyRelationTypes.isJuridical(wc, "where", "contact");
223  return createDao().stream(Contact.class, wc, 1000);
224  }
225 
226  private List<String> getSpringPaths(Contact contact) {
227  return contact.getWorkers().stream().map(c -> "/contact/" + c.getId()).collect(Collectors.toList());
228  }
229 
230 }
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
static void isJuridical(WhereClause wc, String separator, String relationField)
static IElephantEntity getController(String path)
Definition: Entities.java:78
void addVariable(String name, VariableType type)
void addExternalVariable(String root, String name)
double getValue(IndicatorVariable variable, Object entity, String relatedPath)
void processRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot)
double getValue(IndicatorVariable variable, Object entity)
void postprocessRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot)
void processMatchingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot, String relatedRoot)
PostprocessClause setSpringPaths(List< String > springPaths)
static PostprocessClause load(String table)
PostprocessClause setEntityPath(String entityPath)
PreprocessClause addCondition(String condition)
PreprocessClause setVariable(IndicatorVariable variable)
PreprocessClause setAggregate(String aggregate)
PreprocessClause setRankingRoot(String entityRoot)
static PreprocessClause load(String table)
PreprocessClause setRankingField(String pathField)
static List< LogCount > getGeneratorActivityCount(Date from)
Definition: Logs.java:32
static boolean near(double value, int digits)
Definition: Zero.java:30
static EnumSet< ContactType > juridicals()
void poolInstance(Object value)