BrightSide Workbench Full Report + Source Code
TreeDaoContentIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.jpa.content;
20 
21 import java.util.List;
22 import java.util.Map;
23 import org.turro.action.IEntityCtrl;
24 import org.turro.action.Plugins;
25 import org.turro.collections.Item;
26 import org.turro.elephant.context.IConstructor;
27 import org.turro.elephant.db.WhereClause;
28 import org.turro.entities.Entities;
29 import org.turro.entities.IElephantEntity;
30 import org.turro.jpa.Dao;
31 import org.turro.jpa.iterator.DaoHtmlTree;
32 import org.turro.marker.ElephantMarker;
33 import org.turro.marker.MarkerHelper;
34 import org.turro.plugin.contacts.IContact;
35 
40 public abstract class TreeDaoContentIterator<E, ID> extends DaoHtmlTree<E, ID> {
41 
42  private boolean restricted, allowInterest, allowComments, allowVotes,
43  allowDescriptions, allowAttachments, allowFiles, allowPolls,
44  allowForum;
45  private final String contextPath;
46  private final IContact contact;
47 
48  public TreeDaoContentIterator(Dao dao, ElephantMarker marker, String contextPath, IContact contact) {
49  super(dao, marker);
50  this.contextPath = contextPath;
51  this.contact = contact;
52  processParameters();
53  }
54 
55  public boolean isRestricted() {
56  return restricted;
57  }
58 
59  public void setRestricted(boolean restricted) {
60  this.restricted = restricted;
61  }
62 
63  public boolean isAllowInterest() {
64  return allowInterest;
65  }
66 
67  public void setAllowInterest(boolean allowInterest) {
68  this.allowInterest = allowInterest;
69  }
70 
71  public boolean isAllowComments() {
72  return allowComments;
73  }
74 
75  public void setAllowComments(boolean allowComments) {
76  this.allowComments = allowComments;
77  }
78 
79  public boolean isAllowVotes() {
80  return allowVotes;
81  }
82 
83  public void setAllowVotes(boolean allowVotes) {
84  this.allowVotes = allowVotes;
85  }
86 
87  public boolean isAllowDescriptions() {
88  return allowDescriptions;
89  }
90 
91  public void setAllowDescriptions(boolean allowDescriptions) {
92  this.allowDescriptions = allowDescriptions;
93  }
94 
95  public boolean isAllowAttachments() {
96  return allowAttachments;
97  }
98 
99  public void setAllowAttachments(boolean allowAttachments) {
100  this.allowAttachments = allowAttachments;
101  }
102 
103  public boolean isAllowFiles() {
104  return allowFiles;
105  }
106 
107  public void setAllowFiles(boolean allowFiles) {
108  this.allowFiles = allowFiles;
109  }
110 
111  public boolean isAllowPolls() {
112  return allowPolls;
113  }
114 
115  public void setAllowPolls(boolean allowPolls) {
116  this.allowPolls = allowPolls;
117  }
118 
119  public boolean isAllowForum() {
120  return allowForum;
121  }
122 
123  public void setAllowForum(boolean allowForum) {
124  this.allowForum = allowForum;
125  }
126 
127  public String getContextPath() {
128  return contextPath;
129  }
130 
131  public IContact getContact() {
132  return contact;
133  }
134 
135  /* Parameters */
136 
137  public static String getSelectionLink(String path) {
138  return MarkerHelper.setObfuscatedPars("selected=" + path);
139  }
140 
141  public String getCssClass(String path) {
142  String selected = getSelectedItem();
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  if(map != null) {
157  setSelectedItem((String) map.get("selected"));
158  }
159  }
160 
161  protected void setSelectedItem(String value) {
162  if(value != null) {
163  constructor.setSessionAttribute(contextPath + "-selected-tree-item", value);
164  }
165  }
166 
167  public String getSelectedItem() {
168  return (String) constructor.getSessionAttribute(contextPath + "-selected-tree-item");
169  }
170 
171  public static String getSelectedItem(IConstructor constructor, String pathOrigin) {
172  return (String) constructor.getSessionAttribute(pathOrigin + "-selected-tree-item");
173  }
174 
175  /* Abstract */
176 
177  protected abstract void prepareTree(ElephantMarker marker, List<Item<E, ID>> items);
178  protected abstract void prepareItem(ElephantMarker marker, E item);
179  protected abstract String getTemplateRoot();
180  protected abstract Object doVotesCtrl(E e);
181  protected abstract Object doInterestCtrl(E e);
182  protected abstract Object doCommentsCtrl(E e);
183  protected abstract Object doAttachmentsCtrl(E e);
184  protected abstract Object doFilesCtrl(E e);
185  protected abstract Object doDescriptionsCtrl(E e);
186  protected abstract Object doPollsCtrl(E e);
187  protected abstract WhereClause getItemClause(String currPath);
188 
189  /* DaoHtmlTree */
190 
191  @Override
192  public void renderSelected() {
194  if(selected != null) {
195  marker.put("iterator", this);
196  marker.put("item", selected);
198  }
199  }
200 
201  @Override
202  public String parseSelected() {
204  if(selected != null) {
205  marker.put("iterator", this);
206  marker.put("item", selected);
208  }
209  return "";
210  }
211 
212  @Override
213  protected void renderTree(ElephantMarker marker, List<Item<E, ID>> items) {
214  marker.put("context", contextPath);
215  marker.put("items", items);
216  marker.put("tree", this);
217  prepareTree(marker, items);
219  }
220 
221  @Override
222  protected void renderItem(ElephantMarker marker, E item) {
223  marker.put("item", item);
224  marker.put("tree", this);
225  prepareItem(marker, item);
227  }
228 
229  @Override
230  protected String parseTree(ElephantMarker marker, List<Item<E, ID>> items) {
231  marker.put("context", contextPath);
232  marker.put("items", items);
233  marker.put("tree", this);
234  prepareTree(marker, items);
236  }
237 
238  @Override
239  protected String parseItem(ElephantMarker marker, E item) {
240  marker.put("item", item);
241  marker.put("tree", this);
242  prepareItem(marker, item);
244  }
245 
246  /* Controls */
247 
248  public Object getVotesCtrl(E e) {
249  if(isAllowVotes()) {
250  return doVotesCtrl(e);
251  }
252  return null;
253  }
254 
255  public Object getInterestCtrl(E e) {
256  if(isAllowInterest()) {
257  return doInterestCtrl(e);
258  }
259  return null;
260  }
261 
262  public Object getCommentsCtrl(E e) {
263  if(isAllowComments()) {
264  return doCommentsCtrl(e);
265  }
266  return null;
267  }
268 
269  public Object getAttachmentsCtrl(E e) {
270  if(isAllowAttachments()) {
271  return doAttachmentsCtrl(e);
272  }
273  return null;
274  }
275 
276  public Object getFilesCtrl(E e) {
277  if(isAllowFiles()) {
278  return doFilesCtrl(e);
279  }
280  return null;
281  }
282 
283  public Object getDescriptionsCtrl(E e) {
284  if(isAllowDescriptions()) {
285  return doDescriptionsCtrl(e);
286  }
287  return null;
288  }
289 
290  public Object getPollsCtrl(E e) {
291  if(isAllowPolls()) {
292  return doPollsCtrl(e);
293  }
294  return null;
295  }
296 
297  public Object getForumCtrl(E e) {
298  if(isAllowForum()) {
299  IEntityCtrl ecf = Plugins.loadImplementation(IEntityCtrl.class, "forum-ctrl");
300  if(ecf != null) {
303  ecf.setEntityPath(iee.getPath());
304  ecf.setTemplate("related");
305  marker.put("forum", ecf);
306  return iee.configureCtrl(ecf, getContact());
307  }
308  }
309  return null;
310  }
311 
312 }
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
static IElephantEntity getController(String path)
Definition: Entities.java:78
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
abstract WhereClause getItemClause(String currPath)
abstract void prepareItem(ElephantMarker marker, E item)
String parseItem(ElephantMarker marker, E item)
void setAllowDescriptions(boolean allowDescriptions)
abstract void prepareTree(ElephantMarker marker, List< Item< E, ID >> items)
static String getSelectedItem(IConstructor constructor, String pathOrigin)
String parseTree(ElephantMarker marker, List< Item< E, ID >> items)
void renderItem(ElephantMarker marker, E item)
TreeDaoContentIterator(Dao dao, ElephantMarker marker, String contextPath, IContact contact)
void renderTree(ElephantMarker marker, List< Item< E, ID >> items)
void process(String rootTmpl, String tmpl)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)
static KeyValueMap getObfuscatedParameters()
static String setObfuscatedPars(String parameters)
void setEntityPath(String entityPath)
void setConstructor(IConstructor constructor)
void setTemplate(String template)
void setSessionAttribute(String key, Object value)
Object configureCtrl(Object ctrl, IContact contact)