001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.krad.datadictionary;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.krad.bo.BusinessObject;
020    import org.kuali.rice.krad.datadictionary.exception.ClassValidationException;
021    
022    import java.util.List;
023    
024    /**
025     * A single BusinessObject entry in the DataDictionary, which contains information relating to the display, validation,
026     * and general maintenance of a BusinessObject and its attributes.
027     *
028     * Note: the setters do copious amounts of validation, to facilitate generating errors during the parsing process
029     *
030     * @author Kuali Rice Team (rice.collab@kuali.org)
031     */
032    public class BusinessObjectEntry extends DataObjectEntry {
033    
034        protected Class<? extends BusinessObject> baseBusinessObjectClass;
035    
036        public void setBusinessObjectClass(Class<? extends BusinessObject> businessObjectClass) {
037            super.setDataObjectClass(businessObjectClass);
038    
039            if (businessObjectClass == null) {
040                throw new IllegalArgumentException("invalid (null) dataObjectClass");
041            }
042    
043            if (getRelationships() != null) {
044                for (RelationshipDefinition rd : getRelationships()) {
045                    rd.setSourceClass(businessObjectClass);
046                }
047            }
048        }
049    
050        public Class<? extends BusinessObject> getBusinessObjectClass() {
051            return (Class<? extends BusinessObject>) super.getDataObjectClass();
052        }
053    
054        /**
055         * The baseBusinessObjectClass is an optional parameter for specifying a superclass
056         * for the dataObjectClass, allowing the data dictionary to index by superclass
057         * in addition to the current class.
058         */
059    
060        public void setBaseBusinessObjectClass(Class<? extends BusinessObject> baseBusinessObjectClass) {
061            this.baseBusinessObjectClass = baseBusinessObjectClass;
062        }
063    
064        public Class<? extends BusinessObject> getBaseBusinessObjectClass() {
065            return baseBusinessObjectClass;
066        }
067    
068        /**
069         * Directly validate simple fields, call completeValidation on Definition fields.
070         */
071        @Override
072        public void completeValidation() {
073            try {
074    
075                if (baseBusinessObjectClass != null && !baseBusinessObjectClass.isAssignableFrom(getDataObjectClass())) {
076                    throw new ClassValidationException("The baseBusinessObjectClass " + baseBusinessObjectClass.getName() +
077                            " is not a superclass of the dataObjectClass " + getDataObjectClass().getName());
078                }
079    
080                super.completeValidation();
081    
082                if (inactivationBlockingDefinitions != null && !inactivationBlockingDefinitions.isEmpty()) {
083                    for (InactivationBlockingDefinition inactivationBlockingDefinition : inactivationBlockingDefinitions) {
084                        inactivationBlockingDefinition.completeValidation(getDataObjectClass(), null);
085                    }
086                }
087            } catch (DataDictionaryException ex) {
088                // just rethrow
089                throw ex;
090            } catch (Exception ex) {
091                throw new DataDictionaryException("Exception validating " + this, ex);
092            }
093        }
094    
095        /**
096         * @see org.kuali.rice.krad.datadictionary.DataDictionaryEntryBase#afterPropertiesSet()
097         */
098        @SuppressWarnings("unchecked")
099        @Override
100        public void afterPropertiesSet() throws Exception {
101            super.afterPropertiesSet();
102            if (inactivationBlockingDefinitions != null) {
103                for (InactivationBlockingDefinition ibd : inactivationBlockingDefinitions) {
104                    ibd.setBusinessObjectClass(getBusinessObjectClass());
105                    if (StringUtils.isNotBlank(ibd.getBlockedReferencePropertyName()) &&
106                            ibd.getBlockedBusinessObjectClass() == null) {
107                        // if the user didn't specify a class name for the blocked reference, determine it here
108                        ibd.setBlockedBusinessObjectClass(DataDictionary
109                                .getAttributeClass(getDataObjectClass(), ibd.getBlockedReferencePropertyName()));
110                    }
111                    ibd.setBlockingReferenceBusinessObjectClass(getBusinessObjectClass());
112                }
113            }
114        }
115    
116        /**
117         * @see java.lang.Object#toString()
118         */
119        @Override
120        public String toString() {
121            return "BusinessObjectEntry for " + getBusinessObjectClass();
122        }
123    }