001: /*
002: * Copyright 2006 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.core.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: import java.util.Vector;
025:
026: import org.apache.commons.beanutils.PropertyUtils;
027: import org.apache.commons.lang.StringUtils;
028: import org.apache.ojb.broker.metadata.ClassDescriptor;
029: import org.apache.ojb.broker.metadata.FieldDescriptor;
030: import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
031: import org.kuali.core.exceptions.IntrospectionException;
032: import org.kuali.core.util.ObjectUtils;
033:
034: public class PersistenceServiceImplBase extends
035: PersistenceServiceStructureImplBase {
036:
037: public Object getFieldValue(Object persistableObject,
038: String fieldName) {
039: ClassDescriptor classDescriptor = getClassDescriptor(persistableObject
040: .getClass());
041: FieldDescriptor fieldDescriptor = classDescriptor
042: .getFieldDescriptorByName(fieldName);
043:
044: // if field is not anonymous, get value from main object
045: Object fieldValue = null;
046: if (!fieldDescriptor.isAnonymous()) {
047: if (PropertyUtils.isReadable(persistableObject, fieldName)) {
048: fieldValue = ObjectUtils.getPropertyValue(
049: persistableObject, fieldName);
050: }
051: } else {
052: // find the value from one of the other reference objects
053: Vector objectReferences = classDescriptor
054: .getObjectReferenceDescriptors();
055: for (Iterator iter2 = objectReferences.iterator(); iter2
056: .hasNext();) {
057: ObjectReferenceDescriptor checkDescriptor = (ObjectReferenceDescriptor) iter2
058: .next();
059:
060: fieldValue = getReferenceFKValue(persistableObject,
061: checkDescriptor, fieldName);
062: if (fieldValue != null
063: && StringUtils
064: .isNotBlank(fieldValue.toString())) {
065: break;
066: }
067: }
068: }
069:
070: return fieldValue;
071: }
072:
073: /**
074: * @param persistableObject
075: * @param referenceDescriptor
076: * @param fkName
077: * @return
078: */
079: protected Object getReferenceFKValue(Object persistableObject,
080: ObjectReferenceDescriptor chkRefCld, String fkName) {
081: ClassDescriptor classDescriptor = getClassDescriptor(persistableObject
082: .getClass());
083: Object referenceObject = ObjectUtils.getPropertyValue(
084: persistableObject, chkRefCld.getAttributeName());
085:
086: if (referenceObject == null) {
087: return null;
088: }
089:
090: FieldDescriptor[] refFkNames = chkRefCld
091: .getForeignKeyFieldDescriptors(classDescriptor);
092: ClassDescriptor refCld = getClassDescriptor(chkRefCld
093: .getItemClass());
094: FieldDescriptor[] refPkNames = refCld.getPkFields();
095:
096: Object fkValue = null;
097: for (int i = 0; i < refFkNames.length; i++) {
098: FieldDescriptor fkField = refFkNames[i];
099:
100: if (fkField.getAttributeName().equals(fkName)) {
101: fkValue = ObjectUtils.getPropertyValue(referenceObject,
102: refPkNames[i].getAttributeName());
103: break;
104: }
105: }
106:
107: return fkValue;
108: }
109:
110: /**
111: * @see org.kuali.core.service.PersistenceMetadataService#getPrimaryKeyFields(java.lang.Object)
112: */
113: public Map getPrimaryKeyFieldValues(Object persistableObject) {
114: return getPrimaryKeyFieldValues(persistableObject, false);
115: }
116:
117: /**
118: * @see org.kuali.core.service.PersistenceMetadataService#getPrimaryKeyFields(java.lang.Object, boolean)
119: */
120: public Map getPrimaryKeyFieldValues(Object persistableObject,
121: boolean sortFieldNames) {
122: if (persistableObject == null) {
123: throw new IllegalArgumentException(
124: "invalid (null) persistableObject");
125: }
126:
127: Map keyValueMap = null;
128: if (sortFieldNames) {
129: keyValueMap = new TreeMap();
130: } else {
131: keyValueMap = new HashMap();
132: }
133:
134: String className = null;
135: String fieldName = null;
136: try {
137: List fields = listPrimaryKeyFieldNames(persistableObject
138: .getClass());
139: for (Iterator i = fields.iterator(); i.hasNext();) {
140: fieldName = (String) i.next();
141: className = persistableObject.getClass().getName();
142: Object fieldValue = PropertyUtils.getSimpleProperty(
143: persistableObject, fieldName);
144:
145: keyValueMap.put(fieldName, fieldValue);
146: }
147: } catch (IllegalAccessException e) {
148: throw new IntrospectionException(
149: "problem accessing property '" + className + "."
150: + fieldName + "'", e);
151: } catch (NoSuchMethodException e) {
152: throw new IntrospectionException(
153: "unable to invoke getter for property '"
154: + className + "." + fieldName + "'", e);
155: } catch (InvocationTargetException e) {
156: throw new IntrospectionException(
157: "problem invoking getter for property '"
158: + className + "." + fieldName + "'", e);
159: }
160:
161: return keyValueMap;
162: }
163:
164: }
|