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.dataaccess.impl;
017    
018    import org.apache.ojb.broker.metadata.MetadataManager;
019    import org.kuali.kfs.coa.businessobject.Organization;
020    import org.kuali.kfs.coa.businessobject.PriorYearOrganization;
021    import org.kuali.kfs.coa.dataaccess.PriorYearOrganizationDao;
022    import org.kuali.rice.kns.dao.jdbc.PlatformAwareDaoBaseJdbc;
023    
024    /**
025     * This class performs actions against the database through direct SQL command calls.
026     */
027    public class PriorYearOrganizationDaoJdbc extends PlatformAwareDaoBaseJdbc implements PriorYearOrganizationDao {
028    
029        /** Constant used to retrieve row counts for tables. Obj_Id value exists in all tables in DB. */
030        private static final String OBJ_ID = "OBJ_ID";
031    
032        /**
033         * This method purges all records in the Prior Year Organization table in the DB.
034         * 
035         * @return Number of records that were purged.
036         * @see org.kuali.kfs.coa.dataaccess.PriorYearOrganizationDao#purgePriorYearOrganizations()
037         */
038        public int purgePriorYearOrganizations() {
039            String priorYrOrgTableName = MetadataManager.getInstance().getGlobalRepository().getDescriptorFor(PriorYearOrganization.class).getFullTableName();
040    
041            // 1. Count how many rows are currently in the prior year org table
042            int count = getSimpleJdbcTemplate().queryForInt("SELECT COUNT(" + OBJ_ID + ") from " + priorYrOrgTableName);
043    
044            // 2. Purge all the rows from the prior year org table
045            getSimpleJdbcTemplate().update("DELETE from " + priorYrOrgTableName);
046    
047            return count;
048        }
049    
050        /**
051         * This method copies all organization records from the current Org table to the Prior Year Organization table.
052         * 
053         * @return Number of records that were copied.
054         * @see org.kuali.kfs.coa.dataaccess.PriorYearOrganizationDao#copyCurrentOrganizationsToPriorYearTable()
055         */
056        public int copyCurrentOrganizationsToPriorYearTable() {
057            String priorYrOrgTableName = MetadataManager.getInstance().getGlobalRepository().getDescriptorFor(PriorYearOrganization.class).getFullTableName();
058            String orgTableName = MetadataManager.getInstance().getGlobalRepository().getDescriptorFor(Organization.class).getFullTableName();
059    
060            // 1. Copy all the rows from the current org table to the prior year org table
061            getSimpleJdbcTemplate().update("INSERT into " + priorYrOrgTableName + " SELECT * from " + orgTableName);
062    
063            // 2. Count how many rows are currently in the prior year org table
064            return getSimpleJdbcTemplate().queryForInt("SELECT COUNT(" + OBJ_ID + ") from " + priorYrOrgTableName);
065        }
066    
067    }