BrightSide Workbench Full Report + Source Code
CommonsIndicator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.stream.Stream;
22 import org.turro.annotation.ElephantIndicator;
23 import org.turro.elephant.db.ElephantPU;
24 import org.turro.elephant.db.WhereClause;
25 import org.turro.jpa.Dao;
26 import org.turro.matching.GenericMatching;
27 import org.turro.matching.IMatching;
28 import org.turro.participation.ParticipationReason;
29 import org.turro.ranking.GenericRanking;
30 import org.turro.ranking.IRanking;
31 
36 @ElephantIndicator
37 public class CommonsIndicator extends AbstractIndicator {
38 
39  public CommonsIndicator() {
40  root = "commons";
41  }
42 
43  @Override
44  protected void initVariables() {
45  addVariable("following", true, VariableType.BOTH_VARIABLE);
46  addVariable("followed", true, VariableType.BOTH_VARIABLE);
47  addVariable("seen", true, VariableType.BOTH_VARIABLE);
48  addVariable("like", true, VariableType.BOTH_VARIABLE);
49  addVariable("apply", true, VariableType.BOTH_VARIABLE);
50  }
51 
52  @Override
53  protected void initExternalVariables() {
54  }
55 
56  @Override
57  protected Dao createDao() {
58  return new ElephantPU();
59  }
60 
61  @Override
62  protected void processRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot) {
63  WhereClause wc = null;
64  if("contact".equals(entityRoot)) {
65  switch(variable.getName()) {
66  case "following":
67  wc = PreprocessClause.load("EntityParticipation")
68  .addCondition("and reason = :reason")
70  .setRankingRoot(entityRoot)
71  .setRankingField("participatorPath")
72  .setVariable(variable)
73  .setAggregate("count(entityPath)")
74  .getClause();
75  break;
76  case "followed":
77  wc = PreprocessClause.load("EntityParticipation")
78  .addCondition("and reason = :reason")
80  .setRankingRoot(entityRoot)
81  .setRankingField("entityPath")
82  .setVariable(variable)
83  .setAggregate("count(participatorPath)")
84  .getClause();
85  break;
86  case "seen":
87  wc = PreprocessClause.load("EntityParticipation")
88  .addCondition("and reason = :reason")
90  .setRankingRoot(entityRoot)
91  .setRankingField("participatorPath")
92  .setVariable(variable)
93  .setAggregate("count(entityPath)")
94  .getClause();
95  break;
96  case "like":
97  wc = PreprocessClause.load("EntityParticipation")
98  .addCondition("and reason = :reason")
100  .setRankingRoot(entityRoot)
101  .setRankingField("participatorPath")
102  .setVariable(variable)
103  .setAggregate("count(entityPath)")
104  .getClause();
105  break;
106  case "apply":
107  wc = PreprocessClause.load("EntityParticipation")
108  .addCondition("and reason = :reason")
110  .setRankingRoot(entityRoot)
111  .setRankingField("participatorPath")
112  .setVariable(variable)
113  .setAggregate("count(entityPath)")
114  .getClause();
115  break;
116  }
117  } else {
118  switch(variable.getName()) {
119  case "followed":
120  wc = PreprocessClause.load("EntityParticipation")
121  .addCondition("and reason = :reason")
123  .setRankingRoot(entityRoot)
124  .setRankingField("entityPath")
125  .setVariable(variable)
126  .setAggregate("count(entityPath)")
127  .getClause();
128  break;
129  case "seen":
130  wc = PreprocessClause.load("EntityParticipation")
131  .addCondition("and reason = :reason")
133  .setRankingRoot(entityRoot)
134  .setRankingField("entityPath")
135  .setVariable(variable)
136  .setAggregate("count(entityPath)")
137  .getClause();
138  break;
139  case "like":
140  wc = PreprocessClause.load("EntityParticipation")
141  .addCondition("and reason = :reason")
143  .setRankingRoot(entityRoot)
144  .setRankingField("entityPath")
145  .setVariable(variable)
146  .setAggregate("count(entityPath)")
147  .getClause();
148  break;
149  case "apply":
150  wc = PreprocessClause.load("EntityParticipation")
151  .addCondition("and reason = :reason")
153  .setRankingRoot(entityRoot)
154  .setRankingField("entityPath")
155  .setVariable(variable)
156  .setAggregate("count(entityPath)")
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  switch(variable.getName()) {
176  case "following":
177  wc = PreprocessClause.load("EntityParticipation")
178  .addCondition("and reason = :reason")
180  .setRankingRoot(entityRoot)
181  .setMatchingRoot(relatedRoot)
182  .setRankingField("entityPath")
183  .setMatchingField("participatorPath")
185  .setVariable(variable)
186  .setAggregate("count(entityPath)")
187  .getClause();
188  break;
189  case "followed":
190  wc = PreprocessClause.load("EntityParticipation")
191  .addCondition("and reason = :reason")
193  .setRankingRoot(relatedRoot)
194  .setMatchingRoot(entityRoot)
195  .setRankingField("participatorPath")
196  .setMatchingField("entityPath")
198  .setVariable(variable)
199  .setAggregate("count(participatorPath)")
200  .getClause();
201  break;
202  case "seen":
203  wc = PreprocessClause.load("EntityParticipation")
204  .addCondition("and reason = :reason")
206  .setRankingRoot(entityRoot)
207  .setMatchingRoot(relatedRoot)
208  .setRankingField("entityPath")
209  .setMatchingField("participatorPath")
211  .setVariable(variable)
212  .setAggregate("count(entityPath)")
213  .getClause();
214  break;
215  case "like":
216  wc = PreprocessClause.load("EntityParticipation")
217  .addCondition("and reason = :reason")
219  .setRankingRoot(entityRoot)
220  .setMatchingRoot(relatedRoot)
221  .setRankingField("entityPath")
222  .setMatchingField("participatorPath")
224  .setVariable(variable)
225  .setAggregate("count(entityPath)")
226  .getClause();
227  break;
228  case "apply":
229  wc = PreprocessClause.load("EntityParticipation")
230  .addCondition("and reason = :reason")
232  .setRankingRoot(entityRoot)
233  .setMatchingRoot(relatedRoot)
234  .setRankingField("entityPath")
235  .setMatchingField("participatorPath")
237  .setVariable(variable)
238  .setAggregate("count(entityPath)")
239  .getClause();
240  break;
241  }
242  }
243  if(wc != null) {
244  try(Stream<GenericMatching> stream = dao.stream(GenericMatching.class, wc, 1000)) {
245  stream.forEach(r -> {
246  preprocessor.poolInstance(r.copyTo((IMatching) preprocessor.createInstance()));
247  });
248  preprocessor.finishPreprocessor();
249  }
250  }
251  }
252 
253 }
void addVariable(String name, VariableType type)
void processMatchingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot, String relatedRoot)
void processRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot)
PreprocessClause setMatchingField(String pathField)
PreprocessClause addCondition(String condition)
PreprocessClause setVariable(IndicatorVariable variable)
PreprocessClause setMatchingRoot(String entityRoot)
PreprocessClause addNamedValue(String name, Object value)
PreprocessClause setAggregate(String aggregate)
PreprocessClause setRankingRoot(String entityRoot)
static PreprocessClause load(String table)
PreprocessClause setRankingField(String pathField)
void poolInstance(Object value)