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.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        @Override
107        public List<TimeBlock> getLatestEndTimestampForEarnCode(String earnCode) { //KPME937
108            List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>();
109            Criteria root = new Criteria();
110            Criteria crit = new Criteria();
111            crit.addEqualTo("earnCode", earnCode);
112            ReportQueryByCriteria endTimestampSubQuery = QueryFactory.newReportQuery(TimeBlock.class, crit);
113            endTimestampSubQuery.setAttributes(new String[]{"max(endTimestamp)"});
114    
115            root.addEqualTo("endTimestamp", endTimestampSubQuery);
116            root.addEqualTo("earnCode", earnCode);
117            Query query = QueryFactory.newQuery(TimeBlock.class, root);
118            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
119    
120            if (c != null) {
121                timeBlocks.addAll(c);
122            }
123    
124            return timeBlocks;
125        }
126    
127        @Override
128        public List<TimeBlock> getOvernightTimeBlocks(String clockLogEndId) {
129            List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>();
130            Criteria root = new Criteria();
131    
132            root.addEqualTo("clockLogEndId", clockLogEndId);
133    
134            Query query = QueryFactory.newQuery(TimeBlock.class, root);
135            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
136    
137            if (c != null) {
138                timeBlocks.addAll(c);
139            }
140    
141            return timeBlocks;
142        }
143        
144        @SuppressWarnings("unchecked")
145            @Override
146        public List<TimeBlock> getTimeBlocksWithEarnCode(String earnCode, Date effDate) {
147             Criteria root = new Criteria();
148             root.addEqualTo("earnCode", earnCode);
149             root.addGreaterOrEqualThan("beginTimestamp", effDate);
150             return (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(TimeBlock.class, root));
151        }
152    
153    }