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.web;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import javax.servlet.jsp.JspException;
22  import javax.servlet.jsp.PageContext;
23  import javax.servlet.jsp.tagext.Tag;
24  
25  import org.kuali.ole.sys.businessobject.AccountingLine;
26  import org.kuali.ole.sys.document.datadictionary.AccountingLineViewOverrideFieldDefinition;
27  import org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation;
28  import org.kuali.ole.sys.document.web.renderers.OverrideFieldRenderer;
29  import org.kuali.rice.kns.util.FieldUtils;
30  import org.kuali.rice.kns.web.ui.Field;
31  import org.kuali.rice.krad.util.ObjectUtils;
32  
33  /**
34   * An override field to be displayed for a field
35   */
36  public class AccountingLineViewOverrideField implements RenderableElement {
37      private AccountingLineViewField parent;
38      private AccountingLineViewOverrideFieldDefinition definition;
39      private Field overrideField;
40      private int arbitrarilyHighIndex;
41      
42      /**
43       * Constructs a AccountingLineViewOverrideField
44       * @param field the owning accounting line view field
45       * @param accountingLineClass the class of the accounting line we're rendering
46       */
47      public AccountingLineViewOverrideField(AccountingLineViewField field, AccountingLineViewOverrideFieldDefinition definition, Class<? extends AccountingLine> accountingLineClass) {
48          this.parent = field;
49          this.definition = definition;
50          overrideField = FieldUtils.getPropertyField(accountingLineClass, definition.getName(), false);
51      }
52  
53      /**
54       * Adds our override field (though not our override needed field - we'll let Struts handle the value population on that
55       * @see org.kuali.ole.sys.document.web.RenderableElement#appendFields(java.util.List)
56       * 
57       * KRAD Conversion: performs adding override field to the fields - No use of data dictionary
58       */
59      public void appendFields(List<Field> fields) {
60          fields.add(overrideField);
61      }
62  
63      /**
64       * This is not an action block
65       * @see org.kuali.ole.sys.document.web.RenderableElement#isActionBlock()
66       */
67      public boolean isActionBlock() {
68          return false;
69      }
70  
71      /**
72       * Empty if our parent AccountingLineViewField is empty
73       * @see org.kuali.ole.sys.document.web.RenderableElement#isEmpty()
74       */
75      public boolean isEmpty() {
76          return parent.isEmpty();
77      }
78  
79      /**
80       * Hidden if our parent AccountingLineViewField is hidden
81       * @see org.kuali.ole.sys.document.web.RenderableElement#isHidden()
82       */
83      public boolean isHidden() {
84          return parent.isHidden();
85      }
86  
87      /**
88       * 
89       * @see org.kuali.ole.sys.document.web.RenderableElement#populateWithTabIndexIfRequested(int[], int)
90       */
91      public void populateWithTabIndexIfRequested(int reallyHighIndex) {
92          arbitrarilyHighIndex = reallyHighIndex;
93      }
94  
95      /**
96       * 
97       * @see org.kuali.ole.sys.document.web.RenderableElement#renderElement(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag, org.kuali.ole.sys.document.web.AccountingLineRenderingContext)
98       */
99      public void renderElement(PageContext pageContext, Tag parentTag, AccountingLineRenderingContext renderingContext) throws JspException {
100         OverrideFieldRenderer renderer = new OverrideFieldRenderer();
101         renderer.setField(overrideField);
102         renderer.setArbitrarilyHighTabIndex(arbitrarilyHighIndex);
103         if (parent.isReadOnly() && definition.isAllowEditDespiteReadOnlyParentWhenAccoutingLineEditable() && renderingContext.isEditableLine()) {
104             renderer.setReadOnly(false);
105         } else {
106             renderer.setReadOnly(parent.isReadOnly());
107         }
108         renderer.setOverrideNeededValue(getOverrideNeededValue(renderingContext.getAccountingLine()));
109         renderer.setAccountingLine(renderingContext.getAccountingLine());
110         renderer.render(pageContext, parentTag);
111         renderer.clear();
112     }
113     
114     /**
115      * Retrieves the value of the override needed value associated with the override field
116      * @param accountingLine the accounting line to get the override needed value from
117      * @return a "Yes" if the override needed value is true, "No" if it is false
118      */
119     protected String getOverrideNeededValue(AccountingLine accountingLine) {
120         String overrideNeededPropertyName = overrideField.getPropertyName()+"Needed";
121         Boolean value = (Boolean)ObjectUtils.getPropertyValue(accountingLine, overrideNeededPropertyName);
122         return value != null && value.booleanValue() ? "Yes" : "No";
123     }
124 
125     /**
126      * Runs a field transformation against all the overrides encapsulated within this field
127      * @param fieldTransformation the field transformation which will utterly change our fields
128      * @param accountingLine the accounting line being rendered
129      * @param unconvertedValues a Map of unconvertedValues
130      */
131     public void transformField(AccountingLineFieldRenderingTransformation fieldTransformation, AccountingLine accountingLine, Map unconvertedValues) {
132         fieldTransformation.transformField(accountingLine, overrideField, definition, unconvertedValues);
133     }
134     
135     /**
136      * Sets the accounting Line Property
137      * @param propertyPrefix the accounting line property
138      */
139     public void setAccountingLineProperty(String propertyPrefix) {
140         overrideField.setPropertyPrefix(propertyPrefix);
141     }
142 }