View Javadoc

1   /**
2    * This file Copyright (c) 2008-2013 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.module.rssaggregator;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.gui.control.ControlImpl;
38  import info.magnolia.freemarker.FreemarkerUtil;
39  
40  import java.util.ArrayList;
41  import java.util.HashMap;
42  import java.util.Iterator;
43  import java.util.List;
44  import java.util.Map;
45  
46  import javax.jcr.RepositoryException;
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpServletResponse;
49  
50  import org.apache.commons.lang.BooleanUtils;
51  import org.apache.commons.lang.StringUtils;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  /**
56   * Multi control providing entry consisting of two combo boxes and one text field.
57   *
58   * @author had
59   */
60  public class FiltersControl extends MultiControl {
61      private static final Logger log = LoggerFactory.getLogger(FiltersControl.class);
62  
63      private List<OptionConfiguration> properties;
64  
65      private List<OptionConfiguration> conditions;
66  
67      /**
68       * Called by the template. It renders the dynamic inner row using trim paths templating mechanism.
69       */
70      public String getInnerHtml() {
71          String name = "/" + StringUtils.replace(FiltersControl.class.getName(), ".", "/") + "Inner.html";
72          Map<String, Object> map = new HashMap<String, Object>();
73          map.put("this", this);
74          return FreemarkerUtil.process(name, map);
75      }
76  
77      @Override
78      public String getSaveInfo() {
79          Boolean renderSaveInfo = BooleanUtils.toBooleanObject(this.getConfigValue("saveInfo"));
80          if (BooleanUtils.toBooleanDefaultIfNull(renderSaveInfo, true)) {
81              ControlImpl dummy = new ControlImpl(this.getName(), (String) null);
82              //dummy.setValueType(ControlImpl.VALUETYPE_MULTIPLE);
83              return dummy.getHtmlSaveInfo();
84          }
85          // don' create the save info
86          return "";
87      }
88  
89      /**
90       * JS function used to create an object out of the input fields.
91       */
92      @Override
93      public String getGetObjectFunction() {
94          return "function(prefix, index){" +
95          " var obj = new Object();" +
96          " obj.condition = document.getElementById(prefix + '_condition').value; " +
97          " obj.property = document.getElementById(prefix + '_property').value; " +
98          " obj.regex = document.getElementById(prefix + '_regex').value; " +
99          " return obj;" +
100                 "}";
101     }
102 
103     /**
104      * JS function used to create a new empty object.
105      */
106     @Override
107     public String getNewObjectFunction() {
108         return "function(){" +
109         " var obj = new Object();" +
110         " obj.condition = ''; " +
111         " obj.property = ''; " +
112         " obj.regex = ''; " +
113         " return obj;" +
114                 "}";
115     }
116 
117     @Override
118     protected String getEmpty() {
119         return "[{condition:'', property:'',regex:''}]";
120     }
121 
122     public List<OptionConfiguration> getConditions() {
123         return conditions;
124     }
125 
126     public List<OptionConfiguration> getProperties() {
127         return properties;
128     }
129 
130     public void setProperties(List<OptionConfiguration> properties) {
131         this.properties = properties;
132     }
133 
134     public void setConditions(List<OptionConfiguration> conditions) {
135         this.conditions = conditions;
136     }
137 
138     @Override
139     public void init(HttpServletRequest request, HttpServletResponse response,
140             Content storageNode, Content configNode) throws RepositoryException {
141         super.init(request, response, storageNode, configNode);
142         // TODO: use Content2Bean
143         this.conditions = new ArrayList<OptionConfiguration>();
144         for (Iterator<Content> iter = configNode.getContent("options-conditions").getChildren().iterator(); iter.hasNext(); ) {
145             Content child = iter.next();
146             OptionConfiguration bc = new OptionConfiguration();
147             bc.setLabel(child.getNodeData("label").getString());
148             bc.setValue(child.getNodeData("value").getString());
149             this.conditions.add(bc);
150         }
151         this.properties = new ArrayList<OptionConfiguration>();
152         for (Iterator<Content> iter = configNode.getContent("options-properties").getChildren().iterator(); iter.hasNext(); ) {
153             Content child = iter.next();
154             OptionConfiguration bc = new OptionConfiguration();
155             bc.setLabel(child.getNodeData("label").getString());
156             bc.setValue(child.getNodeData("value").getString());
157             this.properties.add(bc);
158         }
159     }
160 }