BrightSide Workbench Full Report + Source Code
AttachmentIndicator.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.attach.db.AttachPU;
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 AttachmentIndicator extends AbstractIndicator {
37 
39  root = "attach";
40  }
41 
42  @Override
43  protected void initVariables() {
44  addVariable("attachments", true, VariableType.BOTH_VARIABLE);
45  }
46 
47  @Override
48  protected void initExternalVariables() {
49  }
50 
51  @Override
52  protected Dao createDao() {
53  return new AttachPU();
54  }
55 
56  @Override
57  protected void processRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot) {
58  WhereClause wc = null;
59  switch(variable.getName()) {
60  case "attachments":
61  wc = PreprocessClause.load("Attachment")
62  .setRankingRoot(entityRoot)
63  .setRankingField("path")
64  .setVariable(variable)
65  .setAggregate("count(id)")
66  .getClause();
67  break;
68  }
69  if(wc != null) {
70  try(Stream<GenericRanking> stream = dao.stream(GenericRanking.class, wc, 1000)) {
71  stream.forEach(r -> {
72  preprocessor.poolInstance(r.copyTo((IRanking) preprocessor.createInstance()));
73  });
74  preprocessor.finishPreprocessor();
75  }
76  }
77  }
78 
79  @Override
80  protected void processMatchingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot, String relatedRoot) {
81  WhereClause wc = null;
82  if("contact".equals(relatedRoot)) {
83  switch(variable.getName()) {
84  case "attachments":
85  wc = PreprocessClause.load("Attachment")
86  .setRankingRoot(entityRoot)
87  .setMatchingRoot(relatedRoot)
88  .setRankingField("path")
89  .setMatchingField("owner")
90  .setVariable(variable)
91  .setAggregate("count(id)")
92  .getClause();
93  break;
94  }
95  }
96  if(wc != null) {
97  try(Stream<GenericMatching> stream = dao.stream(GenericMatching.class, wc, 1000)) {
98  stream.forEach(r -> {
99  preprocessor.poolInstance(r.copyTo((IMatching) preprocessor.createInstance()));
100  });
101  preprocessor.finishPreprocessor();
102  }
103  }
104  }
105 
106 }
107 
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)