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.coa.document.validation.impl;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.kuali.ole.coa.businessobject.ObjectConsolidation;
022import org.kuali.ole.coa.businessobject.ObjectLevel;
023import org.kuali.ole.sys.OLEKeyConstants;
024import org.kuali.rice.kns.document.MaintenanceDocument;
025import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
026
027/**
028 * 
029 * This class implements the business rules for {@link ObjLevel}
030 */
031public class ObjectLevelRule extends MaintenanceDocumentRuleBase {
032    /**
033     * This performs rules checks on document save
034     * <ul>
035     * <li>{@link ObjectLevelRule#checkObjConsCode()}</li>
036     * </ul>
037     * This rule does not fail on business rule failures
038     * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
039     */
040    @Override
041    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
042        checkObjConsCode();
043        return true;
044    }
045
046    /**
047     * This performs rules checks on document route
048     * <ul>
049     * <li>{@link ObjectLevelRule#checkObjConsCode()}</li>
050     * </ul>
051     * This rule fails on business rule failures
052     * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
053     */
054    @Override
055    protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
056        boolean success = true;
057        success &= checkObjConsCode();
058        return success;
059    }
060
061    /**
062     * This method checks to see if the Object Consolidation code matches a pre-existing Object Level code that is already entered.
063     * If it does it returns false with an error
064     * 
065     * @param document
066     * @return false if Object Level Code already exists
067     */
068    protected boolean checkObjConsCode() {
069        boolean success = true;
070        ObjectLevel objLevel = (ObjectLevel) super.getNewBo();
071        String chartOfAccountsCode = objLevel.getChartOfAccountsCode();
072        String finConsolidationObjectCode = objLevel.getFinancialObjectLevelCode();
073        Map primaryKeys = new HashMap();
074        primaryKeys.put("chartOfAccountsCode", chartOfAccountsCode);
075        primaryKeys.put("finConsolidationObjectCode", finConsolidationObjectCode);
076        ObjectConsolidation objCons = (ObjectConsolidation) getBoService().findByPrimaryKey(ObjectConsolidation.class, primaryKeys);
077        if (objCons != null) {
078            success = false;
079            putFieldError("financialObjectLevelCode", OLEKeyConstants.ERROR_DOCUMENT_OBJLEVELMAINT_ALREADY_EXISTS_AS_OBJCONS);
080        }
081        return success;
082    }
083}