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.Collections;
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.rule.event.KualiDocumentEventBase;
031: import org.kuali.core.service.KualiRuleService;
032: import org.kuali.core.util.GlobalVariables;
033: import org.kuali.kfs.KFSConstants;
034: import org.kuali.kfs.context.SpringContext;
035: import org.kuali.module.kra.budget.bo.BudgetUser;
036: import org.kuali.module.kra.budget.bo.UserAppointmentTask;
037: import org.kuali.module.kra.budget.bo.UserAppointmentTaskPeriod;
038: import org.kuali.module.kra.budget.rules.event.CalculatePersonnelEvent;
039: import org.kuali.module.kra.budget.rules.event.InsertPersonnelEventBase;
040: import org.kuali.module.kra.budget.rules.event.SavePersonnelEventBase;
041: import org.kuali.module.kra.budget.rules.event.UpdatePersonnelEventBase;
042: import org.kuali.module.kra.budget.service.BudgetPersonnelService;
043: import org.kuali.module.kra.budget.web.struts.form.BudgetForm;
044:
045: /**
046: * This class handles Actions for Research Administration.
047: */
048:
049: public class BudgetPersonnelAction extends BudgetAction {
050:
051: private static final String PERSON = "person";
052: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
053: .getLogger(BudgetPersonnelAction.class);
054:
055: /**
056: * This method overrides the BudgetAction execute method. It does so for the purpose of recalculating Personnel expenses any
057: * time the Personnel page is accessed
058: */
059: public ActionForward execute(ActionMapping mapping,
060: ActionForm form, HttpServletRequest request,
061: HttpServletResponse response) throws Exception {
062:
063: BudgetForm budgetForm = (BudgetForm) form;
064:
065: boolean rulePassed = runRule(budgetForm,
066: new CalculatePersonnelEvent(budgetForm.getDocument()));
067:
068: if (rulePassed) {
069: SpringContext.getBean(BudgetPersonnelService.class)
070: .calculateAllPersonnelCompensation(
071: budgetForm.getBudgetDocument());
072: return super .execute(mapping, form, request, response);
073: } else if (StringUtils.equals(
074: KFSConstants.RELOAD_METHOD_TO_CALL, budgetForm
075: .getMethodToCall())) {
076: GlobalVariables.getErrorMap().clear();
077: return this .reload(mapping, form, request, response);
078: } else {
079: return mapping.findForward(KFSConstants.MAPPING_BASIC);
080: }
081: }
082:
083: public ActionForward updateView(ActionMapping mapping,
084: ActionForm form, HttpServletRequest request,
085: HttpServletResponse response) throws Exception {
086:
087: BudgetForm budgetForm = (BudgetForm) form;
088:
089: boolean rulePassed = runRule(budgetForm,
090: new UpdatePersonnelEventBase(budgetForm.getDocument(),
091: budgetForm.getBudgetDocument().getBudget()
092: .getPersonnel()));
093:
094: return mapping.findForward(KFSConstants.MAPPING_BASIC);
095: }
096:
097: public ActionForward insertPersonnel(ActionMapping mapping,
098: ActionForm form, HttpServletRequest request,
099: HttpServletResponse response) throws Exception {
100:
101: BudgetForm budgetForm = (BudgetForm) form;
102:
103: boolean rulePassed = runRule(budgetForm,
104: new InsertPersonnelEventBase(budgetForm.getDocument(),
105: budgetForm.getNewPersonnel(), !StringUtils
106: .equals(PERSON, budgetForm
107: .getNewPersonnelType())));
108:
109: if (rulePassed) {
110: if (!StringUtils.equals(PERSON, budgetForm
111: .getNewPersonnelType())) {
112: BudgetUser newPersonnel = new BudgetUser();
113: newPersonnel.setRole(budgetForm.getNewPersonnel()
114: .getRole());
115: budgetForm.setNewPersonnel(newPersonnel);
116: }
117:
118: budgetForm.getBudgetDocument().addPersonnel(
119: budgetForm.getNewPersonnel());
120: budgetForm.setNewPersonnel(new BudgetUser());
121:
122: budgetForm.setNewPersonnelType(PERSON);
123:
124: }
125:
126: return mapping.findForward(KFSConstants.MAPPING_BASIC);
127: }
128:
129: public ActionForward savePersonnel(ActionMapping mapping,
130: ActionForm form, HttpServletRequest request,
131: HttpServletResponse response) throws Exception {
132: // Cast
133: BudgetForm budgetForm = (BudgetForm) form;
134:
135: // Cache
136: List personnelList = new ArrayList(budgetForm
137: .getBudgetDocument().getBudget().getPersonnel());
138: Integer personnelNextSequenceNumber = budgetForm
139: .getBudgetDocument().getPersonnelNextSequenceNumber();
140:
141: // Load
142: super .load(mapping, form, request, response);
143:
144: // Set from Cache
145: budgetForm.getBudgetDocument().setPersonnelNextSequenceNumber(
146: personnelNextSequenceNumber);
147: budgetForm.getBudgetDocument().getBudget().setPersonnel(
148: personnelList);
149:
150: // check any business rules that are specific to saving from this page only.
151: SpringContext.getBean(KualiRuleService.class).applyRules(
152: new SavePersonnelEventBase(budgetForm.getDocument(),
153: budgetForm.getBudgetDocument().getBudget()
154: .getPersonnel()));
155:
156: // Super save
157: return super .save(mapping, form, request, response);
158: }
159:
160: public ActionForward calculateCompensation(ActionMapping mapping,
161: ActionForm form, HttpServletRequest request,
162: HttpServletResponse response) throws Exception {
163:
164: return this .updateView(mapping, form, request, response);
165: }
166:
167: public ActionForward deletePersonnel(ActionMapping mapping,
168: ActionForm form, HttpServletRequest request,
169: HttpServletResponse response) throws Exception {
170: BudgetForm budgetForm = (BudgetForm) form;
171: List personnel = budgetForm.getBudgetDocument().getBudget()
172: .getPersonnel();
173:
174: for (Iterator i = personnel.iterator(); i.hasNext();) {
175: BudgetUser budgetUser = (BudgetUser) i.next();
176: if (budgetUser.isDelete()) {
177: i.remove();
178: }
179: }
180:
181: return mapping.findForward(KFSConstants.MAPPING_BASIC);
182: }
183:
184: public ActionForward clearNewPersonnel(ActionMapping mapping,
185: ActionForm form, HttpServletRequest request,
186: HttpServletResponse response) throws Exception {
187: BudgetForm budgetForm = (BudgetForm) form;
188:
189: budgetForm.setNewPersonnel(new BudgetUser());
190: budgetForm.setNewPersonnelType(PERSON);
191:
192: return mapping.findForward(KFSConstants.MAPPING_BASIC);
193: }
194:
195: public ActionForward reload(ActionMapping mapping, ActionForm form,
196: HttpServletRequest request, HttpServletResponse response)
197: throws Exception {
198: BudgetForm budgetForm = (BudgetForm) form;
199:
200: super .reload(mapping, form, request, response);
201:
202: Collections.sort(budgetForm.getBudgetDocument().getBudget()
203: .getPersonnel());
204:
205: SpringContext.getBean(BudgetPersonnelService.class)
206: .calculateAllPersonnelCompensation(
207: budgetForm.getBudgetDocument());
208:
209: return mapping.findForward(KFSConstants.MAPPING_BASIC);
210: }
211:
212: private boolean runRule(BudgetForm budgetForm,
213: KualiDocumentEventBase event) {
214: // check any business rules
215: boolean rulePassed = SpringContext.getBean(
216: KualiRuleService.class).applyRules(event);
217:
218: if (!rulePassed) {
219: // have to reset all of the tasks and appointment types of everyone on the page.
220: for (Iterator i = budgetForm.getBudgetDocument()
221: .getBudget().getPersonnel().iterator(); i.hasNext();) {
222: BudgetUser budgetUser = (BudgetUser) i.next();
223: budgetUser.setCurrentTaskNumber(budgetUser
224: .getPreviousTaskNumber());
225:
226: for (UserAppointmentTask userAppointmentTask : budgetUser
227: .getUserAppointmentTasks()) {
228: if (userAppointmentTask
229: .getInstitutionAppointmentTypeCode()
230: .equals(budgetUser.getAppointmentTypeCode())) {
231: userAppointmentTask
232: .setInstitutionAppointmentTypeCode(budgetUser
233: .getPreviousAppointmentTypeCode());
234: } else if (userAppointmentTask
235: .getInstitutionAppointmentTypeCode()
236: .equals(
237: budgetUser
238: .getSecondaryAppointmentTypeCode())) {
239: userAppointmentTask
240: .setInstitutionAppointmentTypeCode(budgetUser
241: .getPreviousSecondaryAppointmentTypeCode());
242: }
243: for (UserAppointmentTaskPeriod userAppointmentTaskPeriod : userAppointmentTask
244: .getUserAppointmentTaskPeriods()) {
245: userAppointmentTaskPeriod
246: .setInstitutionAppointmentTypeCode(userAppointmentTask
247: .getInstitutionAppointmentTypeCode());
248: }
249: }
250:
251: budgetUser.setAppointmentTypeCode(budgetUser
252: .getPreviousAppointmentTypeCode());
253: budgetUser.setSecondaryAppointmentTypeCode(budgetUser
254: .getPreviousSecondaryAppointmentTypeCode());
255:
256: }
257: }
258:
259: return rulePassed;
260: }
261: }
|