View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.tklm.leave.block.dao;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.log4j.Logger;
25  import org.apache.ojb.broker.query.Criteria;
26  import org.apache.ojb.broker.query.Query;
27  import org.apache.ojb.broker.query.QueryFactory;
28  import org.joda.time.LocalDate;
29  import org.kuali.kpme.core.util.HrConstants;
30  import org.kuali.kpme.tklm.common.LMConstants;
31  import org.kuali.kpme.tklm.leave.block.LeaveBlockHistory;
32  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
33  import org.kuali.kpme.tklm.time.workflow.TimesheetDocumentHeader;
34  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
35  
36  public class LeaveBlockHistoryDaoOjbImpl extends PlatformAwareDaoBaseOjb implements LeaveBlockHistoryDao {
37  
38  	@SuppressWarnings("unused")
39  	private static final Logger LOG = Logger
40  			.getLogger(LeaveBlockHistoryDaoOjbImpl.class);
41  
42  	@Override
43  	public void saveOrUpdate(LeaveBlockHistory leaveBlockHistory) {
44  		this.getPersistenceBrokerTemplate().store(leaveBlockHistory);
45  	}
46  
47  	@Override
48  	public void saveOrUpdate(List<LeaveBlockHistory> leaveBlockHistoryList) {
49  		if (leaveBlockHistoryList != null) {
50  			for (LeaveBlockHistory leaveBlockHistory : leaveBlockHistoryList) {
51  				this.getPersistenceBrokerTemplate().store(leaveBlockHistory);
52  			}
53  
54  		}
55  	}
56  
57  	@Override
58  	public List<LeaveBlockHistory> getLeaveBlockHistoryByLmLeaveBlockId(
59  			String lmLeaveBlockId) {
60  		Criteria recordCriteria = new Criteria();
61  		recordCriteria.addEqualTo("lmLeaveBlockId", lmLeaveBlockId);
62  		Query query = QueryFactory.newQuery(LeaveBlockHistory.class,
63  				recordCriteria);
64  		return (List<LeaveBlockHistory>) this.getPersistenceBrokerTemplate()
65  				.getCollectionByQuery(query);
66  	}
67  
68  	@Override
69  	public List<LeaveBlockHistory> getLeaveBlockHistories(String principalId,
70  			List<String> requestStatus) {
71  		Criteria recordCriteria = new Criteria();
72  		recordCriteria.addEqualTo("principalId", principalId);
73  		if (requestStatus != null) {
74  			recordCriteria.addIn("requestStatus", requestStatus);
75  		}
76  		Query query = QueryFactory.newQuery(LeaveBlockHistory.class,
77  				recordCriteria);
78  		return (List<LeaveBlockHistory>) this.getPersistenceBrokerTemplate()
79  				.getCollectionByQuery(query);
80  	}
81  	
82  	@Override
83  	public List<LeaveBlockHistory> getLeaveBlockHistories(String principalId,String requestStatus, String action, LocalDate currentDate) {
84  		Criteria recordCriteria = new Criteria();
85  		recordCriteria.addEqualTo("principalId", principalId);
86  		if (requestStatus != null) {
87  			recordCriteria.addEqualTo("requestStatus", requestStatus);
88  		}
89  		if(currentDate != null) {
90  			recordCriteria.addGreaterThan("leaveDate", currentDate.toDate());
91  		}
92  		if(action != null) {
93  			recordCriteria.addEqualTo("action", action);
94  		}
95  		Query query = QueryFactory.newQuery(LeaveBlockHistory.class,
96  				recordCriteria);
97  		return (List<LeaveBlockHistory>) this.getPersistenceBrokerTemplate()
98  				.getCollectionByQuery(query);
99  	}
100 
101 	@Override
102 	public List<LeaveBlockHistory> getLeaveBlockHistoriesForLeaveDisplay(String principalId,
103 			LocalDate beginDate, LocalDate endDate, boolean considerModifiedUser) {
104 		
105 		List<LeaveBlockHistory> leaveBlockHistories = new ArrayList<LeaveBlockHistory>();
106 		
107 		Criteria root = new Criteria();
108 		root.addEqualTo("principalId", principalId);
109 		root.addGreaterOrEqualThan("leaveDate", beginDate.toDate());
110 		root.addLessOrEqualThan("leaveDate", endDate.toDate());
111 		root.addEqualTo("action",HrConstants.ACTION.MODIFIED);
112 		if(considerModifiedUser) {
113 			root.addNotEqualTo("principalIdModified", principalId);
114 		} 
115 		
116 		Criteria root1 = new Criteria();
117 		root1.addEqualTo("principalId", principalId);
118 		root1.addGreaterOrEqualThan("leaveDate", beginDate.toDate());
119 		root1.addLessOrEqualThan("leaveDate", endDate.toDate());
120 		root1.addEqualTo("action",HrConstants.ACTION.DELETE);
121 		if(considerModifiedUser) {
122 			root1.addNotEqualTo("principalIdDeleted", principalId);
123 		} 
124 		
125 		root.addOrCriteria(root1);
126 		
127 		Query query = QueryFactory.newQuery(LeaveBlockHistory.class, root);
128 		Collection c = this.getPersistenceBrokerTemplate()
129 				.getCollectionByQuery(query);
130 
131 		if (c != null) {
132 			leaveBlockHistories.addAll(c);
133 		}
134 		return leaveBlockHistories;
135 	}
136 
137 	@Override
138 	public List<LeaveBlockHistory> getLeaveBlockHistoriesForLookup(String documentId,
139 			String principalId, String userPrincipalId, LocalDate fromDate,
140 			LocalDate toDate) {
141 	   	List<LeaveBlockHistory> leaveBlocks = new ArrayList<LeaveBlockHistory>();
142         Criteria criteria = new Criteria();
143 
144         //document id....
145         //get document, and cal entry, fill in query data
146         if (StringUtils.isNotBlank(documentId)) {
147             TimesheetDocumentHeader tsdh = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(documentId);
148             if (tsdh == null) {
149                 return Collections.emptyList();
150             }
151             criteria.addGreaterOrEqualThan("beginTimestamp", tsdh.getBeginDate());
152             criteria.addLessOrEqualThan("endTimestamp",tsdh.getEndDate());
153             criteria.addEqualTo("principalId", tsdh.getPrincipalId());
154         }
155 
156 
157         if(fromDate != null) {
158         	criteria.addGreaterOrEqualThan("beginTimestamp", fromDate.toDate());
159         }
160         if(toDate != null) {
161         	criteria.addLessOrEqualThan("endTimestamp",toDate.toDate());
162         }
163         if(StringUtils.isNotBlank(principalId)) {
164         	criteria.addEqualTo("principalId", principalId);
165         }
166         if(StringUtils.isNotBlank(userPrincipalId)) {
167         	criteria.addEqualTo("principalIdModified", userPrincipalId);
168         }
169         criteria.addEqualTo("leaveBlockType",LMConstants.LEAVE_BLOCK_TYPE.TIME_CALENDAR);
170         Query query = QueryFactory.newQuery(LeaveBlockHistory.class, criteria);
171         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
172         if (c != null) {
173         	leaveBlocks.addAll(c);
174         }
175         return leaveBlocks;
176 	}
177 	@Override
178 	public List<LeaveBlockHistory> getLeaveBlockHistoriesForLookup(String documentId,
179 			String principalId, String userPrincipalId, LocalDate fromDate,
180 			LocalDate toDate, String leaveBlockType) {
181 	   	List<LeaveBlockHistory> leaveBlocks = new ArrayList<LeaveBlockHistory>();
182         Criteria criteria = new Criteria();
183 
184         //document id....
185         //get document, and cal entry, fill in query data
186         if (StringUtils.isNotBlank(documentId)) {
187             TimesheetDocumentHeader tsdh = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(documentId);
188             if (tsdh == null) {
189                 return Collections.emptyList();
190             }
191             criteria.addGreaterOrEqualThan("beginTimestamp", tsdh.getBeginDate());
192             criteria.addLessOrEqualThan("endTimestamp",tsdh.getEndDate());
193             criteria.addEqualTo("principalId", tsdh.getPrincipalId());
194         }
195 
196 
197         if(fromDate != null) {
198         	criteria.addGreaterOrEqualThan("beginTimestamp", fromDate.toDate());
199         }
200         if(toDate != null) {
201         	criteria.addLessOrEqualThan("endTimestamp",toDate.toDate());
202         }
203         if(StringUtils.isNotBlank(principalId)) {
204         	criteria.addEqualTo("principalId", principalId);
205         }
206         if(StringUtils.isNotBlank(userPrincipalId)) {
207         	criteria.addEqualTo("principalIdModified", userPrincipalId);
208         }
209         criteria.addEqualTo("leaveBlockType",leaveBlockType);
210         Query query = QueryFactory.newQuery(LeaveBlockHistory.class, criteria);
211         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
212         if (c != null) {
213         	leaveBlocks.addAll(c);
214         }
215         return leaveBlocks;
216 	}
217 }