View Javadoc

1   /**
2    * Copyright 2004-2013 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.lm.workflow.dao;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Date;
21  import java.util.List;
22  
23  import org.apache.log4j.Logger;
24  import org.apache.ojb.broker.query.Criteria;
25  import org.apache.ojb.broker.query.Query;
26  import org.apache.ojb.broker.query.QueryByCriteria;
27  import org.apache.ojb.broker.query.QueryFactory;
28  import org.apache.ojb.broker.query.ReportQueryByCriteria;
29  import org.kuali.hr.lm.workflow.LeaveCalendarDocumentHeader;
30  import org.kuali.hr.time.util.TkConstants;
31  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
32  
33  public class LeaveCalendarDocumentHeaderDaoImpl extends PlatformAwareDaoBaseOjb implements LeaveCalendarDocumentHeaderDao {
34      private static final Logger LOG = Logger.getLogger(LeaveCalendarDocumentHeaderDaoImpl.class);
35  
36      @Override
37      public LeaveCalendarDocumentHeader getLeaveCalendarDocumentHeader(String documentId) {
38          Criteria crit = new Criteria();
39          crit.addEqualTo("documentId", documentId);
40          return (LeaveCalendarDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(LeaveCalendarDocumentHeader.class, crit));
41      }
42  
43      @Override
44      public LeaveCalendarDocumentHeader getLeaveCalendarDocumentHeader(String principalId, Date beginDate, Date endDate) {
45          Criteria crit = new Criteria();
46          crit.addEqualTo("principalId", principalId);
47          crit.addEqualTo("beginDate", beginDate);
48          crit.addEqualTo("endDate", endDate);
49  
50          return (LeaveCalendarDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(LeaveCalendarDocumentHeader.class, crit));
51      }
52  
53      @Override
54      public void saveOrUpdate(LeaveCalendarDocumentHeader leaveCalendarDocumentHeader) {
55          this.getPersistenceBrokerTemplate().store(leaveCalendarDocumentHeader);
56      }
57      
58      /**
59       * Document header IDs are ordered, so an ID less than the current will
60       * always be previous to current.
61       */
62      public LeaveCalendarDocumentHeader getPreviousDocumentHeader(String principalId, Date beginDate) {
63          Criteria crit = new Criteria();
64          crit.addEqualTo("principalId", principalId);
65          // the pay begin date is the end date of the previous pay period
66          crit.addEqualTo("endDate", beginDate);
67          QueryByCriteria query = new QueryByCriteria(LeaveCalendarDocumentHeader.class, crit);
68          query.addOrderByDescending("documentId");
69          query.setStartAtIndex(0);
70          query.setEndAtIndex(1);
71  
72          return (LeaveCalendarDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
73      }
74  
75      @Override
76      public LeaveCalendarDocumentHeader getNextDocumentHeader(String principalId, Date endDate) {
77          Criteria crit = new Criteria();
78          crit.addEqualTo("principalId", principalId);
79          // the pay end date is the begin date of the next pay period
80          crit.addEqualTo("beginDate", endDate);
81          QueryByCriteria query = new QueryByCriteria(LeaveCalendarDocumentHeader.class, crit);
82          query.setStartAtIndex(0);
83          query.setEndAtIndex(1);
84  
85          return (LeaveCalendarDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
86      }
87      
88      @Override
89      public List<LeaveCalendarDocumentHeader> getDocumentHeaders(Date beginDate, Date endDate) {
90          Criteria crit = new Criteria();
91          List<LeaveCalendarDocumentHeader> lstDocumentHeaders = new ArrayList<LeaveCalendarDocumentHeader>();
92  
93          crit.addEqualTo("beginDate", beginDate);
94          crit.addEqualTo("endDate", endDate);
95          QueryByCriteria query = new QueryByCriteria(LeaveCalendarDocumentHeader.class, crit);
96          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
97          if (c != null) {
98              lstDocumentHeaders.addAll(c);
99          }
100         
101         return lstDocumentHeaders;
102     }
103     
104     @Override
105     public LeaveCalendarDocumentHeader getMaxEndDateApprovedLeaveCalendar(String principalId) {
106     	Criteria root = new Criteria();
107         Criteria crit = new Criteria();
108         
109         crit.addEqualTo("principalId", principalId);
110         crit.addEqualTo("documentStatus", TkConstants.ROUTE_STATUS.FINAL);
111         ReportQueryByCriteria endDateSubQuery = QueryFactory.newReportQuery(LeaveCalendarDocumentHeader.class, crit);
112         endDateSubQuery.setAttributes(new String[]{"max(endDate)"});
113 
114         root.addEqualTo("principalId", principalId);
115         root.addEqualTo("documentStatus", TkConstants.ROUTE_STATUS.FINAL);
116         root.addEqualTo("endDate", endDateSubQuery);
117 
118         Query query = QueryFactory.newQuery(LeaveCalendarDocumentHeader.class, root);
119         return (LeaveCalendarDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
120     }
121     
122     @Override
123 	public LeaveCalendarDocumentHeader getMinBeginDatePendingLeaveCalendar(String principalId) {
124     	Criteria root = new Criteria();
125         Criteria crit = new Criteria();
126         List<String> pendingStatuses = new ArrayList<String>();
127         pendingStatuses.add(TkConstants.ROUTE_STATUS.ENROUTE);
128         pendingStatuses.add(TkConstants.ROUTE_STATUS.INITIATED);
129         pendingStatuses.add(TkConstants.ROUTE_STATUS.SAVED);        
130         
131         crit.addEqualTo("principalId", principalId);
132         crit.addIn("documentStatus", pendingStatuses);
133         ReportQueryByCriteria startDateSubQuery = QueryFactory.newReportQuery(LeaveCalendarDocumentHeader.class, crit);
134         startDateSubQuery.setAttributes(new String[]{"min(beginDate)"});
135 
136         root.addEqualTo("principalId", principalId);
137         root.addIn("documentStatus", pendingStatuses);
138         root.addEqualTo("beginDate", startDateSubQuery);
139 
140         Query query = QueryFactory.newQuery(LeaveCalendarDocumentHeader.class, root);
141         return (LeaveCalendarDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
142 	}
143     @Override
144     public List<LeaveCalendarDocumentHeader> getAllDocumentHeadersForPricipalId(String principalId) {
145    	 	Criteria crit = new Criteria();
146         List<LeaveCalendarDocumentHeader> lstDocumentHeaders = new ArrayList<LeaveCalendarDocumentHeader>();
147 
148         crit.addEqualTo("principalId", principalId);
149         QueryByCriteria query = new QueryByCriteria(LeaveCalendarDocumentHeader.class, crit);
150         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
151         if (c != null) {
152             lstDocumentHeaders.addAll(c);
153         }
154         return lstDocumentHeaders;
155     }
156     
157     @Override
158     public List<LeaveCalendarDocumentHeader> getSubmissionDelinquentDocumentHeaders(String principalId, Date beforeDate) {
159     	Criteria crit = new Criteria();
160         List<LeaveCalendarDocumentHeader> lstDocumentHeaders = new ArrayList<LeaveCalendarDocumentHeader>();
161 
162         crit.addEqualTo("principalId", principalId);
163         crit.addLessThan("endDate", beforeDate);
164         crit.addNotEqualTo("documentStatus", TkConstants.ROUTE_STATUS.INITIATED);
165         crit.addNotEqualTo("documentStatus", TkConstants.ROUTE_STATUS.ENROUTE);
166         crit.addNotEqualTo("documentStatus", TkConstants.ROUTE_STATUS.FINAL);
167         QueryByCriteria query = new QueryByCriteria(LeaveCalendarDocumentHeader.class, crit);
168         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
169         if (c != null) {
170             lstDocumentHeaders.addAll(c);
171         }
172         return lstDocumentHeaders;
173     }
174     
175     @Override
176     public List<LeaveCalendarDocumentHeader> getApprovalDelinquentDocumentHeaders(String principalId) {
177     	Criteria crit = new Criteria();
178         List<LeaveCalendarDocumentHeader> lstDocumentHeaders = new ArrayList<LeaveCalendarDocumentHeader>();
179 
180         crit.addEqualTo("principalId", principalId);
181         crit.addNotEqualTo("documentStatus", TkConstants.ROUTE_STATUS.INITIATED);
182         crit.addNotEqualTo("documentStatus", TkConstants.ROUTE_STATUS.FINAL);
183         QueryByCriteria query = new QueryByCriteria(LeaveCalendarDocumentHeader.class, crit);
184         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
185         if (c != null) {
186             lstDocumentHeaders.addAll(c);
187         }
188         return lstDocumentHeaders;
189     }
190 
191     public void deleteLeaveCalendarHeader(String documentId){
192         Criteria crit = new Criteria();
193         crit.addEqualTo("documentId", documentId);
194         this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(LeaveCalendarDocumentHeader.class, crit));
195     }
196     
197     @Override
198     public List<LeaveCalendarDocumentHeader> getAllDocumentHeadersInRangeForPricipalId(String principalId, Date startDate, Date endDate) {
199     	Criteria root = new Criteria();
200         List<LeaveCalendarDocumentHeader> lstDocumentHeaders = new ArrayList<LeaveCalendarDocumentHeader>();
201 
202         Criteria beginRoot = new Criteria();
203         beginRoot.addEqualTo("principalId", principalId);
204         beginRoot.addLessOrEqualThan("beginDate", startDate);
205         beginRoot.addGreaterOrEqualThan("endDate", startDate);  
206         
207         Criteria endRoot = new Criteria();
208         endRoot.addEqualTo("principalId", principalId);
209         endRoot.addLessOrEqualThan("beginDate", endDate);
210         endRoot.addGreaterOrEqualThan("endDate", endDate); 
211         
212         root.addEqualTo("principalId", principalId);
213         root.addGreaterOrEqualThan("beginDate", startDate);
214         root.addLessOrEqualThan("beginDate", endDate);
215         root.addGreaterOrEqualThan("endDate", startDate);
216         root.addLessOrEqualThan("endDate", endDate);
217         root.addOrCriteria(beginRoot);
218         root.addOrCriteria(endRoot);
219         
220         QueryByCriteria query = new QueryByCriteria(LeaveCalendarDocumentHeader.class, root);
221         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
222         if (c != null) {
223             lstDocumentHeaders.addAll(c);
224         }
225         return lstDocumentHeaders;
226     }
227 }