View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package edu.sampleu.travel.web.action;
17  
18  import edu.sampleu.travel.bo.TravelAccount;
19  import edu.sampleu.travel.document.TravelDocument2;
20  import edu.sampleu.travel.web.form.TravelDocumentForm2;
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.struts.action.ActionForm;
23  import org.apache.struts.action.ActionForward;
24  import org.apache.struts.action.ActionMapping;
25  import org.kuali.rice.core.api.util.RiceConstants;
26  import org.kuali.rice.core.api.util.RiceKeyConstants;
27  import org.kuali.rice.kns.service.KNSServiceLocator;
28  import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
29  import org.kuali.rice.krad.exception.ValidationException;
30  import org.kuali.rice.krad.service.KRADServiceLocator;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  import org.kuali.rice.krad.util.KRADConstants;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  import java.util.Iterator;
37  
38  public class TravelDocumentAction2 extends KualiTransactionalDocumentActionBase {
39  
40      public ActionForward insertAccount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
41          TravelDocumentForm2 travelForm = (TravelDocumentForm2) form;
42          TravelAccount travAcct = (TravelAccount) KNSServiceLocator.getBusinessObjectService().retrieve(travelForm.getTravelAccount());
43          // Make sure a travel account was actually retrieved.
44          if (travAcct == null) {
45          	GlobalVariables.getMessageMap().putError("travelAccount.number", RiceKeyConstants.ERROR_CUSTOM, "Invalid travel account number");
46          	throw new ValidationException("Invalid travel account number");
47          }
48          // Insert the travel account into the list, if the list does not already contain it.
49          boolean containsNewAcct = false;
50          for (Iterator<TravelAccount> travAcctIter = ((TravelDocument2) travelForm.getDocument()).getTravelAccounts().iterator(); travAcctIter.hasNext();) {
51          	if (travAcctIter.next().getNumber().equals(travAcct.getNumber())) {
52          		containsNewAcct = true;
53          		break;
54          	}
55          }
56          if (!containsNewAcct) {
57          	((TravelDocument2) travelForm.getDocument()).getTravelAccounts().add(travAcct);
58          }
59          travelForm.setTravelAccount(new TravelAccount());
60          return mapping.findForward(RiceConstants.MAPPING_BASIC);
61      }
62  
63      public ActionForward deleteAccount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
64      	// Remove the travel account at the index specified in the "methodToCall" parameter.
65      	TravelDocumentForm2 travelForm = (TravelDocumentForm2) form;
66      	String strIndex = StringUtils.substringBetween((String) request.getAttribute(KRADConstants.METHOD_TO_CALL_ATTRIBUTE),
67          		KRADConstants.METHOD_TO_CALL_PARM1_LEFT_DEL, KRADConstants.METHOD_TO_CALL_PARM1_RIGHT_DEL);
68      	if (StringUtils.isNotBlank(strIndex)) {
69      		((TravelDocument2) travelForm.getDocument()).getTravelAccounts().remove(Integer.parseInt(strIndex));
70      	}
71          return mapping.findForward(RiceConstants.MAPPING_BASIC);
72      }
73      
74      public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
75          return mapping.findForward(RiceConstants.MAPPING_BASIC);
76      }
77  }