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.coa.document.validation.impl;
017    
018    import org.kuali.kfs.coa.businessobject.ObjectLevel;
019    import org.kuali.kfs.coa.businessobject.ObjectConsolidation;
020    import org.kuali.kfs.coa.service.ChartService;
021    import org.kuali.kfs.coa.service.ObjectCodeService;
022    import org.kuali.kfs.coa.service.ObjectLevelService;
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    /**
028     * 
029     * This class implements the business rules for {@link ObjectCons}
030     */
031    public class ObjectConsRule extends MaintenanceDocumentRuleBase {
032    
033        protected static ChartService chartService;
034        protected static ObjectLevelService objectLevelService;
035        protected static ObjectCodeService objectCodeService;
036        
037        /**
038         * 
039         * Constructs a {@link ObjectConsRule}
040         * Pseudo-injects some services
041         */
042        public ObjectConsRule() {
043            if (chartService == null) {
044                objectLevelService = SpringContext.getBean(ObjectLevelService.class);
045                objectCodeService = SpringContext.getBean(ObjectCodeService.class);
046                chartService = SpringContext.getBean(ChartService.class);
047            }
048        }
049    
050        /**
051         * This performs rules checks on document save
052         * <ul>
053         * <li>{@link ObjectConsRule#checkObjLevelCode(ObjectCons)}</li>
054         * </ul>
055         * This rule does not fail on business rule failures
056         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
057         */
058        @Override
059        protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
060            ObjectConsolidation objConsolidation = (ObjectConsolidation) getNewBo();
061    
062            checkObjLevelCode(objConsolidation);
063            return true;
064        }
065    
066        /**
067         * This performs rules checks on document route
068         * <ul>
069         * <li>{@link ObjectConsRule#checkObjLevelCode(ObjectCons)}</li>
070         * </ul>
071         * This rule fails on business rule failures
072         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
073         */
074        @Override
075        protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
076            boolean success = true;
077            ObjectConsolidation objConsolidation = (ObjectConsolidation) getNewBo();
078    
079            success &= checkObjLevelCode(objConsolidation);
080            return success;
081        }
082    
083        /**
084         * This method checks to see if the Object Consolidation code matches a pre-existing Object Level code that is already entered.
085         * If it does it returns false with an error
086         * 
087         * @param document
088         * @return false if Object Level Code already exists
089         */
090        protected boolean checkObjLevelCode(ObjectConsolidation objConsolidation) {
091            boolean success = true;
092    
093            ObjectLevel objLevel = objectLevelService.getByPrimaryId(objConsolidation.getChartOfAccountsCode(), objConsolidation.getFinConsolidationObjectCode());
094            if (objLevel != null) {
095                success = false;
096                putFieldError("finConsolidationObjectCode", KFSKeyConstants.ERROR_DOCUMENT_OBJCONSMAINT_ALREADY_EXISTS_AS_OBJLEVEL);
097            }
098            return success;
099        }
100    }