19 package org.turro.dossier.dossier;
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;
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;
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;
52 public DossierCounter(String allowedCategories, String types[], String status[],
boolean restricted) {
53 this.allowedCategories = allowedCategories;
56 this.restricted = restricted;
60 Long value = phases.get(phase);
62 value = phaseCount(phase);
63 phases.put(phase, value);
69 Long value = dossiers.get(category);
71 value = dossierCount(category);
72 dossiers.put(category, value);
77 private Long phaseCount(
int phase) {
80 wc.
addClause(
"select count(distinct d.dossierId) from DWDossier d where 1=1");
81 addCriteria(wc,
null, phase);
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);
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());
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 +
"%");
112 if(!
"all".equals(allowedCategories)) {
114 wc.addClause(
"and (");
116 for(String cat : categories) {
117 String par =
"cat" + wc.getUniqueSuffix();
118 wc.addClause(sep +
"d.categoryPath like :" + par);
119 wc.addNamedValue(par, cat +
"%");
125 wc.addClause(
"and d.phase = :phase");
126 wc.addNamedValue(
"phase", phase);
129 wc.addClause(
"and d.publishable = TRUE");
131 if(types !=
null && types.length > 0) {
132 wc.addClause(
"and (");
134 for(String type : types) {
135 DossierType dt = DossierType.valueOf(type);
137 String par =
"type" + wc.getUniqueSuffix();
138 wc.addClause(sep +
"d.type = :" + par);
139 wc.addNamedValue(par, dt);
145 if(status !=
null && status.length > 0) {
146 wc.addClause(
"and (");
148 for(String stat : status) {
149 DossierStatus ds = DossierStatus.valueOf(stat);
151 String par =
"status" + wc.getUniqueSuffix();
152 wc.addClause(sep +
"d.status = :" + par);
153 wc.addNamedValue(par, ds);
DossierCounter(String allowedCategories, List< String > types, List< String > status, boolean restricted)
Long getPhaseCount(int phase)
Long getDossierCount(String category)
DossierCounter(String allowedCategories, String types[], String status[], boolean restricted)
void addClause(String clause)
Object getSingleResult(WhereClause wc)