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.List;
019    
020    import org.kuali.kfs.module.purap.PurapParameterConstants;
021    import org.kuali.kfs.module.purap.businessobject.FundingSource;
022    import org.kuali.kfs.module.purap.document.RequisitionDocument;
023    import org.kuali.kfs.sys.KFSKeyConstants;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.rice.kns.document.MaintenanceDocument;
026    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
027    import org.kuali.rice.kns.service.DataDictionaryService;
028    import org.kuali.rice.kns.service.ParameterService;
029    
030    /* 
031     * 
032    */
033    public class FundingSourceRule extends MaintenanceDocumentRuleBase {
034    
035        protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
036            LOG.info("processCustomApproveDocumentBusinessRules called");
037            this.setupConvenienceObjects();
038            boolean success = this.checkForSystemParametersExistence();
039            return success && super.processCustomApproveDocumentBusinessRules(document);
040        }
041    
042        protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
043            LOG.info("processCustomRouteDocumentBusinessRules called");
044            this.setupConvenienceObjects();
045            boolean success = this.checkForSystemParametersExistence();
046            return success && super.processCustomRouteDocumentBusinessRules(document);
047        }
048    
049        protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
050            LOG.info("processCustomSaveDocumentBusinessRules called");
051            this.setupConvenienceObjects();
052            boolean success = this.checkForSystemParametersExistence();
053            return success && super.processCustomSaveDocumentBusinessRules(document);
054        }
055    
056        protected boolean checkForSystemParametersExistence() {
057            LOG.info("checkForSystemParametersExistence called");
058            boolean success = true;
059            List<String> parameterValues = SpringContext.getBean(ParameterService.class).getParameterValues(RequisitionDocument.class, PurapParameterConstants.DEFAULT_FUNDING_SOURCE);
060            FundingSource newFundingSource = (FundingSource)getNewBo();
061            FundingSource oldFundingSource= (FundingSource)getOldBo();
062            //If the new funding source code matches with the funding source in the DEFAULT_FUNDING_SOURCE 
063            //system parameters then we can't inactivate this funding source, so return false.
064            if (parameterValues.contains(newFundingSource.getFundingSourceCode()) && ! newFundingSource.isActive() && oldFundingSource.isActive()) {
065                success = false;
066                String documentLabel = SpringContext.getBean(DataDictionaryService.class).getDocumentLabelByClass(newFundingSource.getClass());
067                putGlobalError(KFSKeyConstants.ERROR_CANNOT_INACTIVATE_USED_IN_SYSTEM_PARAMETERS, documentLabel);
068            }
069            return success;
070        }
071    }