SlingPropertyValueHandler.java
16.4 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.PropertyType
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.jcr.ValueFactory
* javax.jcr.nodetype.NodeType
* javax.jcr.nodetype.PropertyDefinition
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.servlets.post.Modification
*/
package com.day.cq.dam.core.impl;
import com.day.cq.dam.core.impl.DateParser;
import com.day.cq.dam.core.impl.RequestProperty;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.PropertyDefinition;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.Modification;
public class SlingPropertyValueHandler {
private static final Map<String, AutoType> AUTO_PROPS = new HashMap<String, AutoType>();
private final List<Modification> changes;
private final DateParser dateParser;
private final Calendar now = Calendar.getInstance();
private static final int PROPERTY_TYPE_WEAKREFERENCE = 10;
public SlingPropertyValueHandler(DateParser dateParser, List<Modification> changes) {
this.dateParser = dateParser;
this.changes = changes;
}
static AutoType getAutoType(String propertyName) {
return AUTO_PROPS.get(propertyName);
}
private PropertyDefinition searchPropertyDefinition(NodeType nodeType, String name) {
if (nodeType.getPropertyDefinitions() != null) {
for (PropertyDefinition pd : nodeType.getPropertyDefinitions()) {
if (!pd.getName().equals(name)) continue;
return pd;
}
}
return null;
}
private PropertyDefinition searchPropertyDefinition(Node node, String name) throws RepositoryException {
PropertyDefinition result = this.searchPropertyDefinition(node.getPrimaryNodeType(), name);
if (result == null && node.getMixinNodeTypes() != null) {
for (NodeType mt : node.getMixinNodeTypes()) {
result = this.searchPropertyDefinition(mt, name);
if (result == null) continue;
return result;
}
}
return result;
}
public void setProperty(Resource parent, RequestProperty prop) throws RepositoryException, PersistenceException {
Modifiable mod = new Modifiable();
mod.resource = parent;
mod.node = (Node)parent.adaptTo(Node.class);
mod.valueMap = (ModifiableValueMap)parent.adaptTo(ModifiableValueMap.class);
if (mod.valueMap == null) {
throw new PersistenceException("Resource at '" + parent.getPath() + "' is not modifiable.");
}
String name = prop.getName();
if (prop.providesValue()) {
this.setPropertyAsIs(mod, prop);
} else if (AUTO_PROPS.containsKey(name)) {
PropertyDefinition pd;
if (mod.node != null && (pd = this.searchPropertyDefinition(mod.node, name)) != null && (mod.node.isNew() && pd.isAutoCreated() || pd.isProtected())) {
return;
}
boolean isNew = mod.node != null ? mod.node.isNew() : true;
switch (SlingPropertyValueHandler.getAutoType(name)) {
case CREATED: {
if (!isNew) break;
this.setCurrentDate(mod, name);
break;
}
case CREATED_BY: {
if (!isNew) break;
this.setCurrentUser(mod, name);
break;
}
case MODIFIED: {
this.setCurrentDate(mod, name);
break;
}
case MODIFIED_BY: {
this.setCurrentUser(mod, name);
}
}
} else {
this.setPropertyAsIs(mod, prop);
}
}
private void setCurrentDate(Modifiable parent, String name) throws RepositoryException, PersistenceException {
this.removePropertyIfExists(parent, name);
parent.valueMap.put((Object)name, (Object)this.now);
this.changes.add(Modification.onModified((String)(parent.resource.getPath() + '/' + name)));
}
private void setCurrentUser(Modifiable parent, String name) throws RepositoryException, PersistenceException {
this.removePropertyIfExists(parent, name);
String user = parent.resource.getResourceResolver().getUserID();
parent.valueMap.put((Object)name, (Object)user);
this.changes.add(Modification.onModified((String)(parent.resource.getPath() + '/' + name)));
}
private String removePropertyIfExists(Modifiable parent, String name) throws RepositoryException, PersistenceException {
if (parent.valueMap.containsKey((Object)name)) {
if (parent.node != null) {
Property prop = parent.node.getProperty(name);
if (!prop.getDefinition().isMandatory()) {
String path = prop.getPath();
prop.remove();
return path;
}
} else {
parent.valueMap.remove((Object)name);
return parent.resource.getPath() + '/' + name;
}
}
return null;
}
private void setPropertyAsIs(Modifiable parent, RequestProperty prop) throws RepositoryException, PersistenceException {
String[] values = prop.getStringValues();
if (values == null || values.length == 1 && values[0].length() == 0) {
this.removeProperty(parent, prop);
} else if (values.length == 0) {
this.clearProperty(parent, prop);
} else {
Session s;
if (prop.isPatch() && (values = this.patch(parent, prop.getName(), values)) == null) {
return;
}
boolean multiValue = this.isMultiValue(parent, prop, values);
int type = this.getType(parent, prop);
if (multiValue) {
this.removeIfSingleValueProperty(parent, prop);
}
if ((s = (Session)parent.resource.getResourceResolver().adaptTo(Session.class)) != null) {
ValueFactory valFac = s.getValueFactory();
if (type == 5 && this.storeAsDate(parent, prop.getName(), values, multiValue, valFac)) {
return;
}
}
this.store(parent, prop.getName(), values, type, multiValue);
}
}
private String[] patch(Modifiable parent, String name, String[] values) throws RepositoryException, PersistenceException {
ArrayList<String> oldValues = new ArrayList<String>();
if (parent.valueMap.containsKey((Object)name)) {
if (parent.node != null) {
Property p = parent.node.getProperty(name);
if (!p.getDefinition().isMultiple()) {
return null;
}
for (String v : p.getValues()) {
oldValues.add(v.getString());
}
} else {
String[] setValues = (String[])parent.valueMap.get(name, String[].class);
if (setValues != null) {
for (String v : setValues) {
oldValues.add(v);
}
}
}
}
boolean modified = false;
for (String v : values) {
if (v == null || v.length() <= 0) continue;
char op = v.charAt(0);
String val = v.substring(1);
if (op == '+') {
if (oldValues.contains(val)) continue;
oldValues.add(val);
modified = true;
continue;
}
if (op != '-') continue;
while (oldValues.remove(val)) {
modified = true;
}
}
if (modified) {
return oldValues.toArray(new String[oldValues.size()]);
}
return null;
}
private boolean isReferencePropertyType(int propertyType) {
return propertyType == 9 || propertyType == 10;
}
private boolean isWeakReference(int propertyType) {
return propertyType == 10;
}
private int getType(Modifiable parent, RequestProperty prop) throws RepositoryException {
int type = 0;
if (prop.getTypeHint() != null) {
try {
type = PropertyType.valueFromName((String)prop.getTypeHint());
}
catch (Exception e) {
// empty catch block
}
}
String[] values = prop.getStringValues();
if (type == 0 && values != null && values.length > 0 && parent.node != null && parent.node.hasProperty(prop.getName())) {
type = parent.node.getProperty(prop.getName()).getType();
}
return type;
}
private boolean isMultiValue(Modifiable parent, RequestProperty prop, String[] values) throws RepositoryException {
if (values != null && values.length > 1) {
return true;
}
if (prop.hasMultiValueTypeHint()) {
return true;
}
if (prop.isPatch()) {
return true;
}
if (parent.node != null) {
if (parent.node.hasProperty(prop.getName())) {
return parent.node.getProperty(prop.getName()).getDefinition().isMultiple();
}
} else {
Object value = parent.valueMap.get((Object)prop.getName());
if (value != null && value.getClass().isArray()) {
return true;
}
}
return false;
}
private void clearProperty(Modifiable parent, RequestProperty prop) throws RepositoryException, PersistenceException {
if (parent.valueMap.containsKey((Object)prop.getName())) {
if (parent.node != null) {
if (parent.node.getProperty(prop.getName()).getDefinition().isMultiple()) {
String removePath = this.removePropertyIfExists(parent, prop.getName());
if (removePath != null) {
this.changes.add(Modification.onDeleted((String)removePath));
}
} else {
this.changes.add(Modification.onModified((String)parent.node.setProperty(prop.getName(), "").getPath()));
}
} else {
parent.valueMap.put((Object)prop.getName(), (Object)"");
this.changes.add(Modification.onModified((String)(parent.resource.getPath() + '/' + prop.getName())));
}
}
}
private void removeProperty(Modifiable parent, RequestProperty prop) throws RepositoryException, PersistenceException {
String removePath = this.removePropertyIfExists(parent, prop.getName());
if (removePath != null) {
this.changes.add(Modification.onDeleted((String)removePath));
}
}
private void removeIfSingleValueProperty(Modifiable parent, RequestProperty prop) throws RepositoryException, PersistenceException {
if (parent.valueMap.containsKey((Object)prop.getName())) {
if (parent.node != null) {
String removePath;
if (!parent.node.getProperty(prop.getName()).getDefinition().isMultiple() && (removePath = this.removePropertyIfExists(parent, prop.getName())) != null) {
this.changes.add(Modification.onDeleted((String)removePath));
}
} else {
String removePath = this.removePropertyIfExists(parent, prop.getName());
if (removePath != null) {
this.changes.add(Modification.onDeleted((String)removePath));
}
}
}
}
private boolean storeAsDate(Modifiable parent, String name, String[] values, boolean multiValued, ValueFactory valFac) throws RepositoryException, PersistenceException {
Calendar c;
if (multiValued) {
Value[] array = this.dateParser.parse(values, valFac);
if (array != null) {
if (parent.node != null) {
parent.node.setProperty(name, array);
} else {
Calendar[] dates = new Calendar[array.length];
int index = 0;
for (Value v : array) {
dates[index] = v.getDate();
++index;
}
parent.valueMap.put((Object)name, (Object)dates);
}
this.changes.add(Modification.onModified((String)(parent.resource.getPath() + '/' + name)));
return true;
}
} else if (values.length >= 1 && (c = this.dateParser.parse(values[0])) != null) {
parent.valueMap.put((Object)name, (Object)c);
this.changes.add(Modification.onModified((String)(parent.resource.getPath() + '/' + name)));
return true;
}
return false;
}
private void store(Modifiable parent, String name, String[] values, int type, boolean multiValued) throws RepositoryException, PersistenceException {
if (parent.node != null) {
Property p = null;
if (multiValued) {
p = type == 0 ? parent.node.setProperty(name, values) : parent.node.setProperty(name, values, type);
} else if (values.length >= 1) {
p = type == 0 ? parent.node.setProperty(name, values[0]) : parent.node.setProperty(name, values[0], type);
}
if (p != null) {
this.changes.add(Modification.onModified((String)p.getPath()));
}
} else if (multiValued) {
parent.valueMap.put((Object)name, SlingPropertyValueHandler.toJavaObject(values, type));
this.changes.add(Modification.onModified((String)(parent.resource.getPath() + '/' + name)));
} else if (values.length >= 1) {
parent.valueMap.put((Object)name, SlingPropertyValueHandler.toJavaObject(values[0], type));
this.changes.add(Modification.onModified((String)(parent.resource.getPath() + '/' + name)));
}
}
private static Object toJavaObject(String value, int type) {
boolean isEmpty = value == null || value.trim().length() == 0;
switch (type) {
case 12: {
return isEmpty ? BigDecimal.ZERO : new BigDecimal(value);
}
case 6: {
return isEmpty ? Boolean.FALSE : Boolean.valueOf(value);
}
case 4: {
return isEmpty ? 0.0 : Double.valueOf(value);
}
case 3: {
return isEmpty ? 0 : Long.valueOf(value);
}
}
return value;
}
private static Object toJavaObject(String[] values, int type) {
Object[] result = new Object[values.length];
for (int i = 0; i < values.length; ++i) {
if (values[i] == null) continue;
result[i] = SlingPropertyValueHandler.toJavaObject(values[i], type);
}
return result;
}
static {
AUTO_PROPS.put("created", AutoType.CREATED);
AUTO_PROPS.put("createdBy", AutoType.CREATED_BY);
AUTO_PROPS.put("jcr:created", AutoType.CREATED);
AUTO_PROPS.put("jcr:createdBy", AutoType.CREATED_BY);
AUTO_PROPS.put("lastModified", AutoType.MODIFIED);
AUTO_PROPS.put("lastModifiedBy", AutoType.MODIFIED_BY);
AUTO_PROPS.put("jcr:lastModified", AutoType.MODIFIED);
AUTO_PROPS.put("jcr:lastModifiedBy", AutoType.MODIFIED_BY);
}
public static final class Modifiable {
public Resource resource;
public ModifiableValueMap valueMap;
public Node node;
}
private static enum AutoType {
CREATED,
CREATED_BY,
MODIFIED,
MODIFIED_BY;
private AutoType() {
}
}
}