View Javadoc

1   /**
2    * Copyright 2005-2012 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.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
19  import org.kuali.rice.krad.datadictionary.validator.ErrorReport;
20  import org.kuali.rice.krad.datadictionary.validator.TracerToken;
21  import org.kuali.rice.krad.util.ExternalizableBusinessObjectUtils;
22  
23  import java.util.ArrayList;
24  
25  /**
26      Support attributes define additional attributes that can be used to generate
27      lookup field conversions and lookup parameters.
28  
29      Field conversions and lookup parameters are normally generated using foreign key relationships
30      defined within OJB and the DD.  Because Person objects are linked in a special way (i.e. they may
31      come from an external data source and not from the DB, such as LDAP), it is often necessary to define
32      extra fields that are related to each other, sort of like a supplemental foreign key.
33  
34      sourceName is the name of the POJO property of the business object
35      targetName is the name of attribute that corresponds to the sourceName in the looked up BO
36      identifier when true, only the field marked as an identifier will be passed in as a lookup parameter
37                 at most one supportAttribute for each relationship should be defined as identifier="true"
38   */
39  public class SupportAttributeDefinition extends PrimitiveAttributeDefinition {
40      private static final long serialVersionUID = -1719022365280776405L;
41      
42  	protected boolean identifier;
43      
44      public SupportAttributeDefinition() {}
45  
46      public boolean isIdentifier() {
47          return identifier;
48      }
49      
50      /**
51       * identifier when true, only the field marked as an identifier will be passed in as a lookup parameter
52                 at most one supportAttribute for each relationship should be defined as identifier="true"
53       */
54      public void setIdentifier(boolean identifier) {
55          this.identifier = identifier;
56      }
57  
58      /**
59       * Directly validate simple fields.
60       * 
61       * @see org.kuali.rice.krad.datadictionary.DataDictionaryDefinition#completeValidation(java.lang.Class, java.lang.Object)
62       */
63      public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) {
64          if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, getSourceName())) {
65              throw new AttributeValidationException("unable to find attribute '" + getSourceName() + "' in relationship class '" + rootBusinessObjectClass + "' (" + "" + ")");
66          }
67          if (!DataDictionary.isPropertyOf(otherBusinessObjectClass, getTargetName())
68          		&& !ExternalizableBusinessObjectUtils.isExternalizableBusinessObjectInterface( otherBusinessObjectClass )) {
69              throw new AttributeValidationException("unable to find attribute '" + getTargetName() + "' in related class '" + otherBusinessObjectClass.getName() + "' (" + "" + ")");
70          }
71      }
72  
73      /**
74       * Directly validate simple fields
75       *
76       * @see org.kuali.rice.krad.datadictionary.DataDictionaryEntry#completeValidation(TracerToken)
77       */
78      public ArrayList<ErrorReport> completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass, TracerToken tracer) {
79          ArrayList<ErrorReport> reports = new ArrayList<ErrorReport>();
80          tracer.addBean(this.getClass().getSimpleName(),TracerToken.NO_BEAN_ID);
81  
82          try{
83              if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, getSourceName())) {
84                  ErrorReport error = ErrorReport.createError("Unable to find attribute in class", tracer);
85                  error.addCurrentValue("attribute = "+getSourceName());
86                  error.addCurrentValue("class = "+rootBusinessObjectClass);
87                  reports.add(error);
88              }
89              if (!DataDictionary.isPropertyOf(otherBusinessObjectClass, getTargetName())
90                      && !ExternalizableBusinessObjectUtils.isExternalizableBusinessObjectInterface( otherBusinessObjectClass )) {
91                  ErrorReport error = ErrorReport.createError("Unable to find attribute in class", tracer);
92                  error.addCurrentValue("attribute = "+getTargetName());
93                  error.addCurrentValue("class = "+otherBusinessObjectClass);
94                  reports.add(error);
95              }
96          }catch (RuntimeException ex) {
97              ErrorReport error = ErrorReport.createError("Unable to validate attribute",tracer);
98              error.addCurrentValue("Exception = "+ex.getMessage());
99              reports.add(error);
100         }
101 
102 
103         return reports;
104     }
105     
106     /**
107      * @see java.lang.Object#toString()
108      */
109     @Override
110     public String toString() {
111         return "SupportAttributeDefinition (" + getSourceName()+","+getTargetName()+","+identifier+")";
112     }
113     
114 }
115