Coverage Report - org.kuali.rice.kns.service.impl.PersistenceServiceStructureImplBase
 
Classes in this File Line Coverage Branch Coverage Complexity
PersistenceServiceStructureImplBase
0%
0/62
0%
0/30
5.2
 
 1  
 /*
 2  
  * Copyright 2006-2007 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kns.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.apache.commons.lang.StringUtils;
 22  
 import org.apache.ojb.broker.metadata.ClassDescriptor;
 23  
 import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
 24  
 import org.apache.ojb.broker.metadata.DescriptorRepository;
 25  
 import org.apache.ojb.broker.metadata.FieldDescriptor;
 26  
 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
 27  
 import org.kuali.rice.core.config.ConfigContext;
 28  
 import org.kuali.rice.core.jpa.metadata.EntityDescriptor;
 29  
 import org.kuali.rice.core.jpa.metadata.MetadataManager;
 30  
 import org.kuali.rice.core.jpa.metadata.ObjectDescriptor;
 31  
 import org.kuali.rice.core.ojb.BaseOjbConfigurer;
 32  
 import org.kuali.rice.core.util.OrmUtils;
 33  
 import org.kuali.rice.kns.bo.PersistableBusinessObject;
 34  
 import org.kuali.rice.kns.exception.ClassNotPersistableException;
 35  
 import org.kuali.rice.kns.exception.ObjectNotABusinessObjectRuntimeException;
 36  
 import org.kuali.rice.kns.util.spring.CacheNoCopy;
 37  
 
 38  
 public class PersistenceServiceStructureImplBase {
 39  
 
 40  
         private DescriptorRepository descriptorRepository;
 41  
 
 42  
         /**
 43  
          * Constructs a PersistenceServiceImpl instance.
 44  
          */
 45  0
         public PersistenceServiceStructureImplBase() {
 46  0
                 String ojbPropertyFileLocation = ConfigContext.getCurrentContextConfig().getProperty(BaseOjbConfigurer.RICE_OJB_PROPERTIES_PARAM);
 47  0
                 if ( StringUtils.isBlank(ojbPropertyFileLocation) ) {
 48  0
                         ojbPropertyFileLocation = BaseOjbConfigurer.DEFAULT_OJB_PROPERTIES;
 49  0
                         ConfigContext.getCurrentContextConfig().putProperty(BaseOjbConfigurer.RICE_OJB_PROPERTIES_PARAM, ojbPropertyFileLocation);
 50  
                 }
 51  0
         String currentValue = System.getProperty(BaseOjbConfigurer.OJB_PROPERTIES_PROP);
 52  
                 try {
 53  0
                         System.setProperty(BaseOjbConfigurer.OJB_PROPERTIES_PROP, ojbPropertyFileLocation);
 54  0
                         org.apache.ojb.broker.metadata.MetadataManager metadataManager = org.apache.ojb.broker.metadata.MetadataManager.getInstance();
 55  0
                         descriptorRepository = metadataManager.getGlobalRepository();
 56  0
             } finally {
 57  0
                 if (currentValue == null) {
 58  0
                     System.getProperties().remove(BaseOjbConfigurer.OJB_PROPERTIES_PROP);
 59  
                 } else {
 60  0
                     System.setProperty(BaseOjbConfigurer.OJB_PROPERTIES_PROP, currentValue);
 61  
                 }
 62  0
             }
 63  0
         }
 64  
 
 65  
         /**
 66  
          * @return DescriptorRepository containing everything OJB knows about persistable classes
 67  
          */
 68  
         protected DescriptorRepository getDescriptorRepository() {
 69  0
                 return descriptorRepository;
 70  
         }
 71  
 
 72  
         /**
 73  
          *
 74  
          * This method returns a list of primary key field names.  This method uses the CacheNoCopy caching method.
 75  
          * "NoCopy" is the faster of the caching annotations, but because it does not make a copy the list that is
 76  
          * returned must not be modified.  To enforce this the returned list is wrapped in a Collections.unmodifiableList
 77  
          * method.  This will cause an exception to be thrown if the list is altered.
 78  
          *
 79  
          * @param clazz
 80  
          * @return unmodifiableList of field names.  Any attempt to alter list will result in an UnsupportedOperationException
 81  
          */
 82  
         @CacheNoCopy
 83  
         public List listPrimaryKeyFieldNames(Class clazz) {
 84  
             // Rice JPA MetadataManager
 85  0
                 if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
 86  0
                         List fieldNames = new ArrayList();
 87  0
                     EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(clazz);
 88  0
                     for (org.kuali.rice.core.jpa.metadata.FieldDescriptor field : descriptor.getPrimaryKeys()) {
 89  0
                             fieldNames.add(field.getName());
 90  
                     }
 91  0
                     return fieldNames;
 92  
                 } else {
 93  
                     // Legacy OJB
 94  0
                         List fieldNamesLegacy = new ArrayList();
 95  0
                         ClassDescriptor classDescriptor = getClassDescriptor(clazz);
 96  0
                         FieldDescriptor keyDescriptors[] = classDescriptor.getPkFields();
 97  
 
 98  0
                         for (int i = 0; i < keyDescriptors.length; ++i) {
 99  0
                                 FieldDescriptor keyDescriptor = keyDescriptors[i];
 100  0
                                 fieldNamesLegacy.add(keyDescriptor.getAttributeName());
 101  
                         }
 102  0
                         return fieldNamesLegacy;
 103  
                 }
 104  
         }
 105  
 
 106  
         /* Not used anywhere... need to check KFS and batch stuff */
 107  
         /**
 108  
          * @param classDescriptor
 109  
          * @return name of the database table associated with given classDescriptor,
 110  
          *         stripped of its leading schemaName
 111  
          */
 112  
         /*
 113  
         @CacheNoCopy
 114  
         protected String getTableName(ClassDescriptor classDescriptor) {
 115  
                 String schemaName = classDescriptor.getSchema();
 116  
                 String fullTableName = classDescriptor.getFullTableName();
 117  
 
 118  
                 String tableName = null;
 119  
                 if (StringUtils.isNotBlank(schemaName)) {
 120  
                         tableName = StringUtils.substringAfter(fullTableName, schemaName + ".");
 121  
                 }
 122  
                 if (StringUtils.isBlank(tableName)) {
 123  
                         tableName = fullTableName;
 124  
                 }
 125  
 
 126  
                 return tableName;
 127  
         }
 128  
         */
 129  
 
 130  
         /**
 131  
          * @param persistableClass
 132  
          * @return ClassDescriptor for the given Class
 133  
          * @throws IllegalArgumentException
 134  
          *             if the given Class is null
 135  
          * @throws ClassNotPersistableException
 136  
          *             if the given Class is unknown to OJB
 137  
          */
 138  
         // Legacy OJB - no need for JPA equivalent
 139  
         protected ClassDescriptor getClassDescriptor(Class persistableClass) {
 140  0
                 if (persistableClass == null) {
 141  0
                         throw new IllegalArgumentException("invalid (null) object");
 142  
                 }
 143  
 
 144  0
                 ClassDescriptor classDescriptor = null;
 145  0
                 DescriptorRepository globalRepository = getDescriptorRepository();
 146  
                 try {
 147  0
                         classDescriptor = globalRepository.getDescriptorFor(persistableClass);
 148  0
                 } catch (ClassNotPersistenceCapableException e) {
 149  0
                         throw new ClassNotPersistableException("class '" + persistableClass.getName() + "' is not persistable", e);
 150  0
                 }
 151  
 
 152  0
                 return classDescriptor;
 153  
         }
 154  
 
 155  
         /**
 156  
          * @see org.kuali.rice.kns.service.PersistenceStructureService#getBusinessObjectAttributeClass(java.lang.Class,
 157  
          *      java.lang.String)
 158  
          */
 159  
         @CacheNoCopy
 160  
         public Class getBusinessObjectAttributeClass(Class clazz, String attributeName) throws ObjectNotABusinessObjectRuntimeException {
 161  0
                 if (clazz.isAssignableFrom(PersistableBusinessObject.class)) {
 162  0
                         throw new ObjectNotABusinessObjectRuntimeException(clazz.getName() + " is not a PersistableBusinessObject");
 163  
                 }
 164  0
                 String baseAttributeName = attributeName;
 165  0
                 String subAttributeString = null;
 166  0
                 if (attributeName.contains(".")) {
 167  0
                         baseAttributeName = attributeName.substring(0, attributeName.indexOf('.'));
 168  0
                         subAttributeString = attributeName.substring(attributeName.indexOf('.') + 1);
 169  
                 }
 170  
 
 171  
             // Rice JPA MetadataManager
 172  0
                 if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
 173  0
                         Class attributeClass = null;
 174  0
                     EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(clazz);
 175  0
                     ObjectDescriptor objectDescriptor = descriptor.getObjectDescriptorByName(baseAttributeName);
 176  0
                         if (objectDescriptor != null) {
 177  0
                                 attributeClass = objectDescriptor.getTargetEntity();
 178  
                         }
 179  
 
 180  
                         // recurse if necessary
 181  0
                         if (subAttributeString != null) {
 182  0
                                 attributeClass = getBusinessObjectAttributeClass(attributeClass, subAttributeString);
 183  
                         }
 184  
 
 185  0
                         return attributeClass;
 186  
                 } else {
 187  
                     // Legacy OJB
 188  0
                         Class attributeClassLegacy = null;
 189  0
                         ClassDescriptor classDescriptor = this.getClassDescriptor(clazz);
 190  0
                         ObjectReferenceDescriptor refDescriptor = classDescriptor.getObjectReferenceDescriptorByName(baseAttributeName);
 191  
 
 192  0
                         if (refDescriptor != null) {
 193  0
                                 attributeClassLegacy = refDescriptor.getItemClass();
 194  
                         }
 195  
 
 196  
                         // recurse if necessary
 197  0
                         if (subAttributeString != null) {
 198  0
                                 attributeClassLegacy = getBusinessObjectAttributeClass(attributeClassLegacy, subAttributeString);
 199  
                         }
 200  
 
 201  0
                         return attributeClassLegacy;
 202  
                 }
 203  
         }
 204  
 
 205  
 }