View Javadoc

1   /**
2    * Copyright 2004-2012 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 org.apache.commons.lang.StringUtils;
24  import org.apache.log4j.Logger;
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.util.TKUtils;
30  import org.kuali.hr.time.workarea.WorkArea;
31  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
32  import org.kuali.rice.krad.service.KRADServiceLocator;
33  
34  public class WorkAreaDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements WorkAreaDao {
35  
36      private static final Logger LOG = Logger.getLogger(WorkAreaDaoSpringOjbImpl.class);
37  
38      @Override
39      public WorkArea getWorkArea(Long workArea, Date asOfDate) {
40  		Criteria root = new Criteria();
41  		Criteria effdt = new Criteria();
42  		Criteria timestamp = new Criteria();
43  
44  		effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
45  		effdt.addLessOrEqualThan("effectiveDate", asOfDate);
46  		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(WorkArea.class, effdt);
47  		effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
48  
49  		timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
50  		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
51  		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(WorkArea.class, timestamp);
52  		timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
53  
54  		root.addEqualTo("workArea", workArea);
55  		root.addEqualTo("effectiveDate", effdtSubQuery);
56  		root.addEqualTo("timestamp", timestampSubQuery);
57  
58  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
59  		activeFilter.addEqualTo("active", true);
60  		root.addAndCriteria(activeFilter);
61  
62  		Query query = QueryFactory.newQuery(WorkArea.class, root);
63  		return (WorkArea) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
64      }
65  
66      @Override
67      public List<WorkArea> getWorkArea(String department, Date asOfDate) {
68          Criteria root = new Criteria();
69          Criteria effdt = new Criteria();
70          Criteria timestamp = new Criteria();
71  
72          effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
73          effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
74          effdt.addLessOrEqualThan("effectiveDate", asOfDate);
75          ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(WorkArea.class, effdt);
76          effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
77  
78          timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
79          timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
80          timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
81          ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(WorkArea.class, timestamp);
82          timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
83  
84          root.addEqualTo("dept", department);
85          root.addEqualTo("effectiveDate", effdtSubQuery);
86          root.addEqualTo("timestamp", timestampSubQuery);
87  
88          Criteria activeFilter = new Criteria(); // Inner Join For Activity
89          activeFilter.addEqualTo("active", true);
90          root.addAndCriteria(activeFilter);
91  
92          Query query = QueryFactory.newQuery(WorkArea.class, root);
93          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
94          List<WorkArea> wal = new ArrayList<WorkArea>(c.size());
95          wal.addAll(c);
96          return wal;
97      }
98  
99      @Override
100     public void saveOrUpdate(WorkArea workArea) {
101     	this.getPersistenceBrokerTemplate().store(workArea);
102     }
103 
104 	@Override
105 	public WorkArea getWorkArea(String tkWorkAreaId) {
106 		Criteria crit = new Criteria();
107 		crit.addEqualTo("tkWorkAreaId", tkWorkAreaId);
108 		
109 		Query query = QueryFactory.newQuery(WorkArea.class, crit);
110 		return (WorkArea)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
111 	}
112 	
113 	public Long getNextWorkAreaKey(){
114 		 return KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("tk_work_area_key_s");
115 	}
116 
117 
118 
119 	@Override
120 	public List<WorkArea> getWorkAreas(String dept, String workArea,
121 			String workAreaDescr, Date fromEffdt, Date toEffdt, String active,
122 			String showHistory) {
123 		Criteria crit = new Criteria();
124 		Criteria effdt = new Criteria();
125 		Criteria timestamp = new Criteria();
126 		
127 		List<WorkArea> results = new ArrayList<WorkArea>();
128 		if(StringUtils.isNotBlank(dept)){
129 			crit.addLike("dept", dept);
130 		}
131 		if(StringUtils.isNotBlank(workArea)){
132 			crit.addLike("workArea", workArea);
133 		}
134 		if(StringUtils.isNotBlank(workAreaDescr)){
135 			crit.addLike("description", workAreaDescr);
136 		}
137 		if(fromEffdt != null){
138 			crit.addGreaterOrEqualThan("effectiveDate", fromEffdt);
139 		} 
140 		if(toEffdt != null){
141 			crit.addLessOrEqualThan("effectiveDate", toEffdt);
142 		} else {
143 			crit.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
144 		}
145 		
146 		if(StringUtils.isEmpty(active) && StringUtils.equals(showHistory,"Y")){
147 	        Query query = QueryFactory.newQuery(WorkArea.class, crit);
148 	        Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
149 	        results.addAll(c);
150 		}
151 		else if(StringUtils.isEmpty(active) && StringUtils.equals(showHistory, "N")){
152 			effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
153 			if(toEffdt != null){
154 				effdt.addLessOrEqualThan("effectiveDate", toEffdt);
155 			}
156 			ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(WorkArea.class, effdt);
157 			effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
158 
159 			timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
160 			timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
161 			ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(WorkArea.class, timestamp);
162 			timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
163 			if(StringUtils.isNotBlank(workArea)){
164 				crit.addEqualTo("workArea", workArea);
165 			}
166 			crit.addEqualTo("effectiveDate", effdtSubQuery);
167 			crit.addEqualTo("timestamp", timestampSubQuery);
168 
169 	        Query query = QueryFactory.newQuery(WorkArea.class, crit);
170 	        Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
171 	        results.addAll(c);
172 		}
173 		
174 		else if(StringUtils.equals(active, "Y") && StringUtils.equals("N", showHistory)){
175 			effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
176 			if(toEffdt != null){
177 				effdt.addLessOrEqualThan("effectiveDate", toEffdt);
178 			}
179 			ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(WorkArea.class, effdt);
180 			effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
181 
182 			timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
183 			timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
184 			ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(WorkArea.class, timestamp);
185 			timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
186 			if(StringUtils.isNotBlank(workArea)){
187 				crit.addEqualTo("workArea", workArea);
188 			}
189 			crit.addEqualTo("effectiveDate", effdtSubQuery);
190 			crit.addEqualTo("timestamp", timestampSubQuery);
191 
192 			Criteria activeFilter = new Criteria(); // Inner Join For Activity
193 			activeFilter.addEqualTo("active", true);
194 			crit.addAndCriteria(activeFilter);
195 	        Query query = QueryFactory.newQuery(WorkArea.class, crit);
196 	        Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
197 	        results.addAll(c);
198 		} //return all active records from the database
199 		else if(StringUtils.equals(active, "Y") && StringUtils.equals("Y", showHistory)){
200 			Criteria activeFilter = new Criteria(); // Inner Join For Activity
201 			activeFilter.addEqualTo("active", true);
202 			crit.addAndCriteria(activeFilter);
203 	        Query query = QueryFactory.newQuery(WorkArea.class, crit);
204 	        Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
205 	        results.addAll(c);
206 		} 
207 		//return all inactive records in the database
208 		else if(StringUtils.equals(active, "N") && StringUtils.equals(showHistory, "Y")){
209 			Criteria activeFilter = new Criteria(); // Inner Join For Activity
210 			activeFilter.addEqualTo("active", false);
211 			crit.addAndCriteria(activeFilter);
212 	        Query query = QueryFactory.newQuery(WorkArea.class, crit);
213 	        Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
214 	        results.addAll(c);
215 		}
216 		
217 		//return the most effective inactive rows if there are no active rows <= the curr date
218 		else if(StringUtils.equals(active, "N") && StringUtils.equals(showHistory, "N")){
219 			effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
220 			if(toEffdt != null){
221 				effdt.addLessOrEqualThan("effectiveDate", toEffdt);
222 			}
223 			ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(WorkArea.class, effdt);
224 			effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
225 
226 			timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
227 			timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
228 			ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(WorkArea.class, timestamp);
229 			timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
230 			if(StringUtils.isNotBlank(workArea)){
231 				crit.addEqualTo("workArea", workArea);
232 			}
233 			crit.addEqualTo("effectiveDate", effdtSubQuery);
234 			crit.addEqualTo("timestamp", timestampSubQuery);
235 
236 			Criteria activeFilter = new Criteria(); // Inner Join For Activity
237 			activeFilter.addEqualTo("active", false);
238 			crit.addAndCriteria(activeFilter);
239 	        Query query = QueryFactory.newQuery(WorkArea.class, crit);
240 	        Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
241 	        results.addAll(c);
242 			
243 		}
244 		return results;
245 	}
246 
247 	@Override
248 	public int getWorkAreaCount(String dept, Long workArea) {
249 		Criteria crit = new Criteria();
250 		if(dept != null) {
251 			crit.addEqualTo("dept", dept);
252 		}
253 		crit.addEqualTo("workArea", workArea);
254 		Query query = QueryFactory.newQuery(WorkArea.class, crit);
255 		return this.getPersistenceBrokerTemplate().getCount(query); 
256 	}
257 }