BrightSide Workbench Full Report + Source Code
Search.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.elephant.search;
19 
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.util.HashSet;
23 import javax.servlet.ServletException;
24 import org.turro.elephant.context.IElement;
25 import org.turro.elephant.impl.abstracts.AbstractElement;
26 import org.turro.elephant.impl.util.Localizer;
27 import org.turro.elephant.layout.IManageable;
28 import org.turro.elephant.layout.IRenderable;
29 import org.turro.elephant.security.IDefendable;
30 import org.turro.elephant.web.ElContext;
31 import org.turro.elephant.web.ElContextMap;
32 
37 public class Search extends AbstractElement implements IElement, IRenderable, IManageable, IDefendable {
38 
39  protected Localizer localizer;
40  protected FoundList results = null;
41 
43  public Search() {
44  }
45 
46  public ISearchable[] getElements() {
47  HashSet els = new HashSet();
48  IElement sel;
49  for(ElContext ctx : ElContextMap.getInstance().values()) {
50  if(ctx.isSelectable() && ctx.isInRole() && !ctx.getWebContext().isDynamic()) {
51  if(ctx instanceof ISearchable) {
52  els.add(ctx);
53  }
54  sel = ctx.getElement();
55  if(sel instanceof ISearchable) {
56  els.add(sel);
57  }
58  }
59  }
60  return (ISearchable[])els.toArray(new ISearchable[0]);
61  }
62 
63  public FoundList getResults() {
64  return search(
65  getConstructor().getParameter("query"),
66  getConstructor().getParameter("ic") != null);
67  }
68 
69 
70  public FoundList search(String value, boolean ignoreCase) {
71  if(results == null) {
72  results = new FoundList();
73  if(value != null && value.length() > 0) {
74  ISearchable[] els = getElements();
75  for(int i = 0; i < els.length; i++) {
76  results.addResults((els[i]).search(value, ignoreCase));
77  }
78  }
79  }
80  return results;
81  }
82 
83  @Override
84  public void startConstruction() throws ServletException, IOException {
85  getConstructor().addCSS("/_internal/css/search.css");
87  PrintWriter out = getConstructor().getOut();
88  out.print(getStart());
89  out.print(getStartBody());
90  getConstructor().setRequestAttribute("_search", this);
92  "/WEB-INF/elephant/elements/search.jsp"
93  );
94  out.print(getEndBody());
95  out.print(getEnd());
96  }
97 
98  @Override
99  public String getStart() {
100  return "<div class='search'>";
101  }
102 
103  @Override
104  public String getEnd() {
105  return "</div>";
106  }
107 
108 }
void addResults(FoundList newList)
Definition: FoundList.java:60
ISearchable[] getElements()
Definition: Search.java:46
FoundList search(String value, boolean ignoreCase)
Definition: Search.java:70
void setRequestAttribute(String key, Object value)