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