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 com.google.common.collect.ImmutableList;
24  import org.apache.commons.collections.CollectionUtils;
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.ojb.broker.query.Criteria;
27  import org.apache.ojb.broker.query.Query;
28  import org.apache.ojb.broker.query.QueryFactory;
29  import org.apache.ojb.broker.query.ReportQueryByCriteria;
30  import org.kuali.hr.core.util.OjbSubQueryUtil;
31  import org.kuali.hr.time.dept.lunch.DeptLunchRule;
32  import org.kuali.hr.time.service.base.TkServiceLocator;
33  import org.kuali.hr.time.util.TKUtils;
34  import org.kuali.hr.time.workarea.WorkArea;
35  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
36  
37  public class DepartmentLunchRuleDaoImpl  extends PlatformAwareDaoBaseOjb implements DepartmentLunchRuleDao {
38      private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
39              .add("dept")
40              .add("workArea")
41              .add("principalId")
42              .add("jobNumber")
43              .build();
44  
45  	@Override
46  	public DeptLunchRule getDepartmentLunchRule(String dept, Long workArea, String principalId, 
47  												Long jobNumber, Date asOfDate) {
48  		Criteria root = new Criteria();
49  
50  		root.addEqualTo("dept", dept);
51  		root.addEqualTo("workArea", workArea);
52  		root.addEqualTo("principalId", principalId);
53  		root.addEqualTo("jobNumber", jobNumber);
54          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(DeptLunchRule.class, asOfDate, EQUAL_TO_FIELDS, false));
55          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(DeptLunchRule.class, EQUAL_TO_FIELDS, false));
56  //		root.addEqualTo("active", true);
57  
58  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
59  		activeFilter.addEqualTo("active", true);
60  		root.addAndCriteria(activeFilter);
61  		
62  		
63  		Query query = QueryFactory.newQuery(DeptLunchRule.class, root);
64  		return (DeptLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
65  		
66  
67  	}
68  
69  	@Override
70  	public DeptLunchRule getDepartmentLunchRule(String tkDeptLunchRuleId) {
71  		Criteria crit = new Criteria();
72  		crit.addEqualTo("tkDeptLunchRuleId", tkDeptLunchRuleId);
73  		
74  		Query query = QueryFactory.newQuery(DeptLunchRule.class, crit);
75  		return (DeptLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
76  	}
77  
78  	@Override
79      @SuppressWarnings("unchecked")
80      public List<DeptLunchRule> getDepartmentLunchRules(String dept, String workArea, String principalId, String jobNumber,
81                                                         Date fromEffdt, Date toEffdt, String active, String showHistory) {
82          List<DeptLunchRule> results = new ArrayList<DeptLunchRule>();
83          
84          Criteria root = new Criteria();
85  
86          if (StringUtils.isNotBlank(dept)) {
87              root.addLike("dept", dept);
88          }
89          
90          if (StringUtils.isNotBlank(principalId)) {
91              root.addLike("principalId", principalId);
92          }
93          
94          if (StringUtils.isNotBlank(jobNumber)) {
95              root.addLike("jobNumber", jobNumber);
96          }
97          
98          if (StringUtils.isNotBlank(dept)) {
99              Criteria workAreaCriteria = new Criteria();
100             Date asOfDate = TKUtils.getCurrentDate();
101             Collection<WorkArea> workAreasForDept = TkServiceLocator.getWorkAreaService().getWorkAreas(dept,asOfDate);
102             if (CollectionUtils.isNotEmpty(workAreasForDept)) {
103                 List<Long> longWorkAreas = new ArrayList<Long>();
104                 for(WorkArea cwa : workAreasForDept){
105                     longWorkAreas.add(cwa.getWorkArea());
106                 }
107                 workAreaCriteria.addIn("workArea", longWorkAreas);
108             }
109             root.addAndCriteria(workAreaCriteria);
110         }
111         
112         if (StringUtils.isNotBlank(workArea)) {
113             OjbSubQueryUtil.addNumericCriteria(root, "workArea", workArea);
114         }
115         
116         if (StringUtils.isNotBlank(active)) {
117         	Criteria activeFilter = new Criteria();
118             if (StringUtils.equals(active, "Y")) {
119                 activeFilter.addEqualTo("active", true);
120             } else if (StringUtils.equals(active, "N")) {
121                 activeFilter.addEqualTo("active", false);
122             }
123             root.addAndCriteria(activeFilter);
124         }
125 
126         Criteria effectiveDateFilter = new Criteria();
127         if (fromEffdt != null) {
128             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
129         }
130         if (toEffdt != null) {
131             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
132         }
133         if (fromEffdt == null && toEffdt == null) {
134             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
135         }
136 
137         if (StringUtils.equals(showHistory, "N")) {
138             root.addAndCriteria(effectiveDateFilter);
139         }
140         if (StringUtils.equals(showHistory, "N")) {
141             root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(DeptLunchRule.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
142             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(DeptLunchRule.class, EQUAL_TO_FIELDS, false));
143         }
144         
145         Query query = QueryFactory.newQuery(DeptLunchRule.class, root);
146         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
147 
148         return results;
149     }
150 
151 }