View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Configers the search based on the dialog.
55   * @author Philipp Bracher
56   * @version $Revision: 41137 $ ($Author: gjoseph $)
57   */
58  public class DialogBasedSearchConfig extends SearchConfigImpl {
59  
60      Logger log = LoggerFactory.getLogger(DialogBasedSearchConfig.class);
61  
62      /**
63       * @param dialogPath
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       * gets all the controls defined in the dialog
76       * @throws Exception
77       */
78      protected void init(Content dialogNode) throws Exception {
79          // ordered definition
80          SortedMap sortedByOrder = new TreeMap();
81          SortedMap sortedByName = new TreeMap();
82          
83          // for all tabs
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         // add them now (after ordering)
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                 // this happens if the search Type is not a class
152             }
153             catch (Exception e) {
154                 log.error("can't instantiate search control definition " + searchType , e);
155             }
156         }
157         // default to the normal search field
158         return new SearchControlDefinition(name, label, searchType);
159     }
160 }