View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.workflow.dao;
17  
18  import java.text.DateFormat;
19  import java.text.ParseException;
20  import java.text.SimpleDateFormat;
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Date;
24  import java.util.List;
25  
26  import org.apache.ojb.broker.query.Criteria;
27  import org.apache.ojb.broker.query.QueryByCriteria;
28  import org.apache.ojb.broker.query.QueryFactory;
29  import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
30  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
31  
32  public class TimesheetDocumentHeaderDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TimesheetDocumentHeaderDao {
33  
34      @Override
35      public TimesheetDocumentHeader getTimesheetDocumentHeader(String documentId) {
36          Criteria crit = new Criteria();
37          crit.addEqualTo("documentId", documentId);
38          return (TimesheetDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TimesheetDocumentHeader.class, crit));
39      }
40  
41      @Override
42      public void saveOrUpdate(TimesheetDocumentHeader documentHeader) {
43          if (documentHeader != null) {
44              this.getPersistenceBrokerTemplate().store(documentHeader);
45          }
46      }
47      
48      public void deleteTimesheetHeader(String documentId){
49          Criteria crit = new Criteria();
50          crit.addEqualTo("documentId", documentId);
51          this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(TimesheetDocumentHeader.class, crit));
52      }
53  
54      @Override
55      public TimesheetDocumentHeader getTimesheetDocumentHeader(String principalId, Date payBeginDate, Date payEndDate) {
56          Criteria crit = new Criteria();
57          crit.addEqualTo("principalId", principalId);
58          crit.addEqualTo("payEndDate", payEndDate);
59          crit.addEqualTo("payBeginDate", payBeginDate);
60  
61          return (TimesheetDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TimesheetDocumentHeader.class, crit));
62      }
63  
64      /**
65       * Document header IDs are ordered, so an ID less than the current will
66       * always be previous to current.
67       */
68      public TimesheetDocumentHeader getPreviousDocumentHeader(String principalId, Date payBeginDate) {
69          Criteria crit = new Criteria();
70          crit.addEqualTo("principalId", principalId);
71          // the pay begin date is the end date of the previous pay period
72          crit.addEqualTo("payEndDate", payBeginDate);
73          QueryByCriteria query = new QueryByCriteria(TimesheetDocumentHeader.class, crit);
74          query.addOrderByDescending("documentId");
75          query.setStartAtIndex(0);
76          query.setEndAtIndex(1);
77  
78          return (TimesheetDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
79      }
80  
81      @Override
82      public TimesheetDocumentHeader getNextDocumentHeader(String principalId, Date payEndDate) {
83          Criteria crit = new Criteria();
84          crit.addEqualTo("principalId", principalId);
85          // the pay end date is the begin date of the next pay period
86          crit.addEqualTo("payBeginDate", payEndDate);
87          QueryByCriteria query = new QueryByCriteria(TimesheetDocumentHeader.class, crit);
88          query.setStartAtIndex(0);
89          query.setEndAtIndex(1);
90  
91          return (TimesheetDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
92      }
93  
94      public List<TimesheetDocumentHeader> getDocumentHeaders(Date payBeginDate) {
95          Criteria crit = new Criteria();
96          List<TimesheetDocumentHeader> lstDocumentHeaders = new ArrayList<TimesheetDocumentHeader>();
97  
98          crit.addEqualTo("payBeginDate", payBeginDate);
99          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 }