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.sys.dataaccess.impl;
017    
018    import java.sql.Date;
019    import java.util.Collection;
020    
021    import org.apache.ojb.broker.query.Criteria;
022    import org.apache.ojb.broker.query.QueryByCriteria;
023    import org.apache.ojb.broker.query.QueryFactory;
024    import org.apache.ojb.broker.query.ReportQueryByCriteria;
025    import org.kuali.kfs.sys.KFSPropertyConstants;
026    import org.kuali.kfs.sys.businessobject.UniversityDate;
027    import org.kuali.kfs.sys.dataaccess.UniversityDateDao;
028    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
029    import org.kuali.rice.kns.util.spring.CacheNoCopy;
030    
031    /**
032     * The OJB implementation of the UniversityDateDao
033     */
034    public class UniversityDateDaoOjb extends PlatformAwareDaoBaseOjb implements UniversityDateDao {
035        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UniversityDateDaoOjb.class);
036    
037        /**
038         * Returns a university date record based on a given java.sql.Date.
039         * 
040         * @param date a Date to find the corresponding University Date record
041         * @return a University Date record if found, null if not
042         * @see org.kuali.kfs.sys.dataaccess.UniversityDateDao#getByPrimaryKey(java.sql.Date)
043         */
044        @CacheNoCopy
045        public UniversityDate getByPrimaryKey(Date date) {
046            LOG.debug("getByPrimaryKey() started");
047    
048            Criteria crit = new Criteria();
049            crit.addEqualTo(KFSPropertyConstants.UNIVERSITY_DATE, date);
050    
051            QueryByCriteria qbc = QueryFactory.newQuery(UniversityDate.class, crit);
052    
053            return (UniversityDate) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
054        }
055    
056        /**
057         * Returns a university date record based on java.util.Date
058         * 
059         * @param date a java.util.Date to find the corresponding University Date record
060         * @return a University Date record if found, null if not
061         * @see org.kuali.kfs.sys.dataaccess.UniversityDateDao#getByPrimaryKey(java.sql.Date)
062         */
063        @CacheNoCopy
064        public UniversityDate getByPrimaryKey(java.util.Date date) {
065            return getByPrimaryKey(convertDate(date));
066        }
067    
068        /**
069         * Converts a java.util.Date to a java.sql.Date
070         * 
071         * @param date a java.util.Date to convert
072         * @return a java.sql.Date
073         */
074        protected java.sql.Date convertDate(java.util.Date date) {
075            return new Date(date.getTime());
076        }
077    
078        /**
079         * Returns the last university date for a given fiscal year
080         * 
081         * @param fiscalYear the fiscal year to find the last date for
082         * @return a UniversityDate record for the last day in the given fiscal year, or null if nothing can be found
083         * @see org.kuali.kfs.sys.dataaccess.UniversityDateDao#getLastFiscalYearDate(java.lang.Integer)
084         */
085        public UniversityDate getLastFiscalYearDate(Integer fiscalYear) {
086            ReportQueryByCriteria subQuery;
087            Criteria subCrit = new Criteria();
088            Criteria crit = new Criteria();
089    
090            subCrit.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, fiscalYear);
091            subQuery = QueryFactory.newReportQuery(UniversityDate.class, subCrit);
092            subQuery.setAttributes(new String[] { "max(univ_dt)" });
093    
094            crit.addGreaterOrEqualThan(KFSPropertyConstants.UNIVERSITY_DATE, subQuery);
095    
096            QueryByCriteria qbc = QueryFactory.newQuery(UniversityDate.class, crit);
097    
098            return (UniversityDate) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
099        }
100    
101        /**
102         * Returns the first university date for a given fiscal year
103         * 
104         * @param fiscalYear the fiscal year to find the first date for
105         * @return a UniversityDate record for the first day of the given fiscal year, or null if nothing can be found
106         * @see org.kuali.kfs.sys.dataaccess.UniversityDateDao#getFirstFiscalYearDate(java.lang.Integer)
107         */
108        public UniversityDate getFirstFiscalYearDate(Integer fiscalYear) {
109            ReportQueryByCriteria subQuery;
110            Criteria subCrit = new Criteria();
111            Criteria crit = new Criteria();
112    
113            subCrit.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, fiscalYear);
114            subQuery = QueryFactory.newReportQuery(UniversityDate.class, subCrit);
115            subQuery.setAttributes(new String[] { "min(univ_dt)" });
116    
117            crit.addGreaterOrEqualThan(KFSPropertyConstants.UNIVERSITY_DATE, subQuery);
118    
119            QueryByCriteria qbc = QueryFactory.newQuery(UniversityDate.class, crit);
120    
121            return (UniversityDate) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
122        }
123    
124        /**
125         * Returns all distinct accounting period codes from the table
126         * 
127         * @return a Collection of all distinct accounting period codes represented by UniversityDate records in the database
128         * @see org.kuali.kfs.sys.dataaccess.UniversityDateDao#getAccountingPeriodCode()
129         */
130        public Collection getAccountingPeriodCode() {
131            Criteria criteria = new Criteria();
132    
133            ReportQueryByCriteria query = QueryFactory.newReportQuery(UniversityDate.class, criteria);
134            query.setAttributes(new String[] { "distinct " + KFSPropertyConstants.UNIVERSITY_FISCAL_ACCOUNTING_PERIOD });
135            query.addOrderByAscending(KFSPropertyConstants.UNIVERSITY_FISCAL_ACCOUNTING_PERIOD);
136    
137            return getPersistenceBrokerTemplate().getCollectionByQuery(query);
138        }
139    }