BrightSide Workbench Full Report + Source Code
DaoSearch.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.File;
22 import java.io.IOException;
23 import java.io.Serializable;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.turro.string.Strings;
29 import org.jdom.Document;
30 import org.jdom.Element;
31 import org.jdom.JDOMException;
32 import org.jdom.input.SAXBuilder;
33 import org.turro.elephant.context.ElephantContext;
34 
39 public class DaoSearch extends LinkedHashMap<String, DaoSearchKey> implements Serializable {
40 
41  protected String sclass, root, template;
42 
43  public DaoSearch(String name) {
44  readItems(name);
45  }
46 
47  public String getSclass() {
48  return sclass;
49  }
50 
51  public String getRoot() {
52  return root;
53  }
54 
55  public String getTemplate() {
56  return template;
57  }
58 
59  public void clearValues() {
60  for(DaoSearchKey dsk : values()) {
61  dsk.setValue(null);
62  }
63  }
64 
65  private void readItems(String name) {
66  File confFile = new File(ElephantContext.getRealPath("/WEB-INF/elephant/conf/dao-search" + name.replaceAll("\\/", "-") + ".xml"));
67  if(confFile.exists()) {
68  try {
69  Element conf = null;
70  SAXBuilder builder = new SAXBuilder();
71  Document doc;
72  doc = builder.build(confFile);
73  conf = doc.getRootElement();
74  if(conf != null) {
75  addItems(conf.getChild("dao-search"));
76  }
77  } catch (JDOMException | IOException ex) {
78  Logger.getLogger(DaoSearch.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
79  }
80  }
81  }
82 
83  private void addItems(Element daoSearch) {
84  int count = 0;
85  if(daoSearch != null) {
86  sclass = daoSearch.getAttributeValue("class");
87  root = daoSearch.getAttributeValue("root");
88  template = daoSearch.getAttributeValue("template");
89  for(Element item : (List<Element>) daoSearch.getChildren()) {
90  String name = item.getAttributeValue("name");
91  if(!Strings.isBlank(name)) {
92  String type = item.getAttributeValue("type");
93  if(Strings.isBlank(type)) {
94  type = "text";
95  }
96  DaoSearchKey dsk = new DaoSearchKey(name, type);
97  dsk.readXML(item);
98  put(name, dsk);
99  } else {
100  name = "#" + item.getName() + count++;
101  DaoSearchKey dsk = new DaoSearchKey(name, null);
102  dsk.readXML(item);
103  put(name, dsk);
104  }
105  }
106  }
107  }
108 
109 }