View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  	 * @see org.kuali.rice.krad.service.PersistenceMetadataService#getPrimaryKeyFields(java.lang.Object)
32  	 */
33  	public Map getPrimaryKeyFieldValues(Object persistableObject) {
34  		return getPrimaryKeyFieldValues(persistableObject, false);
35  	}
36  
37  	/**
38  	 * @see org.kuali.rice.krad.service.PersistenceMetadataService#getPrimaryKeyFields(java.lang.Object,
39  	 *      boolean)
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  }