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 org.apache.commons.lang.StringUtils;
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.kuali.core.datadictionary.exception.AttributeValidationException;
023:
024: /**
025: * Contains field-related information for DataDictionary entries.
026: *
027: * Note: the setters do copious amounts of validation, to facilitate generating errors during the parsing process.
028: *
029: *
030: */
031: public class PrimitiveAttributeDefinition extends
032: DataDictionaryDefinitionBase {
033: // logger
034: private static Log LOG = LogFactory
035: .getLog(PrimitiveAttributeDefinition.class);
036:
037: private String sourceName;
038: private String targetName;
039:
040: public PrimitiveAttributeDefinition() {
041: LOG.debug("creating new PrimitiveAttributeDefinition");
042: }
043:
044: /**
045: * @return sourceName
046: */
047: public String getSourceName() {
048: return sourceName;
049: }
050:
051: /**
052: * Sets sourceName to the given value.
053: *
054: * @param sourceName
055: * @throws IllegalArgumentException if the given sourceName is blank
056: */
057: public void setSourceName(String sourceName) {
058: if (StringUtils.isBlank(sourceName)) {
059: throw new IllegalArgumentException(
060: "invalid (blank) sourceName");
061: }
062: if (LOG.isDebugEnabled()) {
063: LOG.debug("calling setSourceName '" + sourceName + "'");
064: }
065:
066: this .sourceName = sourceName;
067: }
068:
069: /**
070: * @return targetName
071: */
072: public String getTargetName() {
073: return targetName;
074: }
075:
076: /**
077: * Sets targetName to the given value.
078: *
079: * @param targetName
080: * @throws IllegalArgumentException if the given targetName is blank
081: */
082: public void setTargetName(String targetName) {
083: if (StringUtils.isBlank(targetName)) {
084: throw new IllegalArgumentException(
085: "invalid (blank) targetName");
086: }
087: if (LOG.isDebugEnabled()) {
088: LOG.debug("calling setTargetName '" + targetName + "'");
089: }
090:
091: this .targetName = targetName;
092: }
093:
094: /**
095: * Directly validate simple fields.
096: *
097: * @see org.kuali.core.datadictionary.DataDictionaryDefinition#completeValidation(java.lang.Class, java.lang.Object)
098: */
099: public void completeValidation(Class rootBusinessObjectClass,
100: Class otherBusinessObjectClass,
101: ValidationCompletionUtils validationCompletionUtils) {
102: String sourceClassName = rootBusinessObjectClass.getName();
103: if (!validationCompletionUtils.isPropertyOf(
104: rootBusinessObjectClass, sourceName)) {
105: throw new AttributeValidationException(
106: "unable to find attribute '" + sourceName
107: + "' in relationship class '"
108: + rootBusinessObjectClass + "' ("
109: + getParseLocation() + ")");
110: }
111: String targetClassName = otherBusinessObjectClass.getName();
112: if (!validationCompletionUtils.isPropertyOf(
113: otherBusinessObjectClass, targetName)) {
114: throw new AttributeValidationException(
115: "unable to find attribute '" + targetName
116: + "' in related class '"
117: + otherBusinessObjectClass.getName()
118: + "' (" + getParseLocation() + ")");
119: }
120:
121: Class sourceClass = validationCompletionUtils
122: .getAttributeClass(rootBusinessObjectClass, sourceName);
123: Class targetClass = validationCompletionUtils
124: .getAttributeClass(otherBusinessObjectClass, targetName);
125: if ((null == sourceClass && null != targetClass)
126: || (null != sourceClass && null == targetClass)
127: || !StringUtils.equals(sourceClass.getName(),
128: targetClass.getName())) {
129: String sourcePath = sourceClassName + "." + sourceName;
130: String targetPath = targetClassName + "." + targetName;
131:
132: throw new AttributeValidationException("source attribute '"
133: + sourcePath + "' (" + sourceClass
134: + ") and target attribute '" + targetPath + "' ("
135: + targetClass + ") are of differing types ("
136: + getParseLocation() + ")");
137: }
138: }
139:
140: /**
141: * @see java.lang.Object#toString()
142: */
143: @Override
144: public String toString() {
145: return "PrimitiveAttributeDefinition (" + getSourceName() + ","
146: + getTargetName() + ")";
147: }
148: }
|