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.workarea.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.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.time.util.TKUtils;
31  import org.kuali.hr.time.workarea.WorkArea;
32  import org.kuali.rice.core.api.search.SearchOperator;
33  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
34  import org.kuali.rice.krad.service.KRADServiceLocator;
35  
36  public class WorkAreaDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements WorkAreaDao {
37      private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
38              .add("workArea")
39              .build();
40  
41      @Override
42      public WorkArea getWorkArea(Long workArea, Date asOfDate) {
43  		Criteria root = new Criteria();
44  
45  		root.addEqualTo("workArea", workArea);
46          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(WorkArea.class, asOfDate, EQUAL_TO_FIELDS, false));
47          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(WorkArea.class, EQUAL_TO_FIELDS, false));
48  
49  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
50  		activeFilter.addEqualTo("active", true);
51  		root.addAndCriteria(activeFilter);
52  
53  		Query query = QueryFactory.newQuery(WorkArea.class, root);
54  		return (WorkArea) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
55      }
56  
57      @Override
58      public List<WorkArea> getWorkArea(String department, Date asOfDate) {
59          Criteria root = new Criteria();
60  
61          root.addEqualTo("dept", department);
62          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(WorkArea.class, asOfDate, EQUAL_TO_FIELDS, false));
63          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(WorkArea.class, EQUAL_TO_FIELDS, false));
64  
65          Criteria activeFilter = new Criteria(); // Inner Join For Activity
66          activeFilter.addEqualTo("active", true);
67          root.addAndCriteria(activeFilter);
68  
69          Query query = QueryFactory.newQuery(WorkArea.class, root);
70          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
71          List<WorkArea> wal = new ArrayList<WorkArea>(c.size());
72          wal.addAll(c);
73          return wal;
74      }
75  
76      @Override
77      public void saveOrUpdate(WorkArea workArea) {
78      	this.getPersistenceBrokerTemplate().store(workArea);
79      }
80  
81  	@Override
82  	public WorkArea getWorkArea(String tkWorkAreaId) {
83  		Criteria crit = new Criteria();
84  		crit.addEqualTo("tkWorkAreaId", tkWorkAreaId);
85  		
86  		Query query = QueryFactory.newQuery(WorkArea.class, crit);
87  		return (WorkArea)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
88  	}
89  	
90  	public Long getNextWorkAreaKey(){
91  		 return KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("tk_work_area_key_s");
92  	}
93  
94  	@Override
95  	@SuppressWarnings("unchecked")
96  	public List<WorkArea> getWorkAreas(String dept, String workArea, String description, Date fromEffdt, Date toEffdt, String active, String showHistory) {
97  		List<WorkArea> results = new ArrayList<WorkArea>();
98  		
99  		Criteria root = new Criteria();
100 
101 		if (StringUtils.isNotBlank(dept)) {
102 			root.addLike("dept", dept);
103 		}
104 		
105 		if (StringUtils.isNotBlank(workArea)) {
106             OjbSubQueryUtil.addNumericCriteria(root, "workArea", workArea);
107 		}
108 		
109 		if (StringUtils.isNotBlank(description)) {
110 			root.addLike("description", description);
111 		}
112 		
113 		Criteria effectiveDateFilter = new Criteria();
114 		if (fromEffdt != null) {
115 		    effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
116 		}
117 		if (toEffdt != null) {
118 		    effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
119 		}
120 		if (fromEffdt == null && toEffdt == null) {
121 		    effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
122 		}
123 		root.addAndCriteria(effectiveDateFilter);
124 		
125         if (StringUtils.isNotBlank(active)) {
126         	Criteria activeFilter = new Criteria();
127             if (StringUtils.equals(active, "Y")) {
128                 activeFilter.addEqualTo("active", true);
129             } else if (StringUtils.equals(active, "N")) {
130                 activeFilter.addEqualTo("active", false);
131             }
132             root.addAndCriteria(activeFilter);
133         }
134 		
135 		if (StringUtils.equals(showHistory, "N")) {
136             root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(WorkArea.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
137             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(WorkArea.class, EQUAL_TO_FIELDS, false));
138 		}
139 		
140         Query query = QueryFactory.newQuery(WorkArea.class, root);
141         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
142 		
143 		return results;
144 	}
145 
146 	@Override
147 	public int getWorkAreaCount(String dept, Long workArea) {
148 		Criteria crit = new Criteria();
149 		if(dept != null) {
150 			crit.addEqualTo("dept", dept);
151 		}
152 		crit.addEqualTo("workArea", workArea);
153 		Query query = QueryFactory.newQuery(WorkArea.class, crit);
154 		return this.getPersistenceBrokerTemplate().getCount(query); 
155 	}
156 }