001/*
002 * Copyright 2008 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 */
016package org.kuali.ole.sys.document.web;
017
018import java.util.List;
019import java.util.Map;
020
021import javax.servlet.jsp.JspException;
022import javax.servlet.jsp.PageContext;
023import javax.servlet.jsp.tagext.Tag;
024
025import org.kuali.ole.sys.businessobject.AccountingLine;
026import org.kuali.ole.sys.document.datadictionary.AccountingLineViewOverrideFieldDefinition;
027import org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation;
028import org.kuali.ole.sys.document.web.renderers.OverrideFieldRenderer;
029import org.kuali.rice.kns.util.FieldUtils;
030import org.kuali.rice.kns.web.ui.Field;
031import org.kuali.rice.krad.util.ObjectUtils;
032
033/**
034 * An override field to be displayed for a field
035 */
036public class AccountingLineViewOverrideField implements RenderableElement {
037    private AccountingLineViewField parent;
038    private AccountingLineViewOverrideFieldDefinition definition;
039    private Field overrideField;
040    private int arbitrarilyHighIndex;
041    
042    /**
043     * Constructs a AccountingLineViewOverrideField
044     * @param field the owning accounting line view field
045     * @param accountingLineClass the class of the accounting line we're rendering
046     */
047    public AccountingLineViewOverrideField(AccountingLineViewField field, AccountingLineViewOverrideFieldDefinition definition, Class<? extends AccountingLine> accountingLineClass) {
048        this.parent = field;
049        this.definition = definition;
050        overrideField = FieldUtils.getPropertyField(accountingLineClass, definition.getName(), false);
051    }
052
053    /**
054     * Adds our override field (though not our override needed field - we'll let Struts handle the value population on that
055     * @see org.kuali.ole.sys.document.web.RenderableElement#appendFields(java.util.List)
056     * 
057     * KRAD Conversion: performs adding override field to the fields - No use of data dictionary
058     */
059    public void appendFields(List<Field> fields) {
060        fields.add(overrideField);
061    }
062
063    /**
064     * This is not an action block
065     * @see org.kuali.ole.sys.document.web.RenderableElement#isActionBlock()
066     */
067    public boolean isActionBlock() {
068        return false;
069    }
070
071    /**
072     * Empty if our parent AccountingLineViewField is empty
073     * @see org.kuali.ole.sys.document.web.RenderableElement#isEmpty()
074     */
075    public boolean isEmpty() {
076        return parent.isEmpty();
077    }
078
079    /**
080     * Hidden if our parent AccountingLineViewField is hidden
081     * @see org.kuali.ole.sys.document.web.RenderableElement#isHidden()
082     */
083    public boolean isHidden() {
084        return parent.isHidden();
085    }
086
087    /**
088     * 
089     * @see org.kuali.ole.sys.document.web.RenderableElement#populateWithTabIndexIfRequested(int[], int)
090     */
091    public void populateWithTabIndexIfRequested(int reallyHighIndex) {
092        arbitrarilyHighIndex = reallyHighIndex;
093    }
094
095    /**
096     * 
097     * @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)
098     */
099    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}