View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.datadictionary;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
20  
21  /**
22                      The primitiveAttribute element identifies one pair of
23                      corresponding fields in the primary business object and
24                      the related business object.
25  
26                      JSTL: primitiveAttribute is a Map which is accessed by the
27                      sequential key of "0", "1", etc.  Each entry contains the following
28                      keys:
29                          * sourceName (String)
30                          * targetName (String)
31                      The value corresponding to the sourceName key is the attribute name defined
32                      for the primary business object.
33                      The value corresponding to the targetName key is the attribute name for
34                      the object being referenced by objectAttributeName.
35   */
36  public class PrimitiveAttributeDefinition extends DataDictionaryDefinitionBase {
37      private static final long serialVersionUID = -715128943756700821L;
38      
39  	protected String sourceName;
40      protected String targetName;
41  
42      public PrimitiveAttributeDefinition() {}
43  
44  
45      /**
46       * @return sourceName
47       */
48      public String getSourceName() {
49          return sourceName;
50      }
51  
52      /**
53       * sourceName is the name of the POJO property of the business object
54       * 
55       * @throws IllegalArgumentException if the given sourceName is blank
56       */
57      public void setSourceName(String sourceName) {
58          if (StringUtils.isBlank(sourceName)) {
59              throw new IllegalArgumentException("invalid (blank) sourceName");
60          }
61  
62          this.sourceName = sourceName;
63      }
64  
65  
66      /**
67       * @return targetName
68       */
69      public String getTargetName() {
70          return targetName;
71      }
72  
73      /**
74       * targetName is the name of attribute that corresponds to the sourceName in the looked up BO
75       * 
76       * @throws IllegalArgumentException if the given targetName is blank
77       */
78      public void setTargetName(String targetName) {
79          if (StringUtils.isBlank(targetName)) {
80              throw new IllegalArgumentException("invalid (blank) targetName");
81          }
82  
83          this.targetName = targetName;
84      }
85  
86  
87      /**
88       * Directly validate simple fields.
89       * 
90       * @see org.kuali.rice.krad.datadictionary.DataDictionaryDefinition#completeValidation(java.lang.Class, java.lang.Object)
91       */
92      public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) {
93          if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, sourceName)) {
94              throw new AttributeValidationException("unable to find attribute '" + sourceName + "' in relationship class '" + rootBusinessObjectClass + "' (" + "" + ")");
95          }
96          if (!DataDictionary.isPropertyOf(otherBusinessObjectClass, targetName)) {
97              throw new AttributeValidationException("unable to find attribute '" + targetName + "' in related class '" + otherBusinessObjectClass.getName() + "' (" + "" + ")");
98          }
99  
100         Class sourceClass = DataDictionary.getAttributeClass(rootBusinessObjectClass, sourceName);
101         Class targetClass = DataDictionary.getAttributeClass(otherBusinessObjectClass, targetName);
102         if ((null == sourceClass && null != targetClass) || (null != sourceClass && null == targetClass) || !StringUtils.equals(sourceClass.getName(), targetClass.getName())) {            
103         	String sourceClassName = rootBusinessObjectClass.getName();
104             String targetClassName = otherBusinessObjectClass.getName();
105             String sourcePath = sourceClassName + "." + sourceName;
106             String targetPath = targetClassName + "." + targetName;
107             
108             // Just a temp hack to ignore null Person objects
109             if ((sourcePath != null && !StringUtils.contains(sourcePath, ".principalId")) && (targetPath != null && !StringUtils.contains(targetPath, ".principalId"))) {
110             	throw new AttributeValidationException("source attribute '" + sourcePath + "' (" + sourceClass + ") and target attribute '" + targetPath + "' (" + targetClass + ") are of differing types (" + "" + ")");
111             }
112         }
113     }
114 
115 
116     /**
117      * @see java.lang.Object#toString()
118      */
119     @Override
120     public String toString() {
121         return "PrimitiveAttributeDefinition (" + getSourceName()+","+getTargetName()+")";
122     }
123 }