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 public class PersistenceServiceImplBase extends PersistenceServiceStructureImplBase {
29
30
31
32
33 public Map getPrimaryKeyFieldValues(Object persistableObject) {
34 return getPrimaryKeyFieldValues(persistableObject, false);
35 }
36
37
38
39
40
41 public Map getPrimaryKeyFieldValues(Object persistableObject, boolean sortFieldNames) {
42 if (persistableObject == null) {
43 throw new IllegalArgumentException("invalid (null) persistableObject");
44 }
45
46 Map keyValueMap = null;
47 if (sortFieldNames) {
48 keyValueMap = new TreeMap();
49 } else {
50 keyValueMap = new HashMap();
51 }
52
53 String className = null;
54 String fieldName = null;
55 try {
56 List fields = listPrimaryKeyFieldNames(persistableObject.getClass());
57 for (Iterator i = fields.iterator(); i.hasNext();) {
58 fieldName = (String) i.next();
59 className = persistableObject.getClass().getName();
60 Object fieldValue = PropertyUtils.getSimpleProperty(persistableObject, fieldName);
61
62 keyValueMap.put(fieldName, fieldValue);
63 }
64 } catch (IllegalAccessException e) {
65 throw new IntrospectionException("problem accessing property '" + className + "." + fieldName + "'", e);
66 } catch (NoSuchMethodException e) {
67 throw new IntrospectionException("unable to invoke getter for property '" + className + "." + fieldName + "'", e);
68 } catch (InvocationTargetException e) {
69 throw new IntrospectionException("problem invoking getter for property '" + className + "." + fieldName + "'", e);
70 }
71
72 return keyValueMap;
73 }
74
75 }