View Javadoc
1   /*
2    * Copyright 2010 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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  
20  import javax.servlet.jsp.JspException;
21  import javax.servlet.jsp.PageContext;
22  import javax.servlet.jsp.tagext.Tag;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.ole.sys.OLEConstants;
26  import org.kuali.ole.sys.businessobject.AccountingLine;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.ole.sys.document.datadictionary.AccountingLineViewMultipleReadOnlyFieldsDefinition;
29  import org.kuali.ole.sys.document.web.renderers.MultipleReadOnlyFieldsRenderer;
30  import org.kuali.rice.kns.service.DataDictionaryService;
31  import org.kuali.rice.kns.util.FieldUtils;
32  import org.kuali.rice.kns.web.ui.Field;
33  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
34  import org.kuali.rice.krad.util.ObjectUtils;
35  
36  /**
37   * Represents multiple fields displaying with their values in a single cell
38   */
39  public class AccountingLineViewMultipleReadOnlyFields extends FieldTableJoiningWithHeader {
40      private AccountingLineViewMultipleReadOnlyFieldsDefinition definition;
41      private List<Field> fields;
42      private static DataDictionaryService dataDictionaryService;
43      
44      /**
45       * Constructs a AccountingLineViewMultipleReadOnlyFields
46       * @param definition data dictionary definition which created this
47       * @param fields the fields to render as read only 
48       * 
49       * KRAD Conversion: Customization of the fields - No use of data dictionary
50       */
51      public AccountingLineViewMultipleReadOnlyFields(AccountingLineViewMultipleReadOnlyFieldsDefinition definition, List<Field> fields) {
52          this.definition = definition;
53          this.fields = fields;
54      }
55  
56      /**
57       * 
58       * @see org.kuali.ole.sys.document.web.TableJoiningWithHeader#createHeaderLabel()
59       */
60      public HeaderLabel createHeaderLabel() {
61          return new LiteralHeaderLabel(OLEConstants.BLANK_SPACE);
62      }
63  
64      /**
65       * Returns the top field name given in the definition
66       * @see org.kuali.ole.sys.document.web.ElementNamable#getName()
67       */
68      public String getName() {
69          return definition.getFieldNames().get(0);
70      }
71  
72      /**
73       * None of the read only fields will be associated with quickfinders, so this method does nothing
74       * @see org.kuali.ole.sys.document.web.RenderableElement#appendFields(java.util.List)
75       */
76      public void appendFields(List<Field> fields) {}
77  
78      /**
79       * There are no input fields here, so no need to set tab indices
80       * @see org.kuali.ole.sys.document.web.RenderableElement#populateWithTabIndexIfRequested(int)
81       */
82      public void populateWithTabIndexIfRequested(int reallyHighIndex) {}
83  
84      /**
85       * @return the fields associated with this Multiple read only fields
86       * 
87       * KRAD Conversion: Gets the fields - No use of data dictionary
88       */
89      public List<Field> getFields() {
90          return fields;
91      }
92  
93      /**
94       * 
95       * @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)
96       * 
97       * KRAD Conversion: Customization of the fields - No use of data dictionary
98       */
99      public void renderElement(PageContext pageContext, Tag parentTag, AccountingLineRenderingContext renderingContext) throws JspException {
100         final org.kuali.rice.krad.datadictionary.BusinessObjectEntry boEntry = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(renderingContext.getAccountingLine().getClass().getName());
101         if (fields != null && !fields.isEmpty()) {
102             for (Field field : fields) {
103                 setShortLabelsForFields(field, boEntry);
104                 setValueForField(field, renderingContext.getAccountingLine());
105                 setInquiryUrlForField(field, renderingContext.getAccountingLine());
106             }
107         }
108         
109         MultipleReadOnlyFieldsRenderer renderer = new MultipleReadOnlyFieldsRenderer();
110         renderer.setFields(getFields());
111         renderer.render(pageContext, parentTag);
112         renderer.clear();
113     }
114     
115     /**
116      * For each field, set the short label, or, failing that, set the label
117      * @param boEntry the business object entry for the accounting line
118      * 
119      * KRAD Conversion: Customization of the fields - Uses data dictionary
120      * 
121      */
122     protected void setShortLabelsForFields(Field field, org.kuali.rice.krad.datadictionary.BusinessObjectEntry boEntry) {
123         final AttributeDefinition propertyDefinition = boEntry.getAttributeDefinition(field.getPropertyName());
124         final String label = (propertyDefinition == null) ? "" : (!StringUtils.isBlank(propertyDefinition.getShortLabel()) ? propertyDefinition.getShortLabel() : propertyDefinition.getLabel());
125         field.setFieldLabel(label);
126     }
127     
128     /**
129      * Sets the value for the field before rendering
130      * @param field the field to set the value of
131      * @param accountingLine the accounting line the field is associated with, which holds the value
132      * 
133      * KRAD Conversion: Setting the property value of the field - No use of data dictionary
134      */
135     protected void setValueForField(Field field, AccountingLine accountingLine) {
136         field.setPropertyValue(ObjectUtils.getPropertyValue(accountingLine, field.getPropertyName()));
137     }
138     
139     /**
140      * Populates the inquiry url on the field if possible
141      * @param field the field to set the inquiry url on
142      * @param accountingLine the accounting line holding values for the field
143      * 
144      * KRAD Conversion: Setting inquiry url for the fields - No use of data dictionary
145      */
146     protected void setInquiryUrlForField(Field field, AccountingLine accountingLine) {
147         if (!StringUtils.isBlank(field.getPropertyValue())) {
148             FieldUtils.setInquiryURL(field, accountingLine, field.getPropertyName());
149         }
150     }
151     
152     /**
153      * @return the implementation of the DataDictionaryService
154      */
155     protected DataDictionaryService getDataDictionaryService() {
156         if (dataDictionaryService == null) {
157             dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
158         }
159         return dataDictionaryService;
160     }
161 
162 }