|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| package net.sf.crispy.util; |
|
6 |
| |
|
7 |
| import java.beans.IntrospectionException; |
|
8 |
| import java.lang.reflect.Array; |
|
9 |
| import java.lang.reflect.Constructor; |
|
10 |
| import java.lang.reflect.Method; |
|
11 |
| import java.math.BigDecimal; |
|
12 |
| import java.math.BigInteger; |
|
13 |
| import java.security.AccessController; |
|
14 |
| import java.sql.Time; |
|
15 |
| import java.sql.Timestamp; |
|
16 |
| import java.text.DateFormat; |
|
17 |
| import java.text.ParseException; |
|
18 |
| import java.text.SimpleDateFormat; |
|
19 |
| import java.util.ArrayList; |
|
20 |
| import java.util.Collection; |
|
21 |
| import java.util.Comparator; |
|
22 |
| import java.util.Date; |
|
23 |
| import java.util.HashSet; |
|
24 |
| import java.util.Hashtable; |
|
25 |
| import java.util.Iterator; |
|
26 |
| import java.util.Locale; |
|
27 |
| import java.util.Map; |
|
28 |
| import java.util.Properties; |
|
29 |
| import java.util.Set; |
|
30 |
| import java.util.TreeMap; |
|
31 |
| import java.util.Vector; |
|
32 |
| import java.util.Map.Entry; |
|
33 |
| |
|
34 |
| import net.sf.crispy.InvocationException; |
|
35 |
| import net.sf.crispy.impl.ServiceManager; |
|
36 |
| |
|
37 |
| import org.apache.commons.logging.Log; |
|
38 |
| import org.apache.commons.logging.LogFactory; |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| public class Converter { |
|
47 |
| |
|
48 |
| public final static String HASH_CODE_KEY = "~hashCode~"; |
|
49 |
| public final static int GET_METHOD = 1; |
|
50 |
| public final static int SET_METHOD = 2; |
|
51 |
| |
|
52 |
| protected static final Log log = LogFactory.getLog (Converter.class); |
|
53 |
| protected static final Map isSimpleDataType = new Hashtable(); |
|
54 |
| protected static final ClassPropertiesCache classPropertiesCacheGetter = new ClassPropertiesCache(); |
|
55 |
| protected static final ClassPropertiesCache classPropertiesCacheSetter = new ClassPropertiesCache(); |
|
56 |
| |
|
57 |
| private String nullValue = null; |
|
58 |
| private boolean withSimpleKeyMapper = false; |
|
59 |
| |
|
60 |
802
| public Converter() { }
|
|
61 |
227
| public Converter(String pvNullValue) {
|
|
62 |
227
| setNullValue(pvNullValue);
|
|
63 |
| } |
|
64 |
3
| public Converter(int pvStartHashCode) {
|
|
65 |
3
| uniqueIdMap = new Hashtable();
|
|
66 |
3
| startHashCode = pvStartHashCode;
|
|
67 |
| } |
|
68 |
| |
|
69 |
339
| public void setWithSimpleKeyMapper(boolean pvWithSimpleKeyMapper) { withSimpleKeyMapper = pvWithSimpleKeyMapper; }
|
|
70 |
2
| public boolean getWithSimpleKeyMapper() { return withSimpleKeyMapper; }
|
|
71 |
| |
|
72 |
2576
| public static boolean isValideEdge(Object pvObject) {
|
|
73 |
1
| if (pvObject == null) { return false; }
|
|
74 |
2575
| if (isSimpleDataType.size() == 0) {
|
|
75 |
12
| isSimpleDataType.put(String.class, String.class);
|
|
76 |
12
| isSimpleDataType.put(Boolean.class, Boolean.class);
|
|
77 |
12
| isSimpleDataType.put(boolean.class, boolean.class);
|
|
78 |
12
| isSimpleDataType.put(Byte.class, Byte.class);
|
|
79 |
12
| isSimpleDataType.put(byte.class, byte.class);
|
|
80 |
12
| isSimpleDataType.put(Short.class, Short.class);
|
|
81 |
12
| isSimpleDataType.put(short.class, short.class);
|
|
82 |
12
| isSimpleDataType.put(Integer.class, Integer.class);
|
|
83 |
12
| isSimpleDataType.put(int.class, int.class);
|
|
84 |
12
| isSimpleDataType.put(Long.class, Long.class);
|
|
85 |
12
| isSimpleDataType.put(long.class, long.class);
|
|
86 |
12
| isSimpleDataType.put(Float.class, Float.class);
|
|
87 |
12
| isSimpleDataType.put(float.class, float.class);
|
|
88 |
12
| isSimpleDataType.put(Double.class, Double.class);
|
|
89 |
12
| isSimpleDataType.put(double.class, double.class);
|
|
90 |
12
| isSimpleDataType.put(Character.class, Character.class);
|
|
91 |
12
| isSimpleDataType.put(char.class, char.class);
|
|
92 |
12
| isSimpleDataType.put(BigDecimal.class, BigDecimal.class);
|
|
93 |
12
| isSimpleDataType.put(StringBuffer.class, StringBuffer.class);
|
|
94 |
12
| isSimpleDataType.put(BigInteger.class, BigInteger.class);
|
|
95 |
12
| isSimpleDataType.put(Class.class, Class.class);
|
|
96 |
12
| isSimpleDataType.put(java.sql.Date.class, java.sql.Date.class);
|
|
97 |
12
| isSimpleDataType.put(java.util.Date.class, java.util.Date.class);
|
|
98 |
12
| isSimpleDataType.put(Time.class, Time.class);
|
|
99 |
12
| isSimpleDataType.put(Timestamp.class, Timestamp.class);
|
|
100 |
| } |
|
101 |
2575
| boolean b = isSimpleDataType.containsKey(pvObject.getClass());
|
|
102 |
| |
|
103 |
2575
| if (b == true) {
|
|
104 |
1682
| return false;
|
|
105 |
| } |
|
106 |
| else { |
|
107 |
893
| return true;
|
|
108 |
| } |
|
109 |
| } |
|
110 |
| |
|
111 |
| private Map uniqueIdMap = null; |
|
112 |
| private Map uniqueIdMap2 = null; |
|
113 |
| private int hashCode = 0; |
|
114 |
| private int startHashCode = -1000000000; |
|
115 |
| |
|
116 |
| |
|
117 |
1
| public String getNullValue() { return nullValue; }
|
|
118 |
228
| public void setNullValue(String pvNullValue) { nullValue = pvNullValue; }
|
|
119 |
| |
|
120 |
15
| public String findNextFreeId (int pvHashCode) {
|
|
121 |
15
| String lvHashCodeStr = Integer.toString(pvHashCode);
|
|
122 |
15
| if (uniqueIdMap.containsKey(lvHashCodeStr)) {
|
|
123 |
7
| int next = pvHashCode + 1;
|
|
124 |
7
| lvHashCodeStr = findNextFreeId (next);
|
|
125 |
| } |
|
126 |
15
| return lvHashCodeStr;
|
|
127 |
| } |
|
128 |
| |
|
129 |
1973
| private String getUniqueId(Object pvObject) {
|
|
130 |
7
| if (pvObject == null) {return null; }
|
|
131 |
| |
|
132 |
1966
| String lvHashCode = (String) uniqueIdMap2.get(pvObject);
|
|
133 |
1966
| if (lvHashCode != null){
|
|
134 |
853
| return lvHashCode;
|
|
135 |
| } |
|
136 |
| |
|
137 |
1113
| lvHashCode = Integer.toString(pvObject.hashCode());
|
|
138 |
1113
| Object o = uniqueIdMap.get(lvHashCode);
|
|
139 |
1113
| if (o == null) {
|
|
140 |
1105
| uniqueIdMap.put(lvHashCode, pvObject);
|
|
141 |
1105
| uniqueIdMap2.put(pvObject, lvHashCode);
|
|
142 |
| } |
|
143 |
| else { |
|
144 |
8
| lvHashCode = findNextFreeId(hashCode);
|
|
145 |
8
| uniqueIdMap.put(lvHashCode, pvObject);
|
|
146 |
8
| uniqueIdMap2.put(pvObject, lvHashCode);
|
|
147 |
| } |
|
148 |
1113
| return lvHashCode;
|
|
149 |
| } |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
729
| public Object makeSimple (Object pvObject) throws Exception {
|
|
161 |
729
| uniqueIdMap = new Hashtable();
|
|
162 |
729
| uniqueIdMap2 = new Hashtable();
|
|
163 |
729
| hashCode = startHashCode;
|
|
164 |
729
| Object lvReturn = null;
|
|
165 |
729
| try {
|
|
166 |
729
| lvReturn = makeSimpleInternal (pvObject, null, new Graph());
|
|
167 |
| } |
|
168 |
| finally { |
|
169 |
729
| uniqueIdMap = null;
|
|
170 |
729
| uniqueIdMap2 = null;
|
|
171 |
729
| hashCode = startHashCode;
|
|
172 |
| } |
|
173 |
727
| return lvReturn;
|
|
174 |
| } |
|
175 |
| |
|
176 |
4290
| private Object makeSimpleInternal (Object pvObject, String pvPropName, Graph pvGraph) throws Exception {
|
|
177 |
4290
| Method lvMethod = null;
|
|
178 |
| |
|
179 |
27
| if (pvObject == null) { return nullValue; }
|
|
180 |
| |
|
181 |
4263
| Class clazz = pvObject.getClass();
|
|
182 |
| |
|
183 |
| |
|
184 |
1559
| if (clazz.equals(String.class)) { return pvObject; }
|
|
185 |
27
| else if (clazz.equals(Date.class)) { return pvObject; }
|
|
186 |
344
| else if (clazz.equals(Integer.class)) { return pvObject; }
|
|
187 |
20
| else if (clazz.equals(Double.class)) { return pvObject; }
|
|
188 |
225
| else if (clazz.equals(Boolean.class)) { return pvObject; }
|
|
189 |
| |
|
190 |
11
| else if ((clazz.isArray()) && (clazz.getComponentType().equals(byte.class))) { return pvObject; }
|
|
191 |
| |
|
192 |
| |
|
193 |
94
| else if (clazz.equals(Long.class)) { return Integer.valueOf(pvObject.toString()); }
|
|
194 |
11
| else if (clazz.equals(Byte.class)) { return Integer.valueOf(pvObject.toString()); }
|
|
195 |
11
| else if (clazz.equals(Float.class)) { return new Double(pvObject.toString()); }
|
|
196 |
11
| else if (clazz.equals(Character.class)) { return pvObject.toString(); }
|
|
197 |
11
| else if (clazz.equals(Short.class)) { return Integer.valueOf(pvObject.toString()); }
|
|
198 |
40
| else if (clazz.equals(BigDecimal.class)) { return new Double(pvObject.toString()); }
|
|
199 |
1
| else if (clazz.equals(StringBuffer.class)) { return pvObject.toString(); }
|
|
200 |
1
| else if (clazz.equals(BigInteger.class)) { return Integer.valueOf(pvObject.toString()); }
|
|
201 |
1
| else if (clazz.equals(Class.class)) { return ((Class) pvObject).getName(); }
|
|
202 |
1
| else if (clazz.equals(java.sql.Date.class)) { return new Date(((java.sql.Date) pvObject).getTime()); }
|
|
203 |
2
| else if (clazz.equals(Time.class)) { return (Date) pvObject; }
|
|
204 |
2
| else if (clazz.equals(Timestamp.class)) { return (Date) pvObject; }
|
|
205 |
| |
|
206 |
| |
|
207 |
1891
| else if (pvObject instanceof Collection) {
|
|
208 |
616
| final Collection coll = (Collection) pvObject;
|
|
209 |
616
| Iterator it = coll.iterator();
|
|
210 |
616
| Vector v = new Vector(coll.size());
|
|
211 |
616
| while (it.hasNext()) {
|
|
212 |
433
| Object lvValue = it.next();
|
|
213 |
433
| String lvHashCode = getUniqueId(lvValue);
|
|
214 |
433
| if (pvGraph.getVertexCount(lvValue) == true) {
|
|
215 |
37
| v.add(HASH_CODE_KEY + lvHashCode);
|
|
216 |
| } else { |
|
217 |
396
| Object o = makeSimpleInternal(lvValue, pvPropName, pvGraph);
|
|
218 |
394
| if (o != null) { v.add(o); }
|
|
219 |
| } |
|
220 |
| } |
|
221 |
616
| return v;
|
|
222 |
| } |
|
223 |
| |
|
224 |
1275
| else if (pvObject instanceof Map) {
|
|
225 |
311
| Map map = (Map) pvObject;
|
|
226 |
311
| Hashtable lvHashtable = new Hashtable(map.size());
|
|
227 |
| |
|
228 |
| |
|
229 |
311
| int pos = 0;
|
|
230 |
311
| SimpleKeyMapper lvKeyMapper = new SimpleKeyMapper();
|
|
231 |
311
| Iterator it = map.entrySet().iterator();
|
|
232 |
311
| while (it.hasNext()) {
|
|
233 |
165
| Map.Entry lvMapEntry = (Entry) it.next();
|
|
234 |
165
| Object lvKey = lvMapEntry.getKey();
|
|
235 |
165
| pos = ++pos;
|
|
236 |
1
| if (isValideEdge(lvKey)) { throw new InvocationException("Complex Map-Key datatypes are not supported: " + lvKey.getClass().getName()); }
|
|
237 |
164
| Object lvValue = lvMapEntry.getValue();
|
|
238 |
164
| String lvHashCode = getUniqueId(lvValue);
|
|
239 |
| |
|
240 |
164
| Class lvKeyClass = lvKey.getClass();
|
|
241 |
| |
|
242 |
164
| lvKey = makeSimpleInternal(lvKey, pvPropName, pvGraph);
|
|
243 |
| |
|
244 |
164
| Object key = lvKeyMapper.toSimpleNewKey(lvKey, lvKeyClass, pos, withSimpleKeyMapper);
|
|
245 |
164
| if (pvGraph.getVertexCount(lvValue) == true) {
|
|
246 |
18
| lvHashtable.put(key, HASH_CODE_KEY + lvHashCode);
|
|
247 |
| } else { |
|
248 |
146
| Object o = makeSimpleInternal(lvValue, pvPropName, pvGraph);
|
|
249 |
145
| if (o != null) { lvHashtable.put(key, o); }
|
|
250 |
| } |
|
251 |
| } |
|
252 |
310
| return lvHashtable;
|
|
253 |
| } |
|
254 |
| |
|
255 |
964
| else if (clazz.isArray()) {
|
|
256 |
84
| int length = Array.getLength(pvObject);
|
|
257 |
84
| Vector v = new Vector(length);
|
|
258 |
84
| for (int i=0;i<length;i++) {
|
|
259 |
479
| Object lvValue = Array.get (pvObject, i);
|
|
260 |
479
| String lvHashCode = getUniqueId(lvValue);
|
|
261 |
479
| if (pvGraph.getVertexCount(lvValue) == true) {
|
|
262 |
18
| v.add(HASH_CODE_KEY + lvHashCode);
|
|
263 |
| } else { |
|
264 |
461
| Object o = makeSimpleInternal(lvValue, pvPropName, pvGraph);
|
|
265 |
459
| if (o != null) { v.add(o); }
|
|
266 |
| } |
|
267 |
| } |
|
268 |
84
| return v;
|
|
269 |
| } |
|
270 |
| |
|
271 |
| |
|
272 |
| else { |
|
273 |
880
| Hashtable map = new Hashtable();
|
|
274 |
880
| String lvHashCode = getUniqueId(pvObject);
|
|
275 |
880
| map.put(HASH_CODE_KEY, lvHashCode);
|
|
276 |
880
| pvGraph.addVertex(pvObject);
|
|
277 |
880
| Map lvGetterMap = getAllGetterMethodWithCache(clazz);
|
|
278 |
879
| Iterator it = lvGetterMap.entrySet().iterator();
|
|
279 |
879
| while (it.hasNext()) {
|
|
280 |
4558
| Map.Entry lvMapEntry = (Entry) it.next();
|
|
281 |
4558
| String propName = (String) lvMapEntry.getKey();
|
|
282 |
4558
| if (propName.equals("class") == false) {
|
|
283 |
3680
| Object lvValue = null;
|
|
284 |
3680
| try {
|
|
285 |
3680
| lvMethod = (Method)lvMapEntry.getValue();
|
|
286 |
3680
| lvValue = lvMethod.invoke(pvObject, null);
|
|
287 |
| } catch (Exception e) { |
|
288 |
| if (log.isDebugEnabled()) { log.debug("Problem by invoke method (in makeSimple): " + lvMethod, e); } |
|
289 |
| if (ServiceManager.DEBUG_MODE_ON) { e.printStackTrace(); } |
|
290 |
| } |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
3680
| if (lvValue != null) {
|
|
295 |
2411
| Edge lvEdge = new Edge(pvObject, lvValue);
|
|
296 |
2411
| if (pvGraph.containsKey(lvEdge) == false) {
|
|
297 |
2409
| if (isValideEdge(lvValue) == true) {
|
|
298 |
892
| pvGraph.put(lvEdge, "");
|
|
299 |
| } |
|
300 |
2409
| Edge lvKnownEdgeToThisNode = pvGraph.isFromContains(lvValue);
|
|
301 |
2409
| if (lvKnownEdgeToThisNode != null) {
|
|
302 |
15
| lvEdge.setMethodName(lvMethod.getName());
|
|
303 |
15
| map.put(propName, HASH_CODE_KEY + getUniqueId(lvValue));
|
|
304 |
| } |
|
305 |
| else { |
|
306 |
2394
| Object o = makeSimpleInternal(lvValue, propName, pvGraph);
|
|
307 |
2393
| map.put(propName, o);
|
|
308 |
| } |
|
309 |
| } |
|
310 |
| |
|
311 |
| else { |
|
312 |
2
| map.put(propName, HASH_CODE_KEY + getUniqueId(lvValue));
|
|
313 |
| } |
|
314 |
| } |
|
315 |
| |
|
316 |
| } else { |
|
317 |
| |
|
318 |
878
| map.put("class", clazz.getName());
|
|
319 |
| } |
|
320 |
| } |
|
321 |
878
| return map;
|
|
322 |
| } |
|
323 |
| } |
|
324 |
| |
|
325 |
303
| public Object makeComplex (Object pvObject) throws Exception {
|
|
326 |
303
| return makeComplex(pvObject, null);
|
|
327 |
| } |
|
328 |
| |
|
329 |
663
| public Object makeComplex (Object pvObject, Class clazz) throws Exception {
|
|
330 |
663
| return makeComplex(pvObject, clazz, null);
|
|
331 |
| } |
|
332 |
| |
|
333 |
681
| public Object makeComplex (Object pvObject, Class clazz, Class clazzInArray) throws Exception {
|
|
334 |
681
| return makeComplexInternal(pvObject, clazz, clazzInArray, new Hashtable());
|
|
335 |
| } |
|
336 |
| |
|
337 |
2814
| private Object makeComplexInternal (final Object pvObject, final Class pvClazz, final Class clazzInArray, final Map pvHashCodeMap) throws Exception {
|
|
338 |
2814
| Method lvMethod = null;
|
|
339 |
2814
| Class clazz = pvClazz;
|
|
340 |
| |
|
341 |
31
| if (pvObject == null || pvObject.equals(nullValue)) { return null; }
|
|
342 |
| |
|
343 |
291
| if (pvClazz == null) { clazz = pvObject.getClass(); }
|
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
2783
| if ((clazz.equals(Object.class)) && (!(pvObject.getClass().equals(Object.class)))) {
|
|
348 |
| if (log.isDebugEnabled()) { |
|
349 |
| log.debug("Class: "+ clazz.getName() + " is different from Object: " + pvObject.getClass().getName()); |
|
350 |
| } |
|
351 |
33
| clazz = pvObject.getClass();
|
|
352 |
| } |
|
353 |
| |
|
354 |
| |
|
355 |
1151
| if (clazz.equals(String.class)) { return pvObject; }
|
|
356 |
42
| else if (clazz.equals(Integer.class)) { return pvObject; }
|
|
357 |
2
| else if (clazz.equals(Double.class)) { return pvObject; }
|
|
358 |
1
| else if (clazz.equals(Boolean.class)) { return pvObject; }
|
|
359 |
22
| else if (clazz.equals(Date.class)) { return pvObject; }
|
|
360 |
8
| else if ((clazz.isArray()) && (clazz.getComponentType().equals(byte.class))) { return pvObject; }
|
|
361 |
| |
|
362 |
| |
|
363 |
152
| else if (clazz.equals(boolean.class)) { return pvObject; }
|
|
364 |
9
| else if (clazz.equals(short.class)) { return new Short(pvObject.toString()); }
|
|
365 |
9
| else if (clazz.equals(byte.class)) { return new Byte(pvObject.toString()); }
|
|
366 |
222
| else if (clazz.equals(int.class)) { return pvObject; }
|
|
367 |
9
| else if (clazz.equals(long.class)) { return new Long(pvObject.toString()); }
|
|
368 |
13
| else if (clazz.equals(double.class)) { return new Double(pvObject.toString()); }
|
|
369 |
9
| else if (clazz.equals(float.class)) { return new Float(pvObject.toString()); }
|
|
370 |
9
| else if (clazz.equals(char.class)) { return new Character(pvObject.toString().charAt(0)); }
|
|
371 |
| |
|
372 |
| |
|
373 |
1
| else if (clazz.equals(Byte.class)) { return new Byte(pvObject.toString()); }
|
|
374 |
1
| else if (clazz.equals(Float.class)) { return new Float(pvObject.toString()); }
|
|
375 |
1
| else if (clazz.equals(Character.class)) { return new Character(pvObject.toString().charAt(0)); }
|
|
376 |
57
| else if (clazz.equals(Long.class)) { return new Long(pvObject.toString()); }
|
|
377 |
1
| else if (clazz.equals(Short.class)) { return new Short(pvObject.toString()); }
|
|
378 |
23
| else if (clazz.equals(BigDecimal.class)) { return new BigDecimal (((Double) pvObject).doubleValue()); }
|
|
379 |
1
| else if (clazz.equals(StringBuffer.class)) { return new StringBuffer(pvObject.toString()); }
|
|
380 |
1
| else if (clazz.equals(BigInteger.class)) { return new BigInteger(pvObject.toString()); }
|
|
381 |
1
| else if (clazz.equals(Class.class)) { return Class.forName(pvObject.toString()); }
|
|
382 |
1
| else if (clazz.equals(java.sql.Date.class)) { return new java.sql.Date(((Date) pvObject).getTime()); }
|
|
383 |
1037
| else if (clazz.equals(Time.class)) {
|
|
384 |
5
| if (pvObject instanceof Time) {
|
|
385 |
1
| return (Time) pvObject;
|
|
386 |
| } |
|
387 |
4
| else if (pvObject instanceof Timestamp) {
|
|
388 |
1
| Timestamp ts = (Timestamp) pvObject;
|
|
389 |
1
| return new Time(ts.getTime());
|
|
390 |
| } |
|
391 |
3
| else if (pvObject instanceof Date) {
|
|
392 |
1
| return new Time(((Date) pvObject).getTime());
|
|
393 |
| } |
|
394 |
2
| else if (pvObject instanceof Long) {
|
|
395 |
1
| Long l = (Long) pvObject;
|
|
396 |
1
| return new Time(l.longValue());
|
|
397 |
| } |
|
398 |
| else { |
|
399 |
1
| throw new InvocationException("Exception by Converter.makeComplex. The datatype: " + pvObject.getClass().getName()
|
|
400 |
| + " is not compatible to java.sql.Time"); |
|
401 |
| } |
|
402 |
| |
|
403 |
| } |
|
404 |
1032
| else if (clazz.equals(Timestamp.class)) {
|
|
405 |
5
| if (pvObject instanceof Timestamp) {
|
|
406 |
1
| return (Timestamp) pvObject;
|
|
407 |
| } |
|
408 |
4
| else if (pvObject instanceof Time) {
|
|
409 |
1
| Time t = (Time) pvObject;
|
|
410 |
1
| return new Timestamp(t.getTime());
|
|
411 |
| } |
|
412 |
3
| else if (pvObject instanceof Date) {
|
|
413 |
1
| return new Timestamp(((Date) pvObject).getTime());
|
|
414 |
| } |
|
415 |
2
| else if (pvObject instanceof Long) {
|
|
416 |
1
| Long l = (Long) pvObject;
|
|
417 |
1
| return new Timestamp(l.longValue());
|
|
418 |
| } |
|
419 |
| else { |
|
420 |
1
| throw new InvocationException("Exception by Converter.makeComplex. The datatype: " + pvObject.getClass().getName()
|
|
421 |
| + " is not compatible to java.sql.Timestamp"); |
|
422 |
| } |
|
423 |
| |
|
424 |
| } |
|
425 |
| |
|
426 |
1027
| else if (clazz.isArray()) {
|
|
427 |
| |
|
428 |
25
| if ((!pvObject.getClass().isArray()) && (pvObject instanceof Collection)) {
|
|
429 |
20
| Collection coll = (Collection) pvObject;
|
|
430 |
20
| Class lvType = (clazzInArray == null ? getArrayType(pvObject) : clazzInArray);
|
|
431 |
20
| Object[] lvTempArray = (Object[]) Array.newInstance(lvType, coll.size());
|
|
432 |
20
| Iterator it = coll.iterator();
|
|
433 |
20
| int i = 0;
|
|
434 |
20
| while (it.hasNext()) {
|
|
435 |
49
| Object objInList = it.next();
|
|
436 |
49
| String lvHashCode = objInList.toString();
|
|
437 |
49
| if (lvHashCode.startsWith(HASH_CODE_KEY) == true) {
|
|
438 |
6
| lvHashCode = lvHashCode.substring(HASH_CODE_KEY.length());
|
|
439 |
6
| Object value = pvHashCodeMap.get(lvHashCode);
|
|
440 |
6
| if (value == null) {
|
|
441 |
1
| throw new InvocationException("Internal Converter-Exception. " +
|
|
442 |
| "For the HashCode: " + lvHashCode + " can not find a object in the Array: " + coll); |
|
443 |
| } else { |
|
444 |
5
| lvTempArray[i] = value;
|
|
445 |
| } |
|
446 |
| } else { |
|
447 |
43
| if (clazzInArray == null) {
|
|
448 |
6
| lvTempArray[i] = objInList;
|
|
449 |
| } |
|
450 |
| else { |
|
451 |
37
| lvTempArray[i] = makeComplexInternal(objInList, clazzInArray, null, pvHashCodeMap);
|
|
452 |
| } |
|
453 |
| } |
|
454 |
48
| i++;
|
|
455 |
| } |
|
456 |
19
| return lvTempArray;
|
|
457 |
| } |
|
458 |
| |
|
459 |
| |
|
460 |
5
| if ((!pvObject.getClass().isArray()) && (pvObject instanceof Map)) {
|
|
461 |
1
| return makeComplexInternal(pvObject, null, null, pvHashCodeMap);
|
|
462 |
| } else { |
|
463 |
4
| int length = Array.getLength(pvObject);
|
|
464 |
4
| Class lvType = (clazzInArray == null ? getArrayType(pvObject) : clazzInArray);
|
|
465 |
4
| Object[] o = (Object[]) Array.newInstance(lvType, length);
|
|
466 |
4
| for (int i=0;i<length;i++) {
|
|
467 |
7
| Object _o = Array.get (pvObject, i);
|
|
468 |
7
| o[i] = makeComplexInternal(_o, lvType, null, pvHashCodeMap);
|
|
469 |
| } |
|
470 |
4
| return o;
|
|
471 |
| } |
|
472 |
| } |
|
473 |
| |
|
474 |
| |
|
475 |
1002
| else if (pvObject instanceof Collection) {
|
|
476 |
380
| Collection coll = (Collection) pvObject;
|
|
477 |
380
| Iterator it = coll.iterator();
|
|
478 |
380
| Collection lvNewColl = new Vector(coll.size());
|
|
479 |
380
| while (it.hasNext()) {
|
|
480 |
377
| Object o = it.next();
|
|
481 |
| |
|
482 |
377
| if (o != null) {
|
|
483 |
374
| String lvHashCode = o.toString();
|
|
484 |
374
| if (lvHashCode.startsWith(HASH_CODE_KEY) == true) {
|
|
485 |
36
| lvHashCode = lvHashCode.substring(HASH_CODE_KEY.length());
|
|
486 |
36
| Object value = pvHashCodeMap.get(lvHashCode);
|
|
487 |
36
| if (value == null) {
|
|
488 |
1
| throw new InvocationException("Internal Converter-Exception. " +
|
|
489 |
| "For the HashCode: " + lvHashCode + " can not find a object in the Collection: " + coll); |
|
490 |
| } else { |
|
491 |
35
| lvNewColl.add(value);
|
|
492 |
| } |
|
493 |
| } else { |
|
494 |
338
| Object value = makeComplexInternal(o, o.getClass(), null, pvHashCodeMap);
|
|
495 |
338
| lvNewColl.add(value);
|
|
496 |
| } |
|
497 |
| } |
|
498 |
| } |
|
499 |
379
| return lvNewColl;
|
|
500 |
| } |
|
501 |
| |
|
502 |
622
| else if (pvObject instanceof Map) {
|
|
503 |
620
| Map map = (Map) pvObject;
|
|
504 |
620
| String aClazzStr = (String) map.get("class");
|
|
505 |
620
| if (aClazzStr == null) {
|
|
506 |
| |
|
507 |
167
| SimpleKeyMapper lvSimpleKeyMapper = new SimpleKeyMapper();
|
|
508 |
167
| map = lvSimpleKeyMapper.orderMapByKey(map, withSimpleKeyMapper);
|
|
509 |
| |
|
510 |
165
| Iterator it = map.entrySet().iterator();
|
|
511 |
165
| Hashtable lvHashtable = new Hashtable(map.size());
|
|
512 |
165
| while (it.hasNext()) {
|
|
513 |
101
| Map.Entry lvMapEntry = (Entry) it.next();
|
|
514 |
101
| Object lvKey = lvMapEntry.getKey();
|
|
515 |
101
| Object key = lvSimpleKeyMapper.toComplexNewKey(lvKey, withSimpleKeyMapper);
|
|
516 |
101
| Object lvValue = lvMapEntry.getValue();
|
|
517 |
101
| String lvHashCode = extractHashCode(lvValue);
|
|
518 |
101
| if (lvHashCode != null) {
|
|
519 |
16
| lvValue = pvHashCodeMap.get(lvHashCode);
|
|
520 |
16
| if (lvValue == null) {
|
|
521 |
1
| throw new InvocationException("Internal Converter-Exception. " +
|
|
522 |
| "For the HashCode: " + lvHashCode + " can not find a object in a map: " + map); |
|
523 |
| } else { |
|
524 |
15
| lvHashtable.put(key, lvValue);
|
|
525 |
| } |
|
526 |
| } else { |
|
527 |
85
| lvValue = makeComplexInternal(lvValue, lvValue.getClass(), null, pvHashCodeMap);
|
|
528 |
84
| lvHashtable.put(key, lvValue);
|
|
529 |
| } |
|
530 |
| } |
|
531 |
163
| return lvHashtable;
|
|
532 |
| |
|
533 |
| } else { |
|
534 |
453
| Class aClazz = null;
|
|
535 |
453
| try {
|
|
536 |
453
| aClazz = Class.forName(aClazzStr);
|
|
537 |
| } catch (Exception e) { |
|
538 |
2
| throw new InvocationException("Can't load class: " + aClazzStr);
|
|
539 |
| } |
|
540 |
451
| Object ret = null;
|
|
541 |
451
| try {
|
|
542 |
451
| ret = aClazz.newInstance();
|
|
543 |
| } catch (Exception e) { |
|
544 |
7
| throw new InvocationException("Can't create object from class: " + aClazzStr + " (No constructor without parameter!)");
|
|
545 |
| } |
|
546 |
| |
|
547 |
444
| String lvMapHashCode = (String) map.get(HASH_CODE_KEY);
|
|
548 |
440
| if (lvMapHashCode != null) { pvHashCodeMap.put(lvMapHashCode, ret); }
|
|
549 |
| |
|
550 |
444
| try {
|
|
551 |
444
| Map lvSetterMap = getAllSetterMethodWithCache(aClazz);
|
|
552 |
444
| Iterator it = lvSetterMap.entrySet().iterator();
|
|
553 |
444
| while (it.hasNext()) {
|
|
554 |
3263
| Map.Entry lvMapEntry = (Entry) it.next();
|
|
555 |
3263
| String propName = (String) lvMapEntry.getKey();
|
|
556 |
3263
| Object value = map.get(propName);
|
|
557 |
3263
| if (value != null) {
|
|
558 |
1681
| String lvHashCode = extractHashCode(value);
|
|
559 |
1681
| lvMethod = (Method) lvMapEntry.getValue();
|
|
560 |
1681
| Class lvParamType = lvMethod.getParameterTypes()[0];
|
|
561 |
| |
|
562 |
1681
| if (lvHashCode != null) {
|
|
563 |
16
| value = pvHashCodeMap.get(lvHashCode);
|
|
564 |
| } else { |
|
565 |
1665
| value = makeComplexInternal(value, lvParamType, lvParamType.getComponentType(), pvHashCodeMap);
|
|
566 |
| } |
|
567 |
1681
| try {
|
|
568 |
1681
| value = makeAssignable(lvParamType, value);
|
|
569 |
1681
| lvMethod.invoke(ret, new Object [] {value});
|
|
570 |
| } catch (Exception e) { |
|
571 |
| if (log.isDebugEnabled()) { log.debug("Problem by invoke method (in makeComplex): " + lvMethod, e); } |
|
572 |
| if (ServiceManager.DEBUG_MODE_ON) { |
|
573 |
| e.printStackTrace(); |
|
574 |
| } |
|
575 |
| } |
|
576 |
| } |
|
577 |
| } |
|
578 |
| } catch (Exception e) { |
|
579 |
| if (log.isWarnEnabled()) { log.warn("Error in Converter.makeComplex by Method: " + lvMethod, e); } |
|
580 |
| if (ServiceManager.DEBUG_MODE_ON) { e.printStackTrace(); } |
|
581 |
| } |
|
582 |
| |
|
583 |
444
| return ret;
|
|
584 |
| } |
|
585 |
| } |
|
586 |
| else { |
|
587 |
| if (log.isInfoEnabled()) { |
|
588 |
| log.info("Invalide Type in makeComplex for Class: " + clazz + " with value: " + pvObject |
|
589 |
| + " (Class from Object: " + pvObject.getClass().getName() + ")"); |
|
590 |
| } |
|
591 |
2
| return null;
|
|
592 |
| } |
|
593 |
| } |
|
594 |
| |
|
595 |
| |
|
596 |
1681
| public static Object makeAssignable(Class pvSourceClass, Object pvValue) {
|
|
597 |
1681
| if (pvSourceClass.equals(Object.class)) {
|
|
598 |
14
| return pvValue;
|
|
599 |
| } |
|
600 |
1667
| else if (pvSourceClass.isAssignableFrom(Set.class) && pvValue instanceof Collection) {
|
|
601 |
157
| return new HashSet((Collection) pvValue);
|
|
602 |
| } |
|
603 |
1510
| else if (pvSourceClass.isAssignableFrom(Properties.class) && pvValue instanceof Map) {
|
|
604 |
133
| Map lvMap = (Map) pvValue;
|
|
605 |
133
| Properties lvProperties = new Properties();
|
|
606 |
133
| Iterator it = lvMap.entrySet().iterator();
|
|
607 |
133
| while (it.hasNext()) {
|
|
608 |
28
| Map.Entry lvMapEntry = (Entry) it.next();
|
|
609 |
28
| lvProperties.put(lvMapEntry.getKey(), lvMapEntry.getValue());
|
|
610 |
| } |
|
611 |
133
| return lvProperties;
|
|
612 |
| } |
|
613 |
1377
| return pvValue;
|
|
614 |
| } |
|
615 |
| |
|
616 |
| |
|
617 |
| |
|
618 |
| |
|
619 |
| |
|
620 |
| |
|
621 |
| |
|
622 |
| |
|
623 |
68
| public static Class getArrayType (Object pvListObj) {
|
|
624 |
68
| Class lvType = Object.class;
|
|
625 |
1
| if (pvListObj == null) { return lvType; }
|
|
626 |
67
| if (pvListObj.getClass().isArray()) {
|
|
627 |
44
| Object o[] = (Object[]) pvListObj;
|
|
628 |
44
| if (o.length > 0) {
|
|
629 |
35
| Class lvClass = o[0].getClass();
|
|
630 |
| |
|
631 |
| |
|
632 |
35
| if(Map.class.isAssignableFrom(lvClass)) {
|
|
633 |
1
| return Object.class;
|
|
634 |
| } |
|
635 |
| |
|
636 |
34
| for (int i = 0; i < o.length; i++) {
|
|
637 |
80
| if (!lvClass.equals(o[i].getClass())) {
|
|
638 |
5
| return lvType;
|
|
639 |
| } |
|
640 |
| } |
|
641 |
29
| return lvClass;
|
|
642 |
| } else { |
|
643 |
9
| return o.getClass().getComponentType();
|
|
644 |
| } |
|
645 |
| } |
|
646 |
23
| else if (pvListObj instanceof Collection) {
|
|
647 |
22
| Collection coll = (Collection) pvListObj;
|
|
648 |
22
| if (coll.size() > 0) {
|
|
649 |
21
| Class lvClass = coll.iterator().next().getClass();
|
|
650 |
21
| Iterator it = coll.iterator();
|
|
651 |
21
| while (it.hasNext()) {
|
|
652 |
49
| if (!lvClass.equals(it.next().getClass())) {
|
|
653 |
4
| return lvType;
|
|
654 |
| } |
|
655 |
| } |
|
656 |
17
| return lvClass;
|
|
657 |
| } else { |
|
658 |
1
| return lvType;
|
|
659 |
| } |
|
660 |
| } |
|
661 |
1
| return lvType;
|
|
662 |
| } |
|
663 |
| |
|
664 |
| |
|
665 |
158
| public static Object convertClassString2Object (String pvClassName) throws Exception {
|
|
666 |
| |
|
667 |
1
| if (pvClassName == null) { return null; }
|
|
668 |
| |
|
669 |
25
| else if (pvClassName.equals(int.class.getName())) { return Integer.valueOf("0"); }
|
|
670 |
1
| else if (pvClassName.equals(short.class.getName())) { return Short.valueOf("0"); }
|
|
671 |
1
| else if (pvClassName.equals(byte.class.getName())) { return Byte.valueOf("0"); }
|
|
672 |
1
| else if (pvClassName.equals(long.class.getName())) { return Long.valueOf("0"); }
|
|
673 |
| |
|
674 |
| |
|
675 |
5
| else if (pvClassName.equals(double.class.getName())) { return Double.valueOf("0"); }
|
|
676 |
1
| else if (pvClassName.equals(float.class.getName())) { return Float.valueOf("0"); }
|
|
677 |
| |
|
678 |
1
| else if (pvClassName.equals(char.class.getName())) { return new Character(' '); }
|
|
679 |
1
| else if (pvClassName.equals(Character.class.getName())) { return new Character(' '); }
|
|
680 |
| |
|
681 |
1
| else if (pvClassName.equals(boolean.class.getName())) { return Boolean.TRUE; }
|
|
682 |
1
| else if (pvClassName.equals(Boolean.class.getName())) { return Boolean.TRUE; }
|
|
683 |
| |
|
684 |
119
| else if ((pvClassName.startsWith("[L")) && (pvClassName.endsWith(";"))) {
|
|
685 |
5
| try {
|
|
686 |
5
| String lvClassName = pvClassName.substring(2, (pvClassName.length() - 1));
|
|
687 |
5
| lvClassName = lvClassName.replaceAll("/", "\\.");
|
|
688 |
5
| Class clazz = Class.forName(lvClassName);
|
|
689 |
4
| Object lvArray = Array.newInstance(clazz, 0);
|
|
690 |
4
| return lvArray;
|
|
691 |
| } catch (Exception e) { |
|
692 |
| if (log.isDebugEnabled()) { log.debug("convertClassString2Object: " + pvClassName, e); } |
|
693 |
| if (ServiceManager.DEBUG_MODE_ON) { e.printStackTrace(); } |
|
694 |
1
| return null;
|
|
695 |
| } |
|
696 |
| } |
|
697 |
| |
|
698 |
| else { |
|
699 |
114
| Object lvReturn = createObjectWithParamConstructor(null, null, pvClassName);
|
|
700 |
114
| if (lvReturn == null) {
|
|
701 |
33
| lvReturn = createObjectWithParamConstructor(new Long(0), long.class, pvClassName);
|
|
702 |
| } |
|
703 |
114
| if (lvReturn == null) {
|
|
704 |
5
| lvReturn = createObjectWithParamConstructor("0", String.class, pvClassName);
|
|
705 |
| } |
|
706 |
114
| return lvReturn;
|
|
707 |
| } |
|
708 |
| |
|
709 |
| } |
|
710 |
| |
|
711 |
152
| public static Object createObjectWithParamConstructor (Object pvParam, Class pvParamType, String pvClassName) {
|
|
712 |
152
| try {
|
|
713 |
152
| Class clazz = Class.forName(pvClassName);
|
|
714 |
149
| if (pvParam == null) {
|
|
715 |
113
| return clazz.newInstance();
|
|
716 |
| } else { |
|
717 |
36
| Constructor lvConstructor = clazz.getConstructor(new Class[] {pvParamType});
|
|
718 |
31
| return lvConstructor.newInstance(new Object [] {pvParam});
|
|
719 |
| } |
|
720 |
| } catch (Exception e) { |
|
721 |
| if (log.isDebugEnabled()) { log.debug("convertString2ClassString: " + pvClassName, e); } |
|
722 |
| if (ServiceManager.DEBUG_MODE_ON) { e.printStackTrace(); } |
|
723 |
| } |
|
724 |
40
| return null;
|
|
725 |
| } |
|
726 |
| |
|
727 |
| |
|
728 |
| |
|
729 |
| |
|
730 |
| |
|
731 |
| |
|
732 |
| |
|
733 |
| |
|
734 |
322
| public static Object convertString2Value (String pvStringValue, String pvClassName) throws Exception {
|
|
735 |
322
| Class clazz = null;
|
|
736 |
| |
|
737 |
1
| if (pvClassName.equals(int.class.getName())) { clazz = int.class; }
|
|
738 |
1
| else if (pvClassName.equals(short.class.getName())) { clazz = short.class; }
|
|
739 |
1
| else if (pvClassName.equals(byte.class.getName())) { clazz = byte.class; }
|
|
740 |
1
| else if (pvClassName.equals(long.class.getName())) { clazz = long.class; }
|
|
741 |
| |
|
742 |
| |
|
743 |
1
| else if (pvClassName.equals(double.class.getName())) { clazz = double.class; }
|
|
744 |
1
| else if (pvClassName.equals(float.class.getName())) { clazz = float.class; }
|
|
745 |
| |
|
746 |
1
| else if (pvClassName.equals(char.class.getName())) { clazz = char.class; }
|
|
747 |
| |
|
748 |
1
| else if (pvClassName.equals(boolean.class.getName())) { clazz = boolean.class; }
|
|
749 |
| |
|
750 |
| else { |
|
751 |
314
| try {
|
|
752 |
314
| clazz = Class.forName(pvClassName);
|
|
753 |
| } catch (Exception e) { |
|
754 |
| if (ServiceManager.DEBUG_MODE_ON) { |
|
755 |
| e.printStackTrace(); |
|
756 |
| } |
|
757 |
| } |
|
758 |
| |
|
759 |
| } |
|
760 |
| |
|
761 |
322
| return convertString2Value (pvStringValue, clazz);
|
|
762 |
| } |
|
763 |
| |
|
764 |
220
| public static Object convertString2Value (Object pvValue, Class pvClass) {
|
|
765 |
220
| if (pvValue instanceof String) {
|
|
766 |
207
| return convertString2Value((String) pvValue, pvClass);
|
|
767 |
| } else { |
|
768 |
13
| return pvValue;
|
|
769 |
| } |
|
770 |
| } |
|
771 |
| |
|
772 |
| |
|
773 |
| |
|
774 |
| |
|
775 |
| |
|
776 |
| |
|
777 |
| |
|
778 |
542
| public static Object convertString2Value (String pvStringValue, Class pvClass) {
|
|
779 |
1
| if ((pvClass == null) || (pvStringValue == null)) { return null; }
|
|
780 |
| |
|
781 |
68
| if ((pvClass.equals(Integer.class)) || (pvClass.equals(int.class))) { return Integer.valueOf(pvStringValue); }
|
|
782 |
4
| else if ((pvClass.equals(Short.class)) || (pvClass.equals(short.class))) { return Short.valueOf(pvStringValue); }
|
|
783 |
4
| else if ((pvClass.equals(Byte.class)) || (pvClass.equals(byte.class))) { return Byte.valueOf(pvStringValue); }
|
|
784 |
21
| else if ((pvClass.equals(Long.class)) || (pvClass.equals(long.class))) { return Long.valueOf(pvStringValue); }
|
|
785 |
| |
|
786 |
| |
|
787 |
16
| else if ((pvClass.equals(Double.class)) || (pvClass.equals(double.class))) { return Double.valueOf(pvStringValue); }
|
|
788 |
4
| else if ((pvClass.equals(Float.class)) || (pvClass.equals(float.class))) { return Float.valueOf(pvStringValue); }
|
|
789 |
14
| else if (pvClass.equals(BigDecimal.class)) { return new BigDecimal (pvStringValue); }
|
|
790 |
| |
|
791 |
| |
|
792 |
32
| else if ((pvClass.equals(Boolean.class)) || (pvClass.equals(boolean.class))) { return Boolean.valueOf(pvStringValue); }
|
|
793 |
| |
|
794 |
| |
|
795 |
4
| else if ((pvClass.equals(Character.class)) || (pvClass.equals(char.class))) { return new Character(pvStringValue.charAt(0)); }
|
|
796 |
366
| else if (pvClass.equals(String.class)) { return pvStringValue; }
|
|
797 |
| |
|
798 |
5
| else if ((pvClass.equals(Date.class)) || (pvClass.equals(java.sql.Date.class))){ return Converter.string2Date(pvStringValue); }
|
|
799 |
| |
|
800 |
| else { |
|
801 |
| |
|
802 |
3
| log.warn("Illegal value in Converter.convertString2Value: " + pvStringValue + " for class: " + pvClass);
|
|
803 |
3
| try {
|
|
804 |
3
| return pvClass.newInstance();
|
|
805 |
| } catch (Exception e) { |
|
806 |
| if (log.isDebugEnabled()) { |
|
807 |
| log.debug("Error by convertString2Value by new Instance for class: " + pvClass, e); |
|
808 |
| if (ServiceManager.DEBUG_MODE_ON) { e.printStackTrace(); } |
|
809 |
| } |
|
810 |
2
| return null;
|
|
811 |
| } |
|
812 |
| } |
|
813 |
| } |
|
814 |
| |
|
815 |
| |
|
816 |
| |
|
817 |
| |
|
818 |
| |
|
819 |
| |
|
820 |
| |
|
821 |
| |
|
822 |
| |
|
823 |
| |
|
824 |
200
| public static Object[] convertStringParams2MethodParams (Method pvMethod, Vector pvParams) {
|
|
825 |
200
| if ((pvMethod == null) || (pvParams == null)) {
|
|
826 |
1
| throw new IllegalArgumentException("The method parameter: " + pvMethod + " or params array: " + pvParams +
|
|
827 |
| " must not null!"); |
|
828 |
| } |
|
829 |
| |
|
830 |
199
| Class lvParamTypes[] = pvMethod.getParameterTypes();
|
|
831 |
199
| if (pvParams.size() != lvParamTypes.length) {
|
|
832 |
2
| throw new IllegalArgumentException("The parameter size: " + pvParams.size()
|
|
833 |
| + " is not equal to method parameter: " + lvParamTypes.length); |
|
834 |
| } |
|
835 |
197
| Object lvNewParams[] = new Object[pvParams.size()];
|
|
836 |
197
| for (int i = 0; i < lvNewParams.length; i++) {
|
|
837 |
220
| lvNewParams[i] = Converter.convertString2Value(pvParams.get(i), lvParamTypes[i]);
|
|
838 |
| } |
|
839 |
197
| return lvNewParams;
|
|
840 |
| } |
|
841 |
| |
|
842 |
| |
|
843 |
| |
|
844 |
| |
|
845 |
| |
|
846 |
| |
|
847 |
| |
|
848 |
| |
|
849 |
| |
|
850 |
10
| public static Date string2Date (String pvDateString) {
|
|
851 |
1
| if (pvDateString == null) { throw new IllegalArgumentException ("The Date-String was null by string2Date."); }
|
|
852 |
| |
|
853 |
9
| Date date = null;
|
|
854 |
| |
|
855 |
| |
|
856 |
9
| DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'CEST' yyyy", Locale.ENGLISH);
|
|
857 |
9
| try {
|
|
858 |
9
| date = df.parse(pvDateString);
|
|
859 |
1
| return date;
|
|
860 |
| } catch (ParseException e) { |
|
861 |
8
| log.debug("Error by string2Date: " + pvDateString , e);
|
|
862 |
| } |
|
863 |
| |
|
864 |
| |
|
865 |
8
| df = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'CET' yyyy", Locale.ENGLISH);
|
|
866 |
8
| try {
|
|
867 |
8
| date = df.parse(pvDateString);
|
|
868 |
6
| return date;
|
|
869 |
| } catch (ParseException e) { |
|
870 |
2
| log.debug("Error by string2Date: " + pvDateString , e);
|
|
871 |
| } |
|
872 |
| |
|
873 |
| |
|
874 |
2
| df = DateFormat.getDateInstance(DateFormat.MEDIUM);
|
|
875 |
2
| try {
|
|
876 |
2
| date = df.parse(pvDateString);
|
|
877 |
1
| return date;
|
|
878 |
| } catch (ParseException e) { |
|
879 |
1
| log.debug("Error by string2Date: " + pvDateString , e);
|
|
880 |
| } |
|
881 |
| |
|
882 |
1
| throw new IllegalStateException ("The String: \"" + pvDateString + "\" is not valid date.");
|
|
883 |
| } |
|
884 |
| |
|
885 |
| |
|
886 |
| |
|
887 |
| |
|
888 |
| |
|
889 |
| |
|
890 |
| |
|
891 |
| |
|
892 |
| |
|
893 |
| |
|
894 |
69
| public static String getClassWithoutPackage(String pvClassWitchPackage, boolean pvToLowerCase) {
|
|
895 |
2
| if (pvClassWitchPackage == null) { return null; }
|
|
896 |
67
| int lvIndex = pvClassWitchPackage.lastIndexOf('.') + 1;
|
|
897 |
67
| String lvClassName = pvClassWitchPackage.substring(lvIndex);
|
|
898 |
2
| if (pvToLowerCase == true) { lvClassName = lvClassName.toLowerCase(); }
|
|
899 |
67
| return lvClassName;
|
|
900 |
| } |
|
901 |
| |
|
902 |
| |
|
903 |
| |
|
904 |
| |
|
905 |
| |
|
906 |
| |
|
907 |
| |
|
908 |
| |
|
909 |
| |
|
910 |
1787
| public static String extractHashCode (Object pvObject) {
|
|
911 |
1
| if (pvObject == null) { return null; }
|
|
912 |
1786
| String lvHashCode = pvObject.toString();
|
|
913 |
1786
| if (lvHashCode.startsWith(HASH_CODE_KEY) == true) {
|
|
914 |
35
| lvHashCode = lvHashCode.substring(HASH_CODE_KEY.length());
|
|
915 |
35
| return lvHashCode;
|
|
916 |
| } else { |
|
917 |
1751
| return null;
|
|
918 |
| } |
|
919 |
| |
|
920 |
| } |
|
921 |
| |
|
922 |
| |
|
923 |
| |
|
924 |
| |
|
925 |
78
| public static Map getAllNotEqualsGetterAndSetterAndRemoveThisProperties(Map pvGetterMap, Map pvSetterMap) {
|
|
926 |
78
| Iterator it = new ArrayList(pvGetterMap.keySet()).iterator();
|
|
927 |
78
| while (it.hasNext()) {
|
|
928 |
650
| Object lvGetterProp = it.next();
|
|
929 |
650
| if (!(pvSetterMap.containsKey(lvGetterProp)) && !(lvGetterProp.equals("class"))) {
|
|
930 |
| if (log.isDebugEnabled()) { |
|
931 |
| log.debug("For Getter: " + lvGetterProp + " --> no Setter!"); |
|
932 |
| } |
|
933 |
133
| pvGetterMap.remove(lvGetterProp);
|
|
934 |
| } |
|
935 |
| } |
|
936 |
78
| return pvGetterMap;
|
|
937 |
| } |
|
938 |
| |
|
939 |
| |
|
940 |
| |
|
941 |
| |
|
942 |
| |
|
943 |
| |
|
944 |
| |
|
945 |
880
| public static Map getAllGetterMethodWithCache(Class pvClass) throws IntrospectionException {
|
|
946 |
880
| Map lvGetterMap = classPropertiesCacheGetter.getClassPropertiesMapByClass(pvClass);
|
|
947 |
880
| if (lvGetterMap == null) {
|
|
948 |
78
| try {
|
|
949 |
78
| lvGetterMap = Converter.getAllGetterMethod(pvClass);
|
|
950 |
77
| Map lvSetterMap = getAllSetterMethodWithCache(pvClass);
|
|
951 |
77
| lvGetterMap = getAllNotEqualsGetterAndSetterAndRemoveThisProperties (lvGetterMap, lvSetterMap);
|
|
952 |
77
| classPropertiesCacheGetter.addClassPropertiesMap(pvClass, lvGetterMap);
|
|
953 |
| } catch (IntrospectionException e) { |
|
954 |
| if (ServiceManager.DEBUG_MODE_ON) { |
|
955 |
| e.printStackTrace(); |
|
956 |
| } |
|
957 |
| } |
|
958 |
| } |
|
959 |
879
| return lvGetterMap;
|
|
960 |
| } |
|
961 |
| |
|
962 |
| |
|
963 |
| |
|
964 |
| |
|
965 |
| |
|
966 |
| |
|
967 |
| |
|
968 |
521
| public static Map getAllSetterMethodWithCache(Class pvClass) throws IntrospectionException {
|
|
969 |
521
| Map lvMap = classPropertiesCacheSetter.getClassPropertiesMapByClass(pvClass);
|
|
970 |
521
| if (lvMap == null) {
|
|
971 |
78
| try {
|
|
972 |
78
| lvMap = Converter.getAllSetterMethod(pvClass);
|
|
973 |
78
| classPropertiesCacheSetter.addClassPropertiesMap(pvClass, lvMap);
|
|
974 |
| } catch (IntrospectionException e) { |
|
975 |
| if (ServiceManager.DEBUG_MODE_ON) { |
|
976 |
| e.printStackTrace(); |
|
977 |
| } |
|
978 |
| } |
|
979 |
| } |
|
980 |
521
| return lvMap;
|
|
981 |
| } |
|
982 |
| |
|
983 |
| |
|
984 |
| |
|
985 |
| |
|
986 |
| |
|
987 |
| |
|
988 |
78
| public static Map getAllGetterMethod(Class pvClass) throws IntrospectionException {
|
|
989 |
78
| return getAllGetterAndSetterMethod(pvClass, GET_METHOD);
|
|
990 |
| } |
|
991 |
| |
|
992 |
| |
|
993 |
| |
|
994 |
| |
|
995 |
| |
|
996 |
| |
|
997 |
| |
|
998 |
78
| public static Map getAllSetterMethod(Class pvClass) throws IntrospectionException {
|
|
999 |
78
| return getAllGetterAndSetterMethod(pvClass, SET_METHOD);
|
|
1000 |
| } |
|
1001 |
| |
|
1002 |
| |
|
1003 |
| |
|
1004 |
| |
|
1005 |
| |
|
1006 |
| |
|
1007 |
| |
|
1008 |
165
| public static Method[] getAllMethodsByClass (Class pvClass) {
|
|
1009 |
165
| Collection lvColl = getAllMethodsByClassIntern(pvClass, new Hashtable());
|
|
1010 |
165
| return (Method[]) getAllMethodsByClassIntern(pvClass, new Hashtable()).toArray(new Method [lvColl.size()]);
|
|
1011 |
| } |
|
1012 |
| |
|
1013 |
| |
|
1014 |
| |
|
1015 |
| |
|
1016 |
| |
|
1017 |
| |
|
1018 |
| |
|
1019 |
542
| private static Collection getAllMethodsByClassIntern (Class pvClass, Map pvMethodsMap) {
|
|
1020 |
542
| putAllMethodsIntern( pvClass.getMethods(), pvMethodsMap) ;
|
|
1021 |
542
| putAllMethodsIntern( pvClass.getDeclaredMethods(), pvMethodsMap);
|
|
1022 |
| |
|
1023 |
542
| if (!(pvClass.getSuperclass().equals(Object.class))) {
|
|
1024 |
212
| getAllMethodsByClassIntern(pvClass.getSuperclass(), pvMethodsMap);
|
|
1025 |
| } |
|
1026 |
| |
|
1027 |
542
| return pvMethodsMap.values();
|
|
1028 |
| } |
|
1029 |
| |
|
1030 |
1084
| private static void putAllMethodsIntern (Method pvAllMethods[], Map pvMethodsMap) {
|
|
1031 |
1084
| for (int i = 0; i < pvAllMethods.length; i++) {
|
|
1032 |
17150
| String lvMethodName = pvAllMethods[i].getName();
|
|
1033 |
17150
| if (lvMethodName.startsWith("set") || lvMethodName.startsWith("get")) {
|
|
1034 |
10054
| pvMethodsMap.put(pvAllMethods[i], pvAllMethods[i]);
|
|
1035 |
| } |
|
1036 |
| } |
|
1037 |
| } |
|
1038 |
| |
|
1039 |
| |
|
1040 |
| |
|
1041 |
| |
|
1042 |
| |
|
1043 |
| |
|
1044 |
| |
|
1045 |
| |
|
1046 |
165
| public static Map getAllGetterAndSetterMethod(Class pvClass, int pvMethodType) throws IntrospectionException {
|
|
1047 |
165
| Method lvAllAmethods[] = getAllMethodsByClass(pvClass);
|
|
1048 |
165
| Map lvGetterOrSetter = new TreeMap();
|
|
1049 |
165
| for (int i = 0; i < lvAllAmethods.length; i++) {
|
|
1050 |
2344
| Method lvMethod = null;
|
|
1051 |
2344
| String lvPropName = lvAllAmethods[i].getName();
|
|
1052 |
2344
| switch (pvMethodType) {
|
|
1053 |
1144
| case GET_METHOD:
|
|
1054 |
1144
| if (lvPropName.startsWith("get")) {
|
|
1055 |
681
| lvMethod = lvAllAmethods[i];
|
|
1056 |
| } |
|
1057 |
1144
| break;
|
|
1058 |
1188
| case SET_METHOD:
|
|
1059 |
1188
| if (lvPropName.startsWith("set")) {
|
|
1060 |
485
| lvMethod = lvAllAmethods[i];
|
|
1061 |
| } |
|
1062 |
1188
| break;
|
|
1063 |
12
| default:
|
|
1064 |
12
| break;
|
|
1065 |
| } |
|
1066 |
2344
| if (lvMethod != null) {
|
|
1067 |
| |
|
1068 |
1166
| AccessController.doPrivileged(new MethodAccessiblePrivilegedAction(lvMethod));
|
|
1069 |
| |
|
1070 |
1166
| lvPropName = lvPropName.substring(3);
|
|
1071 |
| |
|
1072 |
1166
| if (lvPropName.length() > 0) {
|
|
1073 |
1165
| lvPropName = lvPropName.substring(0, 1).toLowerCase()+ lvPropName.substring(1);
|
|
1074 |
1165
| if (lvPropName.equals("class")) {
|
|
1075 |
81
| lvGetterOrSetter.put(lvPropName, pvClass.getName());
|
|
1076 |
| } else { |
|
1077 |
1084
| lvGetterOrSetter.put(lvPropName, lvMethod);
|
|
1078 |
| } |
|
1079 |
| } else { |
|
1080 |
1
| throw new InvocationException("Invalid Property-Name: '" + lvAllAmethods[i].getName()
|
|
1081 |
| + "' (Valid Property-Name is: set[name] themed get[name], eg. setYear and getYear)."); |
|
1082 |
| } |
|
1083 |
| } |
|
1084 |
| } |
|
1085 |
164
| return lvGetterOrSetter;
|
|
1086 |
| } |
|
1087 |
| |
|
1088 |
234
| public static Object createDefaultValueForPrimitiveClass(Class pvClass) {
|
|
1089 |
234
| Object lvReturn = null;
|
|
1090 |
1
| if (pvClass.equals(boolean.class)) { lvReturn = Boolean.FALSE; }
|
|
1091 |
1
| else if (pvClass.equals(short.class)) { lvReturn = Short.valueOf("0"); }
|
|
1092 |
1
| else if (pvClass.equals(byte.class)) { lvReturn = Byte.valueOf("0"); }
|
|
1093 |
14
| else if (pvClass.equals(int.class)) { lvReturn = Integer.valueOf("0"); }
|
|
1094 |
1
| else if (pvClass.equals(long.class)) { lvReturn = Long.valueOf("0"); }
|
|
1095 |
1
| else if (pvClass.equals(double.class)) { lvReturn = Double.valueOf("0"); }
|
|
1096 |
1
| else if (pvClass.equals(float.class)) { lvReturn = Float.valueOf("0"); }
|
|
1097 |
1
| else if (pvClass.equals(char.class)) { lvReturn = new Character(' '); }
|
|
1098 |
| |
|
1099 |
234
| return lvReturn;
|
|
1100 |
| } |
|
1101 |
| |
|
1102 |
| private class SimpleKeyMapper implements Comparator { |
|
1103 |
| |
|
1104 |
| public static final String DELIMITER = "~_-_~"; |
|
1105 |
| |
|
1106 |
| private String pos = null; |
|
1107 |
| private Object key = null; |
|
1108 |
| |
|
1109 |
571
| public SimpleKeyMapper() {}
|
|
1110 |
62
| public SimpleKeyMapper(String pvPos, Object pvKey) {
|
|
1111 |
62
| pos = pvPos;
|
|
1112 |
62
| key = pvKey;
|
|
1113 |
| } |
|
1114 |
| |
|
1115 |
104
| public int getPos () {
|
|
1116 |
104
| Integer integer = Integer.valueOf(pos);
|
|
1117 |
104
| return integer.intValue();
|
|
1118 |
| } |
|
1119 |
| |
|
1120 |
62
| public Object getKey() {
|
|
1121 |
62
| return key;
|
|
1122 |
| } |
|
1123 |
| |
|
1124 |
164
| public Object toSimpleNewKey(final Object pvOldKey, final Class pvOldKeyClass, int pvPos, boolean pvWithSimpleKeyMapper) {
|
|
1125 |
164
| if (pvWithSimpleKeyMapper) {
|
|
1126 |
71
| String lvNewKey = null;
|
|
1127 |
71
| int l = pvOldKey.toString().split(DELIMITER).length;
|
|
1128 |
71
| if (l == 3 || l == 2) {
|
|
1129 |
1
| lvNewKey = pvOldKey.toString();
|
|
1130 |
| } else { |
|
1131 |
70
| if (pvOldKeyClass.equals(String.class)) {
|
|
1132 |
64
| lvNewKey = pvPos + DELIMITER + pvOldKey;
|
|
1133 |
| } else { |
|
1134 |
6
| lvNewKey = pvPos + DELIMITER + pvOldKey + DELIMITER + pvOldKeyClass.getName();
|
|
1135 |
| } |
|
1136 |
| } |
|
1137 |
71
| return lvNewKey;
|
|
1138 |
| } else { |
|
1139 |
93
| return pvOldKey;
|
|
1140 |
| } |
|
1141 |
| } |
|
1142 |
| |
|
1143 |
101
| public Object toComplexNewKey(Object pvOriginalKey, boolean pvWithSimpleKeyMapper) {
|
|
1144 |
101
| if (pvWithSimpleKeyMapper == false) {
|
|
1145 |
39
| return pvOriginalKey;
|
|
1146 |
| } else { |
|
1147 |
62
| Object lvKey = ((SimpleKeyMapper) pvOriginalKey).getKey();
|
|
1148 |
62
| return lvKey;
|
|
1149 |
| } |
|
1150 |
| } |
|
1151 |
| |
|
1152 |
| |
|
1153 |
167
| public Map orderMapByKey (final Map pvMap, boolean pvWithSimpleKeyMapper) throws Exception {
|
|
1154 |
167
| if (pvWithSimpleKeyMapper == false) {
|
|
1155 |
74
| return pvMap;
|
|
1156 |
| } else { |
|
1157 |
93
| Iterator it = pvMap.keySet().iterator();
|
|
1158 |
93
| TreeMap lvTreeMap = new TreeMap(new SimpleKeyMapper());
|
|
1159 |
93
| while (it.hasNext()) {
|
|
1160 |
64
| Object o = it.next();
|
|
1161 |
64
| if ( ! (o instanceof String) ) {
|
|
1162 |
1
| throw new IllegalArgumentException("Expected String and object is: " + o + " -> class: " + o.getClass().getName());
|
|
1163 |
| } |
|
1164 |
63
| String lvKeyStr = (String) o;
|
|
1165 |
63
| String lvKeyArray[] = lvKeyStr.split(DELIMITER);
|
|
1166 |
63
| if (lvKeyArray.length < 2) {
|
|
1167 |
1
| throw new IllegalArgumentException("If the property Converter.withSimpleKeyMappe is true, than must the key contains" +
|
|
1168 |
| " the delimiter: " + DELIMITER + " - " + lvKeyStr + " (" + lvKeyArray.length + ")"); |
|
1169 |
| } |
|
1170 |
| |
|
1171 |
62
| Object lvValue = pvMap.get(lvKeyStr);
|
|
1172 |
| |
|
1173 |
62
| String lvPos = lvKeyArray[0];
|
|
1174 |
62
| String lvKeyValue = lvKeyArray[1];
|
|
1175 |
62
| Object lvKey = null;
|
|
1176 |
62
| if (lvKeyArray.length == 3) {
|
|
1177 |
6
| String lvKeyClass = lvKeyArray[2];
|
|
1178 |
6
| lvKey = Converter.convertString2Value(lvKeyValue, lvKeyClass);
|
|
1179 |
| } else { |
|
1180 |
56
| lvKey = lvKeyValue;
|
|
1181 |
| } |
|
1182 |
| |
|
1183 |
| |
|
1184 |
62
| SimpleKeyMapper skm = new SimpleKeyMapper(lvPos, lvKey);
|
|
1185 |
62
| lvTreeMap.put(skm, lvValue);
|
|
1186 |
| } |
|
1187 |
91
| return lvTreeMap;
|
|
1188 |
| } |
|
1189 |
| } |
|
1190 |
| |
|
1191 |
52
| public int compare(Object pvO1, Object pvO2) {
|
|
1192 |
52
| SimpleKeyMapper skm1 = (SimpleKeyMapper) pvO1;
|
|
1193 |
52
| SimpleKeyMapper skm2 = (SimpleKeyMapper) pvO2;
|
|
1194 |
52
| if (skm1.getPos() > skm2.getPos()) {
|
|
1195 |
24
| return 1;
|
|
1196 |
| } |
|
1197 |
| else { |
|
1198 |
28
| return -1;
|
|
1199 |
| } |
|
1200 |
| } |
|
1201 |
| } |
|
1202 |
| } |