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 020 import javax.servlet.jsp.JspException; 021 import javax.servlet.jsp.PageContext; 022 import javax.servlet.jsp.tagext.Tag; 023 024 import org.kuali.kfs.fp.document.web.struts.VoucherForm; 025 import org.kuali.kfs.sys.context.SpringContext; 026 import org.kuali.kfs.sys.document.datadictionary.AccountingLineViewFieldDefinition; 027 import org.kuali.kfs.sys.document.service.AccountingLineRenderingService; 028 import org.kuali.kfs.sys.document.web.renderers.FieldRenderer; 029 import org.kuali.rice.kns.util.ObjectUtils; 030 import org.kuali.rice.kns.web.ui.Field; 031 032 /** 033 * 034 * This class... 035 */ 036 public class AccountingLineViewDebitCreditAmountField implements RenderableElement, ElementNamable { 037 private Field debitOrCreditField; 038 private AccountingLineViewFieldDefinition definition; 039 private boolean isDebit; 040 private String newLineProperty; 041 private String collectionProperty; 042 private int arbitrarilyHighIndex; 043 044 /** 045 * Constructs a AccountingLineViewDebitOrCreditAmountField 046 * @param debitOrCreditField 047 * @param definition 048 * @param isDebit 049 * @param newLineProperty 050 * @param collectionProperty 051 */ 052 public AccountingLineViewDebitCreditAmountField(Field debitOrCreditField, AccountingLineViewFieldDefinition definition, boolean isDebit, String newLineProperty, String collectionProperty) { 053 this.debitOrCreditField = debitOrCreditField; 054 this.definition = definition; 055 this.isDebit = isDebit; 056 this.newLineProperty = newLineProperty; 057 this.collectionProperty = collectionProperty; 058 } 059 060 /** 061 * @see org.kuali.kfs.sys.document.web.RenderableElement#appendFields(java.util.List) 062 */ 063 public void appendFields(List<Field> fields) { 064 fields.add(debitOrCreditField); 065 } 066 067 /** 068 * This is not an action block 069 * @see org.kuali.kfs.sys.document.web.RenderableElement#isActionBlock() 070 */ 071 public boolean isActionBlock() { 072 return false; 073 } 074 075 /** 076 * This isn't empty 077 * @see org.kuali.kfs.sys.document.web.RenderableElement#isEmpty() 078 */ 079 public boolean isEmpty() { 080 return false; 081 } 082 083 /** 084 * This is not hidden 085 * @see org.kuali.kfs.sys.document.web.RenderableElement#isHidden() 086 */ 087 public boolean isHidden() { 088 return false; 089 } 090 091 /** 092 * @see org.kuali.kfs.sys.document.web.RenderableElement#populateWithTabIndexIfRequested(int) 093 */ 094 public void populateWithTabIndexIfRequested(int reallyHighIndex) { 095 this.arbitrarilyHighIndex = reallyHighIndex; 096 } 097 098 /** 099 * @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) 100 */ 101 public void renderElement(PageContext pageContext, Tag parentTag, AccountingLineRenderingContext renderingContext) throws JspException { 102 if (!renderingContext.isFieldModifyable(debitOrCreditField.getPropertyName())) { 103 debitOrCreditField.setReadOnly(true); 104 } 105 FieldRenderer renderer = SpringContext.getBean(AccountingLineRenderingService.class).getFieldRendererForField(getDebitOrCreditField(), renderingContext.getAccountingLine()); 106 if (renderer != null) { 107 prepareFieldForRendering(getDebitOrCreditField(), (VoucherForm)renderingContext.getForm(), renderingContext.getCurrentLineCount()); 108 renderer.setField(getDebitOrCreditField()); 109 renderer.render(pageContext, parentTag); 110 renderer.clear(); 111 } 112 } 113 114 /** 115 * Sets up the field for rendering by setting the right property name and zeroing out amounts which aren't needed 116 * @param field the field to prepare 117 * @param accountingLine the accounting line being rendered 118 * @param count the count of the current line in the source lines, or null if it's a new line 119 */ 120 protected void prepareFieldForRendering(Field field, VoucherForm form, Integer count) { 121 getDebitOrCreditField().setPropertyPrefix(null); 122 123 // set the right property name 124 if (count == null) { 125 field.setPropertyName(getNewLineProperty()); 126 } else { 127 final String subPropertyName = isDebit ? "debit" : "credit"; 128 field.setPropertyName(getCollectionProperty()+"["+count.toString()+"]."+subPropertyName); 129 } 130 131 // get the value from the form 132 field.setPropertyValue(ObjectUtils.getPropertyValue(form, field.getPropertyName())); 133 } 134 135 /** 136 * Gets the arbitrarilyHighIndex attribute. 137 * @return Returns the arbitrarilyHighIndex. 138 */ 139 public int getArbitrarilyHighIndex() { 140 return arbitrarilyHighIndex; 141 } 142 143 /** 144 * Gets the collectionProperty attribute. 145 * @return Returns the collectionProperty. 146 */ 147 public String getCollectionProperty() { 148 return collectionProperty; 149 } 150 151 /** 152 * Gets the debitOrCreditField attribute. 153 * @return Returns the debitOrCreditField. 154 */ 155 public Field getDebitOrCreditField() { 156 return debitOrCreditField; 157 } 158 159 /** 160 * Gets the definition attribute. 161 * @return Returns the definition. 162 */ 163 public AccountingLineViewFieldDefinition getDefinition() { 164 return definition; 165 } 166 167 /** 168 * Gets the isDebit attribute. 169 * @return Returns the isDebit. 170 */ 171 public boolean isDebit() { 172 return isDebit; 173 } 174 175 /** 176 * Gets the newLineProperty attribute. 177 * @return Returns the newLineProperty. 178 */ 179 public String getNewLineProperty() { 180 return newLineProperty; 181 } 182 183 /** 184 * @see org.kuali.kfs.sys.document.web.ElementNamable#getName() 185 */ 186 public String getName() { 187 return this.getDebitOrCreditField().getPropertyName(); 188 } 189 190 }