BrightSide Workbench Full Report + Source Code
DaoHtmlSearch.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.jpa.search;
20 
21 import java.io.IOException;
22 import java.io.Serializable;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.turro.action.content.IContentIterator;
28 import org.turro.string.Strings;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.context.IConstructor;
31 import org.turro.html.HTMLGenerator;
32 import org.turro.i18n.I_;
33 import org.turro.marker.ElephantMarker;
34 import org.turro.skills.tags.SkillCloud;
35 import org.turro.tags.TagCloud;
36 
41 public class DaoHtmlSearch extends DaoSearch implements Serializable {
42 
43  private final String context;
44  private String tagCloudString, tagsString, tagRoot;
45  //private transient DaoHtmlIterator iterator;
46  private transient IContentIterator iterator;
47 
48  public static void renderInstance(IConstructor constructor) {
50  if(dhs != null) {
51  try {
52  constructor.getOut().print(dhs.render(constructor));
53  } catch (IOException ex) {
54  Logger.getLogger(DaoHtmlSearch.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
55  }
56  }
57  }
58 
59  public static DaoHtmlSearch getInstance(IConstructor constructor, String context) {
60  DaoHtmlSearch search = (DaoHtmlSearch) constructor.getSessionAttribute(context + "-search");
61  if(search == null) {
62  search = new DaoHtmlSearch(context);
63  if(!search.isEmpty()) {
64  constructor.setSessionAttribute(context + "-search", search);
65  } else {
66  search = null;
67  }
68  }
69  if(search != null) search.loadValues(constructor);
70  return search;
71  }
72 
73  private DaoHtmlSearch(String context) {
74  super(context);
75  this.context = context;
76  }
77 
79  return iterator;
80  }
81 
82  public void setIterator(IContentIterator iterator) {
83  this.iterator = iterator;
84  }
85 
86  public boolean isSingleInput() {
87  int input = 0;
88  for(DaoSearchKey dsk : values()) {
89  if("input".equals(dsk.getTag())) {
90  input++;
91  }
92  }
93  return input == 1;
94  }
95 
96  public boolean isTagCloud() {
97  for(DaoSearchKey dsk : values()) {
98  if("tag-cloud".equals(dsk.getTag())) {
99  return true;
100  }
101  }
102  return false;
103  }
104 
105  public void setTagCloudString(String tagCloudString) {
106  this.tagCloudString = tagCloudString;
107  }
108 
109  public String getTagCloudString() {
110  return tagCloudString;
111  }
112 
113  public void setTagsString(String tagsString) {
114  this.tagsString = tagsString;
115  }
116 
117  public String getTagsString() {
118  return tagsString;
119  }
120 
121  public void setTagRoot(String tagRoot) {
122  this.tagRoot = tagRoot;
123  }
124 
125  public String render(IConstructor constructor) {
126  if(ElephantMarker.existsTemplate(constructor, false, "iterator", "search")) {
127  ElephantMarker em = new ElephantMarker(constructor);
128  em.put("iterator", iterator);
129  em.put("keys", this);
130  if(!Strings.isBlank(tagRoot)) {
131  em.put("tags", TagCloud.getTags(constructor, tagRoot));
132  }
133  return em.parse(Strings.isBlank(root, "iterator"), Strings.isBlank(template, "search"));
134  } else {
135  return render(constructor, null);
136  }
137  }
138 
139  public String render(IConstructor constructor, DaoItemRender renderer) {
140  HTMLGenerator hg = new HTMLGenerator();
141  hg.startTag("div", "class=\"" + getSclass() + "\"");
142  hg.startTag("form", "action='" + ElephantContext.getRootWebPath() + context + "' method='get' accept-charset='UTF-8'");
143 
144  hg.startTable("cellpadding='1' cellspacing='1' style='width:100%'")
145  .startTableCol("");
146 
147  for(String key : keySet()) {
148  DaoSearchKey dsk = get(key);
149  renderKey(constructor, hg, dsk, renderer);
150  }
151  hg.endAllTags();
152  return hg.toString();
153  }
154 
155  private void loadValues(IConstructor constructor) {
156  String clear = constructor.getParameter("clear-form");
157  String submited = constructor.getParameter("search-form");
158  if("clear".equals(clear)) {
159  clearValues();
160  TagCloud.resetContext(constructor, tagRoot);
161  SkillCloud.resetContext(constructor, tagRoot);
162  } else if(submited != null) {
163  for(String key : keySet()) {
164  if(!key.startsWith("#")) {
165  DaoSearchKey dsk = get(key);
166  dsk.readParam(constructor);
167  }
168  }
169  }
170  }
171 
172  private void renderKey(IConstructor constructor, HTMLGenerator hg, DaoSearchKey dsk, DaoItemRender renderer) {
173  if(renderer != null && renderer.renderKey(constructor, hg, dsk)) {
174  return;
175  }
176  if("new-row".equals(dsk.getTag())) {
177  StringBuilder sb = new StringBuilder();
178  for(String s : dsk.getAttributes().keySet()) {
179  String v = dsk.getAttributes().get(s);
180  sb.append(" ").append(s).append("=\"").append(v).append("\"");
181  }
182  hg.startTableRow("").startTableCol(sb.toString());
183  } else if("new-col".equals(dsk.getTag())) {
184  StringBuilder sb = new StringBuilder();
185  for(String s : dsk.getAttributes().keySet()) {
186  String v = dsk.getAttributes().get(s);
187  sb.append(" ").append(s).append("=\"").append(v).append("\"");
188  }
189  hg.startTableCol(sb.toString());
190  } else if("search-button".equals(dsk.getTag())) {
191  hg.startTag("button", "type='submit' name='search-form'")
192  .write("<i class=\"fa fa-search\"></i>")
193  .endTag();
194  } else if("clear-button".equals(dsk.getTag())) {
195  hg.startTag("button", "type='submit' name='clear-form' value='clear'")
196  .write("<i class=\"fa fa-close\"></i>")
197  .endTag();
198  } else if("tag-cloud".equals(dsk.getTag())) {
199  if(!Strings.isBlank(tagCloudString)) {
200  String uniqueId = constructor.getUniqueId();
201  hg.startTag("button", "type='button' onclick='$(\"#" + uniqueId + "\").slideToggle()'")
202  .write("<i class=\"fa fa-tags\"></i>")
203  .endTag();
204  if(!Strings.isEmpty(tagsString)) {
205  hg.startTableRow("").startTableCol("colspan='10'")
206  .startTag("div", "class='selected-tag'")
207  .write(tagsString)
208  .endTag();
209  }
210  hg.startTableRow("").startTableCol("colspan='10'");
211  hg.startTag("div", "id='" + uniqueId + "' style='display:none;'")
212  .write(tagCloudString)
213  .endTag();
214  }
215  } else if("input".equals(dsk.getTag())) {
216  StringBuilder sb = new StringBuilder();
217  sb.append(" name=\"").append(dsk.getName()).append("\"");
218  sb.append(" type=\"").append(dsk.getType()).append("\"");
219  sb.append(" value=\"").append(dsk.getValue()).append("\"");
220  for(String s : dsk.getAttributes().keySet()) {
221  String v = dsk.getAttributes().get(s);
222  if("placeholder".equals(s) || "label".equals(s)) {
223  v = I_.get(v);
224  }
225  if("checked".equals(s)) {
226  sb.append(" ").append(s);
227  } else {
228  sb.append(" ").append(s).append("=\"").append(v).append("\"");
229  }
230  }
231  hg.doTag("input", sb.toString());
232  }
233  }
234 
235  /* Attributes */
236 
237  private Map<String, Object> attributes;
238 
239  public boolean hasAttribute(IConstructor constructor, String attribute) {
240  checkAttributtes(constructor);
241  return attributes.containsKey(attribute);
242  }
243 
244  public Object getAttribute(IConstructor constructor, String attribute) {
245  checkAttributtes(constructor);
246  return attributes.get(attribute);
247  }
248 
249  public void setAttribute(IConstructor constructor, String attribute, Object value) {
250  checkAttributtes(constructor);
251  attributes.put(attribute, value);
252  }
253 
254  private void checkAttributtes(IConstructor constructor) {
255  if(attributes == null) {
256  attributes = (Map<String, Object>) constructor.getSessionAttribute(context + "-attributes");
257  }
258  if(attributes == null) {
259  attributes = new HashMap<>();
260  }
261  }
262 
263 }
static String getContextVariable(IConstructor constructor)
HTMLGenerator startTag(String tag)
HTMLGenerator startTableCol(String attributes)
HTMLGenerator startTable(String attributes)
void setAttribute(IConstructor constructor, String attribute, Object value)
String render(IConstructor constructor)
Object getAttribute(IConstructor constructor, String attribute)
void setTagCloudString(String tagCloudString)
void setIterator(IContentIterator iterator)
String render(IConstructor constructor, DaoItemRender renderer)
void setTagsString(String tagsString)
static void renderInstance(IConstructor constructor)
static DaoHtmlSearch getInstance(IConstructor constructor, String context)
boolean hasAttribute(IConstructor constructor, String attribute)
static boolean existsTemplate(IConstructor constructor, boolean mail, String root, String name)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)
static void resetContext(IConstructor constructor, String root)
Definition: SkillCloud.java:56
static TagSet getTags(IConstructor constructor, String root)
Definition: TagCloud.java:36
static void resetContext(IConstructor constructor, String root)
Definition: TagCloud.java:56
void setSessionAttribute(String key, Object value)