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.core.NodeData;
38 import info.magnolia.cms.gui.control.Button;
39 import info.magnolia.cms.gui.control.ControlImpl;
40 import info.magnolia.cms.gui.dialog.DialogBox;
41 import info.magnolia.freemarker.FreemarkerUtil;
42
43 import java.io.IOException;
44 import java.io.PrintWriter;
45 import java.io.Writer;
46 import java.util.ArrayList;
47 import java.util.Iterator;
48 import java.util.List;
49
50 import javax.jcr.PathNotFoundException;
51 import javax.jcr.RepositoryException;
52
53 import org.apache.commons.lang.BooleanUtils;
54 import org.apache.commons.lang.StringUtils;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58
59
60
61
62
63 public abstract class MultiControl extends DialogBox implements MultiValueControl {
64 private static final Logger log = LoggerFactory.getLogger(MultiControl.class);
65
66
67
68
69 public String getJSON() {
70 final List<String> values = this.getValues();
71
72 if (values.size() == 0) {
73 return getEmpty();
74 }
75
76
77 final List<String> objects = new ArrayList<String>();
78 for (Iterator<String> iter = values.iterator(); iter.hasNext();) {
79 final String value = iter.next();
80 log.debug("value:" + value);
81 objects.add("{" + value + "}");
82 }
83 return "[" + StringUtils.join(objects.iterator(), ",") + "]";
84 }
85
86 protected abstract String getEmpty();
87
88 @Override
89 protected List<String> readValues() {
90 List<String> values = new ArrayList<String>();
91 if (this.getStorageNode() != null) {
92 try {
93 Content data = this.getStorageNode().getContent(getName());
94 Iterator<Content> iter = data.getChildren().iterator();
95 while (iter.hasNext()) {
96 Content value = iter.next();
97 Iterator<NodeData> dataVals = value.getNodeDataCollection().iterator();
98 StringBuffer buf = new StringBuffer();
99 while (dataVals.hasNext()) {
100 NodeData partial = dataVals.next();
101 if (buf.length() > 0) {
102 buf.append(",");
103 }
104 buf.append(partial.getName()).append(":'").append(partial.getString()).append("'");
105 }
106 values.add(buf.toString());
107 }
108 } catch (PathNotFoundException e) {
109
110 }
111 catch (RepositoryException re) {
112 log.error("can't set values", re);
113 }
114 }
115 return values;
116 }
117
118 @Override
119 public void drawHtml(Writer w) throws IOException {
120 PrintWriter out = (PrintWriter) w;
121 this.drawHtmlPre(out);
122 out.write(FreemarkerUtil.process(MultiControl.class, this));
123 this.drawHtmlPost(out);
124
125 }
126
127 public String getSaveInfo() {
128 Boolean renderSaveInfo = BooleanUtils.toBooleanObject(this.getConfigValue("saveInfo"));
129 if (BooleanUtils.toBooleanDefaultIfNull(renderSaveInfo, true)) {
130 ControlImpl dummy = new ControlImpl(this.getName(), (String) null);
131
132 return dummy.getHtmlSaveInfo();
133 }
134
135 return "";
136 }
137
138
139
140
141 public String getAddButton() {
142 Button add = new Button();
143 add.setLabel(getMessage("buttons.add"));
144 add.setOnclick(this.getName() + "DynamicTable.addNew();");
145 add.setSmall(true);
146 return add.getHtml();
147 }
148
149
150
151
152 public String getDeleteButton() {
153 Button delete = new Button();
154 delete.setLabel(this.getMessage("buttons.delete"));
155 delete.setOnclick(this.getName() + "DynamicTable.del('${index}');" + this.getName() + "DynamicTable.persist();");
156 delete.setSmall(true);
157 return delete.getHtml();
158 }
159
160
161
162
163
164
165 public String getHiddenFieldName() {
166 return this.getName();
167 }
168
169
170
171
172 public String getGetObjectFunction() {
173 return "function(prefix, index){" +
174 " var obj = new Object();" +
175 " obj.link = document.getElementById(prefix + '_link').value;" +
176 " obj.title = document.getElementById(prefix + '_title').value; " +
177 " return obj;" +
178 "}";
179 }
180
181
182
183
184 public String getNewObjectFunction() {
185 return "function(){" +
186 " var obj = new Object();" +
187 " obj.link = ''; " +
188 " obj.title = ''; " +
189 " return obj;" +
190 "}";
191 }
192 }