001/*
002 * Copyright 2005 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.service.impl;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import org.apache.commons.lang.StringUtils;
026import org.kuali.ole.coa.businessobject.Chart;
027import org.kuali.ole.coa.businessobject.Organization;
028import org.kuali.ole.coa.service.ChartService;
029import org.kuali.ole.sys.OLEConstants;
030import org.kuali.ole.sys.OLEPropertyConstants;
031import org.kuali.ole.sys.context.SpringContext;
032import org.kuali.ole.sys.identity.OleKimAttributes;
033import org.kuali.ole.sys.service.NonTransactional;
034import org.kuali.rice.coreservice.framework.parameter.ParameterService;
035import org.kuali.rice.kim.api.identity.Person;
036import org.kuali.rice.kim.api.identity.PersonService;
037import org.kuali.rice.kim.api.role.RoleService;
038import org.kuali.rice.kim.api.services.KimApiServiceLocator;
039import org.kuali.rice.krad.service.BusinessObjectService;
040import org.springframework.cache.annotation.Cacheable;
041
042/**
043 * This class is the service implementation for the Chart structure. This is the default, Kuali delivered implementation.
044 */
045@NonTransactional
046public class ChartServiceImpl implements ChartService {
047    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ChartServiceImpl.class);
048
049    protected BusinessObjectService businessObjectService;
050    protected RoleService roleService;
051    protected PersonService personService;
052    protected ParameterService parameterService;
053
054    /**
055     * @see org.kuali.ole.coa.service.ChartService#getByPrimaryId(java.lang.String)
056     */
057    @Override
058    @Cacheable(value=Chart.CACHE_NAME,key="'chartOfAccountsCode='+#p0")
059    public Chart getByPrimaryId(String chartOfAccountsCode) {
060        return businessObjectService.findBySinglePrimaryKey(Chart.class, chartOfAccountsCode);
061    }
062
063    /**
064     * @see org.kuali.ole.coa.service.ChartService#getUniversityChart()
065     */
066    @Override
067    @Cacheable(value=Chart.CACHE_NAME,key="'UniversityChart'")
068    public Chart getUniversityChart() {
069        // 1. find the organization with the type which reports to itself
070        String organizationReportsToSelfParameterValue = parameterService.getParameterValueAsString(Organization.class, OLEConstants.ChartApcParms.ORG_MUST_REPORT_TO_SELF_ORG_TYPES);
071
072        Map<String,String> orgCriteria = new HashMap<String, String>(2);
073        orgCriteria.put(OLEPropertyConstants.ORGANIZATION_TYPE_CODE, organizationReportsToSelfParameterValue);
074        orgCriteria.put(OLEPropertyConstants.ACTIVE, OLEConstants.ACTIVE_INDICATOR);
075        Collection<Organization> orgs = businessObjectService.findMatching(Organization.class, orgCriteria);
076        if ( orgs != null && !orgs.isEmpty() ) {
077            return getByPrimaryId(orgs.iterator().next().getChartOfAccountsCode());
078        }
079
080        return null;
081    }
082
083    /**
084     * @see org.kuali.ole.coa.service.ChartService#getAllChartCodes()
085     */
086    @Override
087    @Cacheable(value=Chart.CACHE_NAME,key="'AllChartCodes'")
088    public List<String> getAllChartCodes() {
089        Collection<Chart> charts = businessObjectService.findAllOrderBy(Chart.class, OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, true);
090        List<String> chartCodes = new ArrayList<String>(charts.size());
091        for (Chart chart : charts) {
092            chartCodes.add(chart.getChartOfAccountsCode());
093        }
094
095        return chartCodes;
096    }
097
098    @Override
099    @Cacheable(value=Chart.CACHE_NAME,key="'AllActiveCharts'")
100    public Collection<Chart> getAllActiveCharts() {
101        return businessObjectService.findMatchingOrderBy(Chart.class,
102                Collections.singletonMap(OLEPropertyConstants.ACTIVE, true),
103                OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, true);
104    }
105
106    /**
107     * @see org.kuali.module.chart.service.getReportsToHierarchy()
108     */
109    @Override
110    @Cacheable(value=Chart.CACHE_NAME,key="'ReportsToHierarchy'")
111    public Map<String, String> getReportsToHierarchy() {
112        Map<String, String> reportsToHierarchy = new HashMap<String, String>();
113
114        for ( String chartCode : getAllChartCodes() ) {
115            Chart chart = businessObjectService.findBySinglePrimaryKey(Chart.class, chartCode);
116
117            if (LOG.isDebugEnabled()) {
118                LOG.debug("adding " + chart.getChartOfAccountsCode() + "-->" + chart.getReportsToChartOfAccountsCode());
119            }
120            reportsToHierarchy.put(chart.getChartOfAccountsCode(), chart.getReportsToChartOfAccountsCode());
121        }
122
123        return reportsToHierarchy;
124    }
125
126    @Override
127    @Cacheable(value=Chart.CACHE_NAME,key="'{isParentChart?}'+#p0+'-->'+#p1")
128    public boolean isParentChart(String potentialChildChartCode, String potentialParentChartCode) {
129        if ((potentialChildChartCode == null) || (potentialParentChartCode == null)) {
130            throw new IllegalArgumentException("The isParentChartCode method requires a non-null potentialChildChartCode and potentialParentChartCode");
131        }
132        Chart thisChart = getByPrimaryId(potentialChildChartCode);
133        if ((thisChart == null) || StringUtils.isBlank(thisChart.getChartOfAccountsCode())) {
134            throw new IllegalArgumentException("The isParentChartCode method requires a valid potentialChildChartCode");
135        }
136        if (thisChart.getCode().equals(thisChart.getReportsToChartOfAccountsCode())) {
137            return false;
138        }
139        else if (potentialParentChartCode.equals(thisChart.getReportsToChartOfAccountsCode())) {
140            return true;
141        }
142        else {
143            return isParentChart(thisChart.getReportsToChartOfAccountsCode(), potentialParentChartCode);
144        }
145    }
146
147    /**
148     * @see org.kuali.ole.coa.service.ChartService#getChartManager(java.lang.String)
149     */
150    @Override
151    public Person getChartManager(String chartOfAccountsCode) {
152        String chartManagerId = null;
153        Person chartManager = null;
154
155        Map<String,String> qualification = new HashMap<String,String>();
156        qualification.put(OleKimAttributes.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
157
158        Collection<String> chartManagerList = getRoleService().getRoleMemberPrincipalIds(OLEConstants.CoreModuleNamespaces.OLE, OLEConstants.SysKimApiConstants.CHART_MANAGER_KIM_ROLE_NAME, qualification);
159
160        if (!chartManagerList.isEmpty()) {
161            chartManagerId = chartManagerList.iterator().next();
162        }
163
164        if (chartManagerId != null) {
165            chartManager = getPersonService().getPerson(chartManagerId);
166        }
167
168        return chartManager;
169    }
170
171    protected RoleService getRoleService() {
172        if ( roleService == null ) {
173            roleService = KimApiServiceLocator.getRoleService();
174    }
175        return roleService;
176    }
177    protected PersonService getPersonService() {
178        if (personService == null) {
179            personService = SpringContext.getBean(PersonService.class);
180        }
181        return personService;
182    }
183
184    public void setParameterService(ParameterService parameterService) {
185        this.parameterService = parameterService;
186    }
187    public void setBusinessObjectService(BusinessObjectService businessObjectService) {
188        this.businessObjectService = businessObjectService;
189    }
190
191}