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.HashMap;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.kfs.coa.businessobject.Organization;
024    import org.kuali.kfs.coa.dataaccess.OrganizationDao;
025    import org.kuali.kfs.coa.service.ChartService;
026    import org.kuali.kfs.coa.service.OrganizationService;
027    import org.kuali.kfs.sys.KFSConstants.ChartApcParms;
028    import org.kuali.kfs.sys.context.SpringContext;
029    import org.kuali.kfs.sys.service.NonTransactional;
030    import org.kuali.rice.kns.service.BusinessObjectService;
031    import org.kuali.rice.kns.service.ParameterService;
032    import org.kuali.rice.kns.util.spring.Cached;
033    
034    /**
035     * This class is the service implementation for the Org structure. This is the default implementation, that is delivered with Kuali.
036     */
037    
038    @NonTransactional
039    public class OrganizationServiceImpl implements OrganizationService {
040        private OrganizationDao organizationDao;
041        private ParameterService parameterService;
042        private ChartService chartService;
043        private BusinessObjectService boService;
044    
045        /**
046         * 
047         * @see org.kuali.kfs.coa.service.OrganizationService#getByPrimaryId(java.lang.String, java.lang.String)
048         */
049        public Organization getByPrimaryId(String chartOfAccountsCode, String organizationCode) {
050            return organizationDao.getByPrimaryId(chartOfAccountsCode, organizationCode);
051        }
052    
053        /**
054         * Implements the getByPrimaryId method defined by OrganizationService. Method is used by KualiOrgReviewAttribute to enable
055         * caching of orgs for routing.
056         * 
057         * @see org.kuali.kfs.coa.service.impl.OrganizationServiceImpl#getByPrimaryId(java.lang.String, java.lang.String)
058         */
059        @Cached
060        public Organization getByPrimaryIdWithCaching(String chartOfAccountsCode, String organizationCode) {
061            return organizationDao.getByPrimaryId(chartOfAccountsCode, organizationCode);
062        }
063    
064        /**
065         * @see org.kuali.kfs.coa.service.OrganizationService#getActiveAccountsByOrg(java.lang.String, java.lang.String)
066         */
067        public List getActiveAccountsByOrg(String chartOfAccountsCode, String organizationCode) {
068    
069            if (StringUtils.isBlank(chartOfAccountsCode)) {
070                throw new IllegalArgumentException("String parameter chartOfAccountsCode was null or blank.");
071            }
072            if (StringUtils.isBlank(organizationCode)) {
073                throw new IllegalArgumentException("String parameter organizationCode was null or blank.");
074            }
075    
076            return organizationDao.getActiveAccountsByOrg(chartOfAccountsCode, organizationCode);
077        }
078    
079        /**
080         * @see org.kuali.kfs.coa.service.OrganizationService#getActiveChildOrgs(java.lang.String, java.lang.String)
081         */
082        public List getActiveChildOrgs(String chartOfAccountsCode, String organizationCode) {
083            if (StringUtils.isBlank(chartOfAccountsCode)) {
084                throw new IllegalArgumentException("String parameter chartOfAccountsCode was null or blank.");
085            }
086            if (StringUtils.isBlank(organizationCode)) {
087                throw new IllegalArgumentException("String parameter organizationCode was null or blank.");
088            }
089    
090            return organizationDao.getActiveChildOrgs(chartOfAccountsCode, organizationCode);
091        }
092    
093        @Cached
094        public boolean isParentOrganization( String childChartOfAccountsCode, String childOrganizationCode, String parentChartOfAccountsCode, String parentOrganizationCode ) {
095            if (StringUtils.isBlank(childChartOfAccountsCode)) {
096                throw new IllegalArgumentException("String parameter chartOfAccountsCode was null or blank.");
097            }
098            if (StringUtils.isBlank(childOrganizationCode)) {
099                throw new IllegalArgumentException("String parameter organizationCode was null or blank.");
100            }
101            if (StringUtils.isBlank(parentChartOfAccountsCode)) {
102                throw new IllegalArgumentException("String parameter parentChartOfAccountsCode was null or blank.");
103            }
104            if (StringUtils.isBlank(parentOrganizationCode)) {
105                throw new IllegalArgumentException("String parameter parentOrganizationCode was null or blank.");
106            }
107        
108            Organization currOrg = organizationDao.getByPrimaryId(childChartOfAccountsCode, childOrganizationCode);
109            while ( currOrg != null ) {
110                if ( currOrg.getReportsToChartOfAccountsCode().equals(parentChartOfAccountsCode)
111                        && currOrg.getReportsToOrganizationCode().equals(parentOrganizationCode) ) {
112                    return true;
113                }
114                // if no parent, we've reached the top - stop and return false
115                if ( StringUtils.isBlank(currOrg.getReportsToChartOfAccountsCode())
116                        || StringUtils.isBlank(currOrg.getReportsToOrganizationCode())
117                        || (currOrg.getReportsToChartOfAccountsCode().equals(currOrg.getChartOfAccountsCode())
118                                && currOrg.getReportsToOrganizationCode().equals(currOrg.getOrganizationCode()))
119                        ) {
120                    return false;
121                }
122                currOrg = organizationDao.getByPrimaryId(currOrg.getReportsToChartOfAccountsCode(), currOrg.getReportsToOrganizationCode());
123            }
124            
125            
126            return false;
127        }
128        
129        /**
130         * 
131         * @see org.kuali.kfs.coa.service.OrganizationService#getActiveOrgsByType(java.lang.String)
132         */
133        public List<Organization> getActiveOrgsByType(String organizationTypeCode) {
134            if (StringUtils.isBlank(organizationTypeCode)) {
135                throw new IllegalArgumentException("String parameter organizationTypeCode was null or blank.");
136            }
137    
138            return organizationDao.getActiveOrgsByType(organizationTypeCode);
139        }
140    
141        /**
142         * 
143         * @see org.kuali.kfs.coa.service.OrganizationService#getActiveFinancialOrgs()
144         */
145        public List<Organization> getActiveFinancialOrgs() {
146            Map<String, Object> criteriaMap = new HashMap<String, Object>();
147            criteriaMap.put("organizationInFinancialProcessingIndicator", Boolean.TRUE);
148            criteriaMap.put("active", Boolean.TRUE);
149            return (List<Organization>)boService.findMatching(Organization.class, criteriaMap);
150        }
151        
152        /**
153         * 
154         * @see org.kuali.kfs.coa.service.OrganizationService#getRootOrganizationCode()
155         */
156        public String[] getRootOrganizationCode() {
157            String rootChart = getChartService().getUniversityChart().getChartOfAccountsCode();
158            String selfReportsOrgType = SpringContext.getBean(ParameterService.class).getParameterValue(Organization.class, ChartApcParms.ORG_MUST_REPORT_TO_SELF_ORG_TYPES);
159            return (organizationDao.getRootOrganizationCode(rootChart, selfReportsOrgType));
160        }
161    
162        public void setParameterService(ParameterService parameterService) {
163            this.parameterService = parameterService;
164        }
165    
166        public ChartService getChartService() {
167            return chartService;
168        }
169    
170        public void setChartService(ChartService chartService) {
171            this.chartService = chartService;
172        }
173    
174        /**
175         * @return Returns the organizationDao.
176         */
177        public OrganizationDao getOrganizationDao() {
178            return organizationDao;
179        }
180    
181        /**
182         * @param organizationDao The organizationDao to set.
183         */
184        public void setOrganizationDao(OrganizationDao organizationDao) {
185            this.organizationDao = organizationDao;
186        }
187    
188        /**
189         * Sets the boService attribute value.
190         * @param boService The boService to set.
191         */
192        public void setBusinessObjectService(BusinessObjectService boService) {
193            this.boService = boService;
194        }
195    
196    }