BrightSide Workbench Full Report + Source Code
ForumIndicator.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.ranking.GenericRanking;
29 import org.turro.ranking.IRanking;
30 
35 @ElephantIndicator
36 public class ForumIndicator extends AbstractIndicator {
37 
38  public ForumIndicator() {
39  root = "forum";
40  }
41 
42  @Override
43  protected void initVariables() {
44  addVariable("topics", true, VariableType.BOTH_VARIABLE);
45  addVariable("posts", true, VariableType.BOTH_VARIABLE);
46  }
47 
48  @Override
49  protected void initExternalVariables() {
50  }
51 
52  @Override
53  protected Dao createDao() {
54  return new ElephantPU();
55  }
56 
57  @Override
58  protected void processRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot) {
59  WhereClause wc = null;
60  if("contact".equals(entityRoot)) {
61  switch(variable.getName()) {
62  case "topics":
63  wc = PreprocessClause.load("Topic")
64  .setRankingRoot(entityRoot)
65  .setRankingField("concat('/contact/', authorId)")
66  .setVariable(variable)
67  .setAggregate("count(id)")
68  .getClause();
69  break;
70  case "posts":
71  wc = PreprocessClause.load("Post")
72  .setRankingRoot(entityRoot)
73  .setRankingField("concat('/contact/', authorId)")
74  .setVariable(variable)
75  .setAggregate("count(id)")
76  .getClause();
77  break;
78  }
79  } else {
80  switch(variable.getName()) {
81  case "topics":
82  wc = PreprocessClause.load("Topic")
83  .setRankingRoot(entityRoot)
84  .setRankingField("entityPath")
85  .setVariable(variable)
86  .setAggregate("count(id)")
87  .getClause();
88  break;
89  case "posts":
90  wc = PreprocessClause.load("Post")
91  .setRankingRoot(entityRoot)
92  .setRankingField("topic.entityPath")
93  .setVariable(variable)
94  .setAggregate("count(id)")
95  .getClause();
96  break;
97  }
98  }
99  if(wc != null) {
100  try(Stream<GenericRanking> stream = dao.stream(GenericRanking.class, wc, 1000)) {
101  stream.forEach(r -> {
102  preprocessor.poolInstance(r.copyTo((IRanking) preprocessor.createInstance()));
103  });
104  preprocessor.finishPreprocessor();
105  }
106  }
107  }
108 
109  @Override
110  protected void processMatchingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot, String relatedRoot) {
111  WhereClause wc = null;
112  if("contact".equals(relatedRoot)) {
113  switch(variable.getName()) {
114  case "topics":
115  wc = PreprocessClause.load("Topic")
116  .setRankingRoot(entityRoot)
117  .setMatchingRoot(relatedRoot)
118  .setRankingField("entityPath")
119  .setMatchingField("authorId")
120  .setVariable(variable)
121  .setAggregate("count(id)")
122  .getClause();
123  break;
124  case "posts":
125  wc = PreprocessClause.load("Post")
126  .setRankingRoot(entityRoot)
127  .setMatchingRoot(relatedRoot)
128  .setRankingField("topic.entityPath")
129  .setMatchingField("authorId")
130  .setVariable(variable)
131  .setAggregate("count(id)")
132  .getClause();
133  break;
134  }
135  }
136  if(wc != null) {
137  try(Stream<GenericMatching> stream = dao.stream(GenericMatching.class, wc, 1000)) {
138  stream.forEach(m -> {
139  preprocessor.poolInstance(m.copyTo((IMatching) preprocessor.createInstance()));
140  });
141  preprocessor.finishPreprocessor();
142  }
143  }
144  }
145 
146 }
147 
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 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)