View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.sys.document.web;
20  
21  import java.util.List;
22  
23  import javax.servlet.jsp.JspException;
24  import javax.servlet.jsp.PageContext;
25  import javax.servlet.jsp.tagext.Tag;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.kuali.kfs.sys.KFSConstants;
29  import org.kuali.kfs.sys.businessobject.AccountingLine;
30  import org.kuali.kfs.sys.context.SpringContext;
31  import org.kuali.kfs.sys.document.datadictionary.AccountingLineViewMultipleReadOnlyFieldsDefinition;
32  import org.kuali.kfs.sys.document.web.renderers.MultipleReadOnlyFieldsRenderer;
33  import org.kuali.rice.kns.service.DataDictionaryService;
34  import org.kuali.rice.kns.util.FieldUtils;
35  import org.kuali.rice.kns.web.ui.Field;
36  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
37  import org.kuali.rice.krad.util.ObjectUtils;
38  
39  /**
40   * Represents multiple fields displaying with their values in a single cell
41   */
42  public class AccountingLineViewMultipleReadOnlyFields extends FieldTableJoiningWithHeader {
43      private AccountingLineViewMultipleReadOnlyFieldsDefinition definition;
44      private List<Field> fields;
45      private static DataDictionaryService dataDictionaryService;
46      
47      /**
48       * Constructs a AccountingLineViewMultipleReadOnlyFields
49       * @param definition data dictionary definition which created this
50       * @param fields the fields to render as read only 
51       * 
52       * KRAD Conversion: Customization of the fields - No use of data dictionary
53       */
54      public AccountingLineViewMultipleReadOnlyFields(AccountingLineViewMultipleReadOnlyFieldsDefinition definition, List<Field> fields) {
55          this.definition = definition;
56          this.fields = fields;
57      }
58  
59      /**
60       * 
61       * @see org.kuali.kfs.sys.document.web.TableJoiningWithHeader#createHeaderLabel()
62       */
63      public HeaderLabel createHeaderLabel() {
64          return new LiteralHeaderLabel(KFSConstants.BLANK_SPACE);
65      }
66  
67      /**
68       * Returns the top field name given in the definition
69       * @see org.kuali.kfs.sys.document.web.ElementNamable#getName()
70       */
71      public String getName() {
72          return definition.getFieldNames().get(0);
73      }
74  
75      /**
76       * None of the read only fields will be associated with quickfinders, so this method does nothing
77       * @see org.kuali.kfs.sys.document.web.RenderableElement#appendFields(java.util.List)
78       */
79      public void appendFields(List<Field> fields) {}
80  
81      /**
82       * There are no input fields here, so no need to set tab indices
83       * @see org.kuali.kfs.sys.document.web.RenderableElement#populateWithTabIndexIfRequested(int)
84       */
85      public void populateWithTabIndexIfRequested(int reallyHighIndex) {}
86  
87      /**
88       * @return the fields associated with this Multiple read only fields
89       * 
90       * KRAD Conversion: Gets the fields - No use of data dictionary
91       */
92      public List<Field> getFields() {
93          return fields;
94      }
95  
96      /**
97       * 
98       * @see org.kuali.kfs.sys.document.web.RenderableElement#renderElement(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag, org.kuali.kfs.sys.document.web.AccountingLineRenderingContext)
99       * 
100      * KRAD Conversion: Customization of the fields - No use of data dictionary
101      */
102     public void renderElement(PageContext pageContext, Tag parentTag, AccountingLineRenderingContext renderingContext) throws JspException {
103         final org.kuali.rice.krad.datadictionary.BusinessObjectEntry boEntry = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(renderingContext.getAccountingLine().getClass().getName());
104         if (fields != null && !fields.isEmpty()) {
105             for (Field field : fields) {
106                 setShortLabelsForFields(field, boEntry);
107                 setValueForField(field, renderingContext.getAccountingLine());
108                 setInquiryUrlForField(field, renderingContext.getAccountingLine());
109             }
110         }
111         
112         MultipleReadOnlyFieldsRenderer renderer = new MultipleReadOnlyFieldsRenderer();
113         renderer.setFields(getFields());
114         renderer.render(pageContext, parentTag);
115         renderer.clear();
116     }
117     
118     /**
119      * For each field, set the short label, or, failing that, set the label
120      * @param boEntry the business object entry for the accounting line
121      * 
122      * KRAD Conversion: Customization of the fields - Uses data dictionary
123      * 
124      */
125     protected void setShortLabelsForFields(Field field, org.kuali.rice.krad.datadictionary.BusinessObjectEntry boEntry) {
126         final AttributeDefinition propertyDefinition = boEntry.getAttributeDefinition(field.getPropertyName());
127         final String label = (propertyDefinition == null) ? "" : (!StringUtils.isBlank(propertyDefinition.getShortLabel()) ? propertyDefinition.getShortLabel() : propertyDefinition.getLabel());
128         field.setFieldLabel(label);
129     }
130     
131     /**
132      * Sets the value for the field before rendering
133      * @param field the field to set the value of
134      * @param accountingLine the accounting line the field is associated with, which holds the value
135      * 
136      * KRAD Conversion: Setting the property value of the field - No use of data dictionary
137      */
138     protected void setValueForField(Field field, AccountingLine accountingLine) {
139         field.setPropertyValue(ObjectUtils.getPropertyValue(accountingLine, field.getPropertyName()));
140     }
141     
142     /**
143      * Populates the inquiry url on the field if possible
144      * @param field the field to set the inquiry url on
145      * @param accountingLine the accounting line holding values for the field
146      * 
147      * KRAD Conversion: Setting inquiry url for the fields - No use of data dictionary
148      */
149     protected void setInquiryUrlForField(Field field, AccountingLine accountingLine) {
150         if (!StringUtils.isBlank(field.getPropertyValue())) {
151             FieldUtils.setInquiryURL(field, accountingLine, field.getPropertyName());
152         }
153     }
154     
155     /**
156      * @return the implementation of the DataDictionaryService
157      */
158     protected DataDictionaryService getDataDictionaryService() {
159         if (dataDictionaryService == null) {
160             dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
161         }
162         return dataDictionaryService;
163     }
164 
165 }