CQMetaDataMap.java
8.95 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.workflow.metadata.MetaDataMap
* com.day.cq.workflow.metadata.MetaDataMap
* javax.jcr.Binary
* javax.jcr.RepositoryException
* javax.jcr.Value
* javax.jcr.ValueFormatException
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.workflow.impl.metadata;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.workflow.impl.metadata.CQLegacyMetaDataWrapper;
import com.day.cq.workflow.impl.metadata.CQMetaDataUtil;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.jcr.Binary;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CQMetaDataMap
extends HashMap<String, Object>
implements com.day.cq.workflow.metadata.MetaDataMap {
private static final long serialVersionUID = -9141856147604281545L;
private static Logger log = LoggerFactory.getLogger(CQLegacyMetaDataWrapper.class);
private MetaDataMap graniteMetaDataMap;
public CQMetaDataMap() {
}
public CQMetaDataMap(MetaDataMap metaDataMap) {
this.graniteMetaDataMap = metaDataMap;
}
public <T> T get(String name, Class<T> type) {
if (this.graniteMetaDataMap != null) {
return (T)this.graniteMetaDataMap.get(name, type);
}
Object entry = this.get(name);
if (entry == null) {
return null;
}
if (type == null) {
return (T)entry;
}
if (type.isAssignableFrom(entry.getClass())) {
return (T)entry;
}
if (this.isMulti(entry)) {
if (type.isArray()) {
if (Value[].class.isAssignableFrom(entry.getClass())) {
return (T)this.convertValuesToType((Value[])entry, type.getComponentType());
}
return (T)this.convertToTypeArray((Object[])entry, type.getComponentType());
}
log.error("cannot convert array value into single value type");
return null;
}
if (type.isArray()) {
Object convertedValue = null;
convertedValue = Value.class.isAssignableFrom(entry.getClass()) ? this.convertValueToType((Value)entry, type.getComponentType()) : this.convertToType(entry, type.getComponentType());
if (convertedValue != null) {
Object[] result = (Object[])Array.newInstance(type.getComponentType(), 1);
result[0] = convertedValue;
return (T)result;
}
return null;
}
if (Value.class.isAssignableFrom(entry.getClass())) {
return this.convertValueToType((Value)entry, type);
}
return this.convertToType(entry, type);
}
public <T> T get(String name, T defaultValue) {
if (this.graniteMetaDataMap != null) {
return (T)this.graniteMetaDataMap.get(name, defaultValue);
}
if (defaultValue == null) {
return (T)this.get(name);
}
Class value = this.get(name, (T)defaultValue.getClass());
if (value == null) {
value = defaultValue;
}
return (T)value;
}
private <T> T convertValueToType(Value value, Class<T> type) {
T convertedValue = null;
try {
if (type == String.class) {
return (T)value.getString();
}
if (type == Double.class) {
return value.getDouble();
}
if (type == Long.class) {
return value.getLong();
}
if (type == Boolean.class) {
return value.getBoolean();
}
if (type == Calendar.class) {
return (T)value.getDate();
}
if (type == Date.class) {
return (T)value.getDate().getTime();
}
if (type == Binary.class) {
return (T)value.getBinary();
}
}
catch (ValueFormatException e) {
log.error(e.getMessage(), (Throwable)e);
}
catch (IllegalStateException e) {
log.error(e.getMessage(), (Throwable)e);
}
catch (RepositoryException e) {
log.error(e.getMessage(), (Throwable)e);
}
return convertedValue;
}
private <T> T[] convertValuesToType(Value[] values, Class<T> type) {
ArrayList<T> convertedValues = new ArrayList<T>();
for (Value value : values) {
T convertedValue = this.convertValueToType(value, type);
if (convertedValue == null) continue;
convertedValues.add(convertedValue);
}
Object[] result = (Object[])Array.newInstance(type, convertedValues.size());
return convertedValues.toArray(result);
}
private <T> T convertToType(Object value, Class<T> type) {
T convertedType = null;
Value aValue = CQMetaDataUtil.convertToValue(value);
if (aValue != null) {
convertedType = this.convertValueToType(aValue, type);
}
return convertedType;
}
private <T> T[] convertToTypeArray(Object[] values, Class<T> type) {
ArrayList<T> convertedValues = new ArrayList<T>();
for (Object value : values) {
T convertedValue = this.convertToType(value, type);
if (convertedValue == null) continue;
convertedValues.add(convertedValue);
}
Object[] result = (Object[])Array.newInstance(type, convertedValues.size());
return convertedValues.toArray(result);
}
private boolean isMulti(Object value) {
return value.getClass().isArray();
}
@Override
public void clear() {
if (this.graniteMetaDataMap != null) {
this.graniteMetaDataMap.clear();
} else {
HashMap.super.clear();
}
}
@Override
public boolean containsKey(Object o) {
if (this.graniteMetaDataMap != null) {
return this.graniteMetaDataMap.containsKey(o);
}
return HashMap.super.containsKey(o);
}
@Override
public boolean containsValue(Object o) {
if (this.graniteMetaDataMap != null) {
return this.graniteMetaDataMap.containsValue(o);
}
return HashMap.super.containsValue(o);
}
@Override
public Set entrySet() {
if (this.graniteMetaDataMap != null) {
return this.graniteMetaDataMap.entrySet();
}
return HashMap.super.entrySet();
}
@Override
public boolean equals(Object o) {
if (this.graniteMetaDataMap != null) {
return this.graniteMetaDataMap.equals(o);
}
return HashMap.super.equals(o);
}
@Override
public Object get(Object o) {
if (this.graniteMetaDataMap != null) {
return this.graniteMetaDataMap.get(o);
}
return HashMap.super.get(o);
}
@Override
public int hashCode() {
if (this.graniteMetaDataMap != null) {
return this.graniteMetaDataMap.hashCode();
}
return HashMap.super.hashCode();
}
@Override
public boolean isEmpty() {
if (this.graniteMetaDataMap != null) {
return this.graniteMetaDataMap.isEmpty();
}
return HashMap.super.isEmpty();
}
@Override
public Set<String> keySet() {
if (this.graniteMetaDataMap != null) {
return this.graniteMetaDataMap.keySet();
}
return HashMap.super.keySet();
}
@Override
public Object put(String key, Object value) {
if (this.graniteMetaDataMap != null) {
return this.graniteMetaDataMap.put((Object)key, value);
}
return HashMap.super.put(key, value);
}
@Override
public void putAll(Map map) {
if (this.graniteMetaDataMap != null) {
this.graniteMetaDataMap.putAll(map);
} else {
HashMap.super.putAll(map);
}
}
public Object remove(String key) {
if (this.graniteMetaDataMap != null) {
return this.graniteMetaDataMap.remove((Object)key);
}
return HashMap.super.remove(key);
}
@Override
public int size() {
if (this.graniteMetaDataMap != null) {
return this.graniteMetaDataMap.size();
}
return HashMap.super.size();
}
@Override
public Collection<Object> values() {
if (this.graniteMetaDataMap != null) {
return this.graniteMetaDataMap.values();
}
return HashMap.super.values();
}
@Override
public Object remove(Object key) {
if (this.graniteMetaDataMap != null) {
return this.graniteMetaDataMap.remove(key);
}
return HashMap.super.remove(key);
}
}