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 @Override
63 public Dialog getDialog() {
64 return dialog;
65 }
66
67 @Override
68 public void setDialog(Dialog dialog) {
69 this.dialog = dialog;
70 }
71
72
73
74
75 @Override
76 protected void processString(Content node, String name, int type, int encoding, String[] values, String valueStr)
77 throws PathNotFoundException, RepositoryException, AccessDeniedException {
78 DialogControlImpl control = getControl(name);
79 if (control instanceof UUIDDialogControl) {
80
81 if (StringUtils.isNotEmpty(valueStr)) {
82 valueStr = getUUID(name, valueStr);
83 }
84 }
85
86 super.processString(node, name, type, encoding, values, valueStr);
87 }
88
89
90
91
92 @Override
93 protected void processMultiple(Content node, String name, int type, String[] values) throws RepositoryException,
94 PathNotFoundException, AccessDeniedException {
95 DialogControlImpl control = getControl(name);
96
97 if (control instanceof UUIDDialogControl) {
98 if (values != null && values.length != 0) {
99 String[] uuidValues = new String[values.length];
100 for (int i = 0; i < values.length; i++) {
101 uuidValues[i] = getUUID(name, values[i]);
102 }
103 values = uuidValues;
104 }
105 }
106 super.processMultiple(node, name, type, values);
107 }
108
109
110
111
112
113
114 protected DialogControlImpl getControl(String name) {
115 Object control = this.getDialog().getSub(name);
116 if (control instanceof DialogControlImpl) {
117 return (DialogControlImpl) control;
118 }
119 return null;
120 }
121
122
123
124
125
126
127
128 private String getUUID(String name, String path) {
129 String repository = ((UUIDDialogControl)getControl(name)).getRepository();
130 Content node = ContentUtil.getContent(repository, path);
131 if (node == null) {
132 return path;
133 }
134 return node.getUUID();
135 }
136
137 }