1 /* 2 * Copyright 2005-2006 The Kuali Foundation 3 * 4 * Licensed under the Educational Community License, Version 2.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/ecl2.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.fp.document.web.struts; 17 18 import java.util.ArrayList; 19 import java.util.List; 20 import java.util.Map; 21 22 import javax.servlet.http.HttpServletRequest; 23 24 import org.apache.commons.lang.StringUtils; 25 import org.kuali.ole.coa.businessobject.BalanceType; 26 import org.kuali.ole.coa.service.BalanceTypeService; 27 import org.kuali.ole.fp.document.JournalVoucherDocument; 28 import org.kuali.ole.sys.OLEConstants; 29 import org.kuali.ole.sys.businessobject.SourceAccountingLine; 30 import org.kuali.ole.sys.context.SpringContext; 31 import org.kuali.rice.krad.util.ObjectUtils; 32 33 /** 34 * This class is the Struts specific form object that works in conjunction with the pojo utilities to build the UI for the Journal 35 * Voucher Document. This class is unique in that it leverages a helper data structure called the VoucherAccountingLineHelper 36 * because the Journal Voucher, under certain conditions, presents the user with a debit and credit column for amount entry. In 37 * addition, this form class must keep track of the changes between the old and new balance type selection so that the corresponding 38 * action class and make decisions based upon the differences. New accounting lines use specific credit and debit amount fields b/c 39 * the new line is explicitly known; however, already existing accounting lines need to exist within a list with ordering that 40 * matches the accounting lines source list. 41 */ 42 public class JournalVoucherForm extends VoucherForm { 43 protected List balanceTypes; 44 protected String originalBalanceType; 45 protected BalanceType selectedBalanceType; 46 47 /** 48 * Constructs a JournalVoucherForm instance. 49 */ 50 public JournalVoucherForm() { 51 super(); 52 selectedBalanceType = new BalanceType(OLEConstants.BALANCE_TYPE_ACTUAL); 53 originalBalanceType = ""; 54 } 55 56 @Override 57 protected String getDefaultDocumentTypeName() { 58 return "OLE_JV"; 59 } 60 61 /** 62 * Overrides the parent to call super.populate and then to call the two methods that are specific to loading the two select 63 * lists on the page. In addition, this also makes sure that the credit and debit amounts are filled in for situations where 64 * validation errors occur and the page reposts. 65 * 66 * @see org.kuali.rice.kns.web.struts.pojo.PojoForm#populate(javax.servlet.http.HttpServletRequest) 67 */ 68 @Override 69 public void populate(HttpServletRequest request) { 70 super.populate(request); 71 populateBalanceTypeListForRendering(); 72 } 73 74 /** 75 * Override the parent, to push the chosen accounting period and balance type down into the source accounting line object. In 76 * addition, check the balance type to see if it's the "External Encumbrance" balance and alter the encumbrance update code on 77 * the accounting line appropriately. 78 * 79 * @see org.kuali.rice.kns.web.struts.form.KualiTransactionalDocumentFormBase#populateSourceAccountingLine(org.kuali.rice.krad.bo.SourceAccountingLine) 80 */ 81 @Override 82 public void populateSourceAccountingLine(SourceAccountingLine sourceLine, String accountingLinePropertyName, Map parameterMap) { 83 super.populateSourceAccountingLine(sourceLine, accountingLinePropertyName, parameterMap); 84 populateSourceAccountingLineEncumbranceCode(sourceLine); 85 } 86 87 /** 88 * Sets the encumbrance code of the line based on the balance type. 89 * 90 * @param sourceLine - line to set code on 91 */ 92 protected void populateSourceAccountingLineEncumbranceCode(SourceAccountingLine sourceLine) { 93 BalanceType selectedBalanceType = getSelectedBalanceType(); 94 if (ObjectUtils.isNotNull(selectedBalanceType) && StringUtils.isNotBlank(selectedBalanceType.getCode())) { 95 sourceLine.setBalanceTyp(selectedBalanceType); 96 sourceLine.setBalanceTypeCode(selectedBalanceType.getCode()); 97 98 // set the encumbrance update code appropriately 99 // KFSMI-5565 remove the default encumbrance code 100 // no more default encumbrance code 101 // if (OLEConstants.BALANCE_TYPE_EXTERNAL_ENCUMBRANCE.equals(selectedBalanceType.getCode())) { 102 // sourceLine.setEncumbranceUpdateCode(OLEConstants.JOURNAL_VOUCHER_ENCUMBRANCE_UPDATE_CODE_BALANCE_TYPE_EXTERNAL_ENCUMBRANCE); 103 // } 104 // else { 105 // sourceLine.setEncumbranceUpdateCode(null); 106 // } 107 } 108 else { 109 // it's the first time in, the form will be empty the first time in 110 // set up default selection value 111 selectedBalanceType = getPopulatedBalanceTypeInstance(OLEConstants.BALANCE_TYPE_ACTUAL); 112 setSelectedBalanceType(selectedBalanceType); 113 setOriginalBalanceType(selectedBalanceType.getCode()); 114 115 sourceLine.setEncumbranceUpdateCode(null); 116 } 117 } 118 119 /** 120 * This method retrieves the list of valid balance types to display. 121 * 122 * @return List 123 */ 124 public List getBalanceTypes() { 125 return balanceTypes; 126 } 127 128 /** 129 * This method sets the selected balance type. 130 * 131 * @return BalanceTyp 132 */ 133 public BalanceType getSelectedBalanceType() { 134 return selectedBalanceType; 135 } 136 137 /** 138 * This method retrieves the selected balance type. 139 * 140 * @param selectedBalanceType 141 */ 142 public void setSelectedBalanceType(BalanceType selectedBalanceType) { 143 this.selectedBalanceType = selectedBalanceType; 144 } 145 146 /** 147 * This method sets the list of valid balance types to display. 148 * 149 * @param balanceTypes 150 */ 151 public void setBalanceTypes(List balanceTypes) { 152 this.balanceTypes = balanceTypes; 153 } 154 155 /** 156 * This method returns the journal voucher document associated with this form. 157 * 158 * @return Returns the journalVoucherDocument. 159 */ 160 public JournalVoucherDocument getJournalVoucherDocument() { 161 return (JournalVoucherDocument) getTransactionalDocument(); 162 } 163 164 /** 165 * This method sets the journal voucher document associated with this form. 166 * 167 * @param journalVoucherDocument The journalVoucherDocument to set. 168 */ 169 public void setJournalVoucherDocument(JournalVoucherDocument journalVoucherDocument) { 170 setDocument(journalVoucherDocument); 171 } 172 173 /** 174 * This method retrieves the originalBalanceType attribute. 175 * 176 * @return String 177 */ 178 public String getOriginalBalanceType() { 179 return originalBalanceType; 180 } 181 182 /** 183 * This method sets the originalBalanceType attribute. 184 * 185 * @param changedBalanceType 186 */ 187 public void setOriginalBalanceType(String changedBalanceType) { 188 this.originalBalanceType = changedBalanceType; 189 } 190 191 /** 192 * Using the selected accounting period to determine university fiscal year and look up all the encumbrance 193 * balance type - check if the selected balance type is for encumbrance 194 * 195 * @return true/false - true if it is an encumbrance balance type 196 */ 197 public boolean getIsEncumbranceBalanceType(){ 198 //get encumbrance balance type list 199 BalanceTypeService balanceTypeSerivce = SpringContext.getBean(BalanceTypeService.class); 200 List<String> encumbranceBalanceTypes = balanceTypeSerivce.getEncumbranceBalanceTypes(getSelectedPostingYear()); 201 202 return encumbranceBalanceTypes.contains(selectedBalanceType.getCode()); 203 } 204 205 /** 206 * This method retrieves all of the balance types in the system and prepares them to be rendered in a dropdown UI component. 207 */ 208 protected void populateBalanceTypeListForRendering() { 209 // grab the list of valid balance types 210 ArrayList balanceTypes = new ArrayList(SpringContext.getBean(BalanceTypeService.class).getAllBalanceTypes()); 211 212 // set into the form for rendering 213 this.setBalanceTypes(balanceTypes); 214 215 String selectedBalanceTypeCode = getSelectedBalanceType().getCode(); 216 if (StringUtils.isBlank(selectedBalanceTypeCode)) { 217 selectedBalanceTypeCode = OLEConstants.BALANCE_TYPE_ACTUAL; 218 } 219 220 setSelectedBalanceType(getPopulatedBalanceTypeInstance(selectedBalanceTypeCode)); 221 getJournalVoucherDocument().setBalanceTypeCode(selectedBalanceTypeCode); 222 } 223 224 /** 225 * This method will fully populate a balance type given the passed in code, by calling the business object service that 226 * retrieves the rest of the instances' information. 227 * 228 * @param balanceTypeCode 229 * @return BalanceTyp 230 */ 231 protected BalanceType getPopulatedBalanceTypeInstance(String balanceTypeCode) { 232 // now we have to get the code and the name of the original and new balance types 233 BalanceTypeService bts = SpringContext.getBean(BalanceTypeService.class); 234 return bts.getBalanceTypeByCode(balanceTypeCode); 235 } 236 237 /** 238 * If the balance type is an offset generation balance type, then the user is able to enter the amount as either a debit or a 239 * credit, otherwise, they only need to deal with the amount field in this case we always need to update the underlying bo so 240 * that the debit/credit code along with the amount, is properly set. 241 */ 242 protected void populateCreditAndDebitAmounts() { 243 if (isSelectedBalanceTypeFinancialOffsetGenerationIndicator()) { 244 super.populateCreditAndDebitAmounts(); 245 } 246 } 247 248 /** 249 * This is a convenience helper method that is used several times throughout this action class to determine if the selected 250 * balance type contained within the form instance is a financial offset generation balance type or not. 251 * 252 * @return boolean True if it is an offset generation balance type, false otherwise. 253 */ 254 protected boolean isSelectedBalanceTypeFinancialOffsetGenerationIndicator() { 255 return getPopulatedBalanceTypeInstance(getSelectedBalanceType().getCode()).isFinancialOffsetGenerationIndicator(); 256 } 257 }