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.service.impl;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.HashMap;
021    import java.util.Iterator;
022    import java.util.List;
023    import java.util.Map;
024    
025    import org.apache.commons.lang.StringUtils;
026    import org.kuali.kfs.coa.businessobject.Chart;
027    import org.kuali.kfs.coa.dataaccess.ChartDao;
028    import org.kuali.kfs.coa.service.ChartService;
029    import org.kuali.kfs.sys.KFSConstants;
030    import org.kuali.kfs.sys.context.SpringContext;
031    import org.kuali.kfs.sys.identity.KfsKimAttributes;
032    import org.kuali.kfs.sys.service.NonTransactional;
033    import org.kuali.rice.kim.bo.Person;
034    import org.kuali.rice.kim.bo.role.dto.RoleMembershipInfo;
035    import org.kuali.rice.kim.bo.types.dto.AttributeSet;
036    import org.kuali.rice.kim.service.PersonService;
037    import org.kuali.rice.kim.service.RoleManagementService;
038    import org.kuali.rice.kns.service.BusinessObjectDictionaryService;
039    import org.kuali.rice.kns.util.spring.Cached;
040    
041    /**
042     * This class is the service implementation for the Chart structure. This is the default, Kuali delivered implementation.
043     */
044    @NonTransactional
045    public class ChartServiceImpl implements ChartService {
046        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ChartServiceImpl.class);
047    
048        private ChartDao chartDao;
049        private RoleManagementService roleManagementService;
050        private PersonService<Person> personService;
051    
052        /**
053         * @see org.kuali.kfs.coa.service.ChartService#getByPrimaryId(java.lang.String)
054         */
055        public Chart getByPrimaryId(String chartOfAccountsCode) {
056            return chartDao.getByPrimaryId(chartOfAccountsCode);
057        }
058    
059        /**
060         * 
061         * @see org.kuali.kfs.coa.service.ChartService#getUniversityChart()
062         */
063        public Chart getUniversityChart() {
064            return chartDao.getUniversityChart();
065        }
066    
067        /**
068         * @see org.kuali.kfs.coa.service.ChartService#getAllChartCodes()
069         */
070        public List<String> getAllChartCodes() {
071            Collection<Chart> charts = chartDao.getAll();
072            List<String> chartCodes = new ArrayList<String>();
073            for ( Chart chart : charts ) {
074                chartCodes.add( chart.getChartOfAccountsCode() );
075            }
076    
077            return chartCodes;
078        }
079    
080    
081        /**
082         * @see org.kuali.module.chart.service.getReportsToHierarchy()
083         */
084        @Cached
085        public Map<String, String> getReportsToHierarchy() {
086    
087            LOG.debug("getReportsToHierarchy");
088            Map<String, String> reportsToHierarchy = new HashMap();
089    
090            Iterator iter = getAllChartCodes().iterator();
091            while (iter.hasNext()) {
092                Chart chart = (Chart) getByPrimaryId((String) iter.next());
093    
094                if (LOG.isDebugEnabled()) {
095                    LOG.debug("adding " + chart.getChartOfAccountsCode() + "-->" + chart.getReportsToChartOfAccountsCode());
096                }
097                reportsToHierarchy.put(chart.getChartOfAccountsCode(), chart.getReportsToChartOfAccountsCode());
098            }
099    
100            return reportsToHierarchy;
101        }
102    
103        /**
104         * @see org.kuali.kfs.coa.service.ChartService#getChartsThatUserIsResponsibleFor(org.kuali.rice.kns.bo.user.KualiUser)
105         */
106        public List getChartsThatUserIsResponsibleFor(Person person) {
107            if (LOG.isDebugEnabled()) {
108                LOG.debug("retrieving chartsResponsible list for user " + person.getName());
109            }
110    
111            // gets the list of accounts that the user is the Fiscal Officer of
112            List chartList = chartDao.getChartsThatUserIsResponsibleFor(person);
113            if (LOG.isDebugEnabled()) {
114                LOG.debug("retrieved chartsResponsible list for user " + person.getName());
115            }
116            return chartList;
117        }
118        
119        public boolean isParentChart(String potentialChildChartCode, String potentialParentChartCode) {
120            if ((potentialChildChartCode == null) || (potentialParentChartCode == null)) {
121                throw new IllegalArgumentException("The isParentChartCode method requires a non-null potentialChildChartCode and potentialParentChartCode");
122            }
123            Chart thisChart = getByPrimaryId(potentialChildChartCode);
124            if ((thisChart == null) || StringUtils.isBlank(thisChart.getChartOfAccountsCode())) {
125                throw new IllegalArgumentException("The isParentChartCode method requires a valid potentialChildChartCode");
126            }
127            if (thisChart.getCode().equals(thisChart.getReportsToChartOfAccountsCode())) {
128                return false;
129            }
130            else if (potentialParentChartCode.equals(thisChart.getReportsToChartOfAccountsCode())) {
131                return true;
132            }
133            else {
134                return isParentChart(thisChart.getReportsToChartOfAccountsCode(), potentialParentChartCode);
135            }
136        }
137    
138        /**
139         * @see org.kuali.kfs.coa.service.ChartService#getChartManager(java.lang.String)
140         */
141        public Person getChartManager(String chartOfAccountsCode) {
142            String chartManagerId = null;
143            Person chartManager = null;
144    
145            AttributeSet qualification = new AttributeSet();
146            qualification.put(KfsKimAttributes.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
147    
148            Collection<String> chartManagerList = roleManagementService.getRoleMemberPrincipalIds(KFSConstants.ParameterNamespaces.KFS, KFSConstants.SysKimConstants.CHART_MANAGER_KIM_ROLE_NAME, qualification);
149    
150            if ( !chartManagerList.isEmpty() ) {
151                chartManagerId = chartManagerList.iterator().next();
152            }
153    
154            if (chartManagerId != null) {
155                chartManager = getPersonService().getPerson(chartManagerId);
156            }
157    
158            return chartManager;
159        }
160    
161        /**
162         * @return Returns the chartDao.
163         */
164        public ChartDao getChartDao() {
165            return chartDao;
166        }
167    
168        /**
169         * @param chartDao The chartDao to set.
170         */
171        public void setChartDao(ChartDao chartDao) {
172            this.chartDao = chartDao;
173        }
174    
175        /**
176         * Gets the roleManagementService attribute.
177         * 
178         * @return Returns the roleManagementService.
179         */
180        public RoleManagementService getRoleManagementService() {
181            return roleManagementService;
182        }
183    
184        /**
185         * Sets the roleManagementService attribute value.
186         * 
187         * @param roleManagementService The roleManagementService to set.
188         */
189        public void setRoleManagementService(RoleManagementService roleManagementService) {
190            this.roleManagementService = roleManagementService;
191        }
192    
193        /**
194         * @return Returns the personService.
195         */
196        protected PersonService<Person> getPersonService() {
197            if(personService==null)
198                personService = SpringContext.getBean(PersonService.class);
199            return personService;
200        }
201    
202    }
203