001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.sys.document.web;
017    
018    import java.util.List;
019    import java.util.Map;
020    import java.util.Set;
021    
022    import javax.servlet.jsp.JspException;
023    import javax.servlet.jsp.PageContext;
024    import javax.servlet.jsp.tagext.Tag;
025    
026    import org.kuali.kfs.sys.businessobject.AccountingLine;
027    import org.kuali.kfs.sys.document.datadictionary.AccountingLineViewOverrideFieldDefinition;
028    import org.kuali.kfs.sys.document.service.AccountingLineFieldRenderingTransformation;
029    import org.kuali.kfs.sys.document.web.renderers.OverrideFieldRenderer;
030    import org.kuali.rice.kns.util.FieldUtils;
031    import org.kuali.rice.kns.util.ObjectUtils;
032    import org.kuali.rice.kns.web.ui.Field;
033    
034    /**
035     * An override field to be displayed for a field
036     */
037    public class AccountingLineViewOverrideField implements RenderableElement {
038        private AccountingLineViewField parent;
039        private AccountingLineViewOverrideFieldDefinition definition;
040        private Field overrideField;
041        private int arbitrarilyHighIndex;
042        
043        /**
044         * Constructs a AccountingLineViewOverrideField
045         * @param field the owning accounting line view field
046         * @param accountingLineClass the class of the accounting line we're rendering
047         */
048        public AccountingLineViewOverrideField(AccountingLineViewField field, AccountingLineViewOverrideFieldDefinition definition, Class<? extends AccountingLine> accountingLineClass) {
049            this.parent = field;
050            this.definition = definition;
051            overrideField = FieldUtils.getPropertyField(accountingLineClass, definition.getName(), false);
052        }
053    
054        /**
055         * Adds our override field (though not our override needed field - we'll let Struts handle the value population on that
056         * @see org.kuali.kfs.sys.document.web.RenderableElement#appendFields(java.util.List)
057         */
058        public void appendFields(List<Field> fields) {
059            fields.add(overrideField);
060        }
061    
062        /**
063         * This is not an action block
064         * @see org.kuali.kfs.sys.document.web.RenderableElement#isActionBlock()
065         */
066        public boolean isActionBlock() {
067            return false;
068        }
069    
070        /**
071         * Empty if our parent AccountingLineViewField is empty
072         * @see org.kuali.kfs.sys.document.web.RenderableElement#isEmpty()
073         */
074        public boolean isEmpty() {
075            return parent.isEmpty();
076        }
077    
078        /**
079         * Hidden if our parent AccountingLineViewField is hidden
080         * @see org.kuali.kfs.sys.document.web.RenderableElement#isHidden()
081         */
082        public boolean isHidden() {
083            return parent.isHidden();
084        }
085    
086        /**
087         * 
088         * @see org.kuali.kfs.sys.document.web.RenderableElement#populateWithTabIndexIfRequested(int[], int)
089         */
090        public void populateWithTabIndexIfRequested(int reallyHighIndex) {
091            arbitrarilyHighIndex = reallyHighIndex;
092        }
093    
094        /**
095         * 
096         * @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)
097         */
098        public void renderElement(PageContext pageContext, Tag parentTag, AccountingLineRenderingContext renderingContext) throws JspException {
099            OverrideFieldRenderer renderer = new OverrideFieldRenderer();
100            renderer.setField(overrideField);
101            renderer.setArbitrarilyHighTabIndex(arbitrarilyHighIndex);
102            if (parent.isReadOnly() && definition.isAllowEditDespiteReadOnlyParentWhenAccoutingLineEditable() && renderingContext.isEditableLine()) {
103                renderer.setReadOnly(false);
104            } else {
105                renderer.setReadOnly(parent.isReadOnly());
106            }
107            renderer.setOverrideNeededValue(getOverrideNeededValue(renderingContext.getAccountingLine()));
108            renderer.setAccountingLine(renderingContext.getAccountingLine());
109            renderer.render(pageContext, parentTag);
110            renderer.clear();
111        }
112        
113        /**
114         * Retrieves the value of the override needed value associated with the override field
115         * @param accountingLine the accounting line to get the override needed value from
116         * @return a "Yes" if the override needed value is true, "No" if it is false
117         */
118        protected String getOverrideNeededValue(AccountingLine accountingLine) {
119            String overrideNeededPropertyName = overrideField.getPropertyName()+"Needed";
120            Boolean value = (Boolean)ObjectUtils.getPropertyValue(accountingLine, overrideNeededPropertyName);
121            return value != null && value.booleanValue() ? "Yes" : "No";
122        }
123    
124        /**
125         * Runs a field transformation against all the overrides encapsulated within this field
126         * @param fieldTransformation the field transformation which will utterly change our fields
127         * @param accountingLine the accounting line being rendered
128         * @param unconvertedValues a Map of unconvertedValues
129         */
130        public void transformField(AccountingLineFieldRenderingTransformation fieldTransformation, AccountingLine accountingLine, Map unconvertedValues) {
131            fieldTransformation.transformField(accountingLine, overrideField, definition, unconvertedValues);
132        }
133        
134        /**
135         * Sets the accounting Line Property
136         * @param propertyPrefix the accounting line property
137         */
138        public void setAccountingLineProperty(String propertyPrefix) {
139            overrideField.setPropertyPrefix(propertyPrefix);
140        }
141    }