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.module.purap.document.validation.impl;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import org.kuali.kfs.module.purap.PurapKeyConstants;
022    import org.kuali.kfs.module.purap.businessobject.VendorStipulation;
023    import org.kuali.rice.kns.document.MaintenanceDocument;
024    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
025    import org.kuali.rice.kns.service.BusinessObjectService;
026    
027    /**
028     * Business rule(s) applicable to Purchase Order Contract Language maintenance document.
029     */
030    public class VendorStipulationRule extends MaintenanceDocumentRuleBase {
031    
032        private VendorStipulation newStipulation;
033        private VendorStipulation oldStipulation;
034        private BusinessObjectService boService;
035    
036        /**
037         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#setupConvenienceObjects()
038         */
039        @Override
040        public void setupConvenienceObjects() {
041            // setup newDelegateChange convenience objects, make sure all possible sub-objects are populated
042            newStipulation = (VendorStipulation) super.getNewBo();
043            oldStipulation = (VendorStipulation) super.getOldBo();
044            boService = (BusinessObjectService) super.getBoService();
045            super.setupConvenienceObjects();
046        }
047    
048        /**
049         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
050         */
051        protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
052            LOG.info("processCustomApproveDocumentBusinessRules called");
053            this.setupConvenienceObjects();
054            boolean success = this.checkForDuplicate();
055            return success && super.processCustomApproveDocumentBusinessRules(document);
056        }
057    
058        /**
059         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
060         */
061        protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
062            LOG.info("processCustomRouteDocumentBusinessRules called");
063            this.setupConvenienceObjects();
064            boolean success = this.checkForDuplicate();
065            return success && super.processCustomRouteDocumentBusinessRules(document);
066        }
067    
068        /**
069         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
070         */
071        protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
072            LOG.info("processCustomSaveDocumentBusinessRules called");
073            this.setupConvenienceObjects();
074            boolean success = this.checkForDuplicate();
075            return success && super.processCustomSaveDocumentBusinessRules(document);
076        }
077    
078        /**
079         * Check to see if data duplicates existing data.
080         * 
081         * @return boolean indicating if validation succeeded.
082         */
083        protected boolean checkForDuplicate() {
084            LOG.info("checkForDuplicate called");
085            boolean success = true;
086            Map fieldValues = new HashMap();
087            
088            if (oldStipulation.getVendorStipulationIdentifier() != null && newStipulation.getVendorStipulationIdentifier() != null &&
089                oldStipulation.getVendorStipulationIdentifier().equals(newStipulation.getVendorStipulationIdentifier()) &&
090                oldStipulation.getVendorStipulationName().equals(newStipulation.getVendorStipulationName())){
091                return true;
092            }else{
093                fieldValues.put("vendorStipulationName", newStipulation.getVendorStipulationName());
094                if (boService.countMatching(newStipulation.getClass(), fieldValues) != 0) {
095                    success &= false;
096                    putGlobalError(PurapKeyConstants.PURAP_GENERAL_POTENTIAL_DUPLICATE);
097                }
098            }    
099            
100            return success;
101        }
102    }