BrightSide Workbench Full Report + Source Code
DossierCounter.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.dossier;
20 
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import org.turro.string.Strings;
26 import org.turro.dossier.db.DossierPU;
27 import org.turro.dossier.entity.Category;
28 import org.turro.dossier.entity.DossierStatus;
29 import org.turro.dossier.entity.DossierType;
30 import org.turro.elephant.db.WhereClause;
31 import org.turro.jpa.Dao;
32 
37 public class DossierCounter implements Serializable {
38 
39  private final String allowedCategories, types[], status[];
40  private final HashMap<Integer, Long> phases = new HashMap<>();
41  private final HashMap<String, Long> dossiers = new HashMap<>();
42  private final ArrayList<String> categories = new ArrayList<>();
43  private final boolean restricted;
44 
45  public DossierCounter(String allowedCategories, List<String> types, List<String> status, boolean restricted) {
46  this.allowedCategories = allowedCategories;
47  this.types = types != null ? types.toArray(new String[0]): new String[0];
48  this.status = status != null ? status.toArray(new String[0]): new String[0];
49  this.restricted = restricted;
50  }
51 
52  public DossierCounter(String allowedCategories, String types[], String status[], boolean restricted) {
53  this.allowedCategories = allowedCategories;
54  this.types = types;
55  this.status = status;
56  this.restricted = restricted;
57  }
58 
59  public Long getPhaseCount(int phase) {
60  Long value = phases.get(phase);
61  if(value == null) {
62  value = phaseCount(phase);
63  phases.put(phase, value);
64  }
65  return value;
66  }
67 
68  public Long getDossierCount(String category) {
69  Long value = dossiers.get(category);
70  if(value == null) {
71  value = dossierCount(category);
72  dossiers.put(category, value);
73  }
74  return value;
75  }
76 
77  private Long phaseCount(int phase) {
78  Dao dao = new DossierPU();
79  WhereClause wc = new WhereClause();
80  wc.addClause("select count(distinct d.dossierId) from DWDossier d where 1=1");
81  addCriteria(wc, null, phase);
82  return (Long) dao.getSingleResult(wc);
83  }
84 
85  private Long dossierCount(String category) {
86  Dao dao = new DossierPU();
87  WhereClause wc = new WhereClause();
88  wc.addClause("select count(distinct d.dossierId) from DWDossier d where 1=1");
89  addCriteria(wc, category, -1);
90  return (Long) dao.getSingleResult(wc);
91  }
92 
93  private void doCategories() {
94  if(!Strings.isBlank(allowedCategories) && !"all".equals(allowedCategories) && categories.isEmpty()) {
95  Dao dao = new DossierPU();
96  String[] cats = allowedCategories.split(",");
97  for(String cat : cats) {
98  Category category = dao.find(Category.class, Long.valueOf(cat));
99  if(category != null) {
100  categories.add(category.getFullDescription());
101  }
102  }
103  }
104  }
105 
106  private void addCriteria(WhereClause wc, String category, int phase) {
107  if(!Strings.isBlank(category) && !"all".equals(category)) {
108  String par = "cat" + wc.getUniqueSuffix();
109  wc.addClause("and d.categoryPath like :" + par);
110  wc.addNamedValue(par, category + "%");
111  }
112  if(!"all".equals(allowedCategories)) {
113  doCategories();
114  wc.addClause("and (");
115  String sep = "";
116  for(String cat : categories) {
117  String par = "cat" + wc.getUniqueSuffix();
118  wc.addClause(sep + "d.categoryPath like :" + par);
119  wc.addNamedValue(par, cat + "%");
120  sep = "or ";
121  }
122  wc.addClause(")");
123  }
124  if(phase > -1) {
125  wc.addClause("and d.phase = :phase");
126  wc.addNamedValue("phase", phase);
127  }
128  if(!restricted) {
129  wc.addClause("and d.publishable = TRUE");
130  }
131  if(types != null && types.length > 0) {
132  wc.addClause("and (");
133  String sep = "";
134  for(String type : types) {
135  DossierType dt = DossierType.valueOf(type);
136  if(dt != null) {
137  String par = "type" + wc.getUniqueSuffix();
138  wc.addClause(sep + "d.type = :" + par);
139  wc.addNamedValue(par, dt);
140  sep = "or ";
141  }
142  }
143  wc.addClause(")");
144  }
145  if(status != null && status.length > 0) {
146  wc.addClause("and (");
147  String sep = "";
148  for(String stat : status) {
149  DossierStatus ds = DossierStatus.valueOf(stat);
150  if(ds != null) {
151  String par = "status" + wc.getUniqueSuffix();
152  wc.addClause(sep + "d.status = :" + par);
153  wc.addNamedValue(par, ds);
154  sep = "or ";
155  }
156  }
157  wc.addClause(")");
158  }
159  }
160 
161 }
DossierCounter(String allowedCategories, List< String > types, List< String > status, boolean restricted)
DossierCounter(String allowedCategories, String types[], String status[], boolean restricted)
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380