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.cms.gui.dialog;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.ItemType;
38 import info.magnolia.cms.core.NodeData;
39 import info.magnolia.cms.i18n.Messages;
40 import info.magnolia.cms.i18n.MessagesManager;
41 import info.magnolia.cms.i18n.MessagesUtil;
42 import info.magnolia.cms.util.AlertUtil;
43 import info.magnolia.cms.util.ContentUtil;
44 import info.magnolia.cms.util.NodeDataUtil;
45 import info.magnolia.cms.util.RequestFormUtil;
46
47 import java.io.IOException;
48 import java.io.Writer;
49 import java.util.ArrayList;
50 import java.util.Date;
51 import java.util.Hashtable;
52 import java.util.Iterator;
53 import java.util.List;
54 import java.util.Map;
55 import java.util.regex.Pattern;
56
57 import javax.jcr.PathNotFoundException;
58 import javax.jcr.RepositoryException;
59 import javax.servlet.http.HttpServletRequest;
60 import javax.servlet.http.HttpServletResponse;
61 import javax.servlet.http.HttpSession;
62
63 import org.apache.commons.lang.BooleanUtils;
64 import org.apache.commons.lang.StringUtils;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67
68
69
70
71
72 public abstract class DialogControlImpl implements DialogControl {
73
74 private static final String REQUIRED_PROPERTY = "required";
75
76 public static final String VALIDATION_PATTERN_PROPERTY = "validationPattern";
77
78 public static final String DEFAULT_VALUE_PROPERTY = "defaultValue";
79
80 private static final String I18N_BASENAME_PROPERTY = "i18nBasename";
81
82 public static final String SESSION_ATTRIBUTENAME_DIALOGOBJECT = "mgnlSessionAttribute";
83
84 public static final String SESSION_ATTRIBUTENAME_DIALOGOBJECT_REMOVE = "mgnlSessionAttributeRemove";
85
86
87
88
89 private static Logger log = LoggerFactory.getLogger(DialogControlImpl.class);
90
91
92
93
94 private HttpServletRequest request;
95
96
97
98
99 private HttpServletResponse response;
100
101
102
103
104 private Content storageNode;
105
106
107
108
109 private Map config = new Hashtable();
110
111
112
113
114 private final List subs = new ArrayList();
115
116
117
118
119 private List options = new ArrayList();
120
121
122
123
124
125 private String id = "mgnlControl";
126
127 protected String value;
128
129
130
131
132 private List values;
133
134 private DialogControlImpl parent;
135
136 private DialogControlImpl topParent;
137
138
139
140
141
142 private Messages messages;
143
144
145
146 public void init(HttpServletRequest request, HttpServletResponse response, Content storageNode, Content configNode)
147 throws RepositoryException {
148
149 if (log.isDebugEnabled()) {
150 log.debug("Init " + getClass().getName());
151 }
152
153 this.storageNode = storageNode;
154 this.request = request;
155 this.response = response;
156
157 this.initializeConfig(configNode);
158 }
159
160
161
162
163 public void drawHtml(Writer out) throws IOException {
164 this.drawHtmlPreSubs(out);
165 this.drawSubs(out);
166 this.drawHtmlPostSubs(out);
167 }
168
169 public void addSub(Object o) {
170 this.getSubs().add(o);
171 if(o instanceof DialogControlImpl){
172 ((DialogControlImpl)o).setParent(this);
173 }
174 }
175
176 public void setConfig(String key, String value) {
177 if (value != null) {
178 this.config.put(key, value);
179 }
180 }
181
182 public void setConfig(String key, boolean value) {
183 this.config.put(key, BooleanUtils.toBooleanObject(value).toString());
184 }
185
186 public void setConfig(String key, int value) {
187 this.config.put(key, Integer.toString(value));
188 }
189
190 public String getConfigValue(String key, String nullValue) {
191 if (this.config.containsKey(key)) {
192 return (String) this.config.get(key);
193 }
194
195 return nullValue;
196 }
197
198 public String getConfigValue(String key) {
199 return this.getConfigValue(key, StringUtils.EMPTY);
200 }
201
202 public void setValue(String s) {
203 this.value = s;
204 }
205
206 public String getValue() {
207 if (this.value == null) {
208 if (this.getStorageNode() != null) {
209 this.value = readValue();
210 if(this instanceof UUIDDialogControl){
211 String repository = ((UUIDDialogControl)this).getRepository();
212 this.value = ContentUtil.uuid2path(repository, this.value);
213 }
214 }
215 RequestFormUtil params = new RequestFormUtil(request);
216 if (params.getParameter(this.getName()) != null) {
217 this.value = params.getParameter(this.getName());
218 }
219
220 if (this.value == null && StringUtils.isNotEmpty(getConfigValue(DEFAULT_VALUE_PROPERTY))) {
221 return this.getMessage(this.getConfigValue(DEFAULT_VALUE_PROPERTY));
222 }
223
224 if (this.value == null) {
225 this.value = StringUtils.EMPTY;
226 }
227 }
228 return this.value;
229 }
230
231 protected String readValue() {
232 try {
233 if(!this.getStorageNode().hasNodeData(this.getName())){
234 return null;
235 }
236 }
237 catch (RepositoryException e) {
238 log.error("can't read nodedata [" + this.getName() + "]", e);
239 return null;
240 }
241 return this.getStorageNode().getNodeData(this.getName()).getString();
242 }
243
244 public void setSaveInfo(boolean b) {
245 this.setConfig("saveInfo", b);
246 }
247
248
249
250
251
252 public void setName(String s) {
253 this.setConfig("name", s);
254 }
255
256
257
258
259
260 public String getName() {
261 return this.getConfigValue("name");
262 }
263
264 public void addOption(Object o) {
265 this.getOptions().add(o);
266 }
267
268 public Content getStorageNode() {
269 return this.storageNode;
270 }
271
272 public void setLabel(String s) {
273 this.config.put("label", s);
274 }
275
276 public void setDescription(String s) {
277 this.config.put("description", s);
278 }
279
280 public void removeSessionAttribute() {
281 String name = this.getConfigValue(SESSION_ATTRIBUTENAME_DIALOGOBJECT);
282 HttpServletRequest request = this.getRequest();
283 if (request == null) {
284 request = this.getTopParent().getRequest();
285 }
286 try {
287 HttpSession httpsession = request.getSession(false);
288 if (httpsession != null) {
289 httpsession.removeAttribute(name);
290 }
291 }
292 catch (Exception e) {
293 if (log.isDebugEnabled()) {
294 log.debug("removeSessionAttribute() for " + name + " failed because this.request is null");
295 }
296 }
297 }
298
299 public HttpServletRequest getRequest() {
300 return this.request;
301 }
302
303 public void setOptions(List options) {
304 this.options = options;
305 }
306
307 protected void drawHtmlPreSubs(Writer out) throws IOException {
308
309 }
310
311 protected void drawSubs(Writer out) throws IOException {
312 Iterator it = this.getSubs().iterator();
313 int i = 0;
314 while (it.hasNext()) {
315
316 String dsId = this.getId() + "_" + i;
317 DialogControlImpl ds = (DialogControlImpl) it.next();
318
319 ds.setParent(this);
320 ds.setId(dsId);
321 ds.drawHtml(out);
322 i++;
323 }
324 }
325
326 protected void drawHtmlPostSubs(Writer out) throws IOException {
327
328 }
329
330 public DialogControlImpl getParent() {
331 return this.parent;
332 }
333
334 protected void setTopParent(DialogControlImpl top) {
335 this.topParent = top;
336 }
337
338 public DialogControlImpl getTopParent() {
339 DialogControlImpl topParent = this;
340 if(this.topParent == null){
341 while(topParent.getParent() != null){
342 topParent = topParent.getParent();
343 }
344 this.topParent = topParent;
345 }
346 return this.topParent;
347 }
348
349 public List getSubs() {
350 return this.subs;
351 }
352
353
354
355
356
357
358 public DialogControlImpl getSub(String name) {
359 DialogControlImpl found;
360 for (Iterator iter = subs.iterator(); iter.hasNext();) {
361 Object control = iter.next();
362
363
364 if (control instanceof DialogControlImpl) {
365 if (StringUtils.equals(((DialogControlImpl) control).getName(), name)) {
366 return (DialogControlImpl) control;
367 }
368 found = ((DialogControlImpl) control).getSub(name);
369 if (found != null) {
370 return found;
371 }
372 }
373 }
374 return null;
375 }
376
377 protected HttpServletResponse getResponse() {
378 return this.response;
379 }
380
381
382
383
384 @Deprecated
385 protected void clearWebsiteNode() {
386 this.storageNode = null;
387 }
388
389 public String getId() {
390 return this.id;
391 }
392
393 public String getLabel() {
394 return this.getConfigValue("label", StringUtils.EMPTY);
395 }
396
397 public String getDescription() {
398 return this.getConfigValue("description", StringUtils.EMPTY);
399 }
400
401 public List getOptions() {
402 return this.options;
403 }
404
405 public List getValues() {
406 if (this.values == null) {
407 this.values = readValues();
408
409 if(this instanceof UUIDDialogControl){
410 String repository = ((UUIDDialogControl)this).getRepository();
411 List pathes = new ArrayList();
412 for (Iterator iter = this.values.iterator(); iter.hasNext();) {
413 String uuid = (String) iter.next();
414 String path = ContentUtil.uuid2path(repository, uuid);
415 pathes.add(path);
416 }
417 this.values = pathes;
418 }
419
420 if (request != null) {
421 RequestFormUtil params = new RequestFormUtil(request);
422 String[] values = params.getParameterValues(this.getName());
423 if (values != null && values.length > 0) {
424 this.values.clear();
425 for (int i = 0; i < values.length; i++) {
426 String value = values[i];
427 this.values.add(value);
428 }
429 }
430 }
431 }
432
433 return this.values;
434 }
435
436 protected List readValues() {
437 List values = new ArrayList();
438 if (this.getStorageNode() != null) {
439 try {
440 NodeData node = this.getStorageNode().getNodeData(this.getName());
441 if(node.isMultiValue() == NodeData.MULTIVALUE_TRUE) {
442 values = NodeDataUtil.getValuesStringList(node.getValues());
443 } else {
444 Iterator it = this.getStorageNode().getContent(this.getName()).getNodeDataCollection().iterator();
445 while (it.hasNext()) {
446 NodeData data = (NodeData) it.next();
447 values.add(data.getString());
448 }
449 }
450 }
451 catch (PathNotFoundException e) {
452
453 }
454 catch (RepositoryException re) {
455 log.error("can't set values", re);
456 }
457 }
458 return values;
459 }
460
461
462
463
464 public void setSessionAttribute() {
465 String name = SESSION_ATTRIBUTENAME_DIALOGOBJECT + "_" + this.getName() + "_" + new Date().getTime();
466 this.setConfig(SESSION_ATTRIBUTENAME_DIALOGOBJECT, name);
467 HttpServletRequest request = this.getRequest();
468 if (request == null) {
469 request = this.getTopParent().getRequest();
470 }
471 try {
472
473
474 HttpSession httpsession = request.getSession(true);
475 httpsession.setAttribute(name, this);
476 }
477 catch (Exception e) {
478 log.error("setSessionAttribute() for " + name + " failed because this.request is null");
479 }
480 }
481
482 private void setId(String id) {
483 this.id = id;
484 }
485
486 private void initializeConfig(Content configNodeParent) throws RepositoryException {
487
488
489 Map config = new Hashtable();
490
491 if (configNodeParent == null) {
492
493 return;
494 }
495
496
497 Iterator itProps = configNodeParent.getNodeDataCollection().iterator();
498 while (itProps.hasNext()) {
499 NodeData data = (NodeData) itProps.next();
500 String name = data.getName();
501 String value = data.getString();
502 config.put(name, value);
503 }
504
505 config.put("handle", configNodeParent.getHandle());
506
507
508 if (!config.containsKey("name")) {
509 config.put("name", configNodeParent.getName());
510 }
511
512 this.config = config;
513
514 Iterator it = configNodeParent.getChildren(ItemType.CONTENTNODE).iterator();
515 while (it.hasNext()) {
516 Content configNode = (Content) it.next();
517
518
519 while(configNode.hasNodeData("reference")){
520 configNode = configNode.getNodeData("reference").getReferencedContent();
521 }
522
523 String controlType = configNode.getNodeData("controlType").getString();
524
525 if (StringUtils.isEmpty(controlType)) {
526 String name = configNode.getName();
527 if (!name.startsWith("options")) {
528 log.debug("Missing control type for configNode " + name);
529 }
530 return;
531 }
532
533 if (log.isDebugEnabled()) {
534 log.debug("Loading control \"" + controlType + "\" for " + configNode.getHandle());
535 }
536 DialogControl dialogControl = DialogFactory.loadDialog(request, response, this.getStorageNode(), configNode);
537 this.addSub(dialogControl);
538 }
539 }
540
541 private void setParent(DialogControlImpl parent) {
542 this.parent = parent;
543 }
544
545
546
547
548
549
550 protected Messages getMessages() {
551 if (messages == null) {
552
553 if (this.getParent() == null) {
554 messages = MessagesManager.getMessages();
555 }
556 else {
557
558 messages = this.getParent().getMessages();
559 }
560
561 String basename = this.getConfigValue(I18N_BASENAME_PROPERTY);
562 if (StringUtils.isNotEmpty(basename)) {
563
564 messages = MessagesUtil.chain(basename, messages);
565 }
566 }
567 return messages;
568 }
569
570
571
572
573
574
575 public String getMessage(String key) {
576 return this.getMessages().getWithDefault(key, key);
577 }
578
579
580
581
582
583
584
585 public String getMessage(String key, Object[] args) {
586 return this.getMessages().getWithDefault(key, args, key);
587 }
588
589
590
591
592
593 public boolean validate() {
594
595 if (this.isRequired()) {
596 boolean valueFound = false;
597 for (Iterator iter = this.getValues().iterator(); iter.hasNext();) {
598 String value = (String) iter.next();
599 if(!StringUtils.isEmpty(value)){
600 valueFound = true;
601 break;
602 }
603 }
604 if (!valueFound && StringUtils.isEmpty(this.getValue())) {
605 setValidationMessage("dialogs.validation.required");
606 return false;
607 }
608 }
609 if(StringUtils.isNotEmpty(getValidationPattern()) && StringUtils.isNotEmpty(this.getValue())){
610 if(!Pattern.matches(getValidationPattern(), this.getValue())){
611 setValidationMessage("dialogs.validation.invalid");
612 return false;
613 }
614 }
615 for (Iterator iter = this.getSubs().iterator(); iter.hasNext();) {
616 DialogControl sub = (DialogControl) iter.next();
617 if (sub instanceof DialogControlImpl) {
618 DialogControlImpl subImpl = (DialogControlImpl) sub;
619 subImpl.setParent(this);
620 if (!subImpl.validate()) {
621 return false;
622 }
623 }
624
625 }
626 return true;
627 }
628
629 protected void setValidationMessage(String msg) {
630 String name = this.getMessage(this.getLabel());
631 String tabName = "";
632 if (this.getParent() instanceof DialogTab) {
633 DialogTab tab = (DialogTab) this.getParent();
634 tabName = tab.getMessage(tab.getLabel());
635 }
636 AlertUtil.setMessage(this.getMessage(msg, new Object[]{name, tabName, this.getValue()}));
637 }
638
639 public String getValidationPattern() {
640 return this.getConfigValue(VALIDATION_PATTERN_PROPERTY);
641 }
642
643
644
645
646
647 public boolean isRequired() {
648 if (BooleanUtils.toBoolean(this.getConfigValue(REQUIRED_PROPERTY))) {
649 return true;
650 }
651 return false;
652 }
653
654 public void setRequired(boolean required) {
655 this.setConfig(REQUIRED_PROPERTY, BooleanUtils.toStringTrueFalse(required));
656 }
657
658
659
660
661 @Deprecated
662 public Content getWebsiteNode() {
663 return getStorageNode();
664 }
665
666 }