BrightSide Workbench Full Report + Source Code
CollectionHtmlIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.collections;
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.List;
27 import java.util.Map;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import org.turro.string.ObjectString;
31 import org.turro.string.Strings;
32 import org.turro.action.content.IContentIterator;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.elephant.context.IConstructor;
35 import org.turro.html.HTMLHelper;
36 import org.turro.marker.ElephantMarker;
37 import org.turro.marker.MarkerHelper;
38 
43 public abstract class CollectionHtmlIterator<E, ID> extends CollectionIterator<E, ID> implements IContentIterator {
44 
45  protected HTMLHelper html;
48 
49 // protected DaoHtmlSearch searchEngine;
50 // protected DaoHtmlTree navigationTree;
51 
52  private String summaryTemplate, fullTemplate;
53  private boolean navigatorTop = true, navigatorBottom = true, searchOption = true, forceSummary = false;
54  private int page, currentIndex, currentSize;
55  private ID item;
56  private E entity;
57  private final Class<ID> idClass;
58  private Writer writer;
59  private final Map<String, Object> markerAttributes;
60 
61  public CollectionHtmlIterator(List collection, ElephantMarker marker) {
62  this(collection, marker, null);
63  }
64 
65  public CollectionHtmlIterator(List collection, ElephantMarker marker, Writer writer) {
66  super(collection);
67  this.marker = marker;
68  this.constructor = marker.getConstructor();
69  this.writer = writer;
70  this.idClass = (Class<ID>) ((ParameterizedType) getClass()
71  .getGenericSuperclass()).getActualTypeArguments()[1];
72  markerAttributes = new HashMap<>();
73  }
74 
75  public void setSummaryTemplate(String summaryTemplate) {
76  this.summaryTemplate = summaryTemplate;
77  }
78 
79  public String getSummaryTemplate() {
80  return Strings.isBlank(summaryTemplate) ? "summary" : summaryTemplate;
81  }
82 
83  public void setFullTemplate(String fullTemplate) {
84  this.fullTemplate = fullTemplate;
85  }
86 
87  public String getFullTemplate() {
88  return Strings.isBlank(fullTemplate) ? "full" : fullTemplate;
89  }
90 
91  public boolean isNavigatorTop() {
92  return navigatorTop;
93  }
94 
95  public void setNavigatorTop(boolean navigatorTop) {
96  this.navigatorTop = navigatorTop;
97  }
98 
99  public boolean isNavigatorBottom() {
100  return navigatorBottom;
101  }
102 
103  public void setNavigatorBottom(boolean navigatorBottom) {
104  this.navigatorBottom = navigatorBottom;
105  }
106 
107 // public DaoHtmlTree getNavigationTree() {
108 // return navigationTree;
109 // }
110 //
111 // public void setNavigationTree(DaoHtmlTree navigationTree) {
112 // this.navigationTree = navigationTree;
113 // }
114 //
115 // public DaoHtmlSearch getSearchEngine() {
116 // return searchEngine;
117 // }
118 //
119 // public void setSearchEngine(DaoHtmlSearch searhEngine) {
120 // this.searchEngine = searhEngine;
121 // }
122 
123  public boolean isSearchOption() {
124  return searchOption;
125  }
126 
127  public void setSearchOption(boolean searchOption) {
128  this.searchOption = searchOption;
129  }
130 
131  public boolean isForceSummary() {
132  return forceSummary;
133  }
134 
135  public void setForceSummary(boolean forceSummary) {
136  this.forceSummary = forceSummary;
137  }
138 
139  public boolean wantItem() {
140  return getIdentifier() != null;
141  }
142 
143  public boolean isItem() {
144  return entity != null;
145  }
146 
147  @Override
148  public Object addAttribute(String key, Object value) {
149  return markerAttributes.put(key, value);
150  }
151 
152  @Override
153  public Writer getWriter() {
154  try {
155  return writer != null ? writer : constructor.getOut();
156  } catch (IOException ex) {
157  Logger.getLogger(CollectionHtmlIterator.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
158  }
159  return null;
160  }
161 
162  @Override
163  public void load() {
164  item = getIdentifier();
165  if(item != null) {
166  entity = entity(item);
167  }
168  }
169 
170  public void forceItem(ID item) {
171  this.item = item;
172  if(item != null) {
173  entity = entity(item);
174  }
175  }
176 
177  public String getTitle() {
178  return entity != null ? title(entity) : null;
179  }
180 
181  public Collection<String> getMetas() {
182  return entity != null ? metas(entity) : null;
183  }
184 
185  @Override
186  public void render() {
187  initClauses();
188  marker.putAll(markerAttributes);
189  marker.put("iterator", this);
190  marker.put("navigatorTop", navigatorTop);
191  marker.put("navigatorBottom", navigatorBottom);
192  marker.put("searchOption", searchOption);
193  page = constructor.isHeadless() ? 0 : (Integer) ObjectString.parseString(constructor.getParameter("page"), Integer.class, true);
194  if(entity != null) {
195  if(isValid(entity)) {
196  html = new HTMLHelper(constructor, writer);
197  CollectionNavigator navigator = new CollectionNavigator(this);
198  marker.put("navigator", navigator);
199  marker.put("empty", false);
200  marker.put("divider", false);
201  marker.put("header", true);
202  marker.put("footer", true);
203  marker.put("single", true);
204  renderItem(marker, entity, page);
205  html.endAllTags();
206  }
207  } else {
208  current(page);
209  if(items == null) {
210  return;
211  }
212  html = new HTMLHelper(constructor, writer);
213  CollectionNavigator navigator = new CollectionNavigator(this);
214  marker.put("navigator", navigator);
215  currentIndex = 0;
216  currentSize = items.size();
217  if(currentSize == 0) {
218  marker.put("empty", true);
219  marker.put("divider", false);
220  marker.put("header", true);
221  marker.put("footer", true);
222  marker.put("single", false);
223  renderSummary(marker, null, current());
224  } else {
225  marker.put("empty", false);
226  marker.put("single", false);
227  for(E e : items) {
228  if(isValid(e)) {
229  marker.put("divider", !isFirst());
230  marker.put("header", isFirst());
231  marker.put("footer", isLast());
233  }
234  currentIndex++;
235  }
236  }
237  }
238  }
239 
240  public boolean isFirst() {
241  return currentIndex == 0;
242  }
243 
244  public boolean isLast() {
245  return currentIndex == currentSize - 1;
246  }
247 
248  public String renderSearchEngine() {
249  return ""; //searchEngine != null ? searchEngine.render(constructor) : "";
250  }
251 
252  public String renderTree() {
253  return ""; //navigationTree != null ? navigationTree.parse() : "";
254  }
255 
256  public String renderTreeSelection() {
257  return ""; //navigationTree != null ? navigationTree.parseSelected() : "";
258  }
259 
260  @Override
261  public boolean isEmpty() {
262  return getTotalCount() < 1;
263  }
264 
265  protected abstract E entity(ID value);
266  protected abstract void renderSummary(ElephantMarker marker, E e, int page);
267  protected abstract void renderItem(ElephantMarker marker, E e, int page);
268  protected abstract String title(E e);
269  protected abstract Collection<String> metas(E e);
270 
271  protected ID getIdentifier() {
272  if(constructor.isHeadless() || forceSummary) {
273  return null;
274  }
275  KeyValueMap kvm = MarkerHelper.getObfuscatedParameters();
276  if(kvm != null && !kvm.isEmpty() && kvm.containsKey("item")) {
277  return parseId(kvm.get("item"));
278  }
279  return parseId(constructor.getParameter("item"));
280  //return constructor.isHeadless() ? null : (ID) ObjectString.parseString(constructor.getParameter("item"), idClass, false);
281  }
282 
283  protected void onRenderItem(E e) {}
284 
285  protected boolean isValid(E e) {
286  return e != null;
287  }
288 
289  protected ID parseId(String value) {
290  return (ID) ObjectString.parseString(value, idClass, false);
291  }
292 
293  protected String stringId(ID value) {
294  return String.valueOf(value);
295  }
296 
297 }
CollectionHtmlIterator(List collection, ElephantMarker marker)
CollectionHtmlIterator(List collection, ElephantMarker marker, Writer writer)
abstract void renderItem(ElephantMarker marker, E e, int page)
abstract void renderSummary(ElephantMarker marker, E e, int page)
abstract Collection< String > metas(E e)
Object put(Object key, Object value)
static KeyValueMap getObfuscatedParameters()