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.timeblock.dao;
017    
018    import java.sql.Date;
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.LinkedList;
022    import java.util.List;
023    
024    import org.apache.log4j.Logger;
025    import org.apache.ojb.broker.query.Criteria;
026    import org.apache.ojb.broker.query.Query;
027    import org.apache.ojb.broker.query.QueryFactory;
028    import org.apache.ojb.broker.query.ReportQueryByCriteria;
029    import org.kuali.hr.time.assignment.Assignment;
030    import org.kuali.hr.time.timeblock.TimeBlock;
031    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
032    
033    public class TimeBlockDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TimeBlockDao {
034    
035        @SuppressWarnings("unused")
036        private static final Logger LOG = Logger.getLogger(TimeBlockDaoSpringOjbImpl.class);
037    
038        public void saveOrUpdate(TimeBlock timeBlock) {
039            this.getPersistenceBrokerTemplate().store(timeBlock);
040        }
041    
042        public void saveOrUpdate(List<TimeBlock> timeBlockList) {
043            if (timeBlockList != null) {
044                for (TimeBlock timeBlock : timeBlockList) {
045                    this.getPersistenceBrokerTemplate().store(timeBlock);
046                }
047            }
048        }
049    
050        public TimeBlock getTimeBlock(String tkTimeBlockId) {
051            Criteria currentRecordCriteria = new Criteria();
052            currentRecordCriteria.addEqualTo("tkTimeBlockId", tkTimeBlockId);
053    
054            return (TimeBlock) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TimeBlock.class, currentRecordCriteria));
055        }
056    
057        @SuppressWarnings("unchecked")
058        public List<TimeBlock> getTimeBlocks(String documentId) {
059            Criteria currentRecordCriteria = new Criteria();
060            currentRecordCriteria.addEqualTo("documentId", documentId);
061            Query query = QueryFactory.newQuery(TimeBlock.class, currentRecordCriteria);
062            List<TimeBlock> timeBlocks = (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
063            return timeBlocks == null || timeBlocks.size() == 0 ? new LinkedList<TimeBlock>() : timeBlocks;
064        }
065    
066        @SuppressWarnings("unchecked")
067        public List<TimeBlock> getTimeBlocksForAssignment(Assignment assign) {
068            Criteria rootCriteria = new Criteria();
069            rootCriteria.addEqualTo("principalId", assign.getPrincipalId());
070            rootCriteria.addEqualTo("jobNumber", assign.getJobNumber());
071            rootCriteria.addEqualTo("task", assign.getTask());
072            rootCriteria.addEqualTo("workArea", assign.getWorkArea());
073            Query query = QueryFactory.newQuery(TimeBlock.class, rootCriteria);
074            List<TimeBlock> timeBlocks = (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
075            return timeBlocks == null || timeBlocks.isEmpty() ? new ArrayList<TimeBlock>() : timeBlocks;
076        }
077    
078        public void deleteTimeBlock(TimeBlock timeBlock) {
079            this.getPersistenceBrokerTemplate().delete(timeBlock);
080        }
081    
082    
083        public void deleteTimeBlocksAssociatedWithDocumentId(String documentId) {
084            Criteria crit = new Criteria();
085            crit.addEqualTo("documentId", documentId);
086            this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(TimeBlock.class, crit));
087    
088        }
089    
090        @SuppressWarnings("unchecked")
091        @Override
092        public List<TimeBlock> getTimeBlocksForClockLogEndId(String tkClockLogId) {
093            Criteria crit = new Criteria();
094            crit.addEqualTo("clockLogEndId", tkClockLogId);
095            return (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(TimeBlock.class, crit));
096        }
097    
098        @SuppressWarnings("unchecked")
099        @Override
100        public List<TimeBlock> getTimeBlocksForClockLogBeginId(String tkClockLogId) {
101            Criteria crit = new Criteria();
102            crit.addEqualTo("clockLogBeginId", tkClockLogId);
103            return (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(TimeBlock.class, crit));
104        }
105    
106        public List<TimeBlock> getTimeBlocks() { //KPME937
107            List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>();
108            Criteria crit = new Criteria();
109    
110            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(TimeBlock.class, crit));
111    
112            if (c != null) {
113                timeBlocks.addAll(c);
114            }
115    
116            return timeBlocks;
117        }
118    
119        @Override
120        public List<TimeBlock> getLatestEndTimestamp() { //KPME937
121            List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>();
122            Criteria root = new Criteria();
123            Criteria crit = new Criteria();
124    
125            ReportQueryByCriteria endTimestampSubQuery = QueryFactory.newReportQuery(TimeBlock.class, crit);
126            endTimestampSubQuery.setAttributes(new String[]{"max(endTimestamp)"});
127    
128            root.addEqualTo("endTimestamp", endTimestampSubQuery);
129    
130            Query query = QueryFactory.newQuery(TimeBlock.class, root);
131            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
132    
133            if (c != null) {
134                timeBlocks.addAll(c);
135            }
136    
137            return timeBlocks;
138        }
139    
140        @Override
141        public List<TimeBlock> getOvernightTimeBlocks(String clockLogEndId) {
142            List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>();
143            Criteria root = new Criteria();
144    
145            root.addEqualTo("clockLogEndId", clockLogEndId);
146    
147            Query query = QueryFactory.newQuery(TimeBlock.class, root);
148            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
149    
150            if (c != null) {
151                timeBlocks.addAll(c);
152            }
153    
154            return timeBlocks;
155        }
156        
157        @SuppressWarnings("unchecked")
158            @Override
159        public List<TimeBlock> getTimeBlocksWithEarnCode(String earnCode, Date effDate) {
160             Criteria root = new Criteria();
161             root.addEqualTo("earnCode", earnCode);
162             root.addGreaterOrEqualThan("beginTimestamp", effDate);
163             return (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(TimeBlock.class, root));
164        }
165    
166    }