19 package org.turro.indicator;
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;
41 private final String rankingInstance, matchingInstance;
43 private String entityRoot, entityId, defaultSorting;
45 private Consumer<WhereClause> onCriteria;
46 private boolean allowMatching, allowRanking;
47 private boolean doMatching =
false;
48 private String sortOrder =
"*";
49 private boolean useDaoSearch =
true;
58 defaultSorting = sortOrder;
63 this.contact = contact;
68 this.onCriteria = onCriteria;
73 this.allowMatching = allowMatching && !Strings.isBlank(matchingInstance);
78 this.allowRanking = allowRanking && !Strings.isBlank(rankingInstance);
88 this.sortOrder = sortOrder;
98 if(missingValues())
throw new NullPointerException(
"Missing values!");
99 readDaoSearchValues();
101 if(allowMatching && contact !=
null && contact.
isValid() && doMatching) {
102 Matchings.
addJoin(wc, matchingInstance, entityRoot, entityId, contact, sortOrder);
107 Rankings.
addJoin(wc, rankingInstance, entityRoot, entityId, sortOrder);
110 if(onCriteria !=
null) onCriteria.accept(wc);
111 wc.
addClause(
"order by " + orderBy + defaultSorting);
116 return new Statistics(constructor, rankingInstance, matchingInstance, wc);
121 wc.
addClause(
"select new org.turro.indicator.Aggregates(");
123 wc.
addClause(
"min(t." + aggregateField +
"),");
124 wc.
addClause(
"max(t." + aggregateField +
"),");
125 wc.
addClause(
"avg(t." + aggregateField +
")");
128 if(onFilter !=
null) onFilter.accept(wc);
132 wc.
addClause(
"select t." + aggregateField +
" from " + table +
" t");
133 if(onFilter !=
null) onFilter.accept(wc);
134 wc.
addClause(
"order by t." + aggregateField +
" asc");
141 if(values !=
null && !values.isEmpty()) {
142 if(values.size() == 1) {
144 }
else if(values.size() == 2) {
153 this.constructor = constructor;
154 this.rankingInstance = rankingInstance;
155 this.matchingInstance = matchingInstance;
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);
166 private void readDaoSearchValues() {
168 DaoHtmlSearch dhs = DaoHtmlSearch.getInstance(constructor, ElephantContext.getContextVariable(constructor));
170 DaoSearchKey dsk = dhs.get(
"point-of-view");
172 doMatching =
"1".equals(dsk.getValue());
174 dsk = dhs.get(
"sort-order");
176 sortOrder = dsk.getValue();
void addClause(String clause)
void setMedian(double median)
Statistics allowMatching(boolean allowMatching)
Statistics allowRanking(boolean allowRanking)
Statistics setEntityFields(String root, String id)
static Statistics load(IConstructor constructor, String rankingInstance, String matchingInstance, WhereClause wc)
Statistics doMatching(boolean doIt)
Statistics setSortOrder(String sortOrder)
Statistics useDaoSearch(boolean doIt)
static Aggregates aggregates(Dao dao, String table, String aggregateField, Consumer< WhereClause > onFilter)
Statistics setContact(IContact contact)
Statistics onCriteria(Consumer< WhereClause > onCriteria)
Statistics setDefaultSorting(String sortOrder)
Object getSingleResultOrNull(SqlClause sc)
static String getOrdering()
static void addJoin(WhereClause wc, String table, String entityRoot, String entityId, IContact contact, String sortOrder)
static String getOrdering()
static void addJoin(WhereClause wc, String table, String entityRoot, String entityId, String sortOrder)