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.util.ArrayList;
019: import java.util.List;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.apache.ojb.broker.metadata.ClassDescriptor;
023: import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
024: import org.apache.ojb.broker.metadata.DescriptorRepository;
025: import org.apache.ojb.broker.metadata.FieldDescriptor;
026: import org.apache.ojb.broker.metadata.MetadataManager;
027: import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
028: import org.kuali.core.bo.PersistableBusinessObject;
029: import org.kuali.core.exceptions.ClassNotPersistableException;
030: import org.kuali.core.exceptions.ObjectNotABusinessObjectRuntimeException;
031: import org.kuali.core.util.spring.Cached;
032:
033: public class PersistenceServiceStructureImplBase {
034:
035: private DescriptorRepository descriptorRepository;
036:
037: // This is repeated in BaseOjbConfigurer
038: private static final String OJB_PROPERTIES_PROP = "OJB.properties";
039:
040: private static final String DEFAULT_OJB_PROPERTIES = "org/kuali/rice/ojb/RiceOJB.properties";
041:
042: /**
043: * Constructs a PersistenceServiceImpl instance.
044: */
045: public PersistenceServiceStructureImplBase() {
046: String currentValue = System.getProperty(OJB_PROPERTIES_PROP);
047: System.setProperty(OJB_PROPERTIES_PROP, DEFAULT_OJB_PROPERTIES);
048: try {
049: MetadataManager metadataManager = MetadataManager
050: .getInstance();
051: descriptorRepository = metadataManager
052: .getGlobalRepository();
053: } finally {
054: if (currentValue == null) {
055: System.getProperties().remove(OJB_PROPERTIES_PROP);
056: } else {
057: System.setProperty(OJB_PROPERTIES_PROP, currentValue);
058: }
059: }
060: }
061:
062: /**
063: * @return DescriptorRepository containing everything OJB knows about
064: * persistable classes
065: */
066: protected DescriptorRepository getDescriptorRepository() {
067: return descriptorRepository;
068: }
069:
070: /**
071: * @see org.kuali.core.service.PersistenceMetadataExplorerService#listPrimaryKeyFieldNames(java.lang.Class)
072: */
073: public List listPrimaryKeyFieldNames(Class clazz) {
074: ClassDescriptor classDescriptor = getClassDescriptor(clazz);
075:
076: List fieldNames = new ArrayList();
077:
078: FieldDescriptor keyDescriptors[] = classDescriptor
079: .getPkFields();
080:
081: for (int i = 0; i < keyDescriptors.length; ++i) {
082: FieldDescriptor keyDescriptor = keyDescriptors[i];
083: fieldNames.add(keyDescriptor.getAttributeName());
084: }
085:
086: return fieldNames;
087: }
088:
089: /**
090: * @param classDescriptor
091: * @return name of the database table associated with given classDescriptor,
092: * stripped of its leading schemaName
093: */
094: protected String getTableName(ClassDescriptor classDescriptor) {
095: String schemaName = classDescriptor.getSchema();
096: String fullTableName = classDescriptor.getFullTableName();
097:
098: String tableName = null;
099: if (StringUtils.isNotBlank(schemaName)) {
100: tableName = StringUtils.substringAfter(fullTableName,
101: schemaName + ".");
102: }
103: if (StringUtils.isBlank(tableName)) {
104: tableName = fullTableName;
105: }
106:
107: return tableName;
108: }
109:
110: /**
111: * @param persistableClass
112: * @return ClassDescriptor for the given Class
113: * @throws IllegalArgumentException
114: * if the given Class is null
115: * @throws ClassNotPersistableException
116: * if the given Class is unknown to OJB
117: */
118: protected ClassDescriptor getClassDescriptor(Class persistableClass) {
119: if (persistableClass == null) {
120: throw new IllegalArgumentException("invalid (null) object");
121: }
122:
123: ClassDescriptor classDescriptor = null;
124: DescriptorRepository globalRepository = getDescriptorRepository();
125: try {
126: classDescriptor = globalRepository
127: .getDescriptorFor(persistableClass);
128: } catch (ClassNotPersistenceCapableException e) {
129: throw new ClassNotPersistableException("class '"
130: + persistableClass.getName()
131: + "' is not persistable", e);
132: }
133:
134: return classDescriptor;
135: }
136:
137: /**
138: * @see org.kuali.core.service.PersistenceStructureService#getBusinessObjectAttributeClass(java.lang.Class, java.lang.String)
139: */
140: @Cached
141: public Class getBusinessObjectAttributeClass(Class clazz,
142: String attributeName)
143: throws ObjectNotABusinessObjectRuntimeException {
144: Class attributeClass = null;
145:
146: if (clazz.isAssignableFrom(PersistableBusinessObject.class)) {
147: throw new ObjectNotABusinessObjectRuntimeException(clazz
148: .getName()
149: + " is not a PersistableBusinessObject");
150: }
151: String baseAttributeName = attributeName;
152: String subAttributeString = null;
153: if (attributeName.contains(".")) {
154: baseAttributeName = attributeName.substring(0,
155: attributeName.indexOf('.'));
156: subAttributeString = attributeName.substring(attributeName
157: .indexOf('.') + 1);
158: }
159:
160: ClassDescriptor classDescriptor = this
161: .getClassDescriptor(clazz);
162: ObjectReferenceDescriptor refDescriptor = classDescriptor
163: .getObjectReferenceDescriptorByName(baseAttributeName);
164:
165: if (refDescriptor != null) {
166: attributeClass = refDescriptor.getItemClass();
167: }
168: // recurse if necessary
169: if (subAttributeString != null) {
170: attributeClass = getBusinessObjectAttributeClass(
171: attributeClass, subAttributeString);
172: }
173:
174: return attributeClass;
175: }
176:
177: }
|