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.action;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import org.apache.commons.lang.StringUtils;
027: import org.apache.struts.action.ActionForm;
028: import org.apache.struts.action.ActionForward;
029: import org.apache.struts.action.ActionMapping;
030: import org.kuali.core.service.KualiRuleService;
031: import org.kuali.kfs.KFSConstants;
032: import org.kuali.kfs.context.SpringContext;
033: import org.kuali.module.kra.budget.bo.BudgetNonpersonnel;
034: import org.kuali.module.kra.budget.rules.event.UpdateNonpersonnelEventBase;
035: import org.kuali.module.kra.budget.service.BudgetNonpersonnelService;
036: import org.kuali.module.kra.budget.web.struts.form.BudgetForm;
037: import org.kuali.module.kra.budget.web.struts.form.BudgetNonpersonnelCopyOverFormHelper;
038: import org.kuali.module.kra.budget.web.struts.form.BudgetNonpersonnelFormHelper;
039:
040: public class BudgetNonpersonnelAction extends BudgetAction {
041:
042: /**
043: * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm,
044: * javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
045: */
046: public ActionForward execute(ActionMapping mapping,
047: ActionForm form, HttpServletRequest request,
048: HttpServletResponse response) throws Exception {
049:
050: ActionForward forward = super .execute(mapping, form, request,
051: response);
052:
053: BudgetForm budgetForm = (BudgetForm) form;
054: budgetForm
055: .setBudgetNonpersonnelFormHelper(new BudgetNonpersonnelFormHelper(
056: budgetForm));
057:
058: return forward;
059: }
060:
061: public ActionForward insertNonpersonnelLine(ActionMapping mapping,
062: ActionForm form, HttpServletRequest request,
063: HttpServletResponse response) throws Exception {
064: BudgetForm budgetForm = (BudgetForm) form;
065:
066: int listIndex = super .getSelectedLine(request);
067:
068: // convert the list to an array to make it easier to interact with
069: BudgetNonpersonnel[] newNonpersonnelArray = (BudgetNonpersonnel[]) budgetForm
070: .getNewNonpersonnelList().toArray(
071: new BudgetNonpersonnel[budgetForm
072: .getNewNonpersonnelList().size()]);
073:
074: // get the item out of the array that represents the new nonpersonnel item we want to add and populate it
075: BudgetNonpersonnel newNonpersonnel = (BudgetNonpersonnel) budgetForm
076: .getNewNonpersonnelList().get(listIndex);
077: // add the newNonpersonnel item to the nonpersonnel list
078: budgetForm.getBudgetDocument().addNonpersonnel(newNonpersonnel);
079:
080: // replace the one added to the list with a new one
081: newNonpersonnelArray[listIndex] = new BudgetNonpersonnel();
082:
083: // convert the array back to a list and set into the form
084: budgetForm.setNewNonpersonnelList(Arrays
085: .asList(newNonpersonnelArray));
086:
087: return mapping.findForward(KFSConstants.MAPPING_BASIC);
088: }
089:
090: public ActionForward deleteNonpersonnel(ActionMapping mapping,
091: ActionForm form, HttpServletRequest request,
092: HttpServletResponse response) throws Exception {
093: BudgetForm budgetForm = (BudgetForm) form;
094: List nonpersonnelItems = budgetForm.getBudgetDocument()
095: .getBudget().getNonpersonnelItems();
096:
097: int lineToDelete = getLineToDelete(request);
098: BudgetNonpersonnel budgetNonpersonnelRemoved = (BudgetNonpersonnel) nonpersonnelItems
099: .get(lineToDelete);
100:
101: // Remove the item that the user selected for removal
102: nonpersonnelItems.remove(getLineToDelete(request));
103:
104: if (budgetNonpersonnelRemoved.isCopiedOverItem()) {
105: // Iterate over the whole list of NPRS items and remove the copy over items that have the removed item as origin.
106:
107: // necessary to make copy because period fill up below removes from the list, want to avoid concurrent modification
108: // exception
109: List nonpersonnelItemsTmp = new ArrayList(budgetForm
110: .getBudgetDocument().getBudget()
111: .getNonpersonnelItems());
112:
113: for (Iterator nonpersonnelItemsIter = nonpersonnelItemsTmp
114: .iterator(); nonpersonnelItemsIter.hasNext();) {
115: BudgetNonpersonnel budgetNonpersonnel = (BudgetNonpersonnel) nonpersonnelItemsIter
116: .next();
117:
118: // check for any item that uses this sequence number as the origin (they are all copied over) or for an item that
119: // has the origin sequence number (that is the origin). Coincidentally the second check should not be necessary
120: // since origin items also have origin item sequence number set. But better safe then sorry.
121: if (budgetNonpersonnelRemoved
122: .getBudgetOriginSequenceNumber()
123: .equals(
124: budgetNonpersonnel
125: .getBudgetOriginSequenceNumber())
126: || budgetNonpersonnelRemoved
127: .getBudgetOriginSequenceNumber()
128: .equals(
129: budgetNonpersonnel
130: .getBudgetNonpersonnelSequenceNumber())) {
131: nonpersonnelItems.remove(budgetNonpersonnel);
132: }
133: }
134: }
135:
136: return mapping.findForward(KFSConstants.MAPPING_BASIC);
137: }
138:
139: public ActionForward saveNonpersonnel(ActionMapping mapping,
140: ActionForm form, HttpServletRequest request,
141: HttpServletResponse response) throws Exception {
142: BudgetForm budgetForm = (BudgetForm) form;
143:
144: // check any business rules. This has to be done before saving too because copy over could occur.
145: // copy over can only occur when validation has taken place.
146: boolean rulePassed = SpringContext.getBean(
147: KualiRuleService.class).applyRules(
148: new UpdateNonpersonnelEventBase(budgetForm
149: .getDocument(), budgetForm.getBudgetDocument()
150: .getBudget().getNonpersonnelItems()));
151:
152: if (!rulePassed) {
153: budgetForm.setCurrentTaskNumber(budgetForm
154: .getPreviousTaskNumber());
155: budgetForm.setCurrentPeriodNumber(budgetForm
156: .getPreviousPeriodNumber());
157: return mapping.findForward(KFSConstants.MAPPING_BASIC);
158: }
159:
160: // check if any "Check All" checkbox is checked.
161: for (Iterator nonpersonnelListIter = budgetForm
162: .getBudgetDocument().getBudget().getNonpersonnelItems()
163: .iterator(); nonpersonnelListIter.hasNext();) {
164: BudgetNonpersonnel budgetNonpersonnel = (BudgetNonpersonnel) nonpersonnelListIter
165: .next();
166: if (budgetNonpersonnel.getCopyToFuturePeriods()) {
167: // found a checked one. Go ahead and construct to BudgetNonpersonnelCopyOverFormHelper
168: // based on this, and immediatly deconstruct this. By default this will copy over all
169: // checked items (= checked all) that haven't been copied over yet.
170: List nonpersonnelItems = budgetForm.getBudgetDocument()
171: .getBudget().getNonpersonnelItems();
172: SpringContext.getBean(BudgetNonpersonnelService.class)
173: .refreshNonpersonnelObjectCode(
174: nonpersonnelItems);
175: new BudgetNonpersonnelCopyOverFormHelper(budgetForm)
176: .deconstruct(budgetForm);
177: break;
178: }
179: }
180:
181: // Needs to be done after the copy over check because copy over may be changing nonpersonnel & sequence number data.
182: List nonpersonnelList = new ArrayList(budgetForm
183: .getBudgetDocument().getBudget().getNonpersonnelItems());
184: Integer nonpersonnelNextSequenceNumber = budgetForm
185: .getBudgetDocument()
186: .getNonpersonnelNextSequenceNumber();
187:
188: // we are only saving nonpersonnel items, so load the doc and set the nonpersonnel items to the proper ones.
189: super .load(mapping, form, request, response);
190: budgetForm.getBudgetDocument().getBudget()
191: .setNonpersonnelItems(nonpersonnelList);
192: budgetForm.getBudgetDocument()
193: .setNonpersonnelNextSequenceNumber(
194: nonpersonnelNextSequenceNumber);
195:
196: return super .save(mapping, form, request, response);
197: }
198:
199: public ActionForward recalculate(ActionMapping mapping,
200: ActionForm form, HttpServletRequest request,
201: HttpServletResponse response) throws Exception {
202: BudgetForm budgetForm = (BudgetForm) form;
203:
204: return super .update(mapping, form, request, response);
205: }
206:
207: public ActionForward update(ActionMapping mapping, ActionForm form,
208: HttpServletRequest request, HttpServletResponse response)
209: throws Exception {
210: BudgetForm budgetForm = (BudgetForm) form;
211:
212: // check any business rules
213: boolean rulePassed = SpringContext.getBean(
214: KualiRuleService.class).applyRules(
215: new UpdateNonpersonnelEventBase(budgetForm
216: .getDocument(), budgetForm.getBudgetDocument()
217: .getBudget().getNonpersonnelItems()));
218:
219: if (rulePassed) {
220: super .setupNonpersonnelCategories(budgetForm);
221: } else {
222: budgetForm.setCurrentTaskNumber(budgetForm
223: .getPreviousTaskNumber());
224: budgetForm.setCurrentPeriodNumber(budgetForm
225: .getPreviousPeriodNumber());
226: }
227:
228: return super .update(mapping, form, request, response);
229: }
230:
231: public ActionForward nonpersonnelCopyOver(ActionMapping mapping,
232: ActionForm form, HttpServletRequest request,
233: HttpServletResponse response) throws Exception {
234: BudgetForm budgetForm = (BudgetForm) form;
235:
236: List nonpersonnelItems = budgetForm.getBudgetDocument()
237: .getBudget().getNonpersonnelItems();
238: budgetForm.getBudgetNonpersonnelFormHelper().refresh(
239: nonpersonnelItems);
240:
241: // check any business rules
242: boolean rulePassed = SpringContext.getBean(
243: KualiRuleService.class).applyRules(
244: new UpdateNonpersonnelEventBase(budgetForm
245: .getDocument(), budgetForm.getBudgetDocument()
246: .getBudget().getNonpersonnelItems()));
247:
248: if (!rulePassed) {
249: budgetForm.setCurrentTaskNumber(budgetForm
250: .getPreviousTaskNumber());
251: budgetForm.setCurrentPeriodNumber(budgetForm
252: .getPreviousPeriodNumber());
253: return mapping.findForward(KFSConstants.MAPPING_BASIC);
254: }
255:
256: // Refresh reference objects so that NonpersonnelObjectCode is populated. This
257: // is necessary because it maybe a new object that hasn't been saved to the
258: // database yet.
259: SpringContext.getBean(BudgetNonpersonnelService.class)
260: .refreshNonpersonnelObjectCode(nonpersonnelItems);
261:
262: // prepare data for NPRS copy over page
263: budgetForm
264: .setBudgetNonpersonnelCopyOverFormHelper(new BudgetNonpersonnelCopyOverFormHelper(
265: budgetForm));
266:
267: // preparee what category was selected
268: String parameterName = (String) request
269: .getAttribute(KFSConstants.METHOD_TO_CALL_ATTRIBUTE);
270: String lineCode = StringUtils.substringBetween(parameterName,
271: ".code", ".");
272: budgetForm.setCurrentNonpersonnelCategoryCode(lineCode);
273:
274: // This is so that tab states are not shared between pages.
275: budgetForm.newTabState(false, false);
276:
277: return mapping.findForward("nonpersonnelCopyOver");
278: }
279: }
|