001/*
002 * Copyright 2007 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.kuali.ole.coa.businessobject.Chart;
019import org.kuali.ole.coa.businessobject.Organization;
020import org.kuali.ole.coa.service.ChartService;
021import org.kuali.ole.coa.service.OrganizationService;
022import org.kuali.ole.module.purap.PurapKeyConstants;
023import org.kuali.ole.module.purap.businessobject.OrganizationParameter;
024import org.kuali.ole.sys.OLEPropertyConstants;
025import org.kuali.ole.sys.context.SpringContext;
026import org.kuali.rice.kns.document.MaintenanceDocument;
027import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
028import org.kuali.rice.krad.service.BusinessObjectService;
029import org.kuali.rice.krad.util.GlobalVariables;
030import org.kuali.rice.krad.util.MessageMap;
031import org.kuali.rice.krad.util.ObjectUtils;
032
033/**
034 * Business rule(s) applicable to Organization Parameter maintenance document.
035 */
036public class OrganizationParameterRule extends MaintenanceDocumentRuleBase {
037
038    protected OrganizationParameter newOrganizationParameter;
039    protected BusinessObjectService boService;
040
041    /**
042     * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#setupConvenienceObjects()
043     */
044    @Override
045    public void setupConvenienceObjects() {
046        // setup newDelegateChange convenience objects, make sure all possible sub-objects are populated
047        newOrganizationParameter = (OrganizationParameter) super.getNewBo();
048        boService = (BusinessObjectService) super.getBoService();
049        super.setupConvenienceObjects();
050    }
051
052    /**
053     * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
054     */
055    protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
056        LOG.info("processCustomApproveDocumentBusinessRules called");
057        this.setupConvenienceObjects();
058        boolean success = true;
059        success &= this.checkChartOfAccountsCode();
060        success &= this.checkOrganizationCode();
061        return success && super.processCustomApproveDocumentBusinessRules(document);
062    }
063
064    /**
065     * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
066     */
067    protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
068        LOG.info("processCustomRouteDocumentBusinessRules called");
069        this.setupConvenienceObjects();
070        boolean success = true;
071        success &= this.checkChartOfAccountsCode();
072        success &= this.checkOrganizationCode();
073        return success && super.processCustomRouteDocumentBusinessRules(document);
074    }
075
076    /**
077     * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
078     */
079    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
080        LOG.info("processCustomSaveDocumentBusinessRules called");
081        this.setupConvenienceObjects();
082        boolean success = true;
083        success &= this.checkChartOfAccountsCode();
084        success &= this.checkOrganizationCode();
085        return success && super.processCustomSaveDocumentBusinessRules(document);
086    }
087
088    /**
089     * Validate chart of accounts code
090     *
091     * @return Boolean indicating if validation succeeded
092     */
093    protected boolean checkChartOfAccountsCode() {
094        LOG.info("checkChartOfAccountsCode called");
095        MessageMap errorMap = GlobalVariables.getMessageMap();
096        boolean success = true;
097        Chart chart = SpringContext.getBean(ChartService.class).getByPrimaryId(newOrganizationParameter.getChartOfAccountsCode());
098        if (ObjectUtils.isNull(chart)) {
099            success &= false;
100            errorMap.putError(OLEPropertyConstants.DOCUMENT + "." + OLEPropertyConstants.NEW_MAINTAINABLE_OBJECT + "." + OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, PurapKeyConstants.ERROR_INVALID_CHART_OF_ACCOUNTS_CODE);
101        }
102        return success;
103    }
104
105    /**
106     * Validate organization code
107     *
108     * @return Boolean indicating if validation succeeded
109     */
110    protected boolean checkOrganizationCode() {
111        LOG.info("checkOrganizationCode called");
112        MessageMap errorMap = GlobalVariables.getMessageMap();
113        boolean success = true;
114        Organization org = SpringContext.getBean(OrganizationService.class).getByPrimaryId(newOrganizationParameter.getChartOfAccountsCode(), newOrganizationParameter.getOrganizationCode());
115        if (ObjectUtils.isNull(org)) {
116            success &= false;
117            errorMap.putError(OLEPropertyConstants.DOCUMENT + "." + OLEPropertyConstants.NEW_MAINTAINABLE_OBJECT + "." + OLEPropertyConstants.ORGANIZATION_CODE, PurapKeyConstants.ERROR_INVALID_ORGANIZATION_CODE);
118        }
119        return success;
120    }
121
122}