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