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.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  		return (SystemScheduledTimeOff)this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(SystemScheduledTimeOff.class, root));
56  	}
57  
58  	@Override
59      @SuppressWarnings("unchecked")
60      public List<SystemScheduledTimeOff> getSystemScheduledTimeOffs(LocalDate fromEffdt, LocalDate toEffdt, String earnCode, LocalDate fromAccruedDate,LocalDate toAccruedDate, 
61      		LocalDate fromSchTimeOffDate, LocalDate toSchTimeOffDate, String active, String showHistory) {
62          
63      	List<SystemScheduledTimeOff> results = new ArrayList<SystemScheduledTimeOff>();
64      	
65      	Criteria root = new Criteria();
66  
67          if (StringUtils.isNotBlank(earnCode)) {
68              root.addLike("UPPER(`earn_code`)", earnCode.toUpperCase()); // KPME-2695
69          }
70  
71          Criteria effectiveDateFilter = new Criteria();
72          if (fromEffdt != null) {
73              effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt.toDate());
74          }
75          if (toEffdt != null) {
76              effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt.toDate());
77          }
78          if (fromEffdt == null && toEffdt == null) {
79              effectiveDateFilter.addLessOrEqualThan("effectiveDate", LocalDate.now().toDate());
80          }
81          root.addAndCriteria(effectiveDateFilter);
82  
83          if (fromAccruedDate != null) {
84              root.addGreaterOrEqualThan("accruedDate", fromAccruedDate.toDate());
85          }
86          if (toAccruedDate != null) {
87              root.addLessOrEqualThan("accruedDate", toAccruedDate.toDate());
88          }
89  
90          if (fromSchTimeOffDate != null) {
91              root.addGreaterOrEqualThan("scheduledTimeOffDate", fromSchTimeOffDate.toDate());
92          }
93          if (toSchTimeOffDate != null) {
94              root.addLessOrEqualThan("scheduledTimeOffDate", toSchTimeOffDate.toDate());
95          }
96          
97          if (StringUtils.isNotBlank(active)) {
98          	Criteria activeFilter = new Criteria();
99              if (StringUtils.equals(active, "Y")) {
100                 activeFilter.addEqualTo("active", true);
101             } else if (StringUtils.equals(active, "N")) {
102                 activeFilter.addEqualTo("active", false);
103             }
104             root.addAndCriteria(activeFilter);
105         }
106         
107         if (StringUtils.equals(showHistory, "N")) {
108 //			      ImmutableList<String> fields = new ImmutableList.Builder<String>()
109 //                    .add("earnCode")
110 //                    .add("accruedDate") //Not part of Primary Business Key
111 //                    .build();
112 //        	KPME-2273/1965 Primary Business Keys List. Using list from now on
113             root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(SystemScheduledTimeOff.class, effectiveDateFilter, SystemScheduledTimeOff.fields, false));
114             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(SystemScheduledTimeOff.class, SystemScheduledTimeOff.fields, false));
115         }
116         
117         Query query = QueryFactory.newQuery(SystemScheduledTimeOff.class, root);
118         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
119         
120         return results;
121     }
122 	
123 	@Override
124     @SuppressWarnings("unchecked")
125     public List<SystemScheduledTimeOff> getSystemScheduledTimeOffsForLeavePlan(LocalDate fromAccruedDate,LocalDate toAccruedDate, String leavePlan) {
126     	List<SystemScheduledTimeOff> results = new ArrayList<SystemScheduledTimeOff>();
127     	Criteria root = new Criteria();
128 
129         if (fromAccruedDate != null) {
130             root.addGreaterOrEqualThan("accruedDate", fromAccruedDate.toDate());
131         }
132         if (toAccruedDate != null) {
133             root.addLessOrEqualThan("accruedDate", toAccruedDate.toDate());
134         }
135         
136         if(StringUtils.isNotEmpty(leavePlan)) {
137         	root.addEqualTo("leavePlan", leavePlan);
138         }
139     	Criteria activeFilter = new Criteria();
140         activeFilter.addEqualTo("active", true);
141         root.addAndCriteria(activeFilter);
142 
143 //        ImmutableList<String> fields = new ImmutableList.Builder<String>()
144 //                .add("leavePlan")
145 //                .add("accruedDate") //Not part of Primary Business Key
146 //                .build();
147 //    	KPME-2273/1965 Primary Business Keys List. Using list from now on
148         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(SystemScheduledTimeOff.class, SystemScheduledTimeOff.fields, false));
149         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(SystemScheduledTimeOff.class, SystemScheduledTimeOff.fields, false));
150         
151         Query query = QueryFactory.newQuery(SystemScheduledTimeOff.class, root);
152         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
153         
154         return results;
155     }
156 
157 }