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.module.endow.document.service.impl;
017    
018    import java.util.ArrayList;
019    import java.util.Calendar;
020    import java.util.Collection;
021    import java.util.Date;
022    import java.util.HashMap;
023    import java.util.List;
024    import java.util.Map;
025    
026    import org.kuali.kfs.module.endow.EndowPropertyConstants;
027    import org.kuali.kfs.module.endow.businessobject.MonthEndDate;
028    import org.kuali.kfs.module.endow.dataaccess.MonthEndDateDao;
029    import org.kuali.kfs.module.endow.document.service.MonthEndDateService;
030    import org.kuali.kfs.sys.context.SpringContext;
031    import org.kuali.rice.kns.service.BusinessObjectService;
032    import org.kuali.rice.kns.service.DateTimeService;
033    import org.kuali.rice.kns.util.KualiInteger;
034    import org.kuali.rice.kns.util.ObjectUtils;
035    import org.springframework.transaction.annotation.Transactional;
036    
037    /**
038     * This class provides service for MonthEndDateService
039     */
040    @Transactional
041    public class MonthEndDateServiceImpl implements MonthEndDateService {
042    
043        private BusinessObjectService businessObjectService;
044        private MonthEndDateDao monthEndDateDao;
045    
046        /**
047         * @see org.kuali.kfs.module.endow.document.service.MonthEndDateService#getMonthEndIdDate(KualiInteger)
048         */
049        public java.sql.Date getByPrimaryKey(KualiInteger monthEndId) {
050            java.sql.Date monthEndDate = null;
051            
052            Map primaryKeys = new HashMap();
053            
054            primaryKeys.put(EndowPropertyConstants.MONTH_END_DATE_ID, monthEndId);
055            MonthEndDate monthEndDateRecord = (MonthEndDate) businessObjectService.findByPrimaryKey(MonthEndDate.class, primaryKeys);
056            
057            return monthEndDateRecord.getMonthEndDate();
058        }
059        
060        /**
061         * @see org.kuali.kfs.module.endow.document.service.MonthEndDateService#getMostRecentDate()
062         */
063        public java.sql.Date getMostRecentDate() {
064            java.sql.Date mostRecentDate = null;
065            
066            Map primaryKeys = new HashMap();
067            Collection<MonthEndDate> monthEndDateRecords = businessObjectService.findAll(MonthEndDate.class);
068            
069            for (MonthEndDate monthEndDateRecord : monthEndDateRecords) {
070                mostRecentDate = monthEndDateRecord.getMonthEndDate();
071            }        
072            
073            return (java.sql.Date) mostRecentDate;
074        }
075        
076        /**
077         * @see org.kuali.kfs.module.endow.document.service.MonthEndDateService#getMonthEndId(Date)
078         */
079        public KualiInteger getMonthEndId(Date monthEndDate) {
080    
081            KualiInteger monthEndId = new KualiInteger("0");
082            
083            if (ObjectUtils.isNotNull(monthEndDate)) {
084                Map criteria = new HashMap();
085                
086                criteria.put("monthEndDate", monthEndDate);
087                Collection<MonthEndDate> monthEndDateRecords = businessObjectService.findMatching(MonthEndDate.class, criteria);
088    
089                for (MonthEndDate monthEndDateRecord : monthEndDateRecords) {
090                    monthEndId = monthEndDateRecord.getMonthEndDateId();
091                }
092            }
093            
094            return monthEndId;
095        }
096        
097        /**
098         * @see org.kuali.kfs.module.endow.document.service.MonthEndDateService#getNextMonthEndIdForNewRecord()
099         */
100        public KualiInteger getNextMonthEndIdForNewRecord(){
101            //Search END_ME_DT_T, get all records, find the last one with the biggest MonthEndId.
102            //Increase it by one and then return this as the next new MonthEndId that should be used
103            //for inserting a new record.
104            KualiInteger largestMonthEndId= new KualiInteger("0");
105            KualiInteger monthEndId = new KualiInteger("0");
106            Collection<MonthEndDate> monthEndDateRecords = businessObjectService.findAll(MonthEndDate.class);
107            for (MonthEndDate monthEndDateRecord : monthEndDateRecords) {
108                monthEndId = monthEndDateRecord.getMonthEndDateId();
109                if (monthEndId.isGreaterThan(largestMonthEndId)){
110                    largestMonthEndId = monthEndId;
111                }                
112            }
113            return largestMonthEndId.add(new KualiInteger("1"));
114        }
115        
116        /**
117         * 
118         * @see org.kuali.kfs.module.endow.document.service.MonthEndDateService#getBeginningDates()
119         */
120        public List<String> getBeginningDates() {
121                    
122            DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
123            List<String> beginningDates = new ArrayList<String>();
124            List<MonthEndDate> monthEndDateRecords = monthEndDateDao.getAllMonthEndDatesOrderByDescending();
125            // remove the latest one because it cannot be greater than the latest ending date
126            monthEndDateRecords.remove(0);
127            Calendar calendar = Calendar.getInstance();
128            for (MonthEndDate monthEndDate : monthEndDateRecords) {
129                calendar.setTime(monthEndDate.getMonthEndDate());
130                calendar.add(Calendar.DATE, 1);
131                beginningDates.add(dateTimeService.toDateString(new java.sql.Date(calendar.getTimeInMillis())));
132            }
133            return beginningDates;
134        }
135        
136        /** 
137         * 
138         * @see org.kuali.kfs.module.endow.document.service.MonthEndDateService#getBeginningDates()
139         */
140        public List<String> getEndingDates() {
141            DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
142            List<MonthEndDate> monthEndDateRecords = monthEndDateDao.getAllMonthEndDatesOrderByDescending();
143            List<String> endingDates = new ArrayList<String>();
144            for (MonthEndDate monthEndDate : monthEndDateRecords) {
145                endingDates.add(dateTimeService.toDateString(monthEndDate.getMonthEndDate()));
146            }        
147            return endingDates;
148        }
149            
150        /**
151         * This method gets the businessObjectService.
152         * 
153         * @return businessObjectService
154         */
155        public BusinessObjectService getBusinessObjectService() {
156            return businessObjectService;
157        }
158    
159        /**
160         * This method sets the businessObjectService
161         * 
162         * @param businessObjectService
163         */
164        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
165            this.businessObjectService = businessObjectService;
166        }
167    
168        /**
169         * Sets the monthEndDateDao
170         * 
171         * @param monthEndDateDao
172         */
173        public void setMonthEndDateDao(MonthEndDateDao monthEndDateDao) {
174            this.monthEndDateDao = monthEndDateDao;
175        }
176        
177    }