BrightSide Workbench Full Report + Source Code
RankingControl.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.ranking;
20 
21 import org.turro.string.Strings;
22 import org.turro.elephant.db.WhereClause;
23 import org.turro.indicator.Formulas;
24 import org.turro.indicator.Indicators;
25 import org.turro.indicator.VariablesListbox;
26 import org.turro.log.SystemLogType;
27 import org.turro.log.SystemLogger;
28 import org.turro.reflection.Instances;
29 import org.turro.reflection.Reflections;
30 import org.zkoss.zk.ui.Executions;
31 import org.zkoss.zk.ui.IdSpace;
32 import org.zkoss.zk.ui.select.Selectors;
33 import org.zkoss.zk.ui.select.annotation.Listen;
34 import org.zkoss.zk.ui.select.annotation.Wire;
35 import org.zkoss.zul.Grid;
36 import org.zkoss.zul.ListModel;
37 import org.zkoss.zul.ListModelList;
38 import org.zkoss.zul.Panel;
39 import org.zkoss.zul.Textbox;
40 
45 public class RankingControl extends Panel implements IdSpace {
46 
47  @Wire("#ranking")
48  private RankingCombobox ranking;
49 
50  @Wire("#formula")
51  private Textbox formula;
52 
53  @Wire("#variables")
54  private VariablesListbox variables;
55 
56  @Wire("#orderBy")
57  private VariablesListbox orderBy;
58 
59  @Wire("#result")
60  private Grid result;
61 
62  @Listen("onSelect = #ranking")
63  public void onRanking() {
64  for(ProcessRanking process : Instances.cached().byAnnotation(ElephantRanking.class, ProcessRanking.class)) {
65  if(process.instanceClass().getName().equals(ranking.getObjectValue())) {
66  variables.setModel(new ListModelList(Indicators.getRankingVariables(process.indicatorsRoot())));
67  orderBy.setModel(new ListModelList(Indicators.getRankingVariablesFinal(process.indicatorsRoot())));
68  }
69  }
70  updateControls();
71  }
72 
73  @Listen("onSelect = #orderBy")
74  public void onOrderBy() {
75  updateControls();
76  }
77 
78  @Listen("onSelect = #variables")
79  public void onVariable() {
80  if(variables.getObjectValue() != null) {
81  formula.setValue(formula.getValue() + " " + variables.getObjectValue().getName());
82  }
83  }
84 
85  @Listen("onClick = #execute")
86  public void onExecute() {
87  if(!Strings.isBlank(ranking.getObjectValue())) {
88  Formulas.load().setFormula(Reflections.of(ranking.getObjectValue()).simpleName(), formula.getValue());
89  executeFor(ranking.getObjectValue());
90  updateControls();
91  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, "/ranking", Reflections.of(ranking.getObjectValue()).simpleName(), formula.getValue());
92  }
93  }
94 
95  private void updateControls() {
96  if(!Strings.isBlank(ranking.getObjectValue())) {
97  formula.setValue(Formulas.load().getFormula(Reflections.of(ranking.getObjectValue()).simpleName()));
98  result.setModel(getModelFor(ranking.getObjectValue()));
99  } else {
100  formula.setValue(null);
101  variables.setModel(new ListModelList());
102  orderBy.setModel(new ListModelList());
103  result.setModel(new ListModelList());
104  }
105  }
106 
107  private ListModel<?> getModelFor(String rankingEntity) {
108  ListModelList list = new ListModelList();
109  for(ProcessRanking process : Instances.cached().byAnnotation(ElephantRanking.class, ProcessRanking.class)) {
110  if(process.instanceClass().getName().equals(rankingEntity)) {
111  WhereClause wc = new WhereClause();
112  wc.addClause("select r from " + process.instanceClass().getSimpleName() + " r");
113  wc.addClause("where r.concept = :concept");
114  if(orderBy.getObjectValue() == null) {
115  wc.addNamedValue("concept", "*");
116  } else {
117  wc.addNamedValue("concept", orderBy.getObjectValue().getName());
118  }
119  wc.addClause("order by r.ranking DESC");
120  list.addAll(process.createDao().getResultList(wc));
121  return list;
122  }
123  }
124  return list;
125  }
126 
127  private void executeFor(String rankingEntity) {
128  for(ProcessRanking process : Instances.cached().byAnnotation(ElephantRanking.class, ProcessRanking.class)) {
129  if(process.instanceClass().getName().equals(rankingEntity)) {
130  process.startProcess();
131  }
132  }
133  }
134 
135  /* Controls */
136 
137  public RankingControl() {
138  Executions.createComponents("/WEB-INF/_zul/bs/comps/ranking/rankingControl.zul", this, null);
139  Selectors.wireComponents(this, this, false);
140  Selectors.wireEventListeners(this, this);
141  }
142 
143 }
static Formulas load()
Definition: Formulas.java:90
void setFormula(String classContext, String value)
Definition: Formulas.java:54
String getFormula(Class javaClass)
Definition: Formulas.java:38
static List< IndicatorVariable > getRankingVariables(String root)
Definition: Indicators.java:42
static List< IndicatorVariable > getRankingVariablesFinal(String root)
Definition: Indicators.java:52
static ISystemLogger getInstance()
void doLog(SystemLogType type, Object entity, String comment, Serializable data)