001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.financial.web.struts.form;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import javax.servlet.http.HttpServletRequest;
022:
023: import org.apache.commons.lang.StringUtils;
024: import org.kuali.kfs.KFSConstants;
025: import org.kuali.kfs.bo.SourceAccountingLine;
026: import org.kuali.kfs.context.SpringContext;
027: import org.kuali.module.chart.bo.codes.BalanceTyp;
028: import org.kuali.module.chart.service.BalanceTypService;
029: import org.kuali.module.financial.document.JournalVoucherDocument;
030:
031: /**
032: * This class is the Struts specific form object that works in conjunction with the pojo utilities to build the UI for the Journal
033: * Voucher Document. This class is unique in that it leverages a helper data structure called the VoucherAccountingLineHelper
034: * because the Journal Voucher, under certain conditions, presents the user with a debit and credit column for amount entry. In
035: * addition, this form class must keep track of the changes between the old and new balance type selection so that the corresponding
036: * action class and make decisions based upon the differences. New accounting lines use specific credit and debit amount fields b/c
037: * the new line is explicitly known; however, already existing accounting lines need to exist within a list with ordering that
038: * matches the accounting lines source list.
039: */
040: public class JournalVoucherForm extends VoucherForm {
041: private List balanceTypes;
042: private String originalBalanceType;
043: private BalanceTyp selectedBalanceType;
044:
045: /**
046: * Constructs a JournalVoucherForm instance.
047: */
048: public JournalVoucherForm() {
049: super ();
050: setDocument(new JournalVoucherDocument());
051: selectedBalanceType = new BalanceTyp(
052: KFSConstants.BALANCE_TYPE_ACTUAL);
053: originalBalanceType = "";
054: }
055:
056: /**
057: * Overrides the parent to call super.populate and then to call the two methods that are specific to loading the two select
058: * lists on the page. In addition, this also makes sure that the credit and debit amounts are filled in for situations where
059: * validation errors occur and the page reposts.
060: *
061: * @see org.kuali.core.web.struts.pojo.PojoForm#populate(javax.servlet.http.HttpServletRequest)
062: */
063: public void populate(HttpServletRequest request) {
064: super .populate(request);
065: populateBalanceTypeListForRendering();
066: }
067:
068: /**
069: * Override the parent, to push the chosen accounting period and balance type down into the source accounting line object. In
070: * addition, check the balance type to see if it's the "External Encumbrance" balance and alter the encumbrance update code on
071: * the accounting line appropriately.
072: *
073: * @see org.kuali.core.web.struts.form.KualiTransactionalDocumentFormBase#populateSourceAccountingLine(org.kuali.core.bo.SourceAccountingLine)
074: */
075: public void populateSourceAccountingLine(
076: SourceAccountingLine sourceLine) {
077: super .populateSourceAccountingLine(sourceLine);
078: populateSourceAccountingLineEncumbranceCode(sourceLine);
079: }
080:
081: /**
082: * Sets the encumbrance code of the line based on the balance type.
083: *
084: * @param sourceLine - line to set code on
085: */
086: protected void populateSourceAccountingLineEncumbranceCode(
087: SourceAccountingLine sourceLine) {
088: BalanceTyp selectedBalanceType = getSelectedBalanceType();
089: if (selectedBalanceType != null
090: && StringUtils
091: .isNotBlank(selectedBalanceType.getCode())) {
092: sourceLine.setBalanceTyp(selectedBalanceType);
093: sourceLine
094: .setBalanceTypeCode(selectedBalanceType.getCode());
095:
096: // set the encumbrance update code appropriately
097: if (KFSConstants.BALANCE_TYPE_EXTERNAL_ENCUMBRANCE
098: .equals(selectedBalanceType.getCode())) {
099: sourceLine
100: .setEncumbranceUpdateCode(KFSConstants.JOURNAL_VOUCHER_ENCUMBRANCE_UPDATE_CODE_BALANCE_TYPE_EXTERNAL_ENCUMBRANCE);
101: } else {
102: sourceLine.setEncumbranceUpdateCode(null);
103: }
104: } else {
105: // it's the first time in, the form will be empty the first time in
106: // set up default selection value
107: selectedBalanceType = SpringContext.getBean(
108: BalanceTypService.class).getBalanceTypByCode(
109: KFSConstants.BALANCE_TYPE_ACTUAL);
110: setSelectedBalanceType(selectedBalanceType);
111: setOriginalBalanceType(selectedBalanceType.getCode());
112:
113: sourceLine.setEncumbranceUpdateCode(null);
114: }
115: }
116:
117: /**
118: * This method retrieves the list of valid balance types to display.
119: *
120: * @return List
121: */
122: public List getBalanceTypes() {
123: return balanceTypes;
124: }
125:
126: /**
127: * This method sets the selected balance type.
128: *
129: * @return BalanceTyp
130: */
131: public BalanceTyp getSelectedBalanceType() {
132: return selectedBalanceType;
133: }
134:
135: /**
136: * This method retrieves the selected balance type.
137: *
138: * @param selectedBalanceType
139: */
140: public void setSelectedBalanceType(BalanceTyp selectedBalanceType) {
141: this .selectedBalanceType = selectedBalanceType;
142: }
143:
144: /**
145: * This method sets the list of valid balance types to display.
146: *
147: * @param balanceTypes
148: */
149: public void setBalanceTypes(List balanceTypes) {
150: this .balanceTypes = balanceTypes;
151: }
152:
153: /**
154: * This method returns the journal voucher document associated with this form.
155: *
156: * @return Returns the journalVoucherDocument.
157: */
158: public JournalVoucherDocument getJournalVoucherDocument() {
159: return (JournalVoucherDocument) getTransactionalDocument();
160: }
161:
162: /**
163: * This method sets the journal voucher document associated with this form.
164: *
165: * @param journalVoucherDocument The journalVoucherDocument to set.
166: */
167: public void setJournalVoucherDocument(
168: JournalVoucherDocument journalVoucherDocument) {
169: setDocument(journalVoucherDocument);
170: }
171:
172: /**
173: * This method retrieves the originalBalanceType attribute.
174: *
175: * @return String
176: */
177: public String getOriginalBalanceType() {
178: return originalBalanceType;
179: }
180:
181: /**
182: * This method sets the originalBalanceType attribute.
183: *
184: * @param changedBalanceType
185: */
186: public void setOriginalBalanceType(String changedBalanceType) {
187: this .originalBalanceType = changedBalanceType;
188: }
189:
190: /**
191: * This method retrieves all of the balance types in the system and prepares them to be rendered in a dropdown UI component.
192: */
193: private void populateBalanceTypeListForRendering() {
194: // grab the list of valid balance types
195: ArrayList balanceTypes = new ArrayList(SpringContext.getBean(
196: BalanceTypService.class).getAllBalanceTyps());
197:
198: // set into the form for rendering
199: this .setBalanceTypes(balanceTypes);
200:
201: String selectedBalanceTypeCode = getSelectedBalanceType()
202: .getCode();
203: if (StringUtils.isBlank(selectedBalanceTypeCode)) {
204: selectedBalanceTypeCode = KFSConstants.BALANCE_TYPE_ACTUAL;
205: }
206:
207: setSelectedBalanceType(getPopulatedBalanceTypeInstance(selectedBalanceTypeCode));
208: getJournalVoucherDocument().setBalanceTypeCode(
209: selectedBalanceTypeCode);
210: }
211:
212: /**
213: * This method will fully populate a balance type given the passed in code, by calling the business object service that
214: * retrieves the rest of the instances' information.
215: *
216: * @param balanceTypeCode
217: * @return BalanceTyp
218: */
219: protected BalanceTyp getPopulatedBalanceTypeInstance(
220: String balanceTypeCode) {
221: // now we have to get the code and the name of the original and new balance types
222: BalanceTypService bts = SpringContext
223: .getBean(BalanceTypService.class);
224: return bts.getBalanceTypByCode(balanceTypeCode);
225: }
226:
227: /**
228: * 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
229: * credit, otherwise, they only need to deal with the amount field in this case we always need to update the underlying bo so
230: * that the debit/credit code along with the amount, is properly set.
231: */
232: protected void populateCreditAndDebitAmounts() {
233: if (isSelectedBalanceTypeFinancialOffsetGenerationIndicator()) {
234: super .populateCreditAndDebitAmounts();
235: }
236: }
237:
238: /**
239: * This is a convenience helper method that is used several times throughout this action class to determine if the selected
240: * balance type contained within the form instance is a financial offset generation balance type or not.
241: *
242: * @return boolean True if it is an offset generation balance type, false otherwise.
243: */
244: private boolean isSelectedBalanceTypeFinancialOffsetGenerationIndicator() {
245: return getPopulatedBalanceTypeInstance(
246: getSelectedBalanceType().getCode())
247: .isFinancialOffsetGenerationIndicator();
248: }
249: }
|