001    /**
002     * Copyright 2004-2013 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.List;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.apache.ojb.broker.query.Criteria;
024    import org.apache.ojb.broker.query.Query;
025    import org.apache.ojb.broker.query.QueryFactory;
026    import org.apache.ojb.broker.query.ReportQueryByCriteria;
027    import org.kuali.hr.time.task.Task;
028    import org.kuali.hr.time.util.TKUtils;
029    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
030    
031    public class TaskDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TaskDao {
032            
033            @Override
034            public Task getTask(String tkTaskId) {
035                    Criteria crit = new Criteria();
036                    crit.addEqualTo("tkTaskId", tkTaskId);
037                    
038                    Query query = QueryFactory.newQuery(Task.class, crit);
039                    return (Task)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
040            }
041    
042        @Override
043        public Task getMaxTask() {
044            Criteria root = new Criteria();
045            Criteria crit = new Criteria();
046    
047            ReportQueryByCriteria taskNumberSubQuery = QueryFactory.newReportQuery(Task.class, crit);
048            taskNumberSubQuery.setAttributes(new String[]{"max(task)"});
049    
050            root.addEqualTo("task", taskNumberSubQuery);
051    
052            Query query = QueryFactory.newQuery(Task.class, root);
053            return (Task) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
054        }
055    
056        @Override
057        public Task getTask(Long task, Date asOfDate) {
058            Criteria root = new Criteria();
059            Criteria effdt = new Criteria();
060            Criteria timestamp = new Criteria();
061    
062            effdt.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
063            effdt.addLessOrEqualThan("effectiveDate", asOfDate);
064            ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Task.class, effdt);
065            effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
066    
067            timestamp.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
068            timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
069            ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Task.class, timestamp);
070            timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
071    
072            root.addEqualTo("task", task);
073            root.addEqualTo("effectiveDate", effdtSubQuery);
074            root.addEqualTo("timestamp", timestampSubQuery);
075    
076            Criteria activeFilter = new Criteria(); // Inner Join For Activity
077            activeFilter.addEqualTo("active", true);
078            root.addAndCriteria(activeFilter);
079    
080            Task t = null;
081            Query query = QueryFactory.newQuery(Task.class, root);
082            Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
083            if (obj instanceof Task)
084                t = (Task) obj;
085    
086            return t;
087        }
088    
089        @Override
090        public void saveOrUpdate(Task task) {
091            this.getPersistenceBrokerTemplate().store(task);
092        }
093    
094        @Override
095        public void saveOrUpdate(List<Task> tasks) {
096            for (Task task : tasks)
097                this.getPersistenceBrokerTemplate().store(task);
098        }
099    
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    }