BrightSide Workbench Full Report + Source Code
DossierCombobox.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.dossier.dossier;
19 
20 import java.util.LinkedList;
21 import org.turro.string.Strings;
22 import org.turro.dossier.db.DossierPU;
23 import org.turro.dossier.entity.Dossier;
24 import org.turro.dossier.entity.ParticipantRole;
25 import org.turro.elephant.context.Application;
26 import org.turro.elephant.db.WhereClause;
27 import org.turro.jpa.Dao;
28 import org.turro.util.Chars;
29 import org.turro.zkoss.input.GenericCombobox;
30 
35 public class DossierCombobox extends GenericCombobox<Dossier> {
36 
37  @Override
38  public void populateList(String value, LinkedList list, int nRows) {
39  boolean all = Application.getApplication().isInRole("dossier:all");
40  Dao dao = new DossierPU();
41  long id = getIdFromText(value);
42  WhereClause wc = new WhereClause();
43  wc.addClause("select distinct dossier from Dossier as dossier");
44  if(id > 0) {
45  wc.addClause("where dossier.id = :id");
46  wc.addNamedValue("id", id);
47  } else {
48  wc.addClause("where 1=1");
49  wc.addClause("and not exists (select p from Participant as p");
50  wc.addClause("where p.dossier = dossier and p.role = :rolenot)");
52  if(!Strings.isBlank(value)) {
53  wc.addClause("and (");
54  wc.setPrefix("");
55  wc.addLikeFields(new String[] {"dossier.description"}, getNameFromText(value));
56  wc.setPrefix("or");
57  wc.addLikeFields(new String[] {"dossier.category.fullDescription"}, getCategoryFromText(value));
58  wc.setPrefix(null);
59  wc.addClause(")");
60  }
61  wc.addClause("order by dossier.description");
62  }
63  for(Object o : dao.getResultList(wc, nRows)) {
64  if(all || new DossierWrapper((Dossier) (o)).isParticipant()) {
65  list.add((Dossier) o);
66  }
67  }
68  if(id < 1) {
69  wc = new WhereClause();
70  wc.addClause("select distinct dossier from Dossier as dossier");
71  wc.addClause("left join dossier.participants participant");
72  wc.addClause("where 1=1");
73  wc.addClause("and participant.role = :role");
75  if(!Strings.isBlank(value)) {
76  wc.addClause("and (");
77  wc.setPrefix("");
78  wc.addLikeFields(new String[] {"participant.name"}, getSubjectFromText(value));
79  wc.setPrefix("or");
80  wc.addLikeFields(new String[] {"dossier.description"}, getNameFromText(value));
81  wc.setPrefix("or");
82  wc.addLikeFields(new String[] {"dossier.category.fullDescription"}, getCategoryFromText(value));
83  wc.setPrefix(null);
84  wc.addClause(")");
85  }
86  wc.addClause("order by dossier.description");
87  for(Object o : dao.getResultList(wc, nRows)) {
88  if(all || new DossierWrapper((Dossier) (o)).isParticipant()) {
89  list.add((Dossier) o);
90  }
91  }
92  }
93  }
94 
95  @Override
96  public String getTextFromObject(Dossier value) {
97  return value.getFullDescription() + " #" + value.getId();
98  }
99 
100  public String getSubjectFromText(String text) {
101  String[] s = text.split(Chars.forward().regexp().spaced().toString());
102  if(s.length > 0) {
103  return s[0];
104  }
105  return getNameFromText(text);
106  }
107 
108  public String getNameFromText(String text) {
109  String[] s = text.split(Chars.forward().regexp().spaced().toString());
110  if(s.length == 5) {
111  // subject - store '|' name '|' contract + idStore - category
112  return s[1] + Chars.forward().spaced() + s[2] + Chars.forward().spaced() + s[3];
113  } else if(s.length == 3) {
114  // subject - name - category
115  return s[1];
116  } else if(s.length == 2) {
117  // name - category
118  return s[0];
119  }
120  return text;
121  }
122 
123  public String getCategoryFromText(String text) {
124  String[] s = text.split(Chars.forward().regexp().spaced().toString());
125  if(s.length == 5) {
126  // subject - store '|' name '|' contract + idStore - category
127  return s[4];
128  } else if(s.length == 3) {
129  // subject - name - category
130  return s[2];
131  } else if(s.length == 2) {
132  // name - category
133  return s[1];
134  }
135  return getNameFromText(text);
136  }
137 
138  public long getIdFromText(String text) {
139  long id = 0;
140  int p = text.lastIndexOf("#");
141  try {
142  if(p > -1) {
143  id = Long.valueOf(text.substring(p + 1));
144  } else {
145  id = Long.valueOf(text);
146  }
147  } catch(Exception ex) {}
148  return id;
149  }
150 
151 }
void populateList(String value, LinkedList list, int nRows)
void addLikeFields(String[] fields, String value)
void addNamedValue(String name, Object value)