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.time.dept.lunch.dao;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  
23  import org.apache.commons.collections.CollectionUtils;
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.time.dept.lunch.DeptLunchRule;
30  import org.kuali.hr.time.service.base.TkServiceLocator;
31  import org.kuali.hr.time.util.TKUtils;
32  import org.kuali.hr.time.workarea.WorkArea;
33  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
34  
35  public class DepartmentLunchRuleDaoImpl  extends PlatformAwareDaoBaseOjb implements DepartmentLunchRuleDao {
36  
37  	@Override
38  	public DeptLunchRule getDepartmentLunchRule(String dept, Long workArea, String principalId, 
39  												Long jobNumber, Date asOfDate) {
40  		Criteria root = new Criteria();
41  		Criteria effdt = new Criteria();
42  		Criteria timestamp = new Criteria();
43  
44  		effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
45  		effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
46  		effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
47  		effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
48  		effdt.addLessOrEqualThan("effectiveDate", asOfDate);
49  //		effdt.addEqualTo("active", true);
50  		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(DeptLunchRule.class, effdt);
51  		effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
52  
53  		timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
54  		timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
55  		timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
56  		timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
57  		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
58  //		timestamp.addEqualTo("active", true);
59  		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(DeptLunchRule.class, timestamp);
60  		timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
61  
62  		root.addEqualTo("dept", dept);
63  		root.addEqualTo("workArea", workArea);
64  		root.addEqualTo("principalId", principalId);
65  		root.addEqualTo("jobNumber", jobNumber);
66  		root.addEqualTo("effectiveDate", effdtSubQuery);
67  		root.addEqualTo("timestamp", timestampSubQuery);
68  //		root.addEqualTo("active", true);
69  
70  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
71  		activeFilter.addEqualTo("active", true);
72  		root.addAndCriteria(activeFilter);
73  		
74  		
75  		Query query = QueryFactory.newQuery(DeptLunchRule.class, root);
76  		return (DeptLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
77  		
78  
79  	}
80  
81  	@Override
82  	public DeptLunchRule getDepartmentLunchRule(String tkDeptLunchRuleId) {
83  		Criteria crit = new Criteria();
84  		crit.addEqualTo("tkDeptLunchRuleId", tkDeptLunchRuleId);
85  		
86  		Query query = QueryFactory.newQuery(DeptLunchRule.class, crit);
87  		return (DeptLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
88  	}
89  
90  	@Override
91      @SuppressWarnings("unchecked")
92      public List<DeptLunchRule> getDepartmentLunchRules(String dept, String workArea, String principalId, String jobNumber, String active) {
93          List<DeptLunchRule> results = new ArrayList<DeptLunchRule>();
94          
95          Criteria root = new Criteria();
96  
97          if (StringUtils.isNotBlank(dept)) {
98              root.addLike("dept", dept);
99          }
100         
101         if (StringUtils.isNotBlank(principalId)) {
102             root.addLike("principalId", principalId);
103         }
104         
105         if (StringUtils.isNotBlank(jobNumber)) {
106             root.addLike("jobNumber", jobNumber);
107         }
108         
109         if (StringUtils.isNotBlank(dept)) {
110             Criteria workAreaCriteria = new Criteria();
111             Date asOfDate = TKUtils.getCurrentDate();
112             Collection<WorkArea> workAreasForDept = TkServiceLocator.getWorkAreaService().getWorkAreas(dept,asOfDate);
113             if (CollectionUtils.isNotEmpty(workAreasForDept)) {
114                 List<Long> longWorkAreas = new ArrayList<Long>();
115                 for(WorkArea cwa : workAreasForDept){
116                     longWorkAreas.add(cwa.getWorkArea());
117                 }
118                 workAreaCriteria.addIn("workArea", longWorkAreas);
119             }
120             root.addAndCriteria(workAreaCriteria);
121         }
122         
123         if (StringUtils.isNotBlank(workArea)) {
124             root.addLike("workArea", workArea);
125         }
126         
127         if (StringUtils.isNotBlank(active)) {
128         	Criteria activeFilter = new Criteria();
129             if (StringUtils.equals(active, "Y")) {
130                 activeFilter.addEqualTo("active", true);
131             } else if (StringUtils.equals(active, "N")) {
132                 activeFilter.addEqualTo("active", false);
133             }
134             root.addAndCriteria(activeFilter);
135         }
136         
137         Query query = QueryFactory.newQuery(DeptLunchRule.class, root);
138         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
139 
140         return results;
141     }
142 
143 }