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.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
57
58
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
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 public String getSaveInfo() {
78 Boolean renderSaveInfo = BooleanUtils.toBooleanObject(this.getConfigValue("saveInfo"));
79 if (BooleanUtils.toBooleanDefaultIfNull(renderSaveInfo, true)) {
80 ControlImpl dummy = new ControlImpl(this.getName(), (String) null);
81
82 return dummy.getHtmlSaveInfo();
83 }
84
85 return "";
86 }
87
88
89
90
91 public String getGetObjectFunction() {
92 return "function(prefix, index){" +
93 " var obj = new Object();" +
94 " obj.condition = document.getElementById(prefix + '_condition').value; " +
95 " obj.property = document.getElementById(prefix + '_property').value; " +
96 " obj.regex = document.getElementById(prefix + '_regex').value; " +
97 " return obj;" +
98 "}";
99 }
100
101
102
103
104 public String getNewObjectFunction() {
105 return "function(){" +
106 " var obj = new Object();" +
107 " obj.condition = ''; " +
108 " obj.property = ''; " +
109 " obj.regex = ''; " +
110 " return obj;" +
111 "}";
112 }
113
114 protected String getEmpty() {
115 return "[{condition:'', property:'',regex:''}]";
116 }
117
118 public List<OptionConfiguration> getConditions() {
119 return conditions;
120 }
121
122 public List<OptionConfiguration> getProperties() {
123 return properties;
124 }
125
126 public void setProperties(List<OptionConfiguration> properties) {
127 this.properties = properties;
128 }
129
130 public void setConditions(List<OptionConfiguration> conditions) {
131 this.conditions = conditions;
132 }
133
134 public void init(HttpServletRequest request, HttpServletResponse response,
135 Content storageNode, Content configNode) throws RepositoryException {
136 super.init(request, response, storageNode, configNode);
137
138 this.conditions = new ArrayList<OptionConfiguration>();
139 for (Iterator<Content> iter = configNode.getContent("options-conditions").getChildren().iterator(); iter.hasNext(); ) {
140 Content child = iter.next();
141 OptionConfiguration bc = new OptionConfiguration();
142 bc.setLabel(child.getNodeData("label").getString());
143 bc.setValue(child.getNodeData("value").getString());
144 this.conditions.add(bc);
145 }
146 this.properties = new ArrayList<OptionConfiguration>();
147 for (Iterator<Content> iter = configNode.getContent("options-properties").getChildren().iterator(); iter.hasNext(); ) {
148 Content child = iter.next();
149 OptionConfiguration bc = new OptionConfiguration();
150 bc.setLabel(child.getNodeData("label").getString());
151 bc.setValue(child.getNodeData("value").getString());
152 this.properties.add(bc);
153 }
154 }
155 }