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