BrightSide Workbench Full Report + Source Code
Statistics.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.List;
22 import java.util.function.Consumer;
23 import org.turro.string.Strings;
24 import org.turro.elephant.context.ElephantContext;
25 import org.turro.elephant.context.IConstructor;
26 import org.turro.elephant.db.WhereClause;
27 import org.turro.jpa.Dao;
28 import org.turro.jpa.search.DaoHtmlSearch;
29 import org.turro.jpa.search.DaoSearchKey;
30 import org.turro.matching.Matchings;
31 import org.turro.plugin.contacts.IContact;
32 import org.turro.ranking.Rankings;
33 
38 public class Statistics {
39 
40  private final IConstructor constructor;
41  private final String rankingInstance, matchingInstance;
42  private final WhereClause wc;
43  private String entityRoot, entityId, defaultSorting;
44  private IContact contact;
45  private Consumer<WhereClause> onCriteria;
46  private boolean allowMatching, allowRanking;
47  private boolean doMatching = false;
48  private String sortOrder = "*";
49  private boolean useDaoSearch = true;
50 
51  public Statistics setEntityFields(String root, String id) {
52  entityRoot = root;
53  entityId = id;
54  return this;
55  }
56 
57  public Statistics setDefaultSorting(String sortOrder) {
58  defaultSorting = sortOrder;
59  return this;
60  }
61 
62  public Statistics setContact(IContact contact) {
63  this.contact = contact;
64  return this;
65  }
66 
67  public Statistics onCriteria(Consumer<WhereClause> onCriteria) {
68  this.onCriteria = onCriteria;
69  return this;
70  }
71 
72  public Statistics allowMatching(boolean allowMatching) {
73  this.allowMatching = allowMatching && !Strings.isBlank(matchingInstance);
74  return this;
75  }
76 
77  public Statistics allowRanking(boolean allowRanking) {
78  this.allowRanking = allowRanking && !Strings.isBlank(rankingInstance);
79  return this;
80  }
81 
82  public Statistics doMatching(boolean doIt) {
83  doMatching = doIt;
84  return this;
85  }
86 
87  public Statistics setSortOrder(String sortOrder) {
88  this.sortOrder = sortOrder;
89  return this;
90  }
91 
92  public Statistics useDaoSearch(boolean doIt) {
93  useDaoSearch = doIt;
94  return this;
95  }
96 
98  if(missingValues()) throw new NullPointerException("Missing values!");
99  readDaoSearchValues();
100  String orderBy = "";
101  if(allowMatching && contact != null && contact.isValid() && doMatching) {
102  Matchings.addJoin(wc, matchingInstance, entityRoot, entityId, contact, sortOrder);
103  orderBy += Matchings.getOrdering() + ", ";
104  sortOrder = "*";
105  }
106  if(allowRanking) {
107  Rankings.addJoin(wc, rankingInstance, entityRoot, entityId, sortOrder);
108  orderBy += Rankings.getOrdering() + ", ";
109  }
110  if(onCriteria != null) onCriteria.accept(wc);
111  wc.addClause("order by " + orderBy + defaultSorting);
112  return wc;
113  }
114 
115  public static Statistics load(IConstructor constructor, String rankingInstance, String matchingInstance, WhereClause wc) {
116  return new Statistics(constructor, rankingInstance, matchingInstance, wc);
117  }
118 
119  public static Aggregates aggregates(Dao dao, String table, String aggregateField, Consumer<WhereClause> onFilter) {
120  WhereClause wc = new WhereClause();
121  wc.addClause("select new org.turro.indicator.Aggregates(");
122  wc.addClause("count(t),");
123  wc.addClause("min(t." + aggregateField + "),");
124  wc.addClause("max(t." + aggregateField + "),");
125  wc.addClause("avg(t." + aggregateField + ")");
126  wc.addClause(")");
127  wc.addClause("from " + table + " t");
128  if(onFilter != null) onFilter.accept(wc);
130  if(aggregates != null && aggregates.getCount() > 0) {
131  wc = new WhereClause();
132  wc.addClause("select t." + aggregateField + " from " + table + " t");
133  if(onFilter != null) onFilter.accept(wc);
134  wc.addClause("order by t." + aggregateField + " asc");
135  List<Double> values;
136  if(aggregates.getCount() % 2 == 0) {
137  values = dao.getResultList(wc, (int) Math.floor(aggregates.getCount() / 2), 2);
138  } else {
139  values = dao.getResultList(wc, (int) Math.floor(aggregates.getCount() / 2) + 1, 1);
140  }
141  if(values != null && !values.isEmpty()) {
142  if(values.size() == 1) {
143  aggregates.setMedian(values.get(0));
144  } else if(values.size() == 2) {
145  aggregates.setMedian((values.get(0) + values.get(1)) / 2.0);
146  }
147  }
148  }
149  return aggregates;
150  }
151 
152  private Statistics(IConstructor constructor, String rankingInstance, String matchingInstance, WhereClause wc) {
153  this.constructor = constructor;
154  this.rankingInstance = rankingInstance;
155  this.matchingInstance = matchingInstance;
156  this.wc = wc;
157  }
158 
159  private boolean missingValues() {
160  return constructor == null || (allowRanking && Strings.isBlank(rankingInstance)) ||
161  (allowMatching && Strings.isBlank(matchingInstance) && contact != null) ||
162  wc == null || Strings.isBlank(entityRoot) || Strings.isBlank(entityId) ||
163  Strings.isBlank(defaultSorting);
164  }
165 
166  private void readDaoSearchValues() {
167  if(useDaoSearch) {
168  DaoHtmlSearch dhs = DaoHtmlSearch.getInstance(constructor, ElephantContext.getContextVariable(constructor));
169  if(dhs != null) {
170  DaoSearchKey dsk = dhs.get("point-of-view");
171  if(dsk != null) {
172  doMatching = "1".equals(dsk.getValue());
173  }
174  dsk = dhs.get("sort-order");
175  if(dsk != null) {
176  sortOrder = dsk.getValue();
177  }
178  }
179  }
180  }
181 
182 }
void setMedian(double median)
Definition: Aggregates.java:38
Statistics allowMatching(boolean allowMatching)
Definition: Statistics.java:72
Statistics allowRanking(boolean allowRanking)
Definition: Statistics.java:77
Statistics setEntityFields(String root, String id)
Definition: Statistics.java:51
static Statistics load(IConstructor constructor, String rankingInstance, String matchingInstance, WhereClause wc)
Statistics doMatching(boolean doIt)
Definition: Statistics.java:82
Statistics setSortOrder(String sortOrder)
Definition: Statistics.java:87
Statistics useDaoSearch(boolean doIt)
Definition: Statistics.java:92
static Aggregates aggregates(Dao dao, String table, String aggregateField, Consumer< WhereClause > onFilter)
Statistics setContact(IContact contact)
Definition: Statistics.java:62
Statistics onCriteria(Consumer< WhereClause > onCriteria)
Definition: Statistics.java:67
Statistics setDefaultSorting(String sortOrder)
Definition: Statistics.java:57
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
static String getOrdering()
Definition: Matchings.java:56
static void addJoin(WhereClause wc, String table, String entityRoot, String entityId, IContact contact, String sortOrder)
Definition: Matchings.java:47
static String getOrdering()
Definition: Rankings.java:52
static void addJoin(WhereClause wc, String table, String entityRoot, String entityId, String sortOrder)
Definition: Rankings.java:46