001/*
002 * Copyright 2007 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 */
016package org.kuali.ole.sys.service.impl;
017
018import org.apache.log4j.Logger;
019import org.kuali.ole.sys.businessobject.UniversityDate;
020import org.kuali.ole.sys.context.SpringContext;
021import org.kuali.ole.sys.dataaccess.UniversityDateDao;
022import org.kuali.ole.sys.service.NonTransactional;
023import org.kuali.ole.sys.service.UniversityDateService;
024import org.kuali.ole.sys.util.KfsDateUtils;
025import org.kuali.rice.core.api.datetime.DateTimeService;
026import org.kuali.rice.krad.service.BusinessObjectService;
027import org.springframework.cache.annotation.Cacheable;
028
029/**
030 * 
031 * This is the default implementation of the UniversityDateService interface.
032 */
033
034@NonTransactional
035public class UniversityDateServiceImpl implements UniversityDateService {
036
037    private static final Logger LOG = Logger.getLogger(UniversityDateServiceImpl.class);
038
039    protected UniversityDateDao universityDateDao;
040    protected DateTimeService dateTimeService;
041    
042    /**
043     * This method retrieves a UniversityDate object using today's date to create the instance.
044     * 
045     * @return A UniversityDate instance representing today's date.
046     * 
047     * @see org.kuali.ole.sys.service.UniversityDateService#getCurrentUniversityDate()
048     */
049    @Override
050    public UniversityDate getCurrentUniversityDate() {
051        java.util.Date now = dateTimeService.getCurrentDate();
052        return SpringContext.getBean(BusinessObjectService.class).findBySinglePrimaryKey(UniversityDate.class, new java.sql.Date( KfsDateUtils.clearTimeFields(now).getTime() ));
053    }
054
055    /**
056     * This method retrieves the current fiscal year using today's date.
057     * 
058     * @return The current fiscal year as an Integer.
059     * 
060     * @see org.kuali.rice.core.api.datetime.DateTimeService#getCurrentFiscalYear()
061     */
062    @Override
063    public Integer getCurrentFiscalYear() {
064        java.util.Date now = dateTimeService.getCurrentDate();
065
066        return getFiscalYear(KfsDateUtils.clearTimeFields(now));
067    }
068    
069    /**
070     * This method retrieves the fiscal year associated with the date provided.
071     * 
072     * @param date The date to be used for retrieving the associated fiscal year.
073     * @return The fiscal year that the date provided falls within.
074     * 
075     * @see org.kuali.rice.core.api.datetime.DateTimeService#getFiscalYear(java.util.Date)
076     */
077    @Override
078    @Cacheable(value=UniversityDate.CACHE_NAME, key="'{FiscalYear}'+#p0")
079    public Integer getFiscalYear(java.util.Date date) {
080        if (date == null) {
081            throw new IllegalArgumentException("invalid (null) date");
082        }
083        UniversityDate uDate = SpringContext.getBean(BusinessObjectService.class).findBySinglePrimaryKey(UniversityDate.class, new java.sql.Date( KfsDateUtils.clearTimeFields(date).getTime() ) );
084        return (uDate == null) ? null : uDate.getUniversityFiscalYear();
085    }
086
087    /**
088     * This method retrieves the first date of the fiscal year provided.
089     * 
090     * @param fiscalYear The fiscal year to retrieve the first date for.
091     * @return A Date object representing the first date of the fiscal year given.
092     * 
093     * @see org.kuali.ole.sys.service.UniversityDateService#getFirstDateOfFiscalYear(java.lang.Integer)
094     */
095    @Override
096    @Cacheable(value=UniversityDate.CACHE_NAME, key="'{FirstDateOfFiscalYear}'+#p0")
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.ole.sys.service.UniversityDateService#getLastDateOfFiscalYear(java.lang.Integer)
109     */
110    @Override
111    @Cacheable(value=UniversityDate.CACHE_NAME, key="'{LastDateOfFiscalYear}'+#p0")
112    public java.util.Date getLastDateOfFiscalYear(Integer fiscalYear) {
113        UniversityDate uDate = universityDateDao.getLastFiscalYearDate(fiscalYear);
114        return (uDate == null) ? null : uDate.getUniversityDate();
115    }
116    
117    public void setUniversityDateDao(UniversityDateDao universityDateDao) {
118        this.universityDateDao = universityDateDao;
119    }
120    public void setDateTimeService(DateTimeService dateTimeService) {
121        this.dateTimeService = dateTimeService;
122    }
123    
124}