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.timeoff.dao;
17  
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.ojb.broker.query.Criteria;
24  import org.apache.ojb.broker.query.Query;
25  import org.apache.ojb.broker.query.QueryFactory;
26  import org.joda.time.LocalDate;
27  import org.kuali.kpme.core.util.OjbSubQueryUtil;
28  import org.kuali.kpme.tklm.leave.timeoff.SystemScheduledTimeOff;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  
31  public class SystemScheduledTimeOffDaoOjbImpl extends PlatformAwareDaoBaseOjb implements SystemScheduledTimeOffDao {
32  
33  	@Override
34  	public SystemScheduledTimeOff getSystemScheduledTimeOff(String lmSystemScheduledTimeOffId) {
35  		Criteria crit = new Criteria();
36  		crit.addEqualTo("lmSystemScheduledTimeOffId", lmSystemScheduledTimeOffId);
37  		Query query = QueryFactory.newQuery(SystemScheduledTimeOff.class, crit);
38  		return (SystemScheduledTimeOff) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
39  	}
40  
41  	@Override
42  	public List<SystemScheduledTimeOff> getSystemScheduledTimeOffForPayPeriod(
43  			String leavePlan, LocalDate startDate, LocalDate endDate) {
44  		Criteria root = new Criteria();
45  		root.addEqualTo("leavePlan", leavePlan);
46  		root.addBetween("accruedDate", startDate.toDate(), endDate.toDate());
47  		return (List<SystemScheduledTimeOff>)this.getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(SystemScheduledTimeOff.class, root));
48  	}
49  
50  	@Override
51  	public SystemScheduledTimeOff getSystemScheduledTimeOffByDate(String leavePlan, LocalDate startDate) {
52  		Criteria root = new Criteria();
53  		root.addEqualTo("leavePlan", leavePlan);
54  		root.addEqualTo("accruedDate", startDate.toDate());
55  		root.addEqualTo("active", true);
56  		return (SystemScheduledTimeOff)this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(SystemScheduledTimeOff.class, root));
57  	}
58  
59  	@Override
60      @SuppressWarnings("unchecked")
61      public List<SystemScheduledTimeOff> getSystemScheduledTimeOffs(LocalDate fromEffdt, LocalDate toEffdt, String earnCode, LocalDate fromAccruedDate,LocalDate toAccruedDate, 
62      		LocalDate fromSchTimeOffDate, LocalDate toSchTimeOffDate, String premiumEarnCode, String active, String showHistory) {
63          
64      	List<SystemScheduledTimeOff> results = new ArrayList<SystemScheduledTimeOff>();
65      	
66      	Criteria root = new Criteria();
67  
68          if (StringUtils.isNotBlank(earnCode)) {
69              root.addLike("UPPER(earnCode)", earnCode.toUpperCase()); // KPME-2695
70          }
71          
72          if (StringUtils.isNotBlank(premiumEarnCode)) {
73              root.addLike("UPPER(premiumEarnCode)", premiumEarnCode.toUpperCase()); // KPME-3161
74          }
75  
76          Criteria effectiveDateFilter = new Criteria();
77          if (fromEffdt != null) {
78              effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt.toDate());
79          }
80          if (toEffdt != null) {
81              effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt.toDate());
82          }
83          if (fromEffdt == null && toEffdt == null) {
84              effectiveDateFilter.addLessOrEqualThan("effectiveDate", LocalDate.now().toDate());
85          }
86          root.addAndCriteria(effectiveDateFilter);
87  
88          if (fromAccruedDate != null) {
89              root.addGreaterOrEqualThan("accruedDate", fromAccruedDate.toDate());
90          }
91          if (toAccruedDate != null) {
92              root.addLessOrEqualThan("accruedDate", toAccruedDate.toDate());
93          }
94  
95          if (fromSchTimeOffDate != null) {
96              root.addGreaterOrEqualThan("scheduledTimeOffDate", fromSchTimeOffDate.toDate());
97          }
98          if (toSchTimeOffDate != null) {
99              root.addLessOrEqualThan("scheduledTimeOffDate", toSchTimeOffDate.toDate());
100         }
101         
102         if (StringUtils.isNotBlank(active)) {
103         	Criteria activeFilter = new Criteria();
104             if (StringUtils.equals(active, "Y")) {
105                 activeFilter.addEqualTo("active", true);
106             } else if (StringUtils.equals(active, "N")) {
107                 activeFilter.addEqualTo("active", false);
108             }
109             root.addAndCriteria(activeFilter);
110         }
111         
112         if (StringUtils.equals(showHistory, "N")) {
113 //			      ImmutableList<String> fields = new ImmutableList.Builder<String>()
114 //                    .add("earnCode")
115 //                    .add("accruedDate") //Not part of Primary Business Key
116 //                    .build();
117 //        	KPME-2273/1965 Primary Business Keys List. Using list from now on
118             root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(SystemScheduledTimeOff.class, effectiveDateFilter, SystemScheduledTimeOff.fields, false));
119             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(SystemScheduledTimeOff.class, SystemScheduledTimeOff.fields, false));
120         }
121         
122         Query query = QueryFactory.newQuery(SystemScheduledTimeOff.class, root);
123         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
124         
125         return results;
126     }
127 	
128 	@Override
129     @SuppressWarnings("unchecked")
130     public List<SystemScheduledTimeOff> getSystemScheduledTimeOffsForLeavePlan(LocalDate fromAccruedDate,LocalDate toAccruedDate, String leavePlan) {
131     	List<SystemScheduledTimeOff> results = new ArrayList<SystemScheduledTimeOff>();
132     	Criteria root = new Criteria();
133 
134         if (fromAccruedDate != null) {
135             root.addGreaterOrEqualThan("accruedDate", fromAccruedDate.toDate());
136         }
137         if (toAccruedDate != null) {
138             root.addLessOrEqualThan("accruedDate", toAccruedDate.toDate());
139         }
140         
141         if(StringUtils.isNotEmpty(leavePlan)) {
142         	root.addEqualTo("leavePlan", leavePlan);
143         }
144     	Criteria activeFilter = new Criteria();
145         activeFilter.addEqualTo("active", true);
146         root.addAndCriteria(activeFilter);
147 
148 //        ImmutableList<String> fields = new ImmutableList.Builder<String>()
149 //                .add("leavePlan")
150 //                .add("accruedDate") //Not part of Primary Business Key
151 //                .build();
152 //    	KPME-2273/1965 Primary Business Keys List. Using list from now on
153         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(SystemScheduledTimeOff.class, SystemScheduledTimeOff.fields, false));
154         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(SystemScheduledTimeOff.class, SystemScheduledTimeOff.fields, false));
155         
156         Query query = QueryFactory.newQuery(SystemScheduledTimeOff.class, root);
157         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
158         
159         return results;
160     }
161 
162 }