BrightSide Workbench Full Report + Source Code
GroupsTree.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.www.groupit;
20 
21 import java.util.List;
22 import org.turro.string.Strings;
23 import org.turro.collections.Item;
24 import org.turro.contacts.GroupIt;
25 import org.turro.contacts.db.ContactsPU;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.context.IConstructor;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.elephant.util.Images;
30 import org.turro.groupit.GroupItUtil;
31 import org.turro.jpa.iterator.DaoHtmlTree;
32 import org.turro.marker.ElephantMarker;
33 import org.turro.path.Path;
34 
39 public class GroupsTree extends DaoHtmlTree<Object, Object> {
40 
41  private final String category, context;
42 
43  public GroupsTree(IConstructor constructor, String context, String category) {
44  super(new ContactsPU(), new ElephantMarker(constructor));
45  this.category = category;
46  this.context = context;
47  processParameters();
48  }
49 
50  @Override
51  protected Object getEntity(Object value) {
52  return value instanceof String ? getDao().find(GroupIt.class, (String) value) : value;
53  }
54 
55  @Override
56  protected void renderTree(ElephantMarker marker, List<Item<Object, Object>> items) {
57  String selPath = GroupItUtil.getSelectedGroup(constructor, context);
58  if(Strings.isBlank(selPath) && !items.isEmpty()) {
59  selPath = ((GroupIt) ((Item) items.iterator().next()).getValue()).getPathName();
60  GroupItUtil.setSelectedGroup(constructor, context, selPath);
61  }
62  marker.put("context", context);
63  marker.put("items", items);
64  marker.put("selpath", selPath);
65  marker.put("tree", this);
66  marker.process("groups", getTreeTemplate());
67  }
68 
69  @Override
70  protected void renderItem(ElephantMarker marker, Object item) {
71  marker.put("item", item);
72  marker.process("groups", getFullTemplate());
73  }
74 
75  @Override
76  protected String parseTree(ElephantMarker marker, List<Item<Object, Object>> items) {
77  String selPath = GroupItUtil.getSelectedGroup(constructor, context);
78  if(Strings.isBlank(selPath) && !items.isEmpty()) {
79  selPath = ((GroupIt) ((Item) items.iterator().next()).getValue()).getPathName();
80  GroupItUtil.setSelectedGroup(constructor, context, selPath);
81  }
82  marker.put("context", context);
83  marker.put("items", items);
84  marker.put("selpath", selPath);
85  marker.put("tree", this);
86  return marker.parse("groups", getTreeTemplate());
87  }
88 
89  @Override
90  protected String parseItem(ElephantMarker marker, Object item) {
91  marker.put("item", item);
92  return marker.parse("groups", getFullTemplate());
93  }
94 
95  @Override
96  protected WhereClause getChildrenClause(String currPath) {
97  WhereClause wc = new WhereClause();
98  wc.addClause("select g from GroupIt g");
99  wc.addClause("where g.category = :category");
100  wc.addNamedValue("category", category);
101  wc.addClause("and g.publishable = TRUE");
102  if(Strings.isBlank(currPath)) {
103  wc.addClause("and g.parent is null");
104  } else {
105  wc.addClause("and g.parent.pathName = :pathName");
106  wc.addNamedValue("pathName", currPath);
107  }
108  wc.addClause("order by g.name");
109  return wc;
110  }
111 
112  @Override
113  protected WhereClause getLeafClause(String currPath) {
114  WhereClause wc = new WhereClause();
115  wc.addClause("select g from GroupIt g");
116  wc.addClause("where 1 = 2");
117  wc.addClause("order by g.name");
118  return wc;
119  }
120 
121  @Override
122  protected Object getId(Object item) {
123  return item instanceof GroupIt ? ((GroupIt) item).getName() : new Path((String) item).getLastNode();
124  }
125 
126  @Override
127  protected Object getParentId(Object entity) {
128  return null;
129  }
130 
131  public String getImageSrc(Item item) {
132  return ElephantContext.getRootWebPath() + Images.getImage("group");
133  }
134 
135  public String getFolderImageSrc() {
136  return ElephantContext.getRootWebPath() + Images.getImage("group");
137  }
138 
139  public String getCssClass(Item item) {
140  GroupIt gi = (GroupIt) item.getValue();
141  String selected = GroupItUtil.getSelectedGroup(constructor, context);
142  String path = gi.getPathName();
143  if(path == null || selected == null) {
144  return "";
145  } else if(path.equals(selected)) {
146  return "active";
147  } else if(selected.startsWith(path)) {
148  return "inpath";
149  } else {
150  return "";
151  }
152  }
153 
154  private void processParameters() {
156  GroupItUtil.setCategory(constructor, context, category);
157  }
158 
159 }
void addNamedValue(String name, Object value)
static String getImage(String image)
Definition: Images.java:36
static void setSelectedGroup(IConstructor constructor, String pathOrigin, String value)
static void setCategory(IConstructor constructor, String pathOrigin, String value)
static String getSelectedGroup(IConstructor constructor, String pathOrigin)
void process(String rootTmpl, String tmpl)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)
void renderTree(ElephantMarker marker, List< Item< Object, Object >> items)
Definition: GroupsTree.java:56
String parseTree(ElephantMarker marker, List< Item< Object, Object >> items)
Definition: GroupsTree.java:76
WhereClause getLeafClause(String currPath)
WhereClause getChildrenClause(String currPath)
Definition: GroupsTree.java:96
void renderItem(ElephantMarker marker, Object item)
Definition: GroupsTree.java:70
Object getEntity(Object value)
Definition: GroupsTree.java:51
GroupsTree(IConstructor constructor, String context, String category)
Definition: GroupsTree.java:43
String parseItem(ElephantMarker marker, Object item)
Definition: GroupsTree.java:90
Object getParentId(Object entity)