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.Chart;
019    import org.kuali.kfs.coa.service.ChartService;
020    import org.kuali.kfs.sys.KFSKeyConstants;
021    import org.kuali.rice.kim.bo.Person;
022    import org.kuali.kfs.sys.context.SpringContext;
023    import org.kuali.kfs.sys.service.FinancialSystemUserService;
024    import org.kuali.rice.kim.service.PersonService;
025    import org.kuali.rice.kns.document.MaintenanceDocument;
026    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
027    
028    /**
029     * Business rule(s) applicable to {@link ChartMaintenance} documents.
030     */
031    public class ChartRule extends MaintenanceDocumentRuleBase {
032    
033        /**
034         * This method calls specific rules for routing on Chart Maintenance documents Specifically it checks to make sure that
035         * reportsToChart exists if it is not the same code as the newly created Chart and it checks to make sure that the chart manager
036         * is valid for the Chart Module
037         * 
038         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
039         * @return false if reports to chart code doesn't exist or user is invalid for this module
040         */
041        protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
042    
043            boolean result = true;
044    
045            Chart chart = (Chart) document.getNewMaintainableObject().getBusinessObject();
046            ChartService chartService = SpringContext.getBean(ChartService.class);
047            PersonService personService = SpringContext.getBean(PersonService.class);  
048            
049    
050            String chartCode = chart.getChartOfAccountsCode();
051    
052            String reportsToChartCode = chart.getReportsToChartOfAccountsCode();
053    
054            if (chartCode != null && !chartCode.equals(reportsToChartCode)) { // if not equal to this newly created chart, then must
055                                                                                // exist
056                Chart reportsToChart = chartService.getByPrimaryId(reportsToChartCode);
057                if (reportsToChart == null) {
058                    result = false;
059                    putFieldError("reportsToChartOfAccountsCode", KFSKeyConstants.ERROR_DOCUMENT_CHART_REPORTS_TO_CHART_MUST_EXIST);
060                }
061            }
062    
063            Person chartManager = personService.getPerson(chart.getFinCoaManagerPrincipalId());
064            if ( chartManager == null ) {
065                result = false;
066                putFieldError("finCoaManagerUniversal.principalName", KFSKeyConstants.ERROR_DOCUMENT_CHART_MANAGER_MUST_EXIST);
067            }
068    
069            if (chartManager != null && !SpringContext.getBean(FinancialSystemUserService.class).isActiveFinancialSystemUser(chartManager)) {
070                result = false;
071                putFieldError("finCoaManagerUniversal.principalName", KFSKeyConstants.ERROR_DOCUMENT_CHART_MANAGER_MUST_BE_KUALI_USER);
072            }
073    
074    
075            return result;
076    
077        }
078    
079    }
080