BrightSide Workbench Full Report + Source Code
DatawarehouseVM.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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.dossier.dw;
20 
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.List;
24 import org.amic.util.date.CheckDate;
25 import org.turro.string.Strings;
26 import org.turro.action.Contacts;
27 import org.turro.dossier.db.DossierPU;
28 import org.turro.dossier.entity.Category;
29 import org.turro.dossier.entity.Dossier;
30 import org.turro.dossier.entity.IssueParticipantRole;
31 import org.turro.dossier.entity.IssueResolution;
32 import org.turro.dossier.entity.IssueStatus;
33 import org.turro.dossier.entity.IssueType;
34 import org.turro.elephant.db.WhereClause;
35 import org.turro.jpa.Dao;
36 import org.turro.plugin.contacts.IContact;
37 import org.turro.util.Chars;
38 import org.zkoss.bind.annotation.BindingParam;
39 import org.zkoss.bind.annotation.Command;
40 import org.zkoss.bind.annotation.Init;
41 import org.zkoss.bind.annotation.NotifyChange;
42 import org.zkoss.zul.SimpleCategoryModel;
43 import org.zkoss.zul.SimplePieModel;
44 
49 public class DatawarehouseVM {
50 
51  private Date start, end;
52  private Category category;
53  private Dossier dossier;
54  private IContact contact;
55  private DWChartEngine engine;
56  private List<DWChart> charts;
57  private DWReportType report;
58 
59  public Date getStart() {
60  return start;
61  }
62 
63  public void setStart(Date start) {
64  this.start = start;
65  }
66 
67  public Date getEnd() {
68  return end;
69  }
70 
71  public void setEnd(Date end) {
72  this.end = end;
73  }
74 
75  public Category getCategory() {
76  return category;
77  }
78 
79  public void setCategory(Category category) {
80  this.category = category;
81  }
82 
83  public Dossier getDossier() {
84  return dossier;
85  }
86 
87  public void setDossier(Dossier dossier) {
88  this.dossier = dossier;
89  }
90 
91  public IContact getContact() {
92  return contact;
93  }
94 
95  public void setContact(IContact contact) {
96  this.contact = contact;
97  }
98 
99  @Init
100  public void init() {
101  engine = new DWChartEngine();
102  start = new CheckDate().addYears(-5).setDay(1).getDate();
103  end = new CheckDate().addMonths(1).setDay(31).getDate();
104  charts = new ArrayList<>();
105  report = DWReportType.DW_TIMELINE;
106  }
107 
108  @NotifyChange("*")
109  @Command("update")
110  public void update() {}
111 
112  @NotifyChange("model")
113  @Command
114  public void selectEntity(@BindingParam("entity") Object entity) {
115  if(entity instanceof Category) {
116  this.category = (Category) entity;
117  this.dossier = null;
118  } else if(entity instanceof Dossier) {
119  this.dossier = (Dossier) entity;
120  this.category = null;
121  } else {
122  this.dossier = null;
123  this.category = null;
124  }
125  }
126 
127  @NotifyChange("model")
128  @Command
129  public void selectContact(@BindingParam("contact") IContact contact) {
130  setContact(contact);
131  }
132 
133  @NotifyChange("model")
134  @Command
135  public void selectReport(@BindingParam("type") DWReportType type) {
136  this.report = type;
137  }
138 
139  public List<DWChart> getModel() {
140  charts.clear();
141  if(report.equals(DWReportType.DW_TIMELINE)) {
142  charts.add(getTimelineByStatus(null));
143  charts.add(getTimelineByType(null));
144  charts.add(getTimelineByResolution(null));
145  } else if(report.equals(DWReportType.DW_PARTICIPANTS)) {
149  } else if(report.equals(DWReportType.DW_ISSUES)) {
150  charts.add(getByStatus(null));
151  charts.add(getByType(null));
152  charts.add(getByResolution(null));
153  }
154  return charts;
155  }
156 
158  SimpleCategoryModel cm = new SimpleCategoryModel();
159  WhereClause wc = new WhereClause();
160  wc.addClause("select participantId, resolution, count(distinct issueId)");
161  wc.addClause("from DWIssue");
162  wc.addClause("where participantRole = :role");
163  wc.addNamedValue("role", role);
164  IContact temp = contact;
165  contact = null;
166  addFilter(wc, "startDate", null);
167  contact = temp;
168  wc.addClause("group by participantId, resolution");
169  List<Object[]> results = getDao().getResultList(wc);
170  for(Object o[] : results) {
171  IContact participant = Contacts.getContactById((String) o[0]);
172  if(participant.isValid()) {
173  IssueResolution resolution = (IssueResolution) o[1];
174  cm.setValue(resolution.toString(), participant.getName(), (Number) o[2]);
175  }
176  }
177  DWChart chart = new DWChart();
178  chart.setTitle(role.toString());
179  chart.setModel(cm);
180  chart.setType("bar");
181  chart.setOrient("horizontal");
182  chart.setWidth("900");
183  chart.setHeight((results.size() * 90 + 40) + "");
184  return chart;
185  }
186 
188  SimplePieModel cm = new SimplePieModel();
189  WhereClause wc = new WhereClause();
190  wc.addClause("select status, count(distinct issueId)");
191  wc.addClause("from DWIssue");
192  wc.addClause("where 1=1");
193  addFilter(wc, "startDate", role);
194  wc.addClause("group by status");
195  List<Object[]> results = getDao().getResultList(wc);
196  for(Object o[] : results) {
197  IssueStatus status = (IssueStatus) o[0];
198  cm.setValue(status.toString(), (Number) o[1]);
199  }
200  DWChart chart = new DWChart();
201  chart.setTitle("Status");
202  chart.setModel(cm);
203  chart.setType("pie");
204  chart.setHeight("300");
205  chart.setWidth("500");
206  return chart;
207  }
208 
210  SimplePieModel cm = new SimplePieModel();
211  WhereClause wc = new WhereClause();
212  wc.addClause("select type, count(distinct issueId)");
213  wc.addClause("from DWIssue");
214  wc.addClause("where 1=1");
215  addFilter(wc, "startDate", role);
216  wc.addClause("group by type");
217  List<Object[]> results = getDao().getResultList(wc);
218  for(Object o[] : results) {
219  IssueType type = (IssueType) o[0];
220  cm.setValue(type.toString(), (Number) o[1]);
221  }
222  DWChart chart = new DWChart();
223  chart.setTitle("Type");
224  chart.setModel(cm);
225  chart.setType("pie");
226  chart.setHeight("300");
227  chart.setWidth("500");
228  return chart;
229  }
230 
232  SimplePieModel cm = new SimplePieModel();
233  WhereClause wc = new WhereClause();
234  wc.addClause("select resolution, count(distinct issueId)");
235  wc.addClause("from DWIssue");
236  wc.addClause("where 1=1");
237  addFilter(wc, "startDate", role);
238  wc.addClause("group by resolution");
239  List<Object[]> results = getDao().getResultList(wc);
240  for(Object o[] : results) {
241  IssueResolution resolution = (IssueResolution) o[0];
242  cm.setValue(resolution.toString(), (Number) o[1]);
243  }
244  DWChart chart = new DWChart();
245  chart.setTitle("Resolutions");
246  chart.setModel(cm);
247  chart.setType("pie");
248  chart.setHeight("300");
249  chart.setWidth("500");
250  return chart;
251  }
252 
254  SimpleCategoryModel cm = new SimpleCategoryModel();
255  WhereClause wc = new WhereClause();
256  wc.addClause("select concat(year(solvedDate), '-', month(solvedDate)), resolution, count(distinct issueId)");
257  wc.addClause("from DWIssue");
258  wc.addClause("where 1=1");
259  addFilter(wc, "solvedDate", role);
260  wc.addClause("group by concat(year(solvedDate), '-', month(solvedDate)), resolution");
261  wc.addClause("order by concat(year(solvedDate), '-', month(solvedDate))");
262  List<Object[]> results = getDao().getResultList(wc);
263  for(Object o[] : results) {
264  IssueResolution resolution = (IssueResolution) o[1];
265  String month = (String) o[0];
266  cm.setValue(resolution.toString(), month, (Number) o[2]);
267  }
268  DWChart chart = new DWChart();
269  chart.setTitle("Resolution");
270  chart.setModel(cm);
271  chart.setType("line");
272  chart.setHeight("300");
273  chart.setWidth((cm.getCategories().size() * 70 + 40) + "");
274  return chart;
275  }
276 
278  SimpleCategoryModel cm = new SimpleCategoryModel();
279  WhereClause wc = new WhereClause();
280  wc.addClause("select concat(year(startDate), '-', month(startDate)), status, count(distinct issueId)");
281  wc.addClause("from DWIssue");
282  wc.addClause("where 1=1");
283  addFilter(wc, "startDate", role);
284  wc.addClause("group by concat(year(startDate), '-', month(startDate)), status");
285  wc.addClause("order by concat(year(startDate), '-', month(startDate))");
286  List<Object[]> results = getDao().getResultList(wc);
287  for(Object o[] : results) {
288  IssueStatus status = (IssueStatus) o[1];
289  String month = (String) o[0];
290  cm.setValue(status.toString(), month, (Number) o[2]);
291  }
292  DWChart chart = new DWChart();
293  chart.setTitle("Status");
294  chart.setModel(cm);
295  chart.setType("line");
296  chart.setHeight("300");
297  chart.setWidth((cm.getCategories().size() * 70 + 40) + "");
298  return chart;
299  }
300 
302  SimpleCategoryModel cm = new SimpleCategoryModel();
303  WhereClause wc = new WhereClause();
304  wc.addClause("select concat(year(startDate), '-', month(startDate)), type, count(distinct issueId)");
305  wc.addClause("from DWIssue");
306  wc.addClause("where 1=1");
307  addFilter(wc, "startDate", role);
308  wc.addClause("group by concat(year(startDate), '-', month(startDate)), type");
309  wc.addClause("order by concat(year(startDate), '-', month(startDate))");
310  List<Object[]> results = getDao().getResultList(wc);
311  for(Object o[] : results) {
312  IssueType type = (IssueType) o[1];
313  String month = (String) o[0];
314  cm.setValue(type.toString(), month, (Number) o[2]);
315  }
316  DWChart chart = new DWChart();
317  chart.setTitle("Type");
318  chart.setModel(cm);
319  chart.setType("line");
320  chart.setHeight("300");
321  chart.setWidth((cm.getCategories().size() * 70 + 40) + "");
322  return chart;
323  }
324 
326  return engine;
327  }
328 
329  private Dao _dao;
330 
331  private Dao getDao() {
332  if(_dao == null) {
334  _dao = new DossierPU();
335  }
336  return _dao;
337  }
338 
339  private void addFilter(WhereClause wc, String dateField, IssueParticipantRole role) {
340  if(!Strings.isBlank(dateField)) {
341  wc.addClause("and " + dateField + " >= :start");
342  wc.addClause("and " + dateField + " <= :end");
343  wc.addNamedValue("start", start);
344  wc.addNamedValue("end", end);
345  }
346  if(category != null) {
347  wc.addClause("and (");
348  wc.addClause("categoryPath = :fullCatDesc");
349  wc.addClause("or categoryPath like concat(:fullCatDesc, '" + Chars.backward().spaced() + "%')");
350  wc.addNamedValue("fullCatDesc", category.getFullDescription());
351  wc.addClause(")");
352  }
353  if(dossier != null) {
354  wc.addClause("and dossierId = :dossierId");
355  wc.addNamedValue("dossierId", dossier.getId());
356  }
357  if(contact != null && contact.isValid()) {
358  wc.addClause("and (");
359  wc.addClause("participantId = :contactId");
360  wc.addNamedValue("contactId", contact.getId());
361  if(role != null) {
362  wc.addClause("and participantRole = :contactRole");
363  wc.addNamedValue("contactRole", role);
364  }
365  wc.addClause(")");
366  }
367  }
368 
369 }
static IContact getContactById(String id)
Definition: Contacts.java:72
void setModel(AbstractChartModel model)
Definition: DWChart.java:36
void setWidth(String width)
Definition: DWChart.java:44
void setTitle(String title)
Definition: DWChart.java:60
void setType(String type)
Definition: DWChart.java:68
void setOrient(String orient)
Definition: DWChart.java:76
void setHeight(String height)
Definition: DWChart.java:52
DWChart getByType(IssueParticipantRole role)
DWChart getTimelineByStatus(IssueParticipantRole role)
DWChart getByStatus(IssueParticipantRole role)
void selectEntity(@BindingParam("entity") Object entity)
void selectReport(@BindingParam("type") DWReportType type)
DWChart getTimelineByResolution(IssueParticipantRole role)
void selectContact(@BindingParam("contact") IContact contact)
DWChart getIssuesByParticipant(IssueParticipantRole role)
DWChart getTimelineByType(IssueParticipantRole role)
DWChart getByResolution(IssueParticipantRole role)
void addNamedValue(String name, Object value)