BrightSide Workbench Full Report + Source Code
BrightSide/elephant-dossier/src/main/java/org/turro/dossier/www/CategoriesTree.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.www;
20 
21 import java.util.List;
22 import org.turro.string.Strings;
23 import org.turro.attach.www.AttachCtrl;
24 import org.turro.auth.Authentication;
25 import org.turro.collections.Item;
26 import org.turro.dossier.db.DossierPU;
27 import org.turro.dossier.dossier.DossierCounter;
28 import org.turro.dossier.entity.Category;
29 import org.turro.dossier.entity.DossierStatus;
30 import org.turro.dossier.entity.DossierType;
31 import org.turro.dossier.search.CategoryResults;
32 import org.turro.elephant.context.Application;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.elephant.context.IConstructor;
35 import org.turro.elephant.db.WhereClause;
36 import org.turro.elephant.util.Images;
37 import org.turro.entities.Entities;
38 import org.turro.file.util.FileAttach;
39 import org.turro.jpa.content.TreeDaoContentIterator;
40 import org.turro.marker.ElephantMarker;
41 import org.turro.path.Path;
42 import org.turro.plugin.contacts.IContact;
43 import org.turro.polls.PollsCtrl;
44 import org.turro.util.Arrays;
45 import org.turro.visual.CategoryVisualElements;
46 import org.turro.visual.VisualElements;
47 import org.turro.www.commentit.CommentItCtrl;
48 import org.turro.www.describeit.DescribeItCtrl;
49 import org.turro.www.starit.StarItCtrl;
50 import org.turro.www.voteit.VoteItCtrl;
51 
56 public class CategoriesTree extends TreeDaoContentIterator<Object, Object> {
57 
58  private final String categories, types[], status[];
59 
60  public CategoriesTree(IConstructor constructor, String context, IContact contact, String categories, String types[], String status[]) {
61  super(new DossierPU(), new ElephantMarker(constructor), context, contact);
62  this.categories = categories;
63  this.types = types;
64  this.status = status;
65  }
66 
67  @Override
68  protected WhereClause getItemClause(String currPath) {
69  WhereClause wc = new WhereClause();
70  wc.addClause("select g from Category g");
71  wc.addClause("where 1=1");
72  if(Strings.isBlank(currPath)) {
73  wc.addClause("and 1=2");
74  } else {
75  wc.addClause("and g.fullDescription = :pathName");
76  wc.addNamedValue("pathName", Category.getPathToFullDescription(currPath));
77  }
78  return wc;
79  }
80 
81  @Override
82  protected Object getEntity(Object value) {
83  return value instanceof String ? getDao().find(Category.class, Long.valueOf((String) value)) : value;
84  }
85 
86  @Override
87  protected WhereClause getChildrenClause(String currPath) {
88  WhereClause wc = new WhereClause();
89  wc.addClause("select g from Category g");
90  wc.addClause("where 1=1");
91  if(Strings.isBlank(currPath)) {
92  if("all".equals(categories)) {
93  wc.addClause("and g.parent is null");
94  } else {
95  wc.addClause("and g.id in (" + categories + ")");
96  }
97  } else {
98  wc.addClause("and g.parent.fullDescription = :pathName");
99  wc.addNamedValue("pathName", Category.getPathToFullDescription(currPath));
100  }
101  if(isRestricted() && !Application.getApplication().isInRole("dossier:all")) {
103  }
104  addFilters(wc);
105  wc.addClause("order by g.description");
106  return wc;
107  }
108 
109  @Override
110  protected WhereClause getLeafClause(String currPath) {
111  return null;
112  }
113 
114  @Override
115  protected Object getId(Object item) {
116  return item instanceof Category ? ((Category) item).getDescription() : new Path((String) item).getLastNode();
117  }
118 
119  @Override
120  protected Object getParentId(Object item) {
121  return item instanceof Category ? ((Category) item).getParent().getDescription() : new Path((String) item).getLastNode();
122  }
123 
124  public String getImageSrc(Item item) {
125  return ElephantContext.getRootWebPath() + Images.getImage("group");
126  }
127 
128  public String getFolderImageSrc() {
129  return ElephantContext.getRootWebPath() + Images.getImage("group");
130  }
131 
132  public String getCssClass(Item item) {
133  Category cat = (Category) item.getValue();
134  String selected = getSelectedItem();
135  String path = cat.getPath();
136  if(path == null || selected == null) {
137  return "";
138  } else if(path.equals(selected)) {
139  return "active";
140  } else if(selected.startsWith(path)) {
141  return "inpath";
142  } else {
143  return "";
144  }
145  }
146 
147  @Override
148  protected void prepareTree(ElephantMarker marker, List<Item<Object, Object>> items) {
149  String selPath = getSelectedItem();
150  if(Strings.isBlank(selPath) && !items.isEmpty()) {
151  selPath = ((Category) ((Item) items.iterator().next()).getValue()).getPath();
152  setSelectedItem(selPath);
153  }
154  marker.put("selpath", selPath);
155  marker.put("dossierCounter", new DossierCounter(categories, types, status, isRestricted()));
156  }
157 
158  @Override
159  protected void prepareItem(ElephantMarker marker, Object item) {
160  // do nothing
161  }
162 
163  @Override
164  protected String getTemplateRoot() {
165  return "categories";
166  }
167 
168  @Override
169  protected Object doVotesCtrl(Object e) {
172  }
173 
174  @Override
175  protected Object doInterestCtrl(Object e) {
178  }
179 
180  @Override
181  protected Object doCommentsCtrl(Object e) {
184  }
185 
186  @Override
187  protected Object doAttachmentsCtrl(Object e) {
190  }
191 
192  @Override
193  protected Object doFilesCtrl(Object e) {
196  }
197 
198  @Override
199  protected Object doDescriptionsCtrl(Object e) {
202  }
203 
204  @Override
205  protected Object doPollsCtrl(Object e) {
208  }
209 
210  @Override
211  protected VisualElements loadVisuals() {
212  return CategoryVisualElements.load();
213  }
214 
215  private void addFilters(WhereClause wc) {
216  if(!Arrays.isEmpty(types, status)) {
217  wc.addClause("and exists (");
218  wc.addClause("select d from Dossier d");
219  wc.addClause("where d.uniquePath like concat(g.uniquePath, '/%')");
220  if(!Arrays.isEmpty(types)) {
221  wc.addClause("and d.type in (:types)");
222  wc.addNamedValue("types", Arrays.asList(types).map(t -> typeValue((String) t)));
223  }
224  if(!Arrays.isEmpty(status)) {
225  wc.addClause("and d.status in (:status)");
226  wc.addNamedValue("status", Arrays.asList(status).map(s -> statusValue((String) s)));
227  }
228  wc.addClause(")");
229  }
230  }
231 
232  private DossierType typeValue(String name) {
233  return DossierType.valueOf(name);
234  }
235 
236  private DossierStatus statusValue(String name) {
237  return DossierStatus.valueOf(name);
238  }
239 
240 }
static String getObjectPath(Object object)
Definition: DossierPU.java:66
static String getPathToFullDescription(String path)
Definition: Category.java:263
static void addParticipantCategoryAffiliance(WhereClause wc, String sep, String idContact, String categoryField)
CategoriesTree(IConstructor constructor, String context, IContact contact, String categories, String types[], String status[])
void addNamedValue(String name, Object value)
static String getImage(String image)
Definition: Images.java:36
static IElephantEntity getController(String path)
Definition: Entities.java:78
Object put(Object key, Object value)
Object configureCtrl(Object ctrl, IContact contact)