BrightSide Workbench Full Report + Source Code
CategoryDossierItem.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.dossier;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import org.turro.dossier.db.DossierPU;
24 import org.turro.dossier.entity.Category;
25 import org.turro.dossier.entity.Dossier;
26 import org.turro.elephant.context.Application;
27 import org.turro.elephant.db.WhereClause;
28 import org.turro.i18n.I_;
29 import org.turro.jpa.Dao;
30 import org.zkoss.zk.ui.event.Event;
31 import org.zkoss.zk.ui.event.EventListener;
32 import org.zkoss.zk.ui.event.Events;
33 import org.zkoss.zk.ui.ext.AfterCompose;
34 import org.zkoss.zul.Treechildren;
35 import org.zkoss.zul.Treeitem;
36 
41 public class CategoryDossierItem extends Treeitem implements AfterCompose {
42 
43  private Treechildren children;
44  private boolean loadOnDemand, knowledgeBase;
45 
46  public CategoryDossierItem(Object entity, boolean loadOnDemand, boolean knowledgeBase) {
47  super();
48  this.loadOnDemand = loadOnDemand;
49  this.knowledgeBase = knowledgeBase;
50  setValue(entity);
51  setLabel(entity == null ? I_.get("All") : getDescription(entity));
52  setTooltiptext(getLabel());
53  }
54 
55  @Override
57  return (CategoryDossierTree) super.getTree();
58  }
59 
60  public Object getEntity() {
61  return getValue();
62  }
63 
64  public void showContents() {
65  fillCategory();
66  setOpen(true);
67  }
68 
69  public void reloadContents() {
70  if(children != null) {
71  children.getChildren().clear();
72  showContents();
73  }
74  }
75 
76  public boolean isLeaf() {
77  return getEntity() instanceof Dossier || getCategoryChildren().isEmpty();
78  }
79 
80  private void initLoadOnDemand() {
81  if(!loadOnDemand) return;
82 
83  setOpen(false);
84 
85  addEventListener(Events.ON_OPEN, new EventListener() {
86  @Override
87  public void onEvent(Event event) throws Exception {
88  // load files and add folders
89  if(isOpen()) {
90  fillCategory();
91  }
92  }
93  });
94  }
95 
96  public CategoryDossierItem addEntity(Object entity, boolean loadOnDemand, boolean knowledgeBase) {
97  CategoryDossierItem obj = new CategoryDossierItem(entity, loadOnDemand, knowledgeBase);
98  children.appendChild(obj);
99  obj.afterCompose();
100  return obj;
101  }
102 
103  private void fillCategory() {
104  if(children != null && children.getChildren().isEmpty()
105  && (getEntity() == null || getEntity() instanceof Category)) {
106  for(Object obj : getCategoryChildren()) {
107  addEntity(obj, loadOnDemand, knowledgeBase);
108  }
109  }
110  }
111 
112  private void addChildrenSpace() {
113  if(!isLeaf()) {
114  children = new Treechildren();
115  appendChild(children);
116  if(!loadOnDemand) {
117  fillCategory();
118  setOpen(true);
119  }
120  }
121  }
122 
123  public boolean isLoadOnDemand() {
124  return loadOnDemand;
125  }
126 
127  public void setLoadOnDemand(boolean loadOnDemand) {
128  this.loadOnDemand = loadOnDemand;
129  }
130 
131  public Collection<Object> getCategoryChildren() {
132  boolean all = Application.getApplication().isInRole("dossier:all");
133  ArrayList list = new ArrayList();
134  Dao dao = new DossierPU();
135  WhereClause wc = new WhereClause();
136  wc.addClause("select category from Category as category");
137  if(getEntity() == null) {
138  wc.addClause("where parent is null");
139  } else {
140  wc.addClause("where parent = :category");
141  wc.addNamedValue("category", getEntity());
142  }
143  wc.addClause("order by category.description");
144  for(Object o : dao.getResultList(wc)) {
145  if((knowledgeBase && ((Category) (o)).isKnowledgeBase()) ||
146  (all || new CategoryWrapper((Category) (o)).isChildrenParticipant())) {
147  list.add((Category) o);
148  }
149  }
150  if(getEntity() != null) {
151  wc = new WhereClause();
152  wc.addClause("select dossier from Dossier as dossier");
153  wc.addClause("where category = :category");
154  wc.addNamedValue("category", getEntity());
155  wc.addClause("order by dossier.description");
156  for(Object o : dao.getResultList(wc)) {
157  if(all || new DossierWrapper((Dossier) (o)).isParticipant()) {
158  list.add((Dossier) o);
159  }
160  }
161  }
162  return list;
163  }
164 
165  @Override
166  public void afterCompose() {
167  initLoadOnDemand();
168  addChildrenSpace();
169  }
170 
171  private String getDescription(Object entity) {
172  if(entity instanceof Category) {
173  return ((Category) entity).getDescription();
174  } else if(entity instanceof Dossier) {
175  return ((Dossier) entity).getDescription();
176  } else {
177  return null;
178  }
179  }
180 
181 }
182 
CategoryDossierItem addEntity(Object entity, boolean loadOnDemand, boolean knowledgeBase)
CategoryDossierItem(Object entity, boolean loadOnDemand, boolean knowledgeBase)
void addNamedValue(String name, Object value)
static String get(String msg)
Definition: I_.java:41