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