1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.gui.controlx.search;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.ItemType;
38 import info.magnolia.cms.util.NodeDataUtil;
39
40 import java.util.Collection;
41 import java.util.Iterator;
42 import java.util.SortedMap;
43 import java.util.TreeMap;
44
45 import javax.jcr.RepositoryException;
46
47 import info.magnolia.objectfactory.Classes;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51
52
53
54
55
56
57
58 public class DialogBasedSearchConfig extends SearchConfigImpl {
59
60 Logger log = LoggerFactory.getLogger(DialogBasedSearchConfig.class);
61
62
63
64
65 public DialogBasedSearchConfig(Content dialogNode) {
66 try {
67 init(dialogNode);
68 }
69 catch (Exception e) {
70 log.error("can't configure the search", e);
71 }
72 }
73
74
75
76
77
78 protected void init(Content dialogNode) throws Exception {
79
80 SortedMap sortedByOrder = new TreeMap();
81 SortedMap sortedByName = new TreeMap();
82
83
84 Collection tabNodes = dialogNode.getChildren(ItemType.CONTENTNODE);
85
86 for (Iterator iter = tabNodes.iterator(); iter.hasNext();) {
87 Content tabNode = (Content) iter.next();
88
89 Collection controlNodes = tabNode.getChildren();
90
91 for (Iterator iter2 = controlNodes.iterator(); iter2.hasNext();) {
92 Content controlNode = (Content) iter2.next();
93 String searchable = NodeDataUtil.getString(controlNode, "searchable", "true");
94 if (!searchable.equals("false")) {
95 SearchControlDefinition def = createSearchControl(controlNode);
96
97 try{
98 Integer order = new Integer(searchable);
99 sortedByOrder.put(order, def);
100 }
101 catch(NumberFormatException e){
102 sortedByName.put(def.getLabel(), def);
103 }
104 }
105 }
106 }
107
108
109 for (Iterator iter = sortedByOrder.values().iterator(); iter.hasNext();) {
110 this.addControlDefinition((SearchControlDefinition) iter.next());
111 }
112
113 for (Iterator iter = sortedByName.values().iterator(); iter.hasNext();) {
114 this.addControlDefinition((SearchControlDefinition) iter.next());
115 }
116 }
117
118 protected SearchControlDefinition createSearchControl(Content controlNode) throws Exception {
119 String contolType = controlNode.getNodeData("controlType").getString();
120 String searchType = NodeDataUtil.getString(controlNode, "searchType", contolType);
121
122 String name = controlNode.getNodeData("name").getString();
123 String label = NodeDataUtil.getI18NString(controlNode, "label");
124
125 return createSearchControl(name, label, searchType, controlNode);
126 }
127
128 protected SearchControlDefinition createSearchControl(String name, String label, String searchType, Content controlNode) throws RepositoryException {
129 if (searchType.equals("select")) {
130 SelectSearchControlDefinition select = new SelectSearchControlDefinition(name, label);
131
132 Collection optionNodes = controlNode.getContent("options").getChildren();
133
134 for (Iterator iter = optionNodes.iterator(); iter.hasNext();) {
135 Content optionNode = (Content) iter.next();
136 String optionValue = optionNode.getNodeData("name").getString();
137 String optionLabel = optionNode.getNodeData("value").getString();
138 select.addOption(optionValue, optionLabel);
139 }
140 return select;
141 }
142 else{
143 try {
144 SearchControlDefinition def = Classes.newInstance(searchType, name, label);
145 if(def instanceof DialogBasedSearchControlDefinition){
146 ((DialogBasedSearchControlDefinition)def).init(controlNode);
147 }
148 return def;
149 }
150 catch (ClassNotFoundException e) {
151
152 }
153 catch (Exception e) {
154 log.error("can't instantiate search control definition " + searchType , e);
155 }
156 }
157
158 return new SearchControlDefinition(name, label, searchType);
159 }
160 }