View Javadoc
1   /**
2    * Copyright 2005-2016 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  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
21  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
22  import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
23  
24  /**
25   * The primitiveAttribute element identifies one pair of
26   * corresponding fields in the primary business object and
27   * the related business object.
28   *
29   * JSTL: primitiveAttribute is a Map which is accessed by the
30   * sequential key of "0", "1", etc.  Each entry contains the following
31   * keys:
32   * sourceName (String)
33   * targetName (String)
34   * The value corresponding to the sourceName key is the attribute name defined
35   * for the primary business object.
36   * The value corresponding to the targetName key is the attribute name for
37   * the object being referenced by objectAttributeName.
38   */
39  @BeanTag(name = "primitiveAttributeDefinition-bean")
40  public class PrimitiveAttributeDefinition extends DataDictionaryDefinitionBase {
41      private static final long serialVersionUID = -715128943756700821L;
42  
43      protected String sourceName;
44      protected String targetName;
45  
46      public PrimitiveAttributeDefinition() {}
47  
48      /**
49       * @return sourceName
50       */
51      @BeanTagAttribute(name = "sourceName")
52      public String getSourceName() {
53          return sourceName;
54      }
55  
56      /**
57       * sourceName is the name of the POJO property of the business object
58       *
59       * @throws IllegalArgumentException if the given sourceName is blank
60       */
61      public void setSourceName(String sourceName) {
62          if (StringUtils.isBlank(sourceName)) {
63              throw new IllegalArgumentException("invalid (blank) sourceName");
64          }
65  
66          this.sourceName = sourceName;
67      }
68  
69      /**
70       * @return targetName
71       */
72      @BeanTagAttribute(name = "targetName")
73      public String getTargetName() {
74          return targetName;
75      }
76  
77      /**
78       * targetName is the name of attribute that corresponds to the sourceName in the looked up BO
79       *
80       * @throws IllegalArgumentException if the given targetName is blank
81       */
82      public void setTargetName(String targetName) {
83          if (StringUtils.isBlank(targetName)) {
84              throw new IllegalArgumentException("invalid (blank) targetName");
85          }
86  
87          this.targetName = targetName;
88      }
89  
90      /**
91       * Directly validate simple fields.
92       *
93       * @see org.kuali.rice.krad.datadictionary.DataDictionaryDefinition#completeValidation(java.lang.Class,
94       *      java.lang.Class)
95       */
96      public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) {
97          if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, sourceName)) {
98              throw new AttributeValidationException("unable to find attribute '"
99                      + sourceName
100                     + "' in relationship class '"
101                     + rootBusinessObjectClass
102                     + "' ("
103                     + ""
104                     + ")");
105         }
106         if (!DataDictionary.isPropertyOf(otherBusinessObjectClass, targetName)) {
107             throw new AttributeValidationException(
108                     "unable to find attribute '" + targetName + "' in related class '" + otherBusinessObjectClass
109                             .getName() + "' (" + "" + ")");
110         }
111 
112         Class sourceClass = DataDictionary.getAttributeClass(rootBusinessObjectClass, sourceName);
113         Class targetClass = DataDictionary.getAttributeClass(otherBusinessObjectClass, targetName);
114         if ((null == sourceClass && null != targetClass) || (null != sourceClass && null == targetClass) || !StringUtils
115                 .equals(sourceClass.getName(), targetClass.getName())) {
116             String sourceClassName = rootBusinessObjectClass.getName();
117             String targetClassName = otherBusinessObjectClass.getName();
118             String sourcePath = sourceClassName + "." + sourceName;
119             String targetPath = targetClassName + "." + targetName;
120 
121             // Just a temp hack to ignore null Person objects
122             if ((sourcePath != null && !StringUtils.contains(sourcePath, ".principalId")) && (targetPath != null
123                     && !StringUtils.contains(targetPath, ".principalId"))) {
124                 throw new AttributeValidationException("source attribute '"
125                         + sourcePath
126                         + "' ("
127                         + sourceClass
128                         + ") and target attribute '"
129                         + targetPath
130                         + "' ("
131                         + targetClass
132                         + ") are of differing types ("
133                         + ""
134                         + ")");
135             }
136         }
137     }
138 
139     /**
140      * Directly validate simple fields
141      *
142      * @see org.kuali.rice.krad.datadictionary.DataDictionaryEntry#completeValidation(org.kuali.rice.krad.datadictionary.validator.ValidationTrace)
143      */
144     public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass,
145             ValidationTrace tracer) {
146         tracer.addBean(this.getClass().getSimpleName(), ValidationTrace.NO_BEAN_ID);
147 
148         try {
149             if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, sourceName)) {
150                 String currentValues[] = {"attribute = " + getSourceName(), "class = " + rootBusinessObjectClass};
151                 tracer.createError("Unable to find attribute on class", currentValues);
152             }
153         } catch (RuntimeException ex) {
154             String currentValues[] = {"attribute = " + getSourceName(), "class = " + rootBusinessObjectClass,
155                     "Exception = " + ex.getMessage()};
156             tracer.createError("Unable to find attribute on class", currentValues);
157         }
158         try {
159             if (!DataDictionary.isPropertyOf(otherBusinessObjectClass, targetName)) {
160                 String currentValues[] = {"attribute = " + getTargetName(), "class = " + otherBusinessObjectClass};
161                 tracer.createError("Unable to find attribute on class", currentValues);
162             }
163         } catch (RuntimeException ex) {
164             String currentValues[] = {"attribute = " + getTargetName(), "class = " + otherBusinessObjectClass,
165                     "Exception = " + ex.getMessage()};
166             tracer.createError("Unable to find attribute on class", currentValues);
167         }
168         try {
169             Class sourceClass = DataDictionary.getAttributeClass(rootBusinessObjectClass, sourceName);
170             Class targetClass = DataDictionary.getAttributeClass(otherBusinessObjectClass, targetName);
171             if ((null == sourceClass && null != targetClass)
172                     || (null != sourceClass && null == targetClass)
173                     || !StringUtils.equals(sourceClass.getName(), targetClass.getName())) {
174                 String sourceClassName = rootBusinessObjectClass.getName();
175                 String targetClassName = otherBusinessObjectClass.getName();
176                 String sourcePath = sourceClassName + "." + sourceName;
177                 String targetPath = targetClassName + "." + targetName;
178 
179                 // Just a temp hack to ignore null Person objects
180                 if ((sourcePath != null && !StringUtils.contains(sourcePath, ".principalId")) && (targetPath != null
181                         && !StringUtils.contains(targetPath, ".principalId"))) {
182                     String currentValues[] = {"source = " + sourcePath + "' (" + sourceClass + ")",
183                             "target = " + targetPath + "' (" + targetClass + ")"};
184                     tracer.createError("Source and target of different types", currentValues);
185                 }
186             }
187         } catch (RuntimeException ex) {
188             String currentValues[] = {"Exception = " + ex.getMessage()};
189             tracer.createError("Unable to validate property", currentValues);
190         }
191     }
192 
193     /**
194      * @see java.lang.Object#toString()
195      */
196     @Override
197     public String toString() {
198         return "PrimitiveAttributeDefinition (" + getSourceName() + "," + getTargetName() + ")";
199     }
200 }