View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.coa.service.impl;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.kuali.kfs.coa.businessobject.Chart;
30  import org.kuali.kfs.coa.businessobject.Organization;
31  import org.kuali.kfs.coa.service.ChartService;
32  import org.kuali.kfs.sys.KFSConstants;
33  import org.kuali.kfs.sys.KFSPropertyConstants;
34  import org.kuali.kfs.sys.context.SpringContext;
35  import org.kuali.kfs.sys.identity.KfsKimAttributes;
36  import org.kuali.kfs.sys.service.NonTransactional;
37  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
38  import org.kuali.rice.kim.api.identity.Person;
39  import org.kuali.rice.kim.api.identity.PersonService;
40  import org.kuali.rice.kim.api.role.RoleService;
41  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
42  import org.kuali.rice.krad.service.BusinessObjectService;
43  import org.springframework.cache.annotation.Cacheable;
44  
45  /**
46   * This class is the service implementation for the Chart structure. This is the default, Kuali delivered implementation.
47   */
48  @NonTransactional
49  public class ChartServiceImpl implements ChartService {
50      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ChartServiceImpl.class);
51  
52      protected BusinessObjectService businessObjectService;
53      protected RoleService roleService;
54      protected PersonService personService;
55      protected ParameterService parameterService;
56  
57      /**
58       * @see org.kuali.kfs.coa.service.ChartService#getByPrimaryId(java.lang.String)
59       */
60      @Override
61      @Cacheable(value=Chart.CACHE_NAME,key="'chartOfAccountsCode='+#p0")
62      public Chart getByPrimaryId(String chartOfAccountsCode) {
63          return businessObjectService.findBySinglePrimaryKey(Chart.class, chartOfAccountsCode);
64      }
65  
66      /**
67       * @see org.kuali.kfs.coa.service.ChartService#getUniversityChart()
68       */
69      @Override
70      @Cacheable(value=Chart.CACHE_NAME,key="'UniversityChart'")
71      public Chart getUniversityChart() {
72          // 1. find the organization with the type which reports to itself
73          String organizationReportsToSelfParameterValue = parameterService.getParameterValueAsString(Organization.class, KFSConstants.ChartApcParms.ORG_MUST_REPORT_TO_SELF_ORG_TYPES);
74  
75          Map<String,String> orgCriteria = new HashMap<String, String>(2);
76          orgCriteria.put(KFSPropertyConstants.ORGANIZATION_TYPE_CODE, organizationReportsToSelfParameterValue);
77          orgCriteria.put(KFSPropertyConstants.ACTIVE, KFSConstants.ACTIVE_INDICATOR);
78          Collection<Organization> orgs = businessObjectService.findMatching(Organization.class, orgCriteria);
79          if ( orgs != null && !orgs.isEmpty() ) {
80              return getByPrimaryId(orgs.iterator().next().getChartOfAccountsCode());
81          }
82  
83          return null;
84      }
85  
86      /**
87       * @see org.kuali.kfs.coa.service.ChartService#getAllChartCodes()
88       */
89      @Override
90      @Cacheable(value=Chart.CACHE_NAME,key="'AllChartCodes'")
91      public List<String> getAllChartCodes() {
92          Collection<Chart> charts = businessObjectService.findAllOrderBy(Chart.class, KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, true);
93          return getChartCodes(charts);
94      }
95  
96      /**
97       * @see org.kuali.kfs.coa.service.ChartService#getAllActiveChartCodes()
98       */
99      @Override
100     @Cacheable(value=Chart.CACHE_NAME,key="'AllActiveChartCodes'")
101     public List<String> getAllActiveChartCodes() {
102         return getChartCodes(getAllActiveCharts());
103     }
104 
105     @Override
106     @Cacheable(value=Chart.CACHE_NAME,key="'AllActiveCharts'")
107     public Collection<Chart> getAllActiveCharts() {
108         return businessObjectService.findMatchingOrderBy(Chart.class,
109                 Collections.singletonMap(KFSPropertyConstants.ACTIVE, true),
110                 KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, true);
111     }
112 
113     /**
114      * @see org.kuali.module.chart.service.getReportsToHierarchy()
115      */
116     @Override
117     @Cacheable(value=Chart.CACHE_NAME,key="'ReportsToHierarchy'")
118     public Map<String, String> getReportsToHierarchy() {
119         Map<String, String> reportsToHierarchy = new HashMap<String, String>();
120 
121         for ( String chartCode : getAllChartCodes() ) {
122             Chart chart = businessObjectService.findBySinglePrimaryKey(Chart.class, chartCode);
123 
124             if (LOG.isDebugEnabled()) {
125                 LOG.debug("adding " + chart.getChartOfAccountsCode() + "-->" + chart.getReportsToChartOfAccountsCode());
126             }
127             reportsToHierarchy.put(chart.getChartOfAccountsCode(), chart.getReportsToChartOfAccountsCode());
128         }
129 
130         return reportsToHierarchy;
131     }
132 
133     @Override
134     @Cacheable(value=Chart.CACHE_NAME,key="'{isParentChart?}'+#p0+'-->'+#p1")
135     public boolean isParentChart(String potentialChildChartCode, String potentialParentChartCode) {
136         if ((potentialChildChartCode == null) || (potentialParentChartCode == null)) {
137             throw new IllegalArgumentException("The isParentChartCode method requires a non-null potentialChildChartCode and potentialParentChartCode");
138         }
139         Chart thisChart = getByPrimaryId(potentialChildChartCode);
140         if ((thisChart == null) || StringUtils.isBlank(thisChart.getChartOfAccountsCode())) {
141             throw new IllegalArgumentException("The isParentChartCode method requires a valid potentialChildChartCode");
142         }
143         if (thisChart.getCode().equals(thisChart.getReportsToChartOfAccountsCode())) {
144             return false;
145         }
146         else if (potentialParentChartCode.equals(thisChart.getReportsToChartOfAccountsCode())) {
147             return true;
148         }
149         else {
150             return isParentChart(thisChart.getReportsToChartOfAccountsCode(), potentialParentChartCode);
151         }
152     }
153 
154     @Override
155     public Person getChartManager(String chartOfAccountsCode) {
156         String chartManagerId = null;
157         Person chartManager = null;
158 
159         Map<String,String> qualification = new HashMap<String,String>();
160         qualification.put(KfsKimAttributes.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
161 
162         Collection<String> chartManagerList = getRoleService().getRoleMemberPrincipalIds(KFSConstants.CoreModuleNamespaces.KFS, KFSConstants.SysKimApiConstants.CHART_MANAGER_KIM_ROLE_NAME, qualification);
163 
164         if (!chartManagerList.isEmpty()) {
165             chartManagerId = chartManagerList.iterator().next();
166         }
167 
168         if (chartManagerId != null) {
169             chartManager = getPersonService().getPerson(chartManagerId);
170         }
171 
172         return chartManager;
173     }
174 
175     protected List<String> getChartCodes(Collection<Chart> charts) {
176         List<String> chartCodes = new ArrayList<String>(charts.size());
177         for (Chart chart : charts) {
178             chartCodes.add(chart.getChartOfAccountsCode());
179         }
180         return chartCodes;
181     }
182 
183     protected RoleService getRoleService() {
184         if ( roleService == null ) {
185             roleService = KimApiServiceLocator.getRoleService();
186         }
187         return roleService;
188     }
189     protected PersonService getPersonService() {
190         if (personService == null) {
191             personService = SpringContext.getBean(PersonService.class);
192         }
193         return personService;
194     }
195 
196     public void setParameterService(ParameterService parameterService) {
197         this.parameterService = parameterService;
198     }
199     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
200         this.businessObjectService = businessObjectService;
201     }
202 
203 }