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, String active) {
81          List<DeptLunchRule> results = new ArrayList<DeptLunchRule>();
82          
83          Criteria root = new Criteria();
84  
85          if (StringUtils.isNotBlank(dept)) {
86              root.addLike("dept", dept);
87          }
88          
89          if (StringUtils.isNotBlank(principalId)) {
90              root.addLike("principalId", principalId);
91          }
92          
93          if (StringUtils.isNotBlank(jobNumber)) {
94              root.addLike("jobNumber", jobNumber);
95          }
96          
97          if (StringUtils.isNotBlank(dept)) {
98              Criteria workAreaCriteria = new Criteria();
99              Date asOfDate = TKUtils.getCurrentDate();
100             Collection<WorkArea> workAreasForDept = TkServiceLocator.getWorkAreaService().getWorkAreas(dept,asOfDate);
101             if (CollectionUtils.isNotEmpty(workAreasForDept)) {
102                 List<Long> longWorkAreas = new ArrayList<Long>();
103                 for(WorkArea cwa : workAreasForDept){
104                     longWorkAreas.add(cwa.getWorkArea());
105                 }
106                 workAreaCriteria.addIn("workArea", longWorkAreas);
107             }
108             root.addAndCriteria(workAreaCriteria);
109         }
110         
111         if (StringUtils.isNotBlank(workArea)) {
112             root.addLike("workArea", workArea);
113         }
114         
115         if (StringUtils.isNotBlank(active)) {
116         	Criteria activeFilter = new Criteria();
117             if (StringUtils.equals(active, "Y")) {
118                 activeFilter.addEqualTo("active", true);
119             } else if (StringUtils.equals(active, "N")) {
120                 activeFilter.addEqualTo("active", false);
121             }
122             root.addAndCriteria(activeFilter);
123         }
124         
125         Query query = QueryFactory.newQuery(DeptLunchRule.class, root);
126         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
127 
128         return results;
129     }
130 
131 }