BrightSide Workbench Full Report + Source Code
StudentIndicator.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.time.Duration;
22 import java.time.Instant;
23 import java.util.stream.Stream;
24 import org.turro.annotation.ElephantIndicator;
25 import org.turro.assistant.ParticipationInfo;
26 import org.turro.contacts.Contact;
27 import org.turro.contacts.JobBoard;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.entities.Entities;
30 import org.turro.entities.IElephantEntity;
31 import org.turro.entities.Students;
32 import org.turro.jpa.Dao;
33 import org.turro.matching.GenericMatching;
34 import org.turro.matching.IMatching;
35 import org.turro.participation.ParticipationReason;
36 import org.turro.ranking.GenericRanking;
37 import org.turro.ranking.IRanking;
38 import org.turro.students.db.StudentsPU;
39 import org.turro.students.entities.Challenge;
40 import org.turro.students.entities.PracticalWork;
41 import org.turro.students.entities.Response;
42 import org.turro.zipcode.ZipCodeAPI;
43 import org.zkoss.lang.Strings;
44 
49 @ElephantIndicator
50 public class StudentIndicator extends AbstractIndicator {
51 
52  public StudentIndicator() {
53  root = "student";
54  }
55 
56  @Override
57  protected void initVariables() {
58  addVariable("challenges", true, VariableType.RANKING_VARIABLE);
60  addVariable("responsesLike", VariableType.RANKING_VARIABLE);
61  addVariable("daysCreation", VariableType.RANKING_VARIABLE);
63  }
64 
65  @Override
66  protected void initExternalVariables() {
67  addExternalVariable("commons", "followed");
68  addExternalVariable("commons", "like");
69  addExternalVariable("commons", "apply");
70  }
71 
72  @Override
73  protected Dao createDao() {
74  return new StudentsPU();
75  }
76 
77  @Override
78  public double getValue(IndicatorVariable variable, Object entity) {
79  if((entity instanceof Challenge) && itsMine(variable)) {
80  Challenge challenge = (Challenge) entity;
81  switch(variable.getName()) {
82  case "responses":
83  return getResponsesFor(challenge);
84  case "responsesLike":
85  return getResponsesLikeFor(challenge);
86  case "daysCreation":
87  return challenge.getCreation() != null ? Duration.between(challenge.getCreation().toInstant(), Instant.now()).toDays() : 9999.9;
88  }
89  } else if((entity instanceof Contact) && itsMine(variable)) {
90  Contact contact = (Contact) entity;
91  switch(variable.getName()) {
92  case "responses":
93  return getResponsesFor(contact);
94  case "responsesLike":
95  return getResponsesLikeFor(contact);
96  }
97  } else if((entity instanceof PracticalWork) && itsMine(variable)) {
98  PracticalWork practicalWork = (PracticalWork) entity;
99  switch(variable.getName()) {
100  case "daysCreation":
101  return practicalWork.getCreation() != null ? Duration.between(practicalWork.getCreation().toInstant(), Instant.now()).toDays() : 9999.9;
102  }
103  } else {
104  switch(variable.getName()) {
105  case "responses":
106  return getResponsesFor(entity);
107  case "responsesLike":
108  return getResponsesLikeFor(entity);
109  }
110  }
111  return 0.0;
112  }
113 
114  @Override
115  public double getValue(IndicatorVariable variable, Object entity, String relatedPath) {
116  if((entity instanceof PracticalWork) && itsMine(variable)) {
117  PracticalWork practicalWork = (PracticalWork) entity;
118  IElephantEntity related = Entities.getController(relatedPath);
119  if("contact".equals(related.getRoot())) {
120  switch(variable.getName()) {
121  case "distance":
122  if(!Strings.isBlank(practicalWork.getZipCode())) {
123  Contact crelated = (Contact) related.getEntity();
124  JobBoard jobBoard = crelated.getJobBoard();
125  if(jobBoard != null && !Strings.isBlank(jobBoard.getZipCode())) {
126  return ZipCodeAPI.distanceOrZero(practicalWork.getZipCode(), jobBoard.getZipCode());
127  }
128  }
129  }
130  }
131  }
132  return 0.0;
133  }
134 
135  @Override
136  protected void processRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot) {
137  WhereClause wc = null;
138  if("contact".equals(entityRoot)) {
139  switch(variable.getName()) {
140  case "challenges":
141  wc = PreprocessClause.load("Challenge")
142  .setRankingRoot(entityRoot)
143  .setRankingField("concat('/contact/',idChallenger)")
144  .setVariable(variable)
145  .setAggregate("count(id)")
146  .getClause();
147  break;
148  }
149  } else {
150  switch(variable.getName()) {
151  case "challenges":
152  wc = PreprocessClause.load("Challenge")
153  .setRankingRoot(entityRoot)
154  .setRankingField("entityPath")
155  .setVariable(variable)
156  .setAggregate("count(id)")
157  .getClause();
158  break;
159  }
160  }
161  if(wc != null) {
162  try(Stream<GenericRanking> stream = dao.stream(GenericRanking.class, wc, 1000)) {
163  stream.forEach(r -> {
164  preprocessor.poolInstance(r.copyTo((IRanking) preprocessor.createInstance()));
165  });
166  preprocessor.finishPreprocessor();
167  }
168  }
169  }
170 
171  @Override
172  protected void processMatchingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot, String relatedRoot) {
173  WhereClause wc = null;
174  if("contact".equals(relatedRoot)) {
175  if("contact".equals(entityRoot)) {
176  switch(variable.getName()) {
177  }
178  } else if("praticalwork".equals(entityRoot)) {
179  switch(variable.getName()) {
180  }
181  } else {
182  switch(variable.getName()) {
183  case "challenges":
184  wc = PreprocessClause.load("Challenge")
185  .setRankingRoot(entityRoot)
186  .setMatchingRoot(relatedRoot)
187  .setRankingField("entityPath")
188  .setMatchingField("idChallenger")
189  .setVariable(variable)
190  .setAggregate("count(c.id)")
191  .getClause();
192  break;
193  }
194  }
195  }
196  if(wc != null) {
197  try(Stream<GenericMatching> stream = dao.stream(GenericMatching.class, wc, 1000)) {
198  stream.forEach(r -> {
199  preprocessor.poolInstance(r.copyTo((IMatching) preprocessor.createInstance()));
200  });
201  preprocessor.finishPreprocessor();
202  }
203  }
204  }
205 
206  private double getResponsesFor(Challenge challenge) {
207  return Students.getResponseCountFrom(getDao(), challenge);
208  }
209 
210  private double getResponsesFor(Contact contact) {
211  double count = 0.0;
212  if(contact.getProfile().isStudent()) {
213  count += Students.getResponseCountFrom(contact.getId());
214  }
215  return count;
216  }
217 
218  private double getResponsesFor(Object entity) {
219  return Students.getResponseCountFromEntity(entity);
220  }
221 
222  private double getResponsesLikeFor(Challenge challenge) {
223  return Students.getResponsesFrom(getDao(), challenge).stream()
224  .map(r -> (double) new ParticipationInfo(r, null, ParticipationReason.REASON_LIKE).getCount())
225  .reduce(0.0, (subtotal, count) -> subtotal + count);
226  }
227 
228  private double getResponsesLikeFor(Contact contact) {
229  double count = 0.0;
230  if(contact.getProfile().isStudent()) {
231  for(Response response : Students.getResponsesFrom(contact.getId())) {
232  count += new ParticipationInfo(response, null, ParticipationReason.REASON_LIKE).getCount();
233  }
234  }
235  return count;
236  }
237 
238  private double getResponsesLikeFor(Object entity) {
239  double count = 0.0;
240  for(Response response : Students.getResponsesFromEntity(entity)) {
241  count += new ParticipationInfo(response, null, ParticipationReason.REASON_LIKE).getCount();
242  }
243  return count;
244  }
245 
246 }
static IElephantEntity getController(String path)
Definition: Entities.java:78
static long getResponseCountFrom(IContact contact)
Definition: Students.java:107
void addVariable(String name, VariableType type)
void addExternalVariable(String root, String name)
PreprocessClause setMatchingField(String pathField)
PreprocessClause setVariable(IndicatorVariable variable)
PreprocessClause setMatchingRoot(String entityRoot)
PreprocessClause setAggregate(String aggregate)
PreprocessClause setRankingRoot(String entityRoot)
static PreprocessClause load(String table)
PreprocessClause setRankingField(String pathField)
void processRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot)
double getValue(IndicatorVariable variable, Object entity, String relatedPath)
double getValue(IndicatorVariable variable, Object entity)
void processMatchingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot, String relatedRoot)
void poolInstance(Object value)