BrightSide Workbench Full Report + Source Code
PreprocessClause.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.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import org.turro.string.Strings;
26 import org.turro.elephant.db.WhereClause;
27 
32 public class PreprocessClause {
33 
34  private String table, entityRoot, relatedRoot, pathField,
35  relatedField, aggregate, filterEntity;
36  private boolean matchingPath;
37  private IndicatorVariable variable;
38  private final List<String> joins = new ArrayList<>();
39  private final List<String> conditions = new ArrayList<>();
40  private final Map<String, Object> namedValues = new HashMap<>();
41 
42  public static PreprocessClause load(String table) {
43  return new PreprocessClause(table);
44  }
45 
46  public PreprocessClause setRankingRoot(String entityRoot) {
47  this.entityRoot = entityRoot;
48  return this;
49  }
50 
51  public PreprocessClause setMatchingRoot(String entityRoot) {
52  this.relatedRoot = entityRoot;
53  return this;
54  }
55 
56  public PreprocessClause setRankingField(String pathField) {
57  this.pathField = pathField;
58  return this;
59  }
60 
61  public PreprocessClause setMatchingField(String pathField) {
62  this.relatedField = pathField;
63  return this;
64  }
65 
67  this.matchingPath = true;
68  return this;
69  }
70 
71  public PreprocessClause setAggregate(String aggregate) {
72  this.aggregate = aggregate;
73  return this;
74  }
75 
76  public PreprocessClause setFilter(String filterEntity) {
77  this.filterEntity = filterEntity;
78  return this;
79  }
80 
82  this.variable = variable;
83  return this;
84  }
85 
86  public PreprocessClause addJoin(String join) {
87  joins.add(join);
88  return this;
89  }
90 
91  public PreprocessClause addCondition(String condition) {
92  conditions.add(condition);
93  return this;
94  }
95 
96  public PreprocessClause addNamedValue(String name, Object value) {
97  namedValues.put(name, value);
98  return this;
99  }
100 
102  if(Strings.isBlank(relatedRoot)) {
103  return getRankingClause();
104  } else {
105  return getMatchingClause();
106  }
107  }
108 
110  WhereClause wc = new WhereClause();
111  wc.addClause("select new org.turro.ranking.GenericRanking(");
112  wc.addClause(pathField + ", '" + variable.getName() + "', " + aggregate + ")");
113  wc.addClause("from " + table);
114  for(String join : joins) {
115  wc.addClause(join);
116  }
117  if(!Strings.isBlank(filterEntity)) {
118  wc.addClause("where " + pathField + " like concat(:entity, '%')");
119  wc.addNamedValue("entity", filterEntity);
120  } else {
121  wc.addClause("where " + pathField + " like concat('/', :root, '/%')");
122  wc.addNamedValue("root", entityRoot);
123  }
124  for(String condition : conditions) {
125  wc.addClause(condition);
126  }
127  namedValues.keySet().forEach(k -> wc.addNamedValue(k, namedValues.get(k)));
128  wc.addClause("group by " + pathField);
129  wc.addClause("having " + aggregate + " <> 0");
130  return wc;
131  }
132 
134  WhereClause wc = new WhereClause();
135  wc.addClause("select new org.turro.matching.GenericMatching(");
136  String relatedConcat = matchingPath ? relatedField :
137  "concat('/" + relatedRoot + "/'," + relatedField + ")";
138  wc.addClause(pathField + ", " + relatedConcat + ", '" + variable.getName() + "', " + aggregate + ")");
139  wc.addClause("from " + table);
140  for(String join : joins) {
141  wc.addClause(join);
142  }
143  if(!Strings.isBlank(filterEntity)) {
144  wc.addClause("where " + pathField + " like concat(:entity, '%')");
145  wc.addNamedValue("entity", filterEntity);
146  } else {
147  wc.addClause("where " + pathField + " like concat('/', :root, '/%')");
148  wc.addNamedValue("root", entityRoot);
149  }
150  wc.addClause("and " + relatedConcat + " like concat('/', :rroot, '/%')");
151  wc.addNamedValue("rroot", relatedRoot);
152  for(String condition : conditions) {
153  wc.addClause(condition);
154  }
155  namedValues.keySet().forEach(k -> wc.addNamedValue(k, namedValues.get(k)));
156  wc.addClause("group by " + pathField + "," + relatedConcat);
157  wc.addClause("having " + aggregate + " <> 0");
158  return wc;
159  }
160 
161  private PreprocessClause(String table) {
162  this.table = table;
163  }
164 
165 }
void addNamedValue(String name, Object value)
PreprocessClause setMatchingField(String pathField)
PreprocessClause addCondition(String condition)
PreprocessClause setFilter(String filterEntity)
PreprocessClause setVariable(IndicatorVariable variable)
PreprocessClause addJoin(String join)
PreprocessClause setMatchingRoot(String entityRoot)
PreprocessClause addNamedValue(String name, Object value)
PreprocessClause setAggregate(String aggregate)
PreprocessClause setRankingRoot(String entityRoot)
static PreprocessClause load(String table)
PreprocessClause setRankingField(String pathField)