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.pdp.document.validation.impl;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import org.kuali.kfs.pdp.PdpConstants;
022    import org.kuali.kfs.pdp.PdpPropertyConstants;
023    import org.kuali.kfs.pdp.businessobject.PayeeACHAccount;
024    import org.kuali.kfs.sys.KFSKeyConstants;
025    import org.kuali.kfs.sys.KFSPropertyConstants;
026    import org.kuali.kfs.sys.context.SpringContext;
027    import org.kuali.rice.kns.document.MaintenanceDocument;
028    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
029    import org.kuali.rice.kns.service.BusinessObjectService;
030    import org.kuali.rice.kns.util.KualiInteger;
031    
032    /**
033     * Performs business rules for the Payee ACH Account maintenance document
034     */
035    public class PayeeAchAccountRule extends MaintenanceDocumentRuleBase {
036        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PayeeACHAccount.class);
037    
038        protected PayeeACHAccount oldPayeeAchAccount;
039        protected PayeeACHAccount newPayeeAchAccount;
040    
041        /**
042         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#setupConvenienceObjects()
043         */
044        public void setupConvenienceObjects() {
045            LOG.info("setupConvenienceObjects called");
046    
047            // setup oldPayeeAchAccount convenience objects, make sure all possible sub-objects are populated
048            oldPayeeAchAccount = (PayeeACHAccount) super.getOldBo();
049    
050            // setup newPayeeAchAccount convenience objects, make sure all possible sub-objects are populated
051            newPayeeAchAccount = (PayeeACHAccount) super.getNewBo();
052        }
053    
054        /**
055         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
056         */
057        protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
058            LOG.info("processCustomSaveDocumentBusinessRules called");
059    
060            // call the route rules to report all of the messages, but ignore the result
061            processCustomRouteDocumentBusinessRules(document);
062    
063            // Save always succeeds, even if there are business rule failures
064            return true;
065        }
066    
067        /**
068         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
069         */
070        protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
071            LOG.info("processCustomRouteDocumentBusinessRules called");
072    
073            boolean validEntry = true;
074    
075            setupConvenienceObjects();
076    
077            validEntry &= checkForDuplicateRecord();
078    
079            return validEntry;
080        }
081    
082        /**
083         * Checks to verify record is not a duplicate for payee id. Do not check for a duplicate record if the following conditions are
084         * true 1. editing an existing record (old primary key = new primary key) 2. new PSD code = old PSD code 3. new payee type code
085         * = old payee type code 4. depending of the value of payee type code, new correspoding PayeeId = old corresponding PayeeId
086         * 
087         * @return true if record is not duplicate, false otherwise
088         */
089        protected boolean checkForDuplicateRecord() {
090            String newPayeeIdNumber = newPayeeAchAccount.getPayeeIdNumber();
091            String newPayeeIdTypeCd = newPayeeAchAccount.getPayeeIdentifierTypeCode();
092            String newAchTransactionType = newPayeeAchAccount.getAchTransactionType();
093    
094            boolean valid = true;
095    
096            if (newPayeeAchAccount.getAchAccountGeneratedIdentifier() != null && oldPayeeAchAccount.getAchAccountGeneratedIdentifier() != null && newPayeeAchAccount.getAchAccountGeneratedIdentifier().equals(oldPayeeAchAccount.getAchAccountGeneratedIdentifier())) {
097                if (newPayeeIdTypeCd.equals(oldPayeeAchAccount.getPayeeIdentifierTypeCode()) && newAchTransactionType.equals(oldPayeeAchAccount.getAchTransactionType())) {
098                    if (newPayeeAchAccount.getPayeeIdNumber().equals(oldPayeeAchAccount.getPayeeIdNumber())) {
099                        return valid;
100                    }
101                }
102            }
103    
104            // check for a duplicate record if creating a new record or editing an old one and above mentioned conditions are not true
105            Map<String, Object> criteria = new HashMap<String, Object>();
106    
107            criteria.put(PdpPropertyConstants.ACH_TRANSACTION_TYPE, newAchTransactionType);
108            criteria.put(PdpPropertyConstants.PAYEE_IDENTIFIER_TYPE_CODE, newPayeeIdTypeCd);
109            criteria.put(PdpPropertyConstants.PAYEE_ID_NUMBER, newPayeeIdNumber);
110    
111            int matches = SpringContext.getBean(BusinessObjectService.class).countMatching(PayeeACHAccount.class, criteria);
112            if (matches > 0) {
113                putFieldError(PdpPropertyConstants.PAYEE_ID_NUMBER, KFSKeyConstants.ERROR_DOCUMENT_PAYEEACHACCOUNTMAINT_DUPLICATE_RECORD);
114                valid = false;
115            }
116    
117            return valid;
118        }
119    
120    }