1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
68
69 @Override
70 @Cacheable(value=Chart.CACHE_NAME,key="'UniversityChart'")
71 public Chart getUniversityChart() {
72
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
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
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
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 }