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 com.google.common.collect.ImmutableList;
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.core.util.OjbSubQueryUtil;
029    import org.kuali.hr.time.task.Task;
030    import org.kuali.hr.time.util.TKUtils;
031    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
032    
033    public class TaskDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TaskDao {
034        private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
035                .add("task")
036                .build();
037    
038            @Override
039            public Task getTask(String tkTaskId) {
040                    Criteria crit = new Criteria();
041                    crit.addEqualTo("tkTaskId", tkTaskId);
042                    
043                    Query query = QueryFactory.newQuery(Task.class, crit);
044                    return (Task)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
045            }
046    
047        @Override
048        public Task getMaxTask() {
049            Criteria root = new Criteria();
050            Criteria crit = new Criteria();
051    
052            ReportQueryByCriteria taskNumberSubQuery = QueryFactory.newReportQuery(Task.class, crit);
053            taskNumberSubQuery.setAttributes(new String[]{"max(task)"});
054    
055            root.addEqualTo("task", taskNumberSubQuery);
056    
057            Query query = QueryFactory.newQuery(Task.class, root);
058            return (Task) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
059        }
060    
061        @Override
062        public Task getTask(Long task, Date asOfDate) {
063            Criteria root = new Criteria();
064    
065            root.addEqualTo("task", task);
066            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Task.class, asOfDate, EQUAL_TO_FIELDS, false));
067            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Task.class, EQUAL_TO_FIELDS, false));
068    
069            Criteria activeFilter = new Criteria(); // Inner Join For Activity
070            activeFilter.addEqualTo("active", true);
071            root.addAndCriteria(activeFilter);
072    
073            Task t = null;
074            Query query = QueryFactory.newQuery(Task.class, root);
075            Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
076            if (obj instanceof Task)
077                t = (Task) obj;
078    
079            return t;
080        }
081    
082        @Override
083        public void saveOrUpdate(Task task) {
084            this.getPersistenceBrokerTemplate().store(task);
085        }
086    
087        @Override
088        public void saveOrUpdate(List<Task> tasks) {
089            for (Task task : tasks)
090                this.getPersistenceBrokerTemplate().store(task);
091        }
092    
093        @SuppressWarnings("unchecked")
094            @Override
095        public List<Task> getTasks(Long task, String description, Long workArea, Date fromEffdt, Date toEffdt) {
096            Criteria root = new Criteria();
097    
098            List<Task> results = new ArrayList<Task>();
099    
100            if (task != null) {
101                    root.addLike("task", task);
102            }
103            
104            if (StringUtils.isNotBlank(description)) {
105                    root.addLike("description", description);
106            }
107            
108            if (workArea != null) {
109                    root.addEqualTo("workArea", workArea);
110            }
111            
112            Criteria effectiveDateFilter = new Criteria();
113            if (fromEffdt != null) {
114                effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
115            }
116            if (toEffdt != null) {
117                effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
118            }
119            if (fromEffdt == null && toEffdt == null) {
120                effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
121            }
122            root.addAndCriteria(effectiveDateFilter);
123    
124            Criteria activeFilter = new Criteria();
125            activeFilter.addEqualTo("active", true);
126            root.addAndCriteria(activeFilter);
127    
128            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(Task.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
129            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Task.class, EQUAL_TO_FIELDS, false));
130    
131            Query query = QueryFactory.newQuery(Task.class, root);
132            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
133    
134            return results;
135        }
136       
137        @Override
138        public int getTaskCount(Long task) {
139            Criteria crit = new Criteria();
140                    crit.addEqualTo("task",task);
141                    Query query = QueryFactory.newQuery(Task.class, crit);
142                    return this.getPersistenceBrokerTemplate().getCount(query);
143        }
144    }