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.service.impl;
017    
018    import org.apache.log4j.Logger;
019    import org.kuali.kfs.sys.businessobject.UniversityDate;
020    import org.kuali.kfs.sys.dataaccess.UniversityDateDao;
021    import org.kuali.kfs.sys.service.NonTransactional;
022    import org.kuali.kfs.sys.service.UniversityDateService;
023    import org.kuali.rice.kns.service.DateTimeService;
024    import org.kuali.rice.kns.util.DateUtils;
025    import org.kuali.rice.kns.util.spring.CacheNoCopy;
026    
027    /**
028     * 
029     * This is the default implementation of the UniversityDateService interface.
030     */
031    
032    @NonTransactional
033    public class UniversityDateServiceImpl implements UniversityDateService {
034    
035        private static final Logger LOG = Logger.getLogger(UniversityDateServiceImpl.class);
036    
037        private UniversityDateDao universityDateDao;
038        private DateTimeService dateTimeService;
039        
040        /**
041         * This method retrieves a UniversityDate object using today's date to create the instance.
042         * 
043         * @return A UniversityDate instance representing today's date.
044         * 
045         * @see org.kuali.kfs.sys.service.UniversityDateService#getCurrentUniversityDate()
046         */
047        public UniversityDate getCurrentUniversityDate() {
048            LOG.debug("getCurrentUniversityDate() started");
049            java.util.Date now = dateTimeService.getCurrentDate();
050    
051            return universityDateDao.getByPrimaryKey(DateUtils.clearTimeFields(now));
052        }
053    
054        /**
055         * This method retrieves the current fiscal year using today's date.
056         * 
057         * @return The current fiscal year as an Integer.
058         * 
059         * @see org.kuali.rice.kns.service.DateTimeService#getCurrentFiscalYear()
060         */
061        public Integer getCurrentFiscalYear() {
062            //Timer t0 = new Timer("getCurrentFiscalYear");
063            java.util.Date now = dateTimeService.getCurrentDate();
064    
065            Integer result = getFiscalYear(DateUtils.clearTimeFields(now));
066            //t0.log();
067            return result;
068        }
069        
070        /**
071         * This method retrieves the fiscal year associated with the date provided.
072         * 
073         * @param date The date to be used for retrieving the associated fiscal year.
074         * @return The fiscal year that the date provided falls within.
075         * 
076         * @see org.kuali.rice.kns.service.DateTimeService#getFiscalYear(java.util.Date)
077         */
078        @CacheNoCopy
079        public Integer getFiscalYear(java.util.Date date) {
080            if (date == null) {
081                throw new IllegalArgumentException("invalid (null) date");
082            }
083    
084            UniversityDate uDate = universityDateDao.getByPrimaryKey(date);
085            return (uDate == null) ? null : uDate.getUniversityFiscalYear();
086        }
087    
088        /**
089         * This method retrieves the first date of the fiscal year provided.
090         * 
091         * @param fiscalYear The fiscal year to retrieve the first date for.
092         * @return A Date object representing the first date of the fiscal year given.
093         * 
094         * @see org.kuali.kfs.sys.service.UniversityDateService#getFirstDateOfFiscalYear(java.lang.Integer)
095         */
096        @CacheNoCopy
097        public java.util.Date getFirstDateOfFiscalYear(Integer fiscalYear) {
098            UniversityDate uDate = universityDateDao.getFirstFiscalYearDate(fiscalYear);
099            return (uDate == null) ? null : uDate.getUniversityDate();
100        }
101    
102        /**
103         * This method retrieves the last date of the fiscal year provided.
104         * 
105         * @param fiscalYear The fiscal year to retrieve the last date for.
106         * @return A Date object representing the last date of the fiscal year given.
107         * 
108         * @see org.kuali.kfs.sys.service.UniversityDateService#getLastDateOfFiscalYear(java.lang.Integer)
109         */
110        @CacheNoCopy
111        public java.util.Date getLastDateOfFiscalYear(Integer fiscalYear) {
112            UniversityDate uDate = universityDateDao.getLastFiscalYearDate(fiscalYear);
113            return (uDate == null) ? null : uDate.getUniversityDate();
114        }
115        
116        /**
117         * Sets the universityDateDao attribute value.
118         * @param universityDateDao The universityDateDao to set.
119         */
120        public void setUniversityDateDao(UniversityDateDao universityDateDao) {
121            this.universityDateDao = universityDateDao;
122        }
123    
124        public DateTimeService getDateTimeService() {
125            return dateTimeService;
126        }
127    
128        public void setDateTimeService(DateTimeService dateTimeService) {
129            this.dateTimeService = dateTimeService;
130        }
131        
132    }