BrightSide Workbench Full Report + Source Code
GenericIndicator.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.contacts.db.ContactsPU;
24 import org.turro.elephant.db.WhereClause;
25 import org.turro.jpa.Dao;
26 import org.turro.matching.GenericMatching;
27 import org.turro.matching.GenericSiblings;
28 import org.turro.matching.IMatching;
29 import org.turro.path.Path;
30 import org.turro.ranking.GenericRanking;
31 import org.turro.ranking.IRanking;
32 
37 @ElephantIndicator
38 public class GenericIndicator extends AbstractIndicator {
39 
40  public GenericIndicator() {
41  root = "generic";
42  }
43 
44  @Override
45  protected void initVariables() {
46  addVariable("stars", true, VariableType.BOTH_VARIABLE);
47  addVariable("comments", true, VariableType.BOTH_VARIABLE);
48  }
49 
50  @Override
51  protected void initExternalVariables() {
52  }
53 
54  @Override
55  protected Dao createDao() {
56  return new ContactsPU();
57  }
58 
59  @Override
60  protected void processRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot) {
61  WhereClause wc = null;
62  switch(variable.getName()) {
63  case "stars":
64  wc = PreprocessClause.load("StarIt")
65  .setRankingRoot(entityRoot)
66  .setRankingField("path")
67  .setVariable(variable)
68  .setAggregate("avg(stars)")
69  .getClause();
70  break;
71  case "comments":
72  wc = PreprocessClause.load("CommentIt")
73  .setRankingRoot(entityRoot)
74  .setRankingField("path")
75  .setVariable(variable)
76  .setAggregate("count(id)")
77  .getClause();
78  break;
79  }
80  if(wc != null) {
81  try(Stream<GenericRanking> stream = dao.stream(GenericRanking.class, wc, 1000)) {
82  stream.forEach(r -> {
83  preprocessor.poolInstance(r.copyTo((IRanking) preprocessor.createInstance()));
84  });
85  preprocessor.finishPreprocessor();
86  }
87  }
88  }
89 
90  @Override
91  protected void processMatchingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot, String relatedRoot) {
92  WhereClause wc = null;
93  if("contact".equals(relatedRoot)) {
94  switch(variable.getName()) {
95  case "stars":
96  wc = PreprocessClause.load("StarIt")
97  .setRankingRoot(entityRoot)
98  .setMatchingRoot(relatedRoot)
99  .setRankingField("path")
100  .setMatchingField("creator.id")
101  .setVariable(variable)
102  .setAggregate("avg(stars)")
103  .getClause();
104  break;
105  case "comments":
106  wc = PreprocessClause.load("CommentIt")
107  .setRankingRoot(entityRoot)
108  .setMatchingRoot(relatedRoot)
109  .setRankingField("path")
110  .setMatchingField("creator.id")
111  .setVariable(variable)
112  .setAggregate("count(id)")
113  .getClause();
114  break;
115  }
116  }
117  if(wc != null) {
118  try(Stream<GenericMatching> stream = dao.stream(GenericMatching.class, wc, 1000)) {
119  stream.forEach(r -> {
120  preprocessor.poolInstance(r.copyTo((IMatching) preprocessor.createInstance()));
121  });
122  preprocessor.finishPreprocessor();
123  }
124  }
125  }
126 
127  @Override
128  public Stream<GenericSiblings> getSiblingPaths(String entityPath) {
129  Path path = new Path(entityPath);
130  if("contact".equals(path.getRoot())) {
131  WhereClause wc = new WhereClause();
132  wc.addClause("select new org.turro.matching.GenericSiblings(concat('/contact/', br.contact.id), concat('/contact/', br2.contact.id))");
133  wc.addClause("from BusinessRelation br, BusinessRelation br2");
134  wc.addClause("where br.business.id = br2.business.id");
135  wc.addClause("and br.business.inactive = false");
136  wc.addClause("and br.business.deactivated = false");
137  return createDao().stream(GenericSiblings.class, wc);
138  }
139  return null;
140  }
141 
142  @Override
143  public Stream<String> getEntityPaths(String entityRoot) {
144  Path path = new Path(entityRoot);
145  if("contact".equals(path.getRoot())) {
146  return createDao().stream(String.class, "select concat('/contact/', id) from Contact");
147  }
148  return null;
149  }
150 
151 }
void addVariable(String name, VariableType type)
Stream< String > getEntityPaths(String entityRoot)
void processMatchingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot, String relatedRoot)
void processRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot)
Stream< GenericSiblings > getSiblingPaths(String entityPath)
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 poolInstance(Object value)