001    /**
002     * Copyright 2004-2012 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("payEndDate", payEndDate);
059            crit.addEqualTo("payBeginDate", 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("payEndDate", 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("payBeginDate", 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        public List<TimesheetDocumentHeader> getDocumentHeaders(Date payBeginDate) {
095            Criteria crit = new Criteria();
096            List<TimesheetDocumentHeader> lstDocumentHeaders = new ArrayList<TimesheetDocumentHeader>();
097    
098            crit.addEqualTo("payBeginDate", payBeginDate);
099            QueryByCriteria query = new QueryByCriteria(TimesheetDocumentHeader.class, crit);
100            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
101            if (c != null) {
102                lstDocumentHeaders.addAll(c);
103            }
104            return lstDocumentHeaders;
105    
106        }
107        
108        public List<TimesheetDocumentHeader> getDocumentHeadersForPrincipalId(String principalId) {
109                    Criteria crit = new Criteria();
110            List<TimesheetDocumentHeader> lstDocumentHeaders = new ArrayList<TimesheetDocumentHeader>();
111    
112            crit.addEqualTo("principalId", principalId);
113            QueryByCriteria query = new QueryByCriteria(TimesheetDocumentHeader.class, crit);
114            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
115            if (c != null) {
116                lstDocumentHeaders.addAll(c);
117            }
118            return lstDocumentHeaders;
119       }
120       
121       public List<TimesheetDocumentHeader> getDocumentHeadersForYear(String principalId, String year) {
122                     Criteria crit = new Criteria();
123                 List<TimesheetDocumentHeader> lstDocumentHeaders = new ArrayList<TimesheetDocumentHeader>();
124                 
125                 try {
126                     crit.addEqualTo("principalId", principalId);
127                     DateFormat df = new SimpleDateFormat("yyyy");
128                     java.util.Date cYear = df.parse(year);
129                     String nextYear = Integer.toString((Integer.parseInt(year) + 1));
130                     java.util.Date nYear = df.parse(nextYear);
131                     
132                            crit.addGreaterOrEqualThan("payBeginDate", cYear);
133                        crit.addLessThan("payBeginDate", nYear );
134                        QueryByCriteria query = new QueryByCriteria(TimesheetDocumentHeader.class, crit);
135                        Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
136                        if (c != null) {
137                            lstDocumentHeaders.addAll(c);
138                            }
139                      } catch (ParseException e) {
140                                    // TODO Auto-generated catch block
141                                    e.printStackTrace();
142                      }
143                      return lstDocumentHeaders;
144       }
145       
146       public TimesheetDocumentHeader getDocumentHeaderForDate(String principalId, Date asOfDate) {
147               Criteria crit = new Criteria();
148           crit.addEqualTo("principalId", principalId);
149           crit.addLessOrEqualThan("payBeginDate", asOfDate);
150           crit.addGreaterOrEqualThan("payEndDate", asOfDate);
151           
152           QueryByCriteria query = new QueryByCriteria(TimesheetDocumentHeader.class, crit);
153    
154           return (TimesheetDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(query); 
155       }
156    }