001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.service.impl;
017    
018    import java.lang.reflect.InvocationTargetException;
019    import java.util.HashMap;
020    import java.util.Iterator;
021    import java.util.List;
022    import java.util.Map;
023    import java.util.TreeMap;
024    
025    import org.apache.commons.beanutils.PropertyUtils;
026    import org.kuali.rice.krad.exception.IntrospectionException;
027    
028    public class PersistenceServiceImplBase extends PersistenceServiceStructureImplBase {
029    
030            /**
031             * @see org.kuali.rice.krad.service.PersistenceMetadataService#getPrimaryKeyFields(java.lang.Object)
032             */
033            public Map getPrimaryKeyFieldValues(Object persistableObject) {
034                    return getPrimaryKeyFieldValues(persistableObject, false);
035            }
036    
037            /**
038             * @see org.kuali.rice.krad.service.PersistenceMetadataService#getPrimaryKeyFields(java.lang.Object,
039             *      boolean)
040             */
041            public Map getPrimaryKeyFieldValues(Object persistableObject, boolean sortFieldNames) {
042                    if (persistableObject == null) {
043                            throw new IllegalArgumentException("invalid (null) persistableObject");
044                    }
045    
046                    Map keyValueMap = null;
047                    if (sortFieldNames) {
048                            keyValueMap = new TreeMap();
049                    } else {
050                            keyValueMap = new HashMap();
051                    }
052    
053                    String className = null;
054                    String fieldName = null;
055                    try {
056                            List fields = listPrimaryKeyFieldNames(persistableObject.getClass());
057                            for (Iterator i = fields.iterator(); i.hasNext();) {
058                                    fieldName = (String) i.next();
059                                    className = persistableObject.getClass().getName();
060                                    Object fieldValue = PropertyUtils.getSimpleProperty(persistableObject, fieldName);
061    
062                                    keyValueMap.put(fieldName, fieldValue);
063                            }
064                    } catch (IllegalAccessException e) {
065                            throw new IntrospectionException("problem accessing property '" + className + "." + fieldName + "'", e);
066                    } catch (NoSuchMethodException e) {
067                            throw new IntrospectionException("unable to invoke getter for property '" + className + "." + fieldName + "'", e);
068                    } catch (InvocationTargetException e) {
069                            throw new IntrospectionException("problem invoking getter for property '" + className + "." + fieldName + "'", e);
070                    }
071    
072                    return keyValueMap;
073            }
074    
075    }