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