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 org.apache.ojb.broker.metadata.ClassDescriptor; 019import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException; 020import org.apache.ojb.broker.metadata.DescriptorRepository; 021import org.apache.ojb.broker.metadata.FieldDescriptor; 022import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor; 023import org.kuali.rice.core.api.config.property.ConfigContext; 024import org.kuali.rice.core.framework.persistence.ojb.BaseOjbConfigurer; 025import org.kuali.rice.krad.bo.PersistableBusinessObject; 026import org.kuali.rice.krad.bo.PersistableBusinessObjectExtension; 027import org.kuali.rice.krad.exception.ClassNotPersistableException; 028import org.kuali.rice.krad.util.LegacyDataFramework; 029import org.kuali.rice.krad.util.LegacyUtils; 030 031import java.util.ArrayList; 032import java.util.List; 033 034@Deprecated 035@LegacyDataFramework 036public class PersistenceServiceStructureImplBase { 037 protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PersistenceServiceStructureImplBase.class); 038 private DescriptorRepository descriptorRepository; 039 040 /** 041 * Constructs a PersistenceServiceImpl instance. 042 */ 043 public PersistenceServiceStructureImplBase() { 044 String ojbPropertyFileLocation = ConfigContext.getCurrentContextConfig().getProperty(BaseOjbConfigurer.RICE_OJB_PROPERTIES_PARAM); 045 String currentValue = System.getProperty(BaseOjbConfigurer.OJB_PROPERTIES_PROP); 046 try { 047 System.setProperty(BaseOjbConfigurer.OJB_PROPERTIES_PROP, ojbPropertyFileLocation); 048 org.apache.ojb.broker.metadata.MetadataManager metadataManager = org.apache.ojb.broker.metadata.MetadataManager.getInstance(); 049 descriptorRepository = metadataManager.getGlobalRepository(); 050 } finally { 051 if (currentValue == null) { 052 System.getProperties().remove(BaseOjbConfigurer.OJB_PROPERTIES_PROP); 053 } else { 054 System.setProperty(BaseOjbConfigurer.OJB_PROPERTIES_PROP, currentValue); 055 } 056 } 057 } 058 059 /** 060 * @return DescriptorRepository containing everything OJB knows about persistable classes 061 */ 062 protected DescriptorRepository getDescriptorRepository() { 063 return descriptorRepository; 064 } 065 066 /** 067 * 068 * This method returns a list of primary key field names. This method uses the CacheNoCopy caching method. 069 * "NoCopy" is the faster of the caching annotations, but because it does not make a copy the list that is 070 * returned must not be modified. To enforce this the returned list is wrapped in a Collections.unmodifiableList 071 * method. This will cause an exception to be thrown if the list is altered. 072 * 073 * @param clazz 074 * @return unmodifiableList of field names. Any attempt to alter list will result in an UnsupportedOperationException 075 */ 076 public List listPrimaryKeyFieldNames(Class clazz) { 077 // Legacy OJB 078 List fieldNamesLegacy = new ArrayList(); 079 ClassDescriptor classDescriptor = getClassDescriptor(clazz); 080 FieldDescriptor keyDescriptors[] = classDescriptor.getPkFields(); 081 082 for (int i = 0; i < keyDescriptors.length; ++i) { 083 FieldDescriptor keyDescriptor = keyDescriptors[i]; 084 fieldNamesLegacy.add(keyDescriptor.getAttributeName()); 085 } 086 return fieldNamesLegacy; 087 } 088 089 /* Not used anywhere... need to check KFS and batch stuff */ 090 /** 091 * @param classDescriptor 092 * @return name of the database table associated with given classDescriptor, 093 * stripped of its leading schemaName 094 */ 095 /* 096 @CacheNoCopy 097 protected String getTableName(ClassDescriptor classDescriptor) { 098 String schemaName = classDescriptor.getSchema(); 099 String fullTableName = classDescriptor.getFullTableName(); 100 101 String tableName = null; 102 if (StringUtils.isNotBlank(schemaName)) { 103 tableName = StringUtils.substringAfter(fullTableName, schemaName + "."); 104 } 105 if (StringUtils.isBlank(tableName)) { 106 tableName = fullTableName; 107 } 108 109 return tableName; 110 } 111 */ 112 113 /** 114 * @param persistableClass 115 * @return ClassDescriptor for the given Class 116 * @throws IllegalArgumentException 117 * if the given Class is null 118 * @throws ClassNotPersistableException 119 * if the given Class is unknown to OJB 120 */ 121 // Legacy OJB - no need for JPA equivalent 122 protected ClassDescriptor getClassDescriptor(Class persistableClass) { 123 if (persistableClass == null) { 124 throw new IllegalArgumentException("invalid (null) object"); 125 } 126 127 ClassDescriptor classDescriptor = null; 128 DescriptorRepository globalRepository = getDescriptorRepository(); 129 try { 130 classDescriptor = globalRepository.getDescriptorFor(persistableClass); 131 } catch (ClassNotPersistenceCapableException e) { 132 throw new ClassNotPersistableException("class '" + persistableClass.getName() + "' is not persistable", e); 133 } 134 135 return classDescriptor; 136 } 137 138 /** 139 * Determines if JPA is enabled for the KNS and for the given class 140 * 141 * @param clazz the class to check for JPA enabling of 142 * @return true if JPA is enabled for the class, false otherwise 143 */ 144 public boolean isJpaEnabledForKradClass(Class clazz) { 145 return !LegacyUtils.useLegacy(clazz); 146 } 147 148 /** 149 * @see org.kuali.rice.krad.service.PersistenceStructureService#getBusinessObjectAttributeClass(java.lang.Class, 150 * java.lang.String) 151 */ 152 public Class<? extends PersistableBusinessObjectExtension> getBusinessObjectAttributeClass(Class<? extends PersistableBusinessObject> clazz, String attributeName) { 153 String baseAttributeName = attributeName; 154 String subAttributeString = null; 155 if (attributeName.contains(".")) { 156 baseAttributeName = attributeName.substring(0, attributeName.indexOf('.')); 157 subAttributeString = attributeName.substring(attributeName.indexOf('.') + 1); 158 } 159 160 // Legacy OJB 161 Class attributeClassLegacy = null; 162 ClassDescriptor classDescriptor = null; 163 try{ 164 classDescriptor = this.getClassDescriptor(clazz); 165 }catch (ClassNotPersistableException e){ 166 LOG.warn("Class descriptor for "+ clazz.getName() +"was not found"); 167 } 168 169 ObjectReferenceDescriptor refDescriptor = null; 170 if(classDescriptor != null){ 171 refDescriptor = classDescriptor.getObjectReferenceDescriptorByName(baseAttributeName); 172 } 173 174 if (refDescriptor != null) { 175 attributeClassLegacy = refDescriptor.getItemClass(); 176 } 177 178 // recurse if necessary 179 if (subAttributeString != null) { 180 attributeClassLegacy = getBusinessObjectAttributeClass(attributeClassLegacy, subAttributeString); 181 } 182 183 return attributeClassLegacy; 184 } 185 186}