001    /**
002     * Copyright 2004-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.task.dao;
017    
018    import java.sql.Date;
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.List;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.apache.ojb.broker.query.Criteria;
025    import org.apache.ojb.broker.query.Query;
026    import org.apache.ojb.broker.query.QueryFactory;
027    import org.apache.ojb.broker.query.ReportQueryByCriteria;
028    import org.kuali.hr.time.task.Task;
029    import org.kuali.hr.time.util.TKUtils;
030    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
031    
032    public class TaskDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TaskDao {
033    
034        @Override
035        public Task getMaxTask() {
036            Criteria root = new Criteria();
037            Criteria crit = new Criteria();
038    
039            ReportQueryByCriteria taskNumberSubQuery = QueryFactory.newReportQuery(Task.class, crit);
040            taskNumberSubQuery.setAttributes(new String[]{"max(task)"});
041    
042            root.addEqualTo("task", taskNumberSubQuery);
043    
044            Query query = QueryFactory.newQuery(Task.class, root);
045            return (Task) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
046        }
047    
048        @Override
049        public Task getTask(Long task, Date asOfDate) {
050            Criteria root = new Criteria();
051            Criteria effdt = new Criteria();
052            Criteria timestamp = new Criteria();
053    
054            effdt.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
055            effdt.addLessOrEqualThan("effectiveDate", asOfDate);
056            ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Task.class, effdt);
057            effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
058    
059            timestamp.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
060            timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
061            ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Task.class, timestamp);
062            timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
063    
064            root.addEqualTo("task", task);
065            root.addEqualTo("effectiveDate", effdtSubQuery);
066            root.addEqualTo("timestamp", timestampSubQuery);
067    
068            Criteria activeFilter = new Criteria(); // Inner Join For Activity
069            activeFilter.addEqualTo("active", true);
070            root.addAndCriteria(activeFilter);
071    
072            Task t = null;
073            Query query = QueryFactory.newQuery(Task.class, root);
074            Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
075            if (obj instanceof Task)
076                t = (Task) obj;
077    
078            return t;
079        }
080    
081        @Override
082        public void saveOrUpdate(Task task) {
083            this.getPersistenceBrokerTemplate().store(task);
084        }
085    
086        @Override
087        public void saveOrUpdate(List<Task> tasks) {
088            for (Task task : tasks)
089                this.getPersistenceBrokerTemplate().store(task);
090        }
091    
092        @Override
093        public List<Task> getTasks(Long task, String description, Long workArea, String workAreaDesc, Date fromEffdt, Date toEffdt) {
094            Criteria crit = new Criteria();
095            Criteria effdt = new Criteria();
096            Criteria timestamp = new Criteria();
097    
098            List<Task> results = new ArrayList<Task>();
099    
100            if (task != null) {
101                crit.addLike("task", task);
102            }
103            if (StringUtils.isNotEmpty(description)) {
104                crit.addLike("description", description);
105            }
106            if (workArea != null) {
107                crit.addEqualTo("workArea", workArea);
108            }
109            if (fromEffdt != null) {
110                crit.addGreaterOrEqualThan("effectiveDate", fromEffdt);
111            }
112            if (toEffdt != null) {
113                crit.addLessOrEqualThan("effectiveDate", toEffdt);
114            } else {
115                crit.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
116            }
117    
118            effdt.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
119            if (toEffdt != null) {
120                effdt.addLessOrEqualThan("effectiveDate", toEffdt);
121            }
122            ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Task.class, effdt);
123            effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
124    
125            timestamp.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
126            timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
127            ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Task.class, timestamp);
128            timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
129    
130            crit.addEqualTo("effectiveDate", effdtSubQuery);
131            crit.addEqualTo("timestamp", timestampSubQuery);
132    
133            Criteria activeFilter = new Criteria(); // Inner Join For Activity
134            activeFilter.addEqualTo("active", true);
135            crit.addAndCriteria(activeFilter);
136    
137            Query query = QueryFactory.newQuery(Task.class, crit);
138            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
139            results.addAll(c);
140    
141            return results;
142        }
143       
144        @Override
145        public int getTaskCount(Long task) {
146            Criteria crit = new Criteria();
147                    crit.addEqualTo("task",task);
148                    Query query = QueryFactory.newQuery(Task.class, crit);
149                    return this.getPersistenceBrokerTemplate().getCount(query);
150        }
151    }