001/**
002 * Copyright 2005-2018 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.kuali.rice.krad.datadictionary.parse.BeanTag;
019import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
020import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
021import org.kuali.rice.krad.util.ExternalizableBusinessObjectUtils;
022
023/**
024 * Support attributes define additional attributes that can be used to generate
025 * lookup field conversions and lookup parameters.
026 *
027 * Field conversions and lookup parameters are normally generated using foreign key relationships
028 * defined within OJB and the DD.  Because Person objects are linked in a special way (i.e. they may
029 * come from an external data source and not from the DB, such as LDAP), it is often necessary to define
030 * extra fields that are related to each other, sort of like a supplemental foreign key.
031 *
032 * sourceName is the name of the POJO property of the business object
033 * targetName is the name of attribute that corresponds to the sourceName in the looked up BO
034 * identifier when true, only the field marked as an identifier will be passed in as a lookup parameter
035 * at most one supportAttribute for each relationship should be defined as identifier="true"
036 */
037@BeanTag(name = "supportAttributeDefinition")
038public class SupportAttributeDefinition extends PrimitiveAttributeDefinition {
039    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SupportAttributeDefinition.class);
040    private static final long serialVersionUID = -1719022365280776405L;
041
042    protected boolean identifier;
043
044    public SupportAttributeDefinition() {}
045
046    @BeanTagAttribute(name = "identifier")
047    public boolean isIdentifier() {
048        return identifier;
049    }
050
051    /**
052     * identifier when true, only the field marked as an identifier will be passed in as a lookup parameter
053     * at most one supportAttribute for each relationship should be defined as identifier="true"
054     */
055    public void setIdentifier(boolean identifier) {
056        this.identifier = identifier;
057    }
058
059    /**
060     * Directly validate simple fields.
061     *
062     * {@inheritDoc}
063     */
064    @Override
065    public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) {
066        completeValidation(rootBusinessObjectClass, otherBusinessObjectClass, new ValidationTrace());
067    }
068
069    /**
070     * Directly validate simple fields
071     *
072     * @see org.kuali.rice.krad.datadictionary.DataDictionaryEntry#completeValidation(org.kuali.rice.krad.datadictionary.validator.ValidationTrace)
073     */
074    @Override
075    public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass,
076            ValidationTrace tracer) {
077        tracer.addBean(this.getClass().getSimpleName(), ValidationTrace.NO_BEAN_ID);
078        try {
079            if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, getSourceName())) {
080                String currentValues[] = {"attribute = " + getSourceName(), "class = " + rootBusinessObjectClass};
081                tracer.createError("Unable to find attribute in class", currentValues);
082            }
083            if (!DataDictionary.isPropertyOf(otherBusinessObjectClass, getTargetName())
084                    && !ExternalizableBusinessObjectUtils.isExternalizableBusinessObjectInterface(
085                    otherBusinessObjectClass)) {
086
087                String currentValues[] = {"attribute = " + getTargetName(), "class = " + otherBusinessObjectClass};
088                tracer.createError("Unable to find attribute in class", currentValues);
089            }
090        } catch (RuntimeException ex) {
091            String currentValues[] = {"Exception = " + ex.getMessage()};
092            tracer.createError("Unable to validate attribute", currentValues);
093            LOG.error( "Exception while validating SupportAttributeDefintion on " + rootBusinessObjectClass + ": " + this, ex);
094        }
095    }
096
097    @Override
098    public String toString() {
099        StringBuilder builder = new StringBuilder();
100        builder.append("SupportAttributeDefinition [identifier=").append(this.identifier).append(", sourceName=")
101                .append(this.sourceName).append(", targetName=").append(this.targetName).append("]");
102        return builder.toString();
103    }
104
105}
106