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.datadictionary;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.bo.BusinessObject;
020import org.kuali.rice.krad.datadictionary.exception.ClassValidationException;
021
022import 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 */
032public 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 base class
056     * for the dataObjectClass, allowing the data dictionary to index by the base class
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            super.completeValidation();
076
077            if (inactivationBlockingDefinitions != null && !inactivationBlockingDefinitions.isEmpty()) {
078                for (InactivationBlockingDefinition inactivationBlockingDefinition : inactivationBlockingDefinitions) {
079                    inactivationBlockingDefinition.completeValidation(getDataObjectClass(), null);
080                }
081            }
082        } catch (DataDictionaryException ex) {
083            // just rethrow
084            throw ex;
085        } catch (Exception ex) {
086            throw new DataDictionaryException("Exception validating " + this, ex);
087        }
088    }
089
090    /**
091     * @see org.kuali.rice.krad.datadictionary.DataDictionaryEntryBase#afterPropertiesSet()
092     */
093    @SuppressWarnings("unchecked")
094    @Override
095    public void afterPropertiesSet() throws Exception {
096        super.afterPropertiesSet();
097        if (inactivationBlockingDefinitions != null) {
098            for (InactivationBlockingDefinition ibd : inactivationBlockingDefinitions) {
099                ibd.setBusinessObjectClass(getBusinessObjectClass());
100                if (StringUtils.isNotBlank(ibd.getBlockedReferencePropertyName()) &&
101                        ibd.getBlockedBusinessObjectClass() == null) {
102                    // if the user didn't specify a class name for the blocked reference, determine it here
103                    ibd.setBlockedBusinessObjectClass(DataDictionary
104                            .getAttributeClass(getDataObjectClass(), ibd.getBlockedReferencePropertyName()));
105                }
106                ibd.setBlockingReferenceBusinessObjectClass(getBusinessObjectClass());
107            }
108        }
109    }
110
111    /**
112     * @see java.lang.Object#toString()
113     */
114    @Override
115    public String toString() {
116        return "BusinessObjectEntry for " + getBusinessObjectClass();
117    }
118}