001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.krad.service.impl;
017
018import java.lang.reflect.InvocationTargetException;
019import java.util.HashMap;
020import java.util.Iterator;
021import java.util.List;
022import java.util.Map;
023import java.util.TreeMap;
024
025import org.apache.commons.beanutils.PropertyUtils;
026import org.kuali.rice.krad.exception.IntrospectionException;
027import org.kuali.rice.krad.util.LegacyDataFramework;
028
029@Deprecated
030@LegacyDataFramework
031public class PersistenceServiceImplBase extends PersistenceServiceStructureImplBase {
032
033        /**
034         * @see org.kuali.rice.krad.service.PersistenceMetadataService#getPrimaryKeyFields(java.lang.Object)
035         */
036        public Map getPrimaryKeyFieldValues(Object persistableObject) {
037                return getPrimaryKeyFieldValues(persistableObject, false);
038        }
039
040        /**
041         * @see org.kuali.rice.krad.service.PersistenceMetadataService#getPrimaryKeyFields(java.lang.Object,
042         *      boolean)
043         */
044        public Map getPrimaryKeyFieldValues(Object persistableObject, boolean sortFieldNames) {
045                if (persistableObject == null) {
046                        throw new IllegalArgumentException("invalid (null) persistableObject");
047                }
048
049                Map keyValueMap = null;
050                if (sortFieldNames) {
051                        keyValueMap = new TreeMap();
052                } else {
053                        keyValueMap = new HashMap();
054                }
055
056                String className = null;
057                String fieldName = null;
058                try {
059                        List fields = listPrimaryKeyFieldNames(persistableObject.getClass());
060                        for (Iterator i = fields.iterator(); i.hasNext();) {
061                                fieldName = (String) i.next();
062                                className = persistableObject.getClass().getName();
063                                Object fieldValue = PropertyUtils.getSimpleProperty(persistableObject, fieldName);
064
065                                keyValueMap.put(fieldName, fieldValue);
066                        }
067                } catch (IllegalAccessException e) {
068                        throw new IntrospectionException("problem accessing property '" + className + "." + fieldName + "'", e);
069                } catch (NoSuchMethodException e) {
070                        throw new IntrospectionException("unable to invoke getter for property '" + className + "." + fieldName + "'", e);
071                } catch (InvocationTargetException e) {
072                        throw new IntrospectionException("problem invoking getter for property '" + className + "." + fieldName + "'", e);
073                }
074
075                return keyValueMap;
076        }
077
078}