001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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:
017: package org.kuali.core.datadictionary;
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.List;
022:
023: import org.apache.commons.lang.StringUtils;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.kuali.core.bo.BusinessObject;
027: import org.kuali.core.datadictionary.exception.AttributeValidationException;
028:
029: /**
030: * A single Relationship definition in the DataDictionary, which contains information concerning which primitive attributes of this
031: * class can be used to retrieve an instance of some related Object instance
032: *
033: *
034: */
035: public class RelationshipDefinition extends
036: DataDictionaryDefinitionBase {
037: // logger
038: private static Log LOG = LogFactory
039: .getLog(RelationshipDefinition.class);
040:
041: private String objectAttributeName;
042: private Class sourceClass;
043: private Class targetClass;
044:
045: private List<PrimitiveAttributeDefinition> primitiveAttributes;
046: private List<SupportAttributeDefinition> supportAttributes;
047:
048: public RelationshipDefinition() {
049: LOG.debug("creating new RelationshipDefinition");
050:
051: primitiveAttributes = new ArrayList<PrimitiveAttributeDefinition>();
052: supportAttributes = new ArrayList<SupportAttributeDefinition>();
053: }
054:
055: public String getObjectAttributeName() {
056: return objectAttributeName;
057: }
058:
059: public Class getSourceClass() {
060: return sourceClass;
061: }
062:
063: public Class getTargetClass() {
064: return targetClass;
065: }
066:
067: public void setObjectAttributeName(String objectAttributeName) {
068: if (StringUtils.isBlank(objectAttributeName)) {
069: throw new IllegalArgumentException(
070: "invalid (blank) objectAttributeName");
071: }
072: if (LOG.isDebugEnabled()) {
073: LOG.debug("calling setObjectAttributeName '"
074: + objectAttributeName + "'");
075: }
076:
077: this .objectAttributeName = objectAttributeName;
078: }
079:
080: public void addPrimitiveAttributeDefinition(
081: PrimitiveAttributeDefinition primitiveAttributeDefinition) {
082: if (primitiveAttributeDefinition == null) {
083: throw new IllegalArgumentException(
084: "invalid (null) primitiveAttributeDefinition");
085: }
086:
087: primitiveAttributes.add(primitiveAttributeDefinition);
088: }
089:
090: public void addSupportAttributeDefinition(
091: SupportAttributeDefinition supportAttributeDefinition) {
092: if (supportAttributeDefinition == null) {
093: throw new IllegalArgumentException(
094: "invalid (null) supportAttributeDefinition");
095: }
096:
097: supportAttributes.add(supportAttributeDefinition);
098: }
099:
100: public List<PrimitiveAttributeDefinition> getPrimitiveAttributes() {
101: return Collections.unmodifiableList(primitiveAttributes);
102: }
103:
104: public List<SupportAttributeDefinition> getSupportAttributes() {
105: return supportAttributes;
106: }
107:
108: public boolean hasIdentifier() {
109: for (SupportAttributeDefinition supportAttributeDefinition : supportAttributes) {
110: if (supportAttributeDefinition.isIdentifier()) {
111: return true;
112: }
113: }
114: return false;
115: }
116:
117: public SupportAttributeDefinition getIdentifier() {
118: for (SupportAttributeDefinition supportAttributeDefinition : supportAttributes) {
119: if (supportAttributeDefinition.isIdentifier()) {
120: return supportAttributeDefinition;
121: }
122: }
123: return null;
124: }
125:
126: /**
127: * Directly validate simple fields, call completeValidation on Definition fields.
128: *
129: * @see org.kuali.core.datadictionary.DataDictionaryEntry#completeValidation()
130: */
131: public void completeValidation(Class rootBusinessObjectClass,
132: Class otherBusinessObjectClass,
133: ValidationCompletionUtils validationCompletionUtils) {
134: String propertyName = objectAttributeName;
135: if (!validationCompletionUtils.isPropertyOf(
136: rootBusinessObjectClass, propertyName)) {
137: throw new AttributeValidationException("property '"
138: + propertyName + "' is not an attribute of class '"
139: + rootBusinessObjectClass + "' ("
140: + getParseLocation() + ")");
141: }
142: Class propertyClass = validationCompletionUtils
143: .getAttributeClass(rootBusinessObjectClass,
144: propertyName);
145: if (!validationCompletionUtils.isDescendentClass(propertyClass,
146: BusinessObject.class)) {
147: throw new AttributeValidationException("property '"
148: + propertyName + "' is not a BusinessObject ("
149: + getParseLocation() + ")");
150: }
151:
152: sourceClass = rootBusinessObjectClass;
153: targetClass = propertyClass;
154:
155: for (PrimitiveAttributeDefinition primitiveAttributeDefinition : primitiveAttributes) {
156: primitiveAttributeDefinition.completeValidation(
157: rootBusinessObjectClass, propertyClass,
158: validationCompletionUtils);
159: }
160: for (SupportAttributeDefinition supportAttributeDefinition : supportAttributes) {
161: supportAttributeDefinition.completeValidation(
162: rootBusinessObjectClass, propertyClass,
163: validationCompletionUtils);
164: }
165: }
166:
167: /**
168: * @see java.lang.Object#toString()
169: */
170: @Override
171: public String toString() {
172: return "RelationshipDefinition for relationship "
173: + getObjectAttributeName();
174: }
175: }
|