001    /*
002     * Copyright 2011 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 org.kuali.kfs.sys.service.impl;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.apache.struts.action.ActionForward;
025    import org.apache.struts.action.ActionMapping;
026    import org.kuali.kfs.sys.businessobject.ElectronicPaymentClaim;
027    import org.kuali.kfs.sys.service.ElectronicFundTransferActionHelper;
028    import org.kuali.kfs.sys.service.ElectronicPaymentClaimingService;
029    import org.kuali.kfs.sys.web.struts.ElectronicFundTransferForm;
030    import org.kuali.rice.kim.bo.Person;
031    import org.kuali.rice.kns.exception.AuthorizationException;
032    import org.kuali.rice.kns.service.DataDictionaryService;
033    import org.kuali.rice.kns.service.KNSServiceLocator;
034    import org.kuali.rice.kns.util.GlobalVariables;
035    
036    /**
037     * Represents a web action that occurs when a user returns a bunch of selected claims and redirects to the electronic funds transfer "claimng" page
038     */
039    public class ElectronicFundTransferRefreshActionHelper implements ElectronicFundTransferActionHelper {
040        private ElectronicPaymentClaimingService electronicPaymentClaimingService;
041        private DataDictionaryService ddService;
042        
043        protected static final String BASIC_FORWARD = "basic";
044        protected static final String ACTION_NAME = "claim";
045        protected static final String PORTAL_FORWARD = "portal";
046    
047        /**
048         * @see org.kuali.kfs.sys.service.ElectronicFundTransferActionHelper#performAction(org.kuali.rice.kns.web.struts.form.KualiForm, org.apache.struts.action.ActionMapping)
049         */
050        public ActionForward performAction(ElectronicFundTransferForm form, ActionMapping mapping, Map params, String basePath) {
051            // is the current user able to claim electronic funds?
052            Person currentUser = GlobalVariables.getUserSession().getPerson();
053            if (!form.hasAvailableClaimingDocumentStrategies()) {
054                throw new AuthorizationException(currentUser.getPrincipalName(), ElectronicFundTransferRefreshActionHelper.ACTION_NAME, ddService.getDataDictionary().getBusinessObjectEntry(ElectronicPaymentClaim.class.getName()).getObjectLabel());
055            }
056            // does the current user have claimed funds waiting for them?
057            String lookupResultsSequenceNumber = null;
058            if (params.get("lookupResultsSequenceNumber") != null) {
059                lookupResultsSequenceNumber = ((String[])params.get("lookupResultsSequenceNumber"))[0];
060            }
061            if (StringUtils.isBlank(lookupResultsSequenceNumber)) {
062                return mapping.findForward(PORTAL_FORWARD);
063            }
064            List<ElectronicPaymentClaim> claims = getClaimedPayments(currentUser, lookupResultsSequenceNumber);
065            if (claims.size() == 0) {
066                return mapping.findForward(PORTAL_FORWARD);
067            }
068            // if so, load their currently claimed electronic funds to the form
069            form.setClaims(claims);
070            // and redirect
071            return mapping.findForward(BASIC_FORWARD);
072        }
073        
074        /**
075         * Gets the selected electronic payment claim records from the LookupResults service
076         * @param currentUser the claiming user
077         * @param lookupResultsSequenceNumber the parameter for the lookup results sequence number
078         * @return a list of claims
079         */
080        protected List<ElectronicPaymentClaim> getClaimedPayments(Person currentUser, String lookupResultsSequenceNumber) {
081            List<ElectronicPaymentClaim> claims = new ArrayList<ElectronicPaymentClaim>();
082            try {
083                Collection selectedClaims = KNSServiceLocator.getLookupResultsService().retrieveSelectedResultBOs(lookupResultsSequenceNumber, ElectronicPaymentClaim.class, currentUser.getPrincipalId());
084                for (Object claimAsObj : selectedClaims) {
085                    ElectronicPaymentClaim claim = (ElectronicPaymentClaim) claimAsObj;
086                    if (!claim.getPaymentClaimStatusCode().equals(ElectronicPaymentClaim.ClaimStatusCodes.CLAIMED) && StringUtils.isBlank(claim.getReferenceFinancialDocumentNumber())) {
087                        claims.add(claim);
088                    }
089                }
090            }
091            catch (Exception e) {
092                throw new RuntimeException(e);
093            }        
094            return claims;
095        }
096        
097        /**
098         * Sets the electronicPaymentClaimingService attribute value.
099         * @param electronicPaymentClaimingService The electronicPaymentClaimingService to set.
100         */
101        public void setElectronicPaymentClaimingService(ElectronicPaymentClaimingService electronicPaymentClaimingService) {
102            this.electronicPaymentClaimingService = electronicPaymentClaimingService;
103        }
104        
105        /**
106         * Sets the ddService attribute value.
107         * @param ddService The ddService to set.
108         */
109        public void setDataDictonaryService(DataDictionaryService ddService) {
110            this.ddService = ddService;
111        }
112    }
113