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.time.rules.lunch.department.dao;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.List;
21  
22  import org.apache.commons.collections.CollectionUtils;
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.ojb.broker.query.Criteria;
25  import org.apache.ojb.broker.query.Query;
26  import org.apache.ojb.broker.query.QueryFactory;
27  import org.joda.time.LocalDate;
28  import org.kuali.kpme.core.service.HrServiceLocator;
29  import org.kuali.kpme.core.util.OjbSubQueryUtil;
30  import org.kuali.kpme.core.workarea.WorkArea;
31  import org.kuali.kpme.tklm.time.rules.lunch.department.DeptLunchRule;
32  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
33  
34  public class DepartmentLunchRuleDaoOjbImpl extends PlatformAwareDaoBaseOjb implements DepartmentLunchRuleDao {
35      @Override
36  	public DeptLunchRule getDepartmentLunchRule(String dept, Long workArea, String principalId, 
37  												Long jobNumber, LocalDate asOfDate) {
38  		Criteria root = new Criteria();
39  
40  		root.addEqualTo("dept", dept);
41  		root.addEqualTo("workArea", workArea);
42  		root.addEqualTo("principalId", principalId);
43  		root.addEqualTo("jobNumber", jobNumber);
44          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(DeptLunchRule.class, asOfDate, DeptLunchRule.EQUAL_TO_FIELDS, true));
45          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(DeptLunchRule.class, DeptLunchRule.EQUAL_TO_FIELDS, true));
46  //		root.addEqualTo("active", true);
47  
48  		//Criteria activeFilter = new Criteria(); // Inner Join For Activity
49  		//activeFilter.addEqualTo("active", true);
50  		//root.addAndCriteria(activeFilter);
51  		
52  		
53  		Query query = QueryFactory.newQuery(DeptLunchRule.class, root);
54  		return (DeptLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
55  		
56  
57  	}
58  
59  	@Override
60  	public DeptLunchRule getDepartmentLunchRule(String tkDeptLunchRuleId) {
61  		Criteria crit = new Criteria();
62  		crit.addEqualTo("tkDeptLunchRuleId", tkDeptLunchRuleId);
63  		
64  		Query query = QueryFactory.newQuery(DeptLunchRule.class, crit);
65  		return (DeptLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
66  	}
67  
68  	@Override
69      @SuppressWarnings("unchecked")
70      public List<DeptLunchRule> getDepartmentLunchRules(String dept, String workArea, String principalId, String jobNumber, 
71      												   LocalDate fromEffdt, LocalDate toEffdt, String active, String showHistory) {
72          List<DeptLunchRule> results = new ArrayList<DeptLunchRule>();
73          
74          Criteria root = new Criteria();
75  
76          if (StringUtils.isNotBlank(dept)) {
77              root.addLike("dept", dept);
78          }
79          
80          if (StringUtils.isNotBlank(principalId)) {
81              root.addLike("UPPER(principalId)", principalId.toUpperCase()); // KPME-2695 in case principal id is not a number
82          }
83          
84  //        if (StringUtils.isNotBlank(jobNumber)) {
85  //            root.addLike("jobNumber", jobNumber);
86  //        }
87          
88          if (StringUtils.isNotBlank(jobNumber)) {
89          	OjbSubQueryUtil.addNumericCriteria(root, "jobNumber", jobNumber);
90          }
91          
92          if (StringUtils.isNotBlank(dept)) {
93              Criteria workAreaCriteria = new Criteria();
94              LocalDate asOfDate = LocalDate.now();
95              List<Long> workAreasForDept = HrServiceLocator.getWorkAreaService().getWorkAreasForDepartment(dept, asOfDate);
96              if (CollectionUtils.isNotEmpty(workAreasForDept)) {
97                  workAreaCriteria.addIn("workArea", workAreasForDept);
98              }
99              root.addAndCriteria(workAreaCriteria);
100         }
101         
102         if (StringUtils.isNotBlank(workArea)) {
103         	OjbSubQueryUtil.addNumericCriteria(root, "workArea", workArea);
104         }
105         
106         if (StringUtils.isNotBlank(active)) {
107         	Criteria activeFilter = new Criteria();
108             if (StringUtils.equals(active, "Y")) {
109                 activeFilter.addEqualTo("active", true);
110             } else if (StringUtils.equals(active, "N")) {
111                 activeFilter.addEqualTo("active", false);
112             }
113             root.addAndCriteria(activeFilter);
114         }
115         
116         Criteria effectiveDateFilter = new Criteria();
117         if (fromEffdt != null) {
118         	effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt.toDate());
119         }
120         if (toEffdt != null) {
121         	effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt.toDate());
122         }
123         if (fromEffdt == null && toEffdt == null) {
124         	effectiveDateFilter.addLessOrEqualThan("effectiveDate", LocalDate.now().toDate());
125         }
126 
127         if (StringUtils.equals(showHistory, "N")) {
128         	root.addAndCriteria(effectiveDateFilter);
129         }
130         if (StringUtils.equals(showHistory, "N")) {
131         	root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(DeptLunchRule.class, effectiveDateFilter, DeptLunchRule.EQUAL_TO_FIELDS, false));
132         	root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(DeptLunchRule.class, DeptLunchRule.EQUAL_TO_FIELDS, false));
133         }
134         
135         Query query = QueryFactory.newQuery(DeptLunchRule.class, root);
136         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
137 
138         return results;
139     }
140 
141 }