KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > controlx > search > DialogBasedSearchConfig


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.controlx.search;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.ItemType;
17 import info.magnolia.cms.util.NodeDataUtil;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.SortedMap JavaDoc;
22 import java.util.TreeMap JavaDoc;
23
24 import javax.jcr.RepositoryException;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30 /**
31  * Configers the search based on the dialog.
32  * @author Philipp Bracher
33  * @version $Revision: 6341 $ ($Author: philipp $)
34  */

35 public class DialogBasedSearchConfig extends SearchConfigImpl {
36
37     Logger log = LoggerFactory.getLogger(DialogBasedSearchConfig.class);
38
39     /**
40      * @param dialogPath
41      */

42     public DialogBasedSearchConfig(Content dialogNode) {
43         try {
44             init(dialogNode);
45         }
46         catch (Exception JavaDoc e) {
47             log.error("can't configure the search", e);
48         }
49     }
50
51     /**
52      * gets all the controls defined in the dialog
53      * @throws Exception
54      */

55     protected void init(Content dialogNode) throws Exception JavaDoc {
56         // ordered definition
57
SortedMap JavaDoc sortMap = new TreeMap JavaDoc();
58
59         // for all tabs
60
Collection JavaDoc tabNodes = dialogNode.getChildren(ItemType.CONTENTNODE);
61
62         for (Iterator JavaDoc iter = tabNodes.iterator(); iter.hasNext();) {
63             Content tabNode = (Content) iter.next();
64
65             Collection JavaDoc controlNodes = tabNode.getChildren();
66
67             for (Iterator JavaDoc iter2 = controlNodes.iterator(); iter2.hasNext();) {
68                 Content controlNode = (Content) iter2.next();
69
70                 if (controlNode.hasNodeData("searchable")) {
71                     String JavaDoc searchable = NodeDataUtil.getString(controlNode, "searchable");
72
73                     SearchControlDefinition def = createSearchControl(controlNode);
74
75                     sortMap.put(searchable, def);
76                 }
77             }
78         }
79
80         // add them now (after ordering)
81
for (Iterator JavaDoc iter = sortMap.values().iterator(); iter.hasNext();) {
82             SearchControlDefinition def = (SearchControlDefinition) iter.next();
83             this.addControlDefinition(def);
84         }
85     }
86
87     protected SearchControlDefinition createSearchControl(Content controlNode) throws Exception JavaDoc {
88         String JavaDoc type = controlNode.getNodeData("searchType").getString();
89         String JavaDoc name = controlNode.getNodeData("name").getString();
90         String JavaDoc label = controlNode.getNodeData("label").getString();
91
92         if (type.equals("select")) {
93             SelectSearchControlDefinition select = new SelectSearchControlDefinition(name, label);
94             configureSelect(select, controlNode);
95             return select;
96         }
97         return new SearchControlDefinition(name, label, type);
98     }
99
100     public void configureSelect(SelectSearchControlDefinition def, Content node) throws RepositoryException {
101         Collection JavaDoc optionNodes = node.getContent("options").getChildren();
102
103         for (Iterator JavaDoc iter = optionNodes.iterator(); iter.hasNext();) {
104             Content optionNode = (Content) iter.next();
105             String JavaDoc value = optionNode.getNodeData("value").getString();
106             String JavaDoc label = optionNode.getNodeData("value").getString();
107             def.addOption(value, label);
108         }
109     }
110
111 }
112
Popular Tags