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.task.dao;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.ojb.broker.query.Criteria;
24  import org.apache.ojb.broker.query.Query;
25  import org.apache.ojb.broker.query.QueryFactory;
26  import org.apache.ojb.broker.query.ReportQueryByCriteria;
27  import org.kuali.hr.time.task.Task;
28  import org.kuali.hr.time.util.TKUtils;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  
31  public class TaskDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TaskDao {
32  	
33  	@Override
34  	public Task getTask(String tkTaskId) {
35  		Criteria crit = new Criteria();
36  		crit.addEqualTo("tkTaskId", tkTaskId);
37  		
38  		Query query = QueryFactory.newQuery(Task.class, crit);
39  		return (Task)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
40  	}
41  
42      @Override
43      public Task getMaxTask() {
44          Criteria root = new Criteria();
45          Criteria crit = new Criteria();
46  
47          ReportQueryByCriteria taskNumberSubQuery = QueryFactory.newReportQuery(Task.class, crit);
48          taskNumberSubQuery.setAttributes(new String[]{"max(task)"});
49  
50          root.addEqualTo("task", taskNumberSubQuery);
51  
52          Query query = QueryFactory.newQuery(Task.class, root);
53          return (Task) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
54      }
55  
56      @Override
57      public Task getTask(Long task, Date asOfDate) {
58          Criteria root = new Criteria();
59          Criteria effdt = new Criteria();
60          Criteria timestamp = new Criteria();
61  
62          effdt.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
63          effdt.addLessOrEqualThan("effectiveDate", asOfDate);
64          ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Task.class, effdt);
65          effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
66  
67          timestamp.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
68          timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
69          ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Task.class, timestamp);
70          timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
71  
72          root.addEqualTo("task", task);
73          root.addEqualTo("effectiveDate", effdtSubQuery);
74          root.addEqualTo("timestamp", timestampSubQuery);
75  
76          Criteria activeFilter = new Criteria(); // Inner Join For Activity
77          activeFilter.addEqualTo("active", true);
78          root.addAndCriteria(activeFilter);
79  
80          Task t = null;
81          Query query = QueryFactory.newQuery(Task.class, root);
82          Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
83          if (obj instanceof Task)
84              t = (Task) obj;
85  
86          return t;
87      }
88  
89      @Override
90      public void saveOrUpdate(Task task) {
91          this.getPersistenceBrokerTemplate().store(task);
92      }
93  
94      @Override
95      public void saveOrUpdate(List<Task> tasks) {
96          for (Task task : tasks)
97              this.getPersistenceBrokerTemplate().store(task);
98      }
99  
100     @SuppressWarnings("unchecked")
101 	@Override
102     public List<Task> getTasks(Long task, String description, Long workArea, Date fromEffdt, Date toEffdt) {
103         Criteria root = new Criteria();
104 
105         List<Task> results = new ArrayList<Task>();
106 
107         if (task != null) {
108         	root.addLike("task", task);
109         }
110         
111         if (StringUtils.isNotBlank(description)) {
112         	root.addLike("description", description);
113         }
114         
115         if (workArea != null) {
116         	root.addEqualTo("workArea", workArea);
117         }
118         
119         Criteria effectiveDateFilter = new Criteria();
120         if (fromEffdt != null) {
121             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
122         }
123         if (toEffdt != null) {
124             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
125         }
126         if (fromEffdt == null && toEffdt == null) {
127             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
128         }
129         root.addAndCriteria(effectiveDateFilter);
130 
131         Criteria activeFilter = new Criteria();
132         activeFilter.addEqualTo("active", true);
133         root.addAndCriteria(activeFilter);
134 
135         Criteria effdt = new Criteria();
136         effdt.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
137         effdt.addAndCriteria(effectiveDateFilter);
138         ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Task.class, effdt);
139         effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
140         root.addEqualTo("effectiveDate", effdtSubQuery);
141         
142         Criteria timestamp = new Criteria();
143         timestamp.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
144         timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
145         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Task.class, timestamp);
146         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
147         root.addEqualTo("timestamp", timestampSubQuery);
148 
149         Query query = QueryFactory.newQuery(Task.class, root);
150         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
151 
152         return results;
153     }
154    
155     @Override
156     public int getTaskCount(Long task) {
157     	Criteria crit = new Criteria();
158 		crit.addEqualTo("task",task);
159 		Query query = QueryFactory.newQuery(Task.class, crit);
160 		return this.getPersistenceBrokerTemplate().getCount(query);
161     }
162 }