JcrUtil.java
8.58 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.PropertyType
* javax.jcr.Session
* javax.jcr.Value
* javax.jcr.ValueFactory
*/
package com.adobe.aemfd.watchfolder.util;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.Calendar;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyType;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
public class JcrUtil {
private static final String DEFAULT_FOLDER_TYPE = "nt:folder";
public static final String WF_JCR_ROOT = "/etc/fd/watchfolder";
public static void makeSubDirIfRequired(Session s, String subDirName, String type) throws Exception {
Node wfRoot = s.getNode("/etc/fd/watchfolder");
if (!wfRoot.hasNode(subDirName)) {
wfRoot.addNode(subDirName, type == null ? "nt:folder" : type);
s.save();
}
}
private static ValueExtractor getValueExtractor(int pType, boolean nullifyEmptyStrings) throws Exception {
switch (pType) {
case 6: {
return new BooleanExtractor();
}
case 5: {
return new DateValueExtractor();
}
case 12: {
return new DecimalValueExtractor();
}
case 4: {
return new DoubleValueExtractor();
}
case 3: {
return new LongValueExtractor();
}
case 1: {
return new StringValueExtractor(nullifyEmptyStrings);
}
}
throw new Exception("Property type " + PropertyType.nameFromValue((int)pType) + " not supported!");
}
public static Object getPropertyValue(Property p, Boolean expectMultiple, boolean nullifyEmptyStrings) throws Exception {
if (p == null) {
return null;
}
boolean multiple = p.isMultiple();
if (expectMultiple != null && multiple ^ expectMultiple) {
throw new Exception("Multiplicity " + multiple + " for property " + p.getName() + " is at variance with expectation " + expectMultiple);
}
ValueExtractor ve = JcrUtil.getValueExtractor(p.getType(), nullifyEmptyStrings);
if (!multiple) {
return ve.getValue(p.getValue());
}
Value[] multiVals = p.getValues();
if (multiVals == null) {
return null;
}
Object arr = Array.newInstance(ve.getValueType(), multiVals.length);
for (int i = 0; i < multiVals.length; ++i) {
Array.set(arr, i, ve.getValue(multiVals[i]));
}
return arr;
}
public static String toString(Object propVal) {
if (propVal == null) {
return null;
}
return propVal.toString();
}
public static void setPropertyValue(Node node, String propName, int propType, boolean isMultiple, Object valueObj) throws Exception {
if (node == null) {
return;
}
ValueExtractor ve = JcrUtil.getValueExtractor(propType, false);
ValueFactory vf = node.getSession().getValueFactory();
if (!isMultiple) {
node.setProperty(propName, ve.createValue(vf, valueObj), propType);
} else {
Value[] jcrValues = null;
if (valueObj != null) {
jcrValues = new Value[Array.getLength(valueObj)];
for (int i = 0; i < jcrValues.length; ++i) {
jcrValues[i] = ve.createValue(vf, Array.get(valueObj, i));
}
}
node.setProperty(propName, jcrValues, propType);
}
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
private static class StringValueExtractor
extends ValueExtractor<String> {
private boolean nullifyEmptyStrings;
private StringValueExtractor(boolean nullifyEmptyStrings) {
super();
this.nullifyEmptyStrings = nullifyEmptyStrings;
}
@Override
Class<String> getValueType() {
return String.class;
}
@Override
protected String doGetValue(Value v) throws Exception {
String str = v.getString();
if (str == null) {
return null;
}
if (this.nullifyEmptyStrings && str.trim().length() == 0) {
return null;
}
return str;
}
@Override
protected Value doCreateValue(ValueFactory vf, String value) {
return vf.createValue(value);
}
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
private static class LongValueExtractor
extends ValueExtractor<Long> {
private LongValueExtractor() {
super();
}
@Override
Class<Long> getValueType() {
return Long.class;
}
@Override
protected Long doGetValue(Value v) throws Exception {
return v.getLong();
}
@Override
protected Value doCreateValue(ValueFactory vf, Long value) {
return vf.createValue(value.longValue());
}
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
private static class DoubleValueExtractor
extends ValueExtractor<Double> {
private DoubleValueExtractor() {
super();
}
@Override
Class<Double> getValueType() {
return Double.class;
}
@Override
protected Double doGetValue(Value v) throws Exception {
return v.getDouble();
}
@Override
protected Value doCreateValue(ValueFactory vf, Double value) {
return vf.createValue(value.doubleValue());
}
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
private static class DecimalValueExtractor
extends ValueExtractor<BigDecimal> {
private DecimalValueExtractor() {
super();
}
@Override
Class<BigDecimal> getValueType() {
return BigDecimal.class;
}
@Override
protected BigDecimal doGetValue(Value v) throws Exception {
return v.getDecimal();
}
@Override
protected Value doCreateValue(ValueFactory vf, BigDecimal value) {
return vf.createValue(value);
}
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
private static class DateValueExtractor
extends ValueExtractor<Calendar> {
private DateValueExtractor() {
super();
}
@Override
Class<Calendar> getValueType() {
return Calendar.class;
}
@Override
protected Calendar doGetValue(Value v) throws Exception {
return v.getDate();
}
@Override
protected Value doCreateValue(ValueFactory vf, Calendar value) {
return vf.createValue(value);
}
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
private static class BooleanExtractor
extends ValueExtractor<Boolean> {
private BooleanExtractor() {
super();
}
@Override
Class<Boolean> getValueType() {
return Boolean.class;
}
@Override
protected Boolean doGetValue(Value v) throws Exception {
return v.getBoolean();
}
@Override
protected Value doCreateValue(ValueFactory vf, Boolean value) {
return vf.createValue(value.booleanValue());
}
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
private static abstract class ValueExtractor<T> {
private ValueExtractor() {
}
protected abstract T doGetValue(Value var1) throws Exception;
protected abstract Value doCreateValue(ValueFactory var1, T var2);
abstract Class<T> getValueType();
T getValue(Value v) throws Exception {
if (v == null) {
return null;
}
return this.doGetValue(v);
}
Value createValue(ValueFactory vf, T value) {
if (value == null) {
return null;
}
return this.doCreateValue(vf, value);
}
}
}