BrightSide Workbench Full Report + Source Code
DaoHtmlIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.iterator;
20 
21 import java.io.IOException;
22 import java.io.Writer;
23 import java.lang.reflect.ParameterizedType;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.turro.string.ObjectString;
30 import org.turro.string.Strings;
31 import org.turro.action.content.IContentIterator;
32 import org.turro.assistant.ParticipationInfo;
33 import org.turro.collections.KeyValueMap;
34 import org.turro.elephant.context.ElephantContext;
35 import org.turro.elephant.context.IConstructor;
36 import org.turro.elephant.web.ElContext;
37 import org.turro.html.HTMLHelper;
38 import org.turro.jpa.Dao;
39 import org.turro.jpa.entity.EntityWebUrls;
40 import org.turro.jpa.search.DaoHtmlSearch;
41 import org.turro.marker.ElephantMarker;
42 import org.turro.marker.MarkerHelper;
43 import org.turro.participation.ParticipationReason;
44 import org.turro.path.Path;
45 import org.turro.skills.tags.SkillCloud;
46 import org.turro.tags.TagCloud;
47 import org.turro.tags.TagSet;
48 import org.turro.tags.Tags;
49 import org.turro.visual.EmptyVisualElements;
50 import org.turro.visual.VisualElements;
51 
58 public abstract class DaoHtmlIterator<E, ID> extends DaoIterator<E, ID> implements IContentIterator {
59 
60  protected HTMLHelper html;
65 
66  private String summaryTemplate, fullTemplate;
67  private boolean restricted;
68  private boolean allowInterest, allowComments, allowVotes, allowForum,
69  allowDescriptions, allowAttachments, allowFiles, allowPolls;
70  private boolean allowRanking = true, allowMatching = true, allowSorting = true;
71  private String rankingInstance, matchingInstance;
72  private boolean doMatching;
73  private boolean navigatorTop = true, navigatorBottom = true,
74  searchOption = true, forceSummary = false;
75  private int page, currentIndex, currentSize;
76  private ID item;
77  private E entity;
78  private final Class<ID> idClass;
79  private Writer writer;
80  private final Map<String, Object> markerAttributes;
81 
82 
84  this(dao, marker, null);
85  }
86 
87  public DaoHtmlIterator(Dao dao, ElephantMarker marker, Writer writer) {
88  super(dao);
89  this.marker = marker;
90  this.constructor = marker.getConstructor();
91  this.writer = writer;
92  this.idClass = (Class<ID>) ((ParameterizedType) getClass()
93  .getGenericSuperclass()).getActualTypeArguments()[1];
94  markerAttributes = new HashMap<>();
95  }
96 
97  public void setSummaryTemplate(String summaryTemplate) {
98  this.summaryTemplate = summaryTemplate;
99  }
100 
101  public String getSummaryTemplate() {
102  return Strings.isBlank(summaryTemplate) ? "summary" : summaryTemplate;
103  }
104 
105  public void setFullTemplate(String fullTemplate) {
106  this.fullTemplate = fullTemplate;
107  }
108 
109  public String getFullTemplate() {
110  return Strings.isBlank(fullTemplate) ? "full" : fullTemplate;
111  }
112 
113  public boolean isRestricted() {
114  return restricted;
115  }
116 
117  public void setRestricted(boolean restricted) {
118  this.restricted = restricted;
119  }
120 
121  public boolean isAllowInterest() {
122  return allowInterest;
123  }
124 
125  public void setAllowInterest(boolean allowInterest) {
126  this.allowInterest = allowInterest;
127  }
128 
129  public boolean isAllowComments() {
130  return allowComments;
131  }
132 
133  public void setAllowComments(boolean allowComments) {
134  this.allowComments = allowComments;
135  }
136 
137  public boolean isAllowVotes() {
138  return allowVotes;
139  }
140 
141  public void setAllowVotes(boolean allowVotes) {
142  this.allowVotes = allowVotes;
143  }
144 
145  public boolean isAllowForum() {
146  return allowForum;
147  }
148 
149  public void setAllowForum(boolean allowForum) {
150  this.allowForum = allowForum;
151  }
152 
153  public boolean isAllowDescriptions() {
154  return allowDescriptions;
155  }
156 
157  public void setAllowDescriptions(boolean allowDescriptions) {
158  this.allowDescriptions = allowDescriptions;
159  }
160 
161  public boolean isAllowAttachments() {
162  return allowAttachments;
163  }
164 
165  public void setAllowAttachments(boolean allowAttachments) {
166  this.allowAttachments = allowAttachments;
167  }
168 
169  public boolean isAllowFiles() {
170  return allowFiles;
171  }
172 
173  public void setAllowFiles(boolean allowFiles) {
174  this.allowFiles = allowFiles;
175  }
176 
177  public boolean isAllowPolls() {
178  return allowPolls;
179  }
180 
181  public void setAllowPolls(boolean allowPolls) {
182  this.allowPolls = allowPolls;
183  }
184 
185  public boolean isAllowRanking() {
186  return allowRanking;
187  }
188 
189  public void setAllowRanking(boolean allowRanking) {
190  this.allowRanking = allowRanking;
191  }
192 
193  public boolean isAllowMatching() {
194  return allowMatching;
195  }
196 
197  public void setAllowMatching(boolean allowMatching) {
198  this.allowMatching = allowMatching;
199  }
200 
201  public boolean isDoMatching() {
202  return doMatching;
203  }
204 
205  public void setDoMatching(boolean doMatching) {
206  this.doMatching = doMatching;
207  }
208 
209  public String getRankingInstance() {
210  return rankingInstance;
211  }
212 
213  public void setRankingInstance(String rankingInstance) {
214  this.rankingInstance = rankingInstance;
215  }
216 
217  public String getMatchingInstance() {
218  return matchingInstance;
219  }
220 
221  public void setMatchingInstance(String matchingInstance) {
222  this.matchingInstance = matchingInstance;
223  }
224 
225  public boolean isAllowSorting() {
226  return allowSorting;
227  }
228 
229  public void setAllowSorting(boolean allowSorting) {
230  this.allowSorting = allowSorting;
231  }
232 
233  public boolean isNavigatorTop() {
234  return navigatorTop;
235  }
236 
237  public void setNavigatorTop(boolean navigatorTop) {
238  this.navigatorTop = navigatorTop;
239  }
240 
241  public boolean isNavigatorBottom() {
242  return navigatorBottom;
243  }
244 
245  public void setNavigatorBottom(boolean navigatorBottom) {
246  this.navigatorBottom = navigatorBottom;
247  }
248 
250  return navigationTree;
251  }
252 
254  this.navigationTree = navigationTree;
255  }
256 
258  return searchEngine;
259  }
260 
262  this.searchEngine = searchEngine;
263  if(searchEngine != null) searchEngine.setIterator(this);
264  }
265 
266  public boolean isSearchOption() {
267  return searchOption && !constructor.isHeadless();
268  }
269 
270  public void setSearchOption(boolean searchOption) {
271  this.searchOption = searchOption;
272  }
273 
274  public boolean isForceSummary() {
275  return forceSummary;
276  }
277 
278  public void setForceSummary(boolean forceSummary) {
279  this.forceSummary = forceSummary;
280  }
281 
282  public boolean wantItem() {
283  return getIdentifier() != null;
284  }
285 
286  public boolean isItem() {
287  return entity != null;
288  }
289 
290  public boolean checkEntity(ID value) {
291  return entity(value) != null;
292  }
293 
294  @Override
295  public Object addAttribute(String key, Object value) {
296  return markerAttributes.put(key, value);
297  }
298 
299  @Override
300  public Writer getWriter() {
301  try {
302  return writer != null ? writer : constructor.getOut();
303  } catch (IOException ex) {
304  Logger.getLogger(DaoHtmlIterator.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
305  }
306  return null;
307  }
308 
309  @Override
310  public void load() {
311  if(PRELOAD_TMPL.equals(summaryTemplate)) {
312  preload();
313  } else {
314  if(!constructor.isHeadless()) {
318  }
319  item = getIdentifier();
320  if(item != null) {
321  entity = entity(item);
322  }
323  }
324  }
325 
326  public void forceItem(ID item) {
327  this.item = item;
328  if(item != null) {
329  entity = entity(item);
330  }
331  }
332 
333  public String getTitle() {
334  return entity != null ? title(entity) : null;
335  }
336 
337  public Collection<String> getMetas() {
338  return entity != null ? metas(entity) : null;
339  }
340 
341  @Override
342  public void render() {
343  if(PRELOAD_TMPL.equals(summaryTemplate)) return;
344  initClauses();
345  marker.putAll(markerAttributes);
346  marker.put("iterator", this);
347  marker.put("navigatorTop", navigatorTop);
348  marker.put("navigatorBottom", navigatorBottom);
349  marker.put("searchOption", searchOption);
350  page = constructor.isHeadless() ? 0 : (Integer) ObjectString.parseString(constructor.getParameter("page"), Integer.class, true);
351  if(entity != null) {
352  if(isValid(entity)) {
353  html = new HTMLHelper(constructor, writer);
354  DaoNavigator navigator = new DaoNavigator(this);
355  marker.put("navigator", navigator);
356  marker.put("empty", false);
357  marker.put("divider", false);
358  marker.put("header", true);
359  marker.put("footer", true);
360  marker.put("single", true);
361  if(canShow(entity)) {
362  renderItem(marker, entity, page);
363  } else {
364  renderForbidden(marker, page);
365  }
366  html.endAllTags();
367  markAsSeen(entity);
368  }
369  } else {
370  current(page);
371  if(items == null) {
372  return;
373  }
374  html = new HTMLHelper(constructor, writer);
375  DaoNavigator navigator = new DaoNavigator(this);
376  marker.put("navigator", navigator);
377  currentIndex = 0;
378  currentSize = items.size();
379  if(currentSize == 0) {
380  marker.put("empty", true);
381  marker.put("divider", false);
382  marker.put("header", true);
383  marker.put("footer", true);
384  marker.put("single", false);
385  renderSummary(marker, null, current());
386  } else {
387  marker.put("empty", false);
388  marker.put("single", false);
389  int countValids = 0;
390  for(E e : items) {
391  if(isValid(e)) {
392  countValids++;
393  marker.put("divider", !isFirst());
394  marker.put("header", isFirst());
395  marker.put("footer", isLast());
396  renderSummary(marker, e, page);
397  }
398  currentIndex++;
399  }
400  if(countValids == 0) {
401  marker.put("empty", true);
402  marker.put("divider", false);
403  marker.put("header", true);
404  marker.put("footer", true);
405  marker.put("single", false);
406  renderSummary(marker, null, current());
407  }
408  }
409  }
410  }
411 
412  public boolean isFirst() {
413  return currentIndex == 0;
414  }
415 
416  public boolean isLast() {
417  return currentIndex == currentSize - 1;
418  }
419 
420  /* Engines */
421 
422  public String renderSearchEngine() {
423  return searchEngine != null ? searchEngine.render(constructor) : "";
424  }
425 
426  public String renderTree() {
427  return navigationTree != null ? navigationTree.parse() : "";
428  }
429 
430  public String renderTreeSelection() {
431  return navigationTree != null ? navigationTree.parseSelected() : "";
432  }
433 
434  public TagSet getCloudTags() {
436  }
437 
438  public TagSet getEntityTags(Object entity) {
439  return Tags.getTags(entity);
440  }
441 
442  public boolean isSeen(Object entity) {
444  }
445 
446  /* End engines */
447 
448  /* Visuals */
449 
450  public VisualElements getVisuals() {
451  if(_visuals == null) {
452  _visuals = loadVisuals();
453  }
454  return _visuals;
455  }
456 
457  private VisualElements _visuals;
458 
459  protected VisualElements loadVisuals() {
460  return new EmptyVisualElements();
461  }
462 
463  /* End visuals */
464 
465  @Override
466  public boolean isEmpty() {
467  return getTotalCount() < 1;
468  }
469 
470  protected abstract String entityRoot();
471  protected abstract E entity(ID value);
472  protected abstract void renderSummary(ElephantMarker marker, E e, int page);
473  protected abstract void renderItem(ElephantMarker marker, E e, int page);
474  protected abstract String title(E e);
475  protected abstract Collection<String> metas(E e);
476 
477  protected void renderForbidden(ElephantMarker marker, int page) {
478  }
479 
480  protected ID getIDFromURL(String root) {
481  if(constructor != null && !constructor.isHeadless()) {
482  String url = ElContext.extractPath(constructor.getRequest().getServletPath()),
483  contextPath = constructor.getCurrentContext().getPath();
484  if(!EntityWebUrls.sameURL(url, contextPath)) {
485  String entityPath = EntityWebUrls.getEntityPath(root, url.substring(contextPath.length()));
486  if(!Strings.isBlank(entityPath)) {
487  Path path = new Path(entityPath);
488  if(path.getSize() == 2) {
489  return parseId(path.getNode(1));
490  }
491  }
492  }
493  }
494  return null;
495  }
496 
497  protected ID getIdentifier() {
498  if(constructor.isHeadless() || forceSummary) {
499  return null;
500  }
501  KeyValueMap kvm = MarkerHelper.getObfuscatedParameters();
502  if(kvm != null && !kvm.isEmpty() && kvm.containsKey("item")) {
503  return parseId(kvm.get("item"));
504  }
505  return parseId(constructor.getParameter("item"));
506  }
507 
508  protected void preload() {}
509  //protected void onRenderItem(E e) {}
510 
511  protected boolean isValid(E e) {
512  return e != null;
513  }
514 
515  protected boolean canShow(E e) {
516  return true;
517  }
518 
519  protected ID parseId(String value) {
520  return (ID) ObjectString.parseString(value, idClass, false);
521  }
522 
523  protected String stringId(ID value) {
524  return String.valueOf(value);
525  }
526 
527  /* Seen */
528 
529  private void markAsSeen(E entity) {
531  }
532 
533 }
static String extractPath(String path)
Definition: ElContext.java:574
static boolean sameURL(String url1, String url2)
static String getEntityPath(String entityRoot, String url)
void setAllowAttachments(boolean allowAttachments)
void setNavigationTree(DaoHtmlTree navigationTree)
void setAllowInterest(boolean allowInterest)
void setFullTemplate(String fullTemplate)
DaoHtmlIterator(Dao dao, ElephantMarker marker)
abstract Collection< String > metas(E e)
void setAllowDescriptions(boolean allowDescriptions)
abstract void renderItem(ElephantMarker marker, E e, int page)
void setSearchEngine(DaoHtmlSearch searchEngine)
void setForceSummary(boolean forceSummary)
void setAllowSorting(boolean allowSorting)
void setRankingInstance(String rankingInstance)
void setAllowComments(boolean allowComments)
void setNavigatorBottom(boolean navigatorBottom)
void setAllowMatching(boolean allowMatching)
void setSearchOption(boolean searchOption)
abstract void renderSummary(ElephantMarker marker, E e, int page)
void setSummaryTemplate(String summaryTemplate)
void setMatchingInstance(String matchingInstance)
DaoHtmlIterator(Dao dao, ElephantMarker marker, Writer writer)
void setNavigatorTop(boolean navigatorTop)
void renderForbidden(ElephantMarker marker, int page)
void setAllowRanking(boolean allowRanking)
Object addAttribute(String key, Object value)
String render(IConstructor constructor)
void setIterator(IContentIterator iterator)
Object put(Object key, Object value)
static KeyValueMap getObfuscatedParameters()
static void toggleSkills(IConstructor constructor, String root, boolean user)
Definition: SkillCloud.java:45
static void toggleTags(IConstructor constructor, String root)
Definition: TagCloud.java:45
static TagSet getTags(IConstructor constructor, String root)
Definition: TagCloud.java:36
static TagSet getTags(Object entity)
Definition: Tags.java:351