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.module.bc.document.service.impl;
017    
018    import java.util.Iterator;
019    import java.util.List;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.apache.log4j.Logger;
023    import org.kuali.kfs.module.bc.BCConstants.OrgSelControlOption;
024    import org.kuali.kfs.module.bc.businessobject.BudgetConstructionOrganizationReports;
025    import org.kuali.kfs.module.bc.businessobject.BudgetConstructionPullup;
026    import org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionDao;
027    import org.kuali.kfs.module.bc.document.dataaccess.BudgetPullupDao;
028    import org.kuali.kfs.module.bc.document.service.BudgetConstructionOrganizationReportsService;
029    import org.kuali.kfs.module.bc.document.service.BudgetOrganizationTreeService;
030    import org.kuali.rice.kns.service.BusinessObjectService;
031    import org.kuali.rice.kns.service.PersistenceService;
032    import org.springframework.transaction.annotation.Transactional;
033    
034    /**
035     * This class implements the BudgetOrganizationTreeService interface
036     */
037    @Transactional
038    public class BudgetOrganizationTreeServiceImpl implements BudgetOrganizationTreeService {
039        private static Logger LOG = org.apache.log4j.Logger.getLogger(BudgetOrganizationTreeServiceImpl.class);
040    
041        private BudgetConstructionOrganizationReportsService budgetConstructionOrganizationReportsService;
042        private BusinessObjectService businessObjectService;
043        private BudgetConstructionDao budgetConstructionDao;
044        private BudgetPullupDao budgetPullupDao;
045    
046        // controls used to trap any runaways due to cycles in the reporting tree
047        protected static final int MAXLEVEL = 50;
048        private int curLevel;
049    
050        /**
051         * @see org.kuali.kfs.module.bc.document.service.BudgetOrganizationTreeService#buildPullup(java.lang.String, java.lang.String,
052         *      java.lang.String)
053         */
054        public void buildPullup(String principalName, String chartOfAccountsCode, String organizationCode) {
055            cleanPullup(principalName);
056            BudgetConstructionOrganizationReports bcOrgRpts = budgetConstructionOrganizationReportsService.getByPrimaryId(chartOfAccountsCode, organizationCode);
057            if (bcOrgRpts != null) {
058                if (bcOrgRpts.getOrganization().isActive()) {
059                    curLevel = 0;
060                    buildSubTree(principalName, bcOrgRpts, curLevel);
061                }
062            }
063        }
064    
065        protected void buildSubTree(String principalName, BudgetConstructionOrganizationReports bcOrgRpts, int curLevel) {
066    
067            curLevel++;
068            BudgetConstructionPullup bcPullup = new BudgetConstructionPullup();
069            bcPullup.setPrincipalId(principalName);
070            bcPullup.setChartOfAccountsCode(bcOrgRpts.getChartOfAccountsCode());
071            bcPullup.setOrganizationCode(bcOrgRpts.getOrganizationCode());
072            bcPullup.setReportsToChartOfAccountsCode(bcOrgRpts.getReportsToChartOfAccountsCode());
073            bcPullup.setReportsToOrganizationCode(bcOrgRpts.getReportsToOrganizationCode());
074            bcPullup.setPullFlag(new Integer(0));
075            businessObjectService.save(bcPullup);
076    
077            if (curLevel <= MAXLEVEL) {
078                // getActiveChildOrgs does not return orgs that report to themselves
079                List childOrgs = budgetConstructionOrganizationReportsService.getActiveChildOrgs(bcOrgRpts.getChartOfAccountsCode(), bcOrgRpts.getOrganizationCode());
080                if (childOrgs.size() > 0) {
081                    for (Iterator iter = childOrgs.iterator(); iter.hasNext();) {
082                        BudgetConstructionOrganizationReports bcOrg = (BudgetConstructionOrganizationReports) iter.next();
083                        buildSubTree(principalName, bcOrg, curLevel);
084                    }
085                }
086            }
087            else {
088                LOG.warn(String.format("\n%s/%s reports to organization more than maxlevel of %d", bcOrgRpts.getChartOfAccountsCode(), bcOrgRpts.getOrganizationCode(), MAXLEVEL));
089            }
090        }
091    
092        /**
093         * @see org.kuali.kfs.module.bc.document.service.BudgetOrganizationTreeService#buildPullupSql(java.lang.String,
094         *      java.lang.String, java.lang.String)
095         */
096        public void buildPullupSql(String principalName, String chartOfAccountsCode, String organizationCode) {
097            cleanPullup(principalName);
098            BudgetConstructionOrganizationReports bcOrgRpts = budgetConstructionOrganizationReportsService.getByPrimaryId(chartOfAccountsCode, organizationCode);
099            if (bcOrgRpts != null) {
100                if (bcOrgRpts.getOrganization().isActive()) {
101                    curLevel = 0;
102                    buildSubTreeSql(principalName, bcOrgRpts, curLevel);
103                }
104            }
105        }
106    
107        protected void buildSubTreeSql(String principalName, BudgetConstructionOrganizationReports bcOrgRpts, int curLevel) {
108    
109            curLevel++;
110            budgetPullupDao.buildSubTree(principalName, bcOrgRpts.getChartOfAccountsCode(), bcOrgRpts.getOrganizationCode(), curLevel);
111    //        budgetPullupDao.initPointOfView(principalName, bcOrgRpts.getChartOfAccountsCode(), bcOrgRpts.getOrganizationCode(), curLevel);
112    //        budgetPullupDao.insertChildOrgs(principalName, curLevel);
113            
114        }
115    
116        /**
117         * @see org.kuali.kfs.module.bc.document.service.BudgetOrganizationTreeService#cleanPullup(java.lang.String)
118         */
119        public void cleanPullup(String principalName) {
120    
121    //        budgetConstructionDao.deleteBudgetConstructionPullupByUserId(principalName);
122            budgetPullupDao.cleanGeneralLedgerObjectSummaryTable(principalName);
123    
124        }
125    
126        /**
127         * @see org.kuali.kfs.module.bc.document.service.BudgetOrganizationTreeService#getPullupChildOrgs(java.lang.String,
128         *      java.lang.String, java.lang.String)
129         */
130        public List<BudgetConstructionPullup> getPullupChildOrgs(String principalId, String chartOfAccountsCode, String organizationCode) {
131    
132            if (StringUtils.isBlank(principalId)) {
133                throw new IllegalArgumentException("String parameter principalId was null or blank.");
134            }
135            if (StringUtils.isBlank(chartOfAccountsCode)) {
136                throw new IllegalArgumentException("String parameter chartOfAccountsCode was null or blank.");
137            }
138            if (StringUtils.isBlank(organizationCode)) {
139                throw new IllegalArgumentException("String parameter organizationCode was null or blank.");
140            }
141    
142            return (List<BudgetConstructionPullup>) budgetConstructionDao.getBudgetConstructionPullupChildOrgs(principalId, chartOfAccountsCode, organizationCode);
143        }
144    
145        /**
146         * @see org.kuali.kfs.module.bc.document.service.BudgetOrganizationTreeService#resetPullFlag(java.lang.String)
147         */
148        public void resetPullFlag(String principalId) {
149    
150            if (StringUtils.isBlank(principalId)) {
151                throw new IllegalArgumentException("String parameter principalId was null or blank.");
152            }
153            List<BudgetConstructionPullup> results = budgetConstructionDao.getBudgetConstructionPullupFlagSetByUserId(principalId);
154            if (!results.isEmpty()) {
155                for (BudgetConstructionPullup selOrg : results) {
156                    selOrg.setPullFlag(OrgSelControlOption.NO.getKey());
157                }
158                businessObjectService.save(results);
159            }
160    
161        }
162    
163        /**
164         * @see org.kuali.kfs.module.bc.document.service.BudgetOrganizationTreeService#getSelectedOrgs(java.lang.String)
165         */
166        public List<BudgetConstructionPullup> getSelectedOrgs(String principalId) {
167    
168            if (StringUtils.isBlank(principalId)) {
169                throw new IllegalArgumentException("String parameter principalId was null or blank.");
170            }
171            return (List<BudgetConstructionPullup>) budgetConstructionDao.getBudgetConstructionPullupFlagSetByUserId(principalId);
172        }
173    
174        /**
175         * Gets the budgetConstructionOrganizationReportsService attribute.
176         * 
177         * @return Returns the budgetConstructionOrganizationReportsService.
178         */
179        public BudgetConstructionOrganizationReportsService getBudgetConstructionOrganizationReportsService() {
180            return budgetConstructionOrganizationReportsService;
181        }
182    
183        /**
184         * Sets the budgetConstructionOrganizationReportsService attribute value.
185         * 
186         * @param budgetConstructionOrganizationReportsService The budgetConstructionOrganizationReportsService to set.
187         */
188        public void setBudgetConstructionOrganizationReportsService(BudgetConstructionOrganizationReportsService budgetConstructionOrganizationReportsService) {
189            this.budgetConstructionOrganizationReportsService = budgetConstructionOrganizationReportsService;
190        }
191    
192        /**
193         * Gets the businessObjectService attribute.
194         * 
195         * @return Returns the businessObjectService.
196         */
197        public BusinessObjectService getBusinessObjectService() {
198            return businessObjectService;
199        }
200    
201        /**
202         * Sets the businessObjectService attribute value.
203         * 
204         * @param businessObjectService The businessObjectService to set.
205         */
206        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
207            this.businessObjectService = businessObjectService;
208        }
209    
210        /**
211         * Gets the budgetConstructionDao attribute.
212         * 
213         * @return Returns the budgetConstructionDao.
214         */
215        public BudgetConstructionDao getBudgetConstructionDao() {
216            return budgetConstructionDao;
217        }
218    
219        /**
220         * Sets the budgetConstructionDao attribute value.
221         * 
222         * @param budgetConstructionDao The budgetConstructionDao to set.
223         */
224        public void setBudgetConstructionDao(BudgetConstructionDao budgetConstructionDao) {
225            this.budgetConstructionDao = budgetConstructionDao;
226        }
227    
228        /**
229         * Gets the budgetPullupDao attribute.
230         * 
231         * @return Returns the budgetPullupDao.
232         */
233        public BudgetPullupDao getBudgetPullupDao() {
234            return budgetPullupDao;
235        }
236    
237        /**
238         * Sets the budgetPullupDao attribute value.
239         * 
240         * @param budgetPullupDao The budgetPullupDao to set.
241         */
242        public void setBudgetPullupDao(BudgetPullupDao budgetPullupDao) {
243            this.budgetPullupDao = budgetPullupDao;
244        }
245    
246    }