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