001 /*
002 * Copyright 2007-2008 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 edu.sampleu.travel.web.action;
017
018 import java.util.Iterator;
019
020 import javax.servlet.http.HttpServletRequest;
021 import javax.servlet.http.HttpServletResponse;
022
023 import org.apache.commons.lang.StringUtils;
024 import org.apache.struts.action.ActionForm;
025 import org.apache.struts.action.ActionForward;
026 import org.apache.struts.action.ActionMapping;
027 import org.kuali.rice.core.util.RiceConstants;
028 import org.kuali.rice.kns.exception.ValidationException;
029 import org.kuali.rice.kns.service.KNSServiceLocator;
030 import org.kuali.rice.kns.util.GlobalVariables;
031 import org.kuali.rice.kns.util.KNSConstants;
032 import org.kuali.rice.kns.util.RiceKeyConstants;
033 import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
034
035 import edu.sampleu.travel.bo.TravelAccount;
036 import edu.sampleu.travel.document.TravelDocument2;
037 import edu.sampleu.travel.web.form.TravelDocumentForm2;
038
039 public class TravelDocumentAction2 extends KualiTransactionalDocumentActionBase {
040
041 public ActionForward insertAccount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
042 TravelDocumentForm2 travelForm = (TravelDocumentForm2) form;
043 TravelAccount travAcct = (TravelAccount) KNSServiceLocator.getBusinessObjectService().retrieve(travelForm.getTravelAccount());
044 // Make sure a travel account was actually retrieved.
045 if (travAcct == null) {
046 GlobalVariables.getMessageMap().putError("travelAccount.number", RiceKeyConstants.ERROR_CUSTOM, "Invalid travel account number");
047 throw new ValidationException("Invalid travel account number");
048 }
049 // Insert the travel account into the list, if the list does not already contain it.
050 boolean containsNewAcct = false;
051 for (Iterator<TravelAccount> travAcctIter = ((TravelDocument2) travelForm.getDocument()).getTravelAccounts().iterator(); travAcctIter.hasNext();) {
052 if (travAcctIter.next().getNumber().equals(travAcct.getNumber())) {
053 containsNewAcct = true;
054 break;
055 }
056 }
057 if (!containsNewAcct) {
058 ((TravelDocument2) travelForm.getDocument()).getTravelAccounts().add(travAcct);
059 }
060 travelForm.setTravelAccount(new TravelAccount());
061 return mapping.findForward(RiceConstants.MAPPING_BASIC);
062 }
063
064 public ActionForward deleteAccount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
065 // Remove the travel account at the index specified in the "methodToCall" parameter.
066 TravelDocumentForm2 travelForm = (TravelDocumentForm2) form;
067 String strIndex = StringUtils.substringBetween((String) request.getAttribute(KNSConstants.METHOD_TO_CALL_ATTRIBUTE),
068 KNSConstants.METHOD_TO_CALL_PARM1_LEFT_DEL, KNSConstants.METHOD_TO_CALL_PARM1_RIGHT_DEL);
069 if (StringUtils.isNotBlank(strIndex)) {
070 ((TravelDocument2) travelForm.getDocument()).getTravelAccounts().remove(Integer.parseInt(strIndex));
071 }
072 return mapping.findForward(RiceConstants.MAPPING_BASIC);
073 }
074
075 public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
076 return mapping.findForward(RiceConstants.MAPPING_BASIC);
077 }
078 }