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.dataaccess.impl;
017    
018    import org.kuali.kfs.module.bc.document.dataaccess.BudgetPullupDao;
019    import org.kuali.rice.kns.service.PersistenceService;
020    
021    /**
022     * This class implemements BudgetPullupDao using Raw Sql
023     */
024    public class BudgetPullupDaoJdbc extends BudgetConstructionDaoJdbcBase implements BudgetPullupDao {
025    
026        private PersistenceService persistenceService;
027    
028        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BudgetPullupDaoJdbc.class);
029        private static final int MAXLEVEL = 50;
030        
031        private static String[] initPointOfViewTemplates = new String[1];
032        private static String[] insertChildOrgTemplates = new String[2];
033        
034        public BudgetPullupDaoJdbc() {
035            
036            StringBuilder sqlText = new StringBuilder(500);
037            sqlText.append("INSERT INTO LD_BCN_PULLUP_T \n");
038            sqlText.append(" (PERSON_UNVL_ID, FIN_COA_CD, ORG_CD, RPTS_TO_FIN_COA_CD, RPTS_TO_ORG_CD, PULL_FLAG) \n");
039            sqlText.append("SELECT ?, r.fin_coa_cd, r.org_cd, r.rpts_to_fin_coa_cd, r.rpts_to_org_cd, ? \n");
040            sqlText.append("FROM LD_BCN_ORG_RPTS_T r \n");
041            sqlText.append("WHERE fin_coa_cd = ? \n");
042            sqlText.append("  AND org_cd = ? \n");
043            initPointOfViewTemplates[0] = sqlText.toString();
044            sqlText.delete(0, sqlText.length());
045            
046            sqlText.append("INSERT INTO LD_BCN_PULLUP_T \n");
047            sqlText.append(" (PERSON_UNVL_ID, FIN_COA_CD, ORG_CD, RPTS_TO_FIN_COA_CD, RPTS_TO_ORG_CD, PULL_FLAG) \n");
048            sqlText.append("SELECT ?, r.fin_coa_cd, r.org_cd, r.rpts_to_fin_coa_cd, r.rpts_to_org_cd, ? \n");
049            sqlText.append("FROM LD_BCN_ORG_RPTS_T r, LD_BCN_PULLUP_T p, CA_ORG_T o \n");
050            sqlText.append("WHERE p.person_unvl_id = ? \n");
051            sqlText.append("  AND p.pull_flag = ? \n");
052            sqlText.append("  AND p.fin_coa_cd = r.rpts_to_fin_coa_cd \n");
053            sqlText.append("  AND p.org_cd = r.rpts_to_org_cd \n");
054            sqlText.append("  AND not (r.fin_coa_cd = r.rpts_to_fin_coa_cd and r.org_cd = r.rpts_to_org_cd)");
055            sqlText.append("  AND o.fin_coa_cd = r.fin_coa_cd \n");
056            sqlText.append("  AND o.org_cd = r.org_cd \n");
057            sqlText.append("  AND o.org_active_cd = 'Y' \n");
058            insertChildOrgTemplates[0] = sqlText.toString();
059            sqlText.delete(0, sqlText.length());
060            
061            sqlText.append("UPDATE LD_BCN_PULLUP_T \n");
062            sqlText.append("SET pull_flag = 0 \n");
063            sqlText.append("WHERE person_unvl_id = ? \n");
064            insertChildOrgTemplates[1] = sqlText.toString();
065    
066        }
067        
068        /**
069         * @see org.kuali.kfs.module.bc.document.dataaccess.BudgetPullupDao#buildSubTree(java.lang.String, java.lang.String, java.lang.String, int)
070         */
071        public void buildSubTree(String principalName, String chartOfAccountsCode, String organizationCode, int currentLevel) {
072    
073            initPointOfView(principalName, chartOfAccountsCode, organizationCode, currentLevel);
074            insertChildOrgs(principalName, currentLevel);
075            persistenceService.clearCache();
076            
077        }
078    
079        /**
080         * This method initializes and inserts the root organization using raw SQL.
081         * 
082         * @see org.kuali.kfs.module.bc.document.dataaccess.BudgetPullupDao#initPointOfView(java.lang.String, java.lang.String, java.lang.String, int)
083         */
084        protected void initPointOfView(String principalName, String chartOfAccountsCode, String organizationCode, int currentLevel) {
085       
086            LOG.debug("initPointOfView() called");
087            
088            getSimpleJdbcTemplate().update(initPointOfViewTemplates[0], principalName, currentLevel, chartOfAccountsCode, organizationCode);
089        }
090    
091        /**
092         * This method is implemented using RawSql
093         * 
094         * @see org.kuali.kfs.module.bc.document.dataaccess.BudgetPullupDao#insertChildOrgs(java.lang.String, int)
095         */
096        protected void insertChildOrgs(String principalName, int previousLevel) {
097            
098            LOG.debug("insertChildOrgs() called");
099    
100            if (previousLevel <= MAXLEVEL) {
101                int currentLevel = previousLevel + 1;
102    
103                // insert the children of the organizations at the current level for the user
104                // and then recursively call on the new level
105                int rowsAffected = getSimpleJdbcTemplate().update(insertChildOrgTemplates[0], principalName, currentLevel, principalName, previousLevel);
106                if (rowsAffected > 0) {
107                    insertChildOrgs(principalName, currentLevel);
108                } else {
109                    // cleanup by resetting the pull_flag to zero for all
110                    getSimpleJdbcTemplate().update(insertChildOrgTemplates[1], principalName);
111                }
112            } else {
113                // overrun problem
114                LOG.warn(String.format("\nWarning: One or more selected organizations have reporting organizations more than maxlevel of %d deep.", MAXLEVEL));
115            }
116        }
117    
118        /**
119         * @see org.kuali.kfs.module.bc.document.dataaccess.BudgetPullupDao#cleanGeneralLedgerObjectSummaryTable(java.lang.String)
120         */
121        public void cleanGeneralLedgerObjectSummaryTable(String principalName) {
122            this.clearTempTableByUnvlId("LD_BCN_PULLUP_T","PERSON_UNVL_ID",principalName);
123            /**
124             * this is necessary to clear any rows for the tables we have just updated from the OJB cache.  otherwise, subsequent calls to OJB will fetch the old, unupdated cached rows.
125             */
126            persistenceService.clearCache();
127        }
128    
129        /**
130         * Sets the persistenceService attribute value.
131         * @param persistenceService The persistenceService to set.
132         */
133        public void setPersistenceService(PersistenceService persistenceService) {
134            this.persistenceService = persistenceService;
135        }
136    
137    }
138