View Javadoc
1   /*
2    * Copyright 2005 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.coa.service.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.ole.coa.businessobject.Chart;
27  import org.kuali.ole.coa.businessobject.Organization;
28  import org.kuali.ole.coa.service.ChartService;
29  import org.kuali.ole.sys.OLEConstants;
30  import org.kuali.ole.sys.OLEPropertyConstants;
31  import org.kuali.ole.sys.context.SpringContext;
32  import org.kuali.ole.sys.identity.OleKimAttributes;
33  import org.kuali.ole.sys.service.NonTransactional;
34  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
35  import org.kuali.rice.kim.api.identity.Person;
36  import org.kuali.rice.kim.api.identity.PersonService;
37  import org.kuali.rice.kim.api.role.RoleService;
38  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
39  import org.kuali.rice.krad.service.BusinessObjectService;
40  import org.springframework.cache.annotation.Cacheable;
41  
42  /**
43   * This class is the service implementation for the Chart structure. This is the default, Kuali delivered implementation.
44   */
45  @NonTransactional
46  public class ChartServiceImpl implements ChartService {
47      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ChartServiceImpl.class);
48  
49      protected BusinessObjectService businessObjectService;
50      protected RoleService roleService;
51      protected PersonService personService;
52      protected ParameterService parameterService;
53  
54      /**
55       * @see org.kuali.ole.coa.service.ChartService#getByPrimaryId(java.lang.String)
56       */
57      @Override
58      @Cacheable(value=Chart.CACHE_NAME,key="'chartOfAccountsCode='+#p0")
59      public Chart getByPrimaryId(String chartOfAccountsCode) {
60          return businessObjectService.findBySinglePrimaryKey(Chart.class, chartOfAccountsCode);
61      }
62  
63      /**
64       * @see org.kuali.ole.coa.service.ChartService#getUniversityChart()
65       */
66      @Override
67      @Cacheable(value=Chart.CACHE_NAME,key="'UniversityChart'")
68      public Chart getUniversityChart() {
69          // 1. find the organization with the type which reports to itself
70          String organizationReportsToSelfParameterValue = parameterService.getParameterValueAsString(Organization.class, OLEConstants.ChartApcParms.ORG_MUST_REPORT_TO_SELF_ORG_TYPES);
71  
72          Map<String,String> orgCriteria = new HashMap<String, String>(2);
73          orgCriteria.put(OLEPropertyConstants.ORGANIZATION_TYPE_CODE, organizationReportsToSelfParameterValue);
74          orgCriteria.put(OLEPropertyConstants.ACTIVE, OLEConstants.ACTIVE_INDICATOR);
75          Collection<Organization> orgs = businessObjectService.findMatching(Organization.class, orgCriteria);
76          if ( orgs != null && !orgs.isEmpty() ) {
77              return getByPrimaryId(orgs.iterator().next().getChartOfAccountsCode());
78          }
79  
80          return null;
81      }
82  
83      /**
84       * @see org.kuali.ole.coa.service.ChartService#getAllChartCodes()
85       */
86      @Override
87      @Cacheable(value=Chart.CACHE_NAME,key="'AllChartCodes'")
88      public List<String> getAllChartCodes() {
89          Collection<Chart> charts = businessObjectService.findAllOrderBy(Chart.class, OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, true);
90          List<String> chartCodes = new ArrayList<String>(charts.size());
91          for (Chart chart : charts) {
92              chartCodes.add(chart.getChartOfAccountsCode());
93          }
94  
95          return chartCodes;
96      }
97  
98      @Override
99      @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 }