View Javadoc
1   /*
2    * Copyright 2008 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.ole.sys.document.datadictionary;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.sys.businessobject.AccountingLine;
20  import org.kuali.ole.sys.context.SpringContext;
21  import org.kuali.ole.sys.document.service.AccountingLineRenderingService;
22  import org.kuali.ole.sys.document.web.AccountingLineViewCurrentBaseAmount;
23  import org.kuali.ole.sys.document.web.TableJoining;
24  import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
25  import org.kuali.rice.kns.service.DataDictionaryService;
26  import org.kuali.rice.kns.util.FieldUtils;
27  import org.kuali.rice.kns.web.ui.Field;
28  import org.kuali.rice.kns.web.ui.FieldBridge;
29  import org.kuali.rice.krad.datadictionary.DataDictionary;
30  import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
31  
32  /**
33   * The definition for an amount field which reports both current and base amounts
34   */
35  public class AccountingLineViewCurrentBaseAmountFieldDefinition extends MaintainableFieldDefinition implements AccountingLineViewRenderableElementDefinition {
36      private String currentAmountPropertyName;
37      private String baseAmountPropertyName;
38      private boolean useShortLabels = false;
39      
40      /**
41       * Creates a property initiated AccountingLineViewCurrentBaseAmount element
42       * @see org.kuali.ole.sys.document.datadictionary.AccountingLineViewRenderableElementDefinition#createLayoutElement(java.lang.Class)
43       */
44      public TableJoining createLayoutElement(Class<? extends AccountingLine> accountingLineClass) {
45          AccountingLineViewCurrentBaseAmount layoutElement = new AccountingLineViewCurrentBaseAmount();
46          
47          layoutElement.setBaseAmountField(createFieldForPropertyName(baseAmountPropertyName, accountingLineClass));
48          layoutElement.setBaseAmountFieldDefinition(createFieldDefinitionForProperty(baseAmountPropertyName));
49          
50          layoutElement.setCurrentAmountField(createFieldForPropertyName(currentAmountPropertyName, accountingLineClass));
51          layoutElement.setCurrentAmountFieldDefinition(createFieldDefinitionForProperty(currentAmountPropertyName));
52          
53          layoutElement.setDefinition(this);
54          
55          return layoutElement;
56      }
57      
58      /**
59       * Creates a field for the given AccountingLine class and property name
60       * @param propertyName the name of the property to create a Field for
61       * @param accountingLineClass the Class of the AccountingLine we're planning on rendering
62       * @return an appropriately created Field
63       */
64      protected Field createFieldForPropertyName(String propertyName, Class<? extends AccountingLine> accountingLineClass) {
65          Field realField = FieldUtils.getPropertyField(accountingLineClass, propertyName, false);
66          FieldBridge.setupField(realField, this, null);
67          if (useShortLabels) {
68              org.kuali.rice.krad.datadictionary.BusinessObjectEntry boEntry = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getBusinessObjectEntry(accountingLineClass.getName());
69              realField.setFieldLabel(boEntry.getAttributeDefinition(propertyName).getShortLabel());
70          }
71          return realField;
72      }
73      
74      /**
75       * Creates an AccountingLineViewFieldDefinition for the given property name
76       * @param propertyName the name of the field property that we're creating a definition for
77       * @return an appropriately created AccountingLineViewFieldDefinition
78       */
79      protected AccountingLineViewFieldDefinition createFieldDefinitionForProperty(String propertyName) {
80          AccountingLineViewFieldDefinition fieldDefinition = SpringContext.getBean(AccountingLineRenderingService.class).createGenericAccountingLineViewFieldDefinition(this);
81          fieldDefinition.setName(propertyName);
82          return fieldDefinition;
83      }
84      
85      /**
86       * @see org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition#completeValidation(java.lang.Class, java.lang.Class)
87       */
88      @Override
89      public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) {
90          if (StringUtils.isBlank(currentAmountPropertyName)) {
91              throw new AttributeValidationException("The currentAmountPropertyName property must be specified on the AccountingLineView-CurentBaseAmountField definition for "+rootBusinessObjectClass.getName());
92          }
93          
94          if (StringUtils.isBlank(baseAmountPropertyName)) {
95              throw new AttributeValidationException("The baseAmountPropertyName property must be specified on the AccountingLineView-CurentBaseAmountField definition for "+rootBusinessObjectClass.getName());
96          }
97          
98          if (!StringUtils.isBlank(getName())) {
99              throw new AttributeValidationException("please do not specify name on the AccountingLineView-CurentBaseAmountField definition for "+rootBusinessObjectClass.getName());
100         }
101         
102         if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, getCurrentAmountPropertyName())) {
103             throw new AttributeValidationException("unable to find attribute or collection named '" + getCurrentAmountPropertyName() + "' in rootBusinessObjectClass '" + rootBusinessObjectClass.getName() + "' (" + "" + ")");
104         }
105 
106         if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, getBaseAmountPropertyName())) {
107             throw new AttributeValidationException("unable to find attribute or collection named '" + getBaseAmountPropertyName() + "' in rootBusinessObjectClass '" + rootBusinessObjectClass.getName() + "' (" + "" + ")");
108         }
109         
110         if (defaultValueFinderClass != null && defaultValue != null) {
111             throw new AttributeValidationException("Both defaultValue and defaultValueFinderClass can not be specified on attribute " + getName() + " in rootBusinessObjectClass " + rootBusinessObjectClass.getName());
112         }
113     }
114 
115     /**
116      * Gets the baseAmountPropertyName attribute. 
117      * @return Returns the baseAmountPropertyName.
118      */
119     public String getBaseAmountPropertyName() {
120         return baseAmountPropertyName;
121     }
122 
123     /**
124      * Sets the baseAmountPropertyName attribute value.
125      * @param baseAmountPropertyName The baseAmountPropertyName to set.
126      */
127     public void setBaseAmountPropertyName(String baseAmountPropertyName) {
128         this.baseAmountPropertyName = baseAmountPropertyName;
129     }
130 
131     /**
132      * Gets the currentAmountPropertyName attribute. 
133      * @return Returns the currentAmountPropertyName.
134      */
135     public String getCurrentAmountPropertyName() {
136         return currentAmountPropertyName;
137     }
138 
139     /**
140      * Sets the currentAmountPropertyName attribute value.
141      * @param currentAmountPropertyName The currentAmountPropertyName to set.
142      */
143     public void setCurrentAmountPropertyName(String currentAmountPropertyName) {
144         this.currentAmountPropertyName = currentAmountPropertyName;
145     }
146 
147     /**
148      * Gets the useShortLabels attribute. 
149      * @return Returns the useShortLabels.
150      */
151     public boolean isUseShortLabels() {
152         return useShortLabels;
153     }
154 
155     /**
156      * Sets the useShortLabels attribute value.
157      * @param useShortLabels The useShortLabels to set.
158      */
159     public void setUseShortLabels(boolean useShortLabels) {
160         this.useShortLabels = useShortLabels;
161     }
162     
163 }