001    /**
002     * Copyright 2004-2013 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.hr.time.workflow.dao;
017    
018    import java.text.DateFormat;
019    import java.text.ParseException;
020    import java.text.SimpleDateFormat;
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.Date;
024    import java.util.List;
025    
026    import org.apache.ojb.broker.query.Criteria;
027    import org.apache.ojb.broker.query.QueryByCriteria;
028    import org.apache.ojb.broker.query.QueryFactory;
029    import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
030    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
031    
032    public class TimesheetDocumentHeaderDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TimesheetDocumentHeaderDao {
033    
034        @Override
035        public TimesheetDocumentHeader getTimesheetDocumentHeader(String documentId) {
036            Criteria crit = new Criteria();
037            crit.addEqualTo("documentId", documentId);
038            return (TimesheetDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TimesheetDocumentHeader.class, crit));
039        }
040    
041        @Override
042        public void saveOrUpdate(TimesheetDocumentHeader documentHeader) {
043            if (documentHeader != null) {
044                this.getPersistenceBrokerTemplate().store(documentHeader);
045            }
046        }
047        
048        public void deleteTimesheetHeader(String documentId){
049            Criteria crit = new Criteria();
050            crit.addEqualTo("documentId", documentId);
051            this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(TimesheetDocumentHeader.class, crit));
052        }
053    
054        @Override
055        public TimesheetDocumentHeader getTimesheetDocumentHeader(String principalId, Date payBeginDate, Date payEndDate) {
056            Criteria crit = new Criteria();
057            crit.addEqualTo("principalId", principalId);
058            crit.addEqualTo("endDate", payEndDate);
059            crit.addEqualTo("beginDate", payBeginDate);
060    
061            return (TimesheetDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TimesheetDocumentHeader.class, crit));
062        }
063    
064        /**
065         * Document header IDs are ordered, so an ID less than the current will
066         * always be previous to current.
067         */
068        public TimesheetDocumentHeader getPreviousDocumentHeader(String principalId, Date payBeginDate) {
069            Criteria crit = new Criteria();
070            crit.addEqualTo("principalId", principalId);
071            // the pay begin date is the end date of the previous pay period
072            crit.addEqualTo("endDate", payBeginDate);
073            QueryByCriteria query = new QueryByCriteria(TimesheetDocumentHeader.class, crit);
074            query.addOrderByDescending("documentId");
075            query.setStartAtIndex(0);
076            query.setEndAtIndex(1);
077    
078            return (TimesheetDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
079        }
080    
081        @Override
082        public TimesheetDocumentHeader getNextDocumentHeader(String principalId, Date payEndDate) {
083            Criteria crit = new Criteria();
084            crit.addEqualTo("principalId", principalId);
085            // the pay end date is the begin date of the next pay period
086            crit.addEqualTo("beginDate", payEndDate);
087            QueryByCriteria query = new QueryByCriteria(TimesheetDocumentHeader.class, crit);
088            query.setStartAtIndex(0);
089            query.setEndAtIndex(1);
090    
091            return (TimesheetDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
092        }
093        
094        @Override
095        public List<TimesheetDocumentHeader> getDocumentHeaders(Date payBeginDate, Date payEndDate) {
096            Criteria crit = new Criteria();
097            List<TimesheetDocumentHeader> lstDocumentHeaders = new ArrayList<TimesheetDocumentHeader>();
098            
099            crit.addEqualTo("endDate", payEndDate);
100            crit.addEqualTo("beginDate", payBeginDate);
101            QueryByCriteria query = new QueryByCriteria(TimesheetDocumentHeader.class, crit);
102            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
103            if (c != null) {
104                lstDocumentHeaders.addAll(c);
105            }
106            
107            return lstDocumentHeaders;
108        }
109        
110        public List<TimesheetDocumentHeader> getDocumentHeadersForPrincipalId(String principalId) {
111                    Criteria crit = new Criteria();
112            List<TimesheetDocumentHeader> lstDocumentHeaders = new ArrayList<TimesheetDocumentHeader>();
113    
114            crit.addEqualTo("principalId", principalId);
115            QueryByCriteria query = new QueryByCriteria(TimesheetDocumentHeader.class, crit);
116            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
117            if (c != null) {
118                lstDocumentHeaders.addAll(c);
119            }
120            return lstDocumentHeaders;
121       }
122       
123       public List<TimesheetDocumentHeader> getDocumentHeadersForYear(String principalId, String year) {
124                     Criteria crit = new Criteria();
125                 List<TimesheetDocumentHeader> lstDocumentHeaders = new ArrayList<TimesheetDocumentHeader>();
126                 
127                 try {
128                     crit.addEqualTo("principalId", principalId);
129                     DateFormat df = new SimpleDateFormat("yyyy");
130                     java.util.Date cYear = df.parse(year);
131                     String nextYear = Integer.toString((Integer.parseInt(year) + 1));
132                     java.util.Date nYear = df.parse(nextYear);
133                     
134                            crit.addGreaterOrEqualThan("beginDate", cYear);
135                        crit.addLessThan("beginDate", nYear );
136                        QueryByCriteria query = new QueryByCriteria(TimesheetDocumentHeader.class, crit);
137                        Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
138                        if (c != null) {
139                            lstDocumentHeaders.addAll(c);
140                            }
141                      } catch (ParseException e) {
142                                    // TODO Auto-generated catch block
143                                    e.printStackTrace();
144                      }
145                      return lstDocumentHeaders;
146       }
147       
148       public TimesheetDocumentHeader getDocumentHeaderForDate(String principalId, Date asOfDate) {
149               Criteria crit = new Criteria();
150           crit.addEqualTo("principalId", principalId);
151           crit.addLessOrEqualThan("beginDate", asOfDate);
152           crit.addGreaterOrEqualThan("endDate", asOfDate);
153           
154           QueryByCriteria query = new QueryByCriteria(TimesheetDocumentHeader.class, crit);
155    
156           return (TimesheetDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(query); 
157       }
158    }