BrightSide Workbench Full Report + Source Code
MatchingControl.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.matching;
20 
21 import org.turro.string.Strings;
22 import org.turro.auth.Authentication;
23 import org.turro.elephant.db.WhereClause;
24 import org.turro.entities.EntityCombobox;
25 import org.turro.indicator.Formulas;
26 import org.turro.indicator.Indicators;
27 import org.turro.indicator.VariablesListbox;
28 import org.turro.log.SystemLogType;
29 import org.turro.log.SystemLogger;
30 import org.turro.path.Path;
31 import org.turro.reflection.Instances;
32 import org.turro.reflection.Reflections;
33 import org.zkoss.zk.ui.Executions;
34 import org.zkoss.zk.ui.IdSpace;
35 import org.zkoss.zk.ui.event.Event;
36 import org.zkoss.zk.ui.select.Selectors;
37 import org.zkoss.zk.ui.select.annotation.Listen;
38 import org.zkoss.zk.ui.select.annotation.Wire;
39 import org.zkoss.zul.Checkbox;
40 import org.zkoss.zul.Grid;
41 import org.zkoss.zul.ListModel;
42 import org.zkoss.zul.ListModelList;
43 import org.zkoss.zul.Panel;
44 import org.zkoss.zul.Textbox;
45 
50 public class MatchingControl extends Panel implements IdSpace {
51 
52  @Wire("#matching")
53  private MatchingCombobox matching;
54 
55  @Wire("#formula")
56  private Textbox formula;
57 
58  @Wire("#variables")
59  private VariablesListbox variables;
60 
61  @Wire("#orderBy")
62  private VariablesListbox orderBy;
63 
64  @Wire("#related")
65  private EntityCombobox related;
66 
67  @Wire("#reversed")
68  private Checkbox reversed;
69 
70  @Wire("#result")
71  private Grid result;
72 
73  @Listen("onSelect = #matching")
74  public void onMatching() {
75  resetProcess();
76  if(getProcess() != null) {
77  variables.setModel(new ListModelList(Indicators.getMatchingVariables(getProcess().indicatorsRoot())));
78  orderBy.setModel(new ListModelList(Indicators.getMatchingVariablesFinal(getProcess().indicatorsRoot())));
79  }
80  updateControls();
81  }
82 
83  @Listen("onSelect = #orderBy")
84  public void onOrderBy() {
85  updateControls();
86  }
87 
88  @Listen("onSelect = #variables")
89  public void onVariable() {
90  if(variables.getObjectValue() != null) {
91  formula.setValue(formula.getValue() + " " + variables.getObjectValue().getName());
92  }
93  }
94 
95  @Listen("onSelect = #related")
96  public void onPointOfView() {
97  updateControls();
98  }
99 
100  @Listen("onCheck = #reversed")
101  public void onReversed() {
102  updateControls();
103  }
104 
105  public void onSelectItem(Event evt) {
106  Path path = new Path((String) evt.getData());
107  related.setRoot(path.getRoot());
108  related.setEntityPath(path.getPath());
109  reversed.setChecked(!reversed.isChecked());
110  updateControls();
111  }
112 
113  @Listen("onClick = #execute")
114  public void onExecute() {
115  if(!Strings.isBlank(matching.getObjectValue())) {
116  Formulas.load().setFormula(Reflections.of(matching.getObjectValue()).simpleName(), formula.getValue());
117  //executeFor(matching.getObjectValue());
118  updateControls();
119  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, "/matching", Reflections.of(matching.getObjectValue()).simpleName(), formula.getValue());
120  }
121  }
122 
123  private void updateControls() {
124  if(getProcess() != null) {
125  String root = reversed.isChecked() ? getProcess().indicatorsRoot() : getProcess().relatedRoot();
126  related.setRoot(root);
127  if(related.getObjectValue() == null && "contact".equals(root)) {
129  }
130  formula.setValue(Formulas.load().getFormula(Reflections.of(matching.getObjectValue()).simpleName()));
131  result.setModel(getModel());
132  } else {
133  formula.setValue(null);
134  variables.setModel(new ListModelList());
135  orderBy.setModel(new ListModelList());
136  result.setModel(new ListModelList());
137  }
138  }
139 
140  private ListModel<?> getModel() {
141  ListModelList list = new ListModelList();
142  if(related.getObjectValue() != null && getProcess() != null) {
143  WhereClause wc = new WhereClause();
144  wc.addClause("select m from " + getProcess().instanceClass().getSimpleName() + " m");
145  wc.addClause("where m.concept = :concept");
146  if(orderBy.getObjectValue() == null) {
147  wc.addNamedValue("concept", "*");
148  } else {
149  wc.addNamedValue("concept", orderBy.getObjectValue().getName());
150  }
151  if(reversed.isChecked()) {
152  wc.addClause("and m.entityPath = :related");
153  } else {
154  wc.addClause("and m.relatedPath = :related");
155  }
156  wc.addNamedValue("related", related.getEntityPath());
157  wc.addClause("order by m.matching DESC");
158  list.addAll(getProcess().createDao().getResultList(wc));
159  }
160  return list;
161  }
162 
163  private void executeFor(String matchingEntity) {
164  if(getProcess() != null) {
165  getProcess().startProcess();
166  }
167  }
168 
169  /* Process */
170 
171  private ProcessMatching currentProcess;
172 
173  private void resetProcess() {
174  currentProcess = null;
175  }
176 
177  private ProcessMatching getProcess() {
178  if(currentProcess == null) {
179  for(ProcessMatching process : Instances.cached().byAnnotation(ElephantMatching.class, ProcessMatching.class)) {
180  if(process.instanceClass().getName().equals(matching.getObjectValue())) {
181  currentProcess = process;
182  }
183  }
184  }
185  return currentProcess;
186  }
187 
188  /* Controls */
189 
190  public MatchingControl() {
191  Executions.createComponents("/WEB-INF/_zul/bs/comps/matching/matchingControl.zul", this, null);
192  Selectors.wireComponents(this, this, false);
193  Selectors.wireEventListeners(this, this);
194  }
195 
196 }
void setEntityPath(String entityPath)
static Formulas load()
Definition: Formulas.java:90
void setFormula(String classContext, String value)
Definition: Formulas.java:54
static List< IndicatorVariable > getMatchingVariables(String root)
Definition: Indicators.java:47
static List< IndicatorVariable > getMatchingVariablesFinal(String root)
Definition: Indicators.java:58
static ISystemLogger getInstance()
void doLog(SystemLogType type, Object entity, String comment, Serializable data)