001: /*
002: * Copyright 2006-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.kra.budget.web.struts.form;
017:
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.kuali.module.kra.budget.bo.Budget;
023: import org.kuali.module.kra.budget.bo.BudgetPeriod;
024: import org.kuali.module.kra.budget.bo.BudgetTask;
025: import org.kuali.module.kra.budget.bo.BudgetTaskPeriodIndirectCost;
026:
027: /**
028: * Used by UI to get totals, counts, aggregations, and other things to render the Indirect Cost page.
029: */
030: public class BudgetIndirectCostFormHelper {
031:
032: /**
033: * List of taskPeriod objects to store task totals across all periods. Each taskPeriod BO will be attached to only a task, not a
034: * period.
035: */
036: private List taskTotals;
037:
038: /**
039: * List of taskPeriod objects to store task totals across all periods. Each taskPeriod BO will be attached to only a task, not a
040: * period.
041: */
042: private List periodTotals;
043:
044: /**
045: * Object to hold period subtotal values. These are for the subtotal line after the period summary.
046: */
047: private BudgetTaskPeriodIndirectCost periodSubTotal;
048:
049: /**
050: * Number of periods, so we know when to echo our subtotal lines.
051: */
052: private Integer numPeriods;
053:
054: /**
055: * Default constructor.
056: */
057: public BudgetIndirectCostFormHelper() {
058: taskTotals = new ArrayList();
059: periodTotals = new ArrayList();
060: numPeriods = new Integer(0);
061: }
062:
063: /**
064: * Non-default constructor, sets default values based on passed BudgetForm object.
065: *
066: * @param budgetForm
067: */
068: public BudgetIndirectCostFormHelper(BudgetForm budgetForm) {
069: this (
070: budgetForm.getBudgetDocument().getBudget().getTasks(),
071: budgetForm.getBudgetDocument().getBudget().getPeriods(),
072: budgetForm.getBudgetDocument().getBudget()
073: .getIndirectCost()
074: .getBudgetTaskPeriodIndirectCostItems());
075: }
076:
077: /**
078: * Non-default constructor, sets default values based on passed Budget object.
079: *
080: * @param budgetForm
081: */
082: public BudgetIndirectCostFormHelper(Budget budget) {
083: this (budget.getTasks(), budget.getPeriods(), budget
084: .getIndirectCost()
085: .getBudgetTaskPeriodIndirectCostItems());
086: }
087:
088: /**
089: * Update all Indirect Cost totals. This is the function generally called from the action class. Used this method to consolidate
090: * all calls.
091: *
092: * @param tasks
093: * @param periods
094: */
095: public BudgetIndirectCostFormHelper(List tasks, List periods,
096: List idcItems) {
097: this ();
098: this .initializeTotals(tasks, periods); // First make sure we have at least zero values.
099: this .updateTotals(idcItems);
100: this .setNumPeriods(new Integer(periods.size()));
101: }
102:
103: /**
104: * Iterate over all tasks and periods to set up HashMaps with default (zero) values.
105: *
106: * @param tasks
107: * @param periods
108: */
109: public void initializeTotals(List tasks, List periods) {
110: // Set up zero-values for the taskHash for all types.
111: for (Iterator taskIterator = tasks.iterator(); taskIterator
112: .hasNext();) {
113: BudgetTask task = (BudgetTask) taskIterator.next();
114:
115: BudgetTaskPeriodIndirectCost taskPeriod = new BudgetTaskPeriodIndirectCost();
116: taskPeriod.setBudgetTaskSequenceNumber(task
117: .getBudgetTaskSequenceNumber());
118: taskPeriod.setTask(task);
119:
120: this .getTaskTotals().add(taskPeriod);
121: }
122:
123: // Set up zero-values for the periodHash for all types.
124: for (Iterator periodIterator = periods.iterator(); periodIterator
125: .hasNext();) {
126: BudgetPeriod period = (BudgetPeriod) periodIterator.next();
127:
128: BudgetTaskPeriodIndirectCost taskPeriod = new BudgetTaskPeriodIndirectCost();
129: taskPeriod.setBudgetPeriodSequenceNumber(period
130: .getBudgetPeriodSequenceNumber());
131: taskPeriod.setPeriod(period);
132:
133: this .getPeriodTotals().add(taskPeriod);
134: }
135: }
136:
137: /**
138: * Iterate over all taskPeriods. For each one, add their totals to the HashMaps.
139: *
140: * @param tasks
141: * @param periods
142: * @param idcItems We should now have a list, since createTaskPeriodIdcList would have set it up for us just now, or on any
143: * previous save.
144: */
145: public void updateTotals(List idcItems) {
146: // Place to store aggregated period values for the period subtotal.
147: BudgetTaskPeriodIndirectCost subTotal = new BudgetTaskPeriodIndirectCost();
148:
149: // Iterate over all existing idc list items and add the appropriate values for each taskPeriodLine
150: // to the corresponding HashMap[#][type].
151: for (Iterator idcItemsIterator = idcItems.iterator(); idcItemsIterator
152: .hasNext();) {
153: BudgetTaskPeriodIndirectCost taskPeriod = (BudgetTaskPeriodIndirectCost) idcItemsIterator
154: .next();
155:
156: // Update task totals.
157: for (Iterator taskTotalIterator = this .getTaskTotals()
158: .iterator(); taskTotalIterator.hasNext();) {
159: BudgetTaskPeriodIndirectCost taskTotal = (BudgetTaskPeriodIndirectCost) taskTotalIterator
160: .next();
161:
162: if (taskTotal.getBudgetTaskSequenceNumber().equals(
163: taskPeriod.getBudgetTaskSequenceNumber())) {
164: taskTotal.setTotalDirectCost(taskTotal
165: .getTotalDirectCost().add(
166: taskPeriod.getTotalDirectCost()));
167: taskTotal.setBaseCost(taskTotal.getBaseCost().add(
168: taskPeriod.getBaseCost()));
169: taskTotal
170: .setCalculatedIndirectCost(taskTotal
171: .getCalculatedIndirectCost()
172: .add(
173: taskPeriod
174: .getCalculatedIndirectCost()));
175: taskTotal.setCostShareBaseCost(taskTotal
176: .getCostShareBaseCost().add(
177: taskPeriod.getCostShareBaseCost()));
178: taskTotal
179: .setCostShareCalculatedIndirectCost(taskTotal
180: .getCostShareCalculatedIndirectCost()
181: .add(
182: taskPeriod
183: .getCostShareCalculatedIndirectCost()));
184: taskTotal
185: .setCostShareUnrecoveredIndirectCost(taskTotal
186: .getCostShareUnrecoveredIndirectCost()
187: .add(
188: taskPeriod
189: .getCostShareUnrecoveredIndirectCost()));
190: }
191: }
192:
193: // Update period totals.
194: for (Iterator periodTotalIterator = this .getPeriodTotals()
195: .iterator(); periodTotalIterator.hasNext();) {
196: BudgetTaskPeriodIndirectCost periodTotal = (BudgetTaskPeriodIndirectCost) periodTotalIterator
197: .next();
198:
199: if (periodTotal.getBudgetPeriodSequenceNumber().equals(
200: taskPeriod.getBudgetPeriodSequenceNumber())) {
201: periodTotal.setTotalDirectCost(periodTotal
202: .getTotalDirectCost().add(
203: taskPeriod.getTotalDirectCost()));
204: periodTotal.setBaseCost(periodTotal.getBaseCost()
205: .add(taskPeriod.getBaseCost()));
206: periodTotal
207: .setCalculatedIndirectCost(periodTotal
208: .getCalculatedIndirectCost()
209: .add(
210: taskPeriod
211: .getCalculatedIndirectCost()));
212: periodTotal.setCostShareBaseCost(periodTotal
213: .getCostShareBaseCost().add(
214: taskPeriod.getCostShareBaseCost()));
215: periodTotal
216: .setCostShareCalculatedIndirectCost(periodTotal
217: .getCostShareCalculatedIndirectCost()
218: .add(
219: taskPeriod
220: .getCostShareCalculatedIndirectCost()));
221: periodTotal
222: .setCostShareUnrecoveredIndirectCost(periodTotal
223: .getCostShareUnrecoveredIndirectCost()
224: .add(
225: taskPeriod
226: .getCostShareUnrecoveredIndirectCost()));
227: }
228: }
229:
230: // Set the period subtotal for each period, since the subtotal spans all periods.
231: subTotal.setTotalDirectCost(subTotal.getTotalDirectCost()
232: .add(taskPeriod.getTotalDirectCost()));
233: subTotal.setBaseCost(subTotal.getBaseCost().add(
234: taskPeriod.getBaseCost()));
235: subTotal.setCalculatedIndirectCost(subTotal
236: .getCalculatedIndirectCost().add(
237: taskPeriod.getCalculatedIndirectCost()));
238: subTotal.setCostShareBaseCost(subTotal
239: .getCostShareBaseCost().add(
240: taskPeriod.getCostShareBaseCost()));
241: subTotal
242: .setCostShareCalculatedIndirectCost(subTotal
243: .getCostShareCalculatedIndirectCost()
244: .add(
245: taskPeriod
246: .getCostShareCalculatedIndirectCost()));
247: subTotal
248: .setCostShareUnrecoveredIndirectCost(subTotal
249: .getCostShareUnrecoveredIndirectCost()
250: .add(
251: taskPeriod
252: .getCostShareUnrecoveredIndirectCost()));
253: }
254:
255: this .setPeriodSubTotal(subTotal);
256: }
257:
258: /**
259: * @return Returns the periodTotals.
260: */
261: public List<BudgetTaskPeriodIndirectCost> getPeriodTotals() {
262: return periodTotals;
263: }
264:
265: /**
266: * @return Returns a periodTotal.
267: */
268: public BudgetTaskPeriodIndirectCost getPeriodTotal(int index) {
269: while (getPeriodTotals().size() <= index) {
270: getPeriodTotals().add(new BudgetTaskPeriodIndirectCost());
271: }
272: return (BudgetTaskPeriodIndirectCost) getPeriodTotals().get(
273: index);
274: }
275:
276: /**
277: * @param periodTotals The periodTotals to set.
278: */
279: public void setPeriodTotals(List periodTotals) {
280: this .periodTotals = periodTotals;
281: }
282:
283: /**
284: * @return Returns the taskTotals.
285: */
286: public List getTaskTotals() {
287: return taskTotals;
288: }
289:
290: /**
291: * @param taskTotals The taskTotals to set.
292: */
293: public void setTaskTotals(List taskTotals) {
294: this .taskTotals = taskTotals;
295: }
296:
297: /**
298: * @return Returns the numPeriods.
299: */
300: public Integer getNumPeriods() {
301: return numPeriods;
302: }
303:
304: /**
305: * @param numPeriods The numPeriods to set.
306: */
307: public void setNumPeriods(Integer numPeriods) {
308: this .numPeriods = numPeriods;
309: }
310:
311: /**
312: * @return Returns the periodSubTotal.
313: */
314: public BudgetTaskPeriodIndirectCost getPeriodSubTotal() {
315: return periodSubTotal;
316: }
317:
318: /**
319: * @param periodSubTotal The periodSubTotal to set.
320: */
321: public void setPeriodSubTotal(
322: BudgetTaskPeriodIndirectCost periodSubTotal) {
323: this.periodSubTotal = periodSubTotal;
324: }
325: }
|