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