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.admininterface;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.gui.dialog.Dialog;
38 import info.magnolia.cms.gui.dialog.DialogControlImpl;
39 import info.magnolia.cms.gui.dialog.UUIDDialogControl;
40 import info.magnolia.cms.security.AccessDeniedException;
41 import info.magnolia.cms.util.ContentUtil;
42
43 import javax.jcr.PathNotFoundException;
44 import javax.jcr.RepositoryException;
45
46 import org.apache.commons.lang.StringUtils;
47
48
49
50
51
52
53
54
55 public class UUIDSaveHandler extends SaveHandlerImpl implements DialogAwareSaveHandler {
56
57
58
59
60 private Dialog dialog;
61
62 public Dialog getDialog() {
63 return dialog;
64 }
65
66 public void setDialog(Dialog dialog) {
67 this.dialog = dialog;
68 }
69
70
71
72
73 protected void processString(Content node, String name, int type, int encoding, String[] values, String valueStr)
74 throws PathNotFoundException, RepositoryException, AccessDeniedException {
75 DialogControlImpl control = getControl(name);
76 if (control instanceof UUIDDialogControl) {
77
78 if (StringUtils.isNotEmpty(valueStr)) {
79 valueStr = getUUID(name, valueStr);
80 }
81 }
82
83 super.processString(node, name, type, encoding, values, valueStr);
84 }
85
86
87
88
89 protected void processMultiple(Content node, String name, int type, String[] values) throws RepositoryException,
90 PathNotFoundException, AccessDeniedException {
91 DialogControlImpl control = getControl(name);
92
93 if (control instanceof UUIDDialogControl) {
94 if (values != null && values.length != 0) {
95 String[] uuidValues = new String[values.length];
96 for (int i = 0; i < values.length; i++) {
97 uuidValues[i] = getUUID(name, values[i]);
98 }
99 values = uuidValues;
100 }
101 }
102 super.processMultiple(node, name, type, values);
103 }
104
105
106
107
108
109
110 protected DialogControlImpl getControl(String name) {
111 Object control = this.getDialog().getSub(name);
112 if (control instanceof DialogControlImpl) {
113 return (DialogControlImpl) control;
114 }
115 return null;
116 }
117
118
119
120
121
122
123
124 private String getUUID(String name, String path) {
125 String repository = ((UUIDDialogControl)getControl(name)).getRepository();
126 Content node = ContentUtil.getContent(repository, path);
127 if (node == null) {
128 return path;
129 }
130 return node.getUUID();
131 }
132
133 }