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