1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.service.impl; |
17 | |
|
18 | |
import java.lang.reflect.InvocationTargetException; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
import java.util.TreeMap; |
24 | |
|
25 | |
import org.apache.commons.beanutils.PropertyUtils; |
26 | |
import org.kuali.rice.krad.exception.IntrospectionException; |
27 | |
|
28 | 0 | public class PersistenceServiceImplBase extends PersistenceServiceStructureImplBase { |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public Map getPrimaryKeyFieldValues(Object persistableObject) { |
34 | 0 | return getPrimaryKeyFieldValues(persistableObject, false); |
35 | |
} |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public Map getPrimaryKeyFieldValues(Object persistableObject, boolean sortFieldNames) { |
42 | 0 | if (persistableObject == null) { |
43 | 0 | throw new IllegalArgumentException("invalid (null) persistableObject"); |
44 | |
} |
45 | |
|
46 | 0 | Map keyValueMap = null; |
47 | 0 | if (sortFieldNames) { |
48 | 0 | keyValueMap = new TreeMap(); |
49 | |
} else { |
50 | 0 | keyValueMap = new HashMap(); |
51 | |
} |
52 | |
|
53 | 0 | String className = null; |
54 | 0 | String fieldName = null; |
55 | |
try { |
56 | 0 | List fields = listPrimaryKeyFieldNames(persistableObject.getClass()); |
57 | 0 | for (Iterator i = fields.iterator(); i.hasNext();) { |
58 | 0 | fieldName = (String) i.next(); |
59 | 0 | className = persistableObject.getClass().getName(); |
60 | 0 | Object fieldValue = PropertyUtils.getSimpleProperty(persistableObject, fieldName); |
61 | |
|
62 | 0 | keyValueMap.put(fieldName, fieldValue); |
63 | 0 | } |
64 | 0 | } catch (IllegalAccessException e) { |
65 | 0 | throw new IntrospectionException("problem accessing property '" + className + "." + fieldName + "'", e); |
66 | 0 | } catch (NoSuchMethodException e) { |
67 | 0 | throw new IntrospectionException("unable to invoke getter for property '" + className + "." + fieldName + "'", e); |
68 | 0 | } catch (InvocationTargetException e) { |
69 | 0 | throw new IntrospectionException("problem invoking getter for property '" + className + "." + fieldName + "'", e); |
70 | 0 | } |
71 | |
|
72 | 0 | return keyValueMap; |
73 | |
} |
74 | |
|
75 | |
} |