View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.timeblock.dao;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.LinkedList;
22  import java.util.List;
23  
24  import org.apache.log4j.Logger;
25  import org.apache.ojb.broker.query.Criteria;
26  import org.apache.ojb.broker.query.Query;
27  import org.apache.ojb.broker.query.QueryFactory;
28  import org.apache.ojb.broker.query.ReportQueryByCriteria;
29  import org.kuali.hr.time.assignment.Assignment;
30  import org.kuali.hr.time.timeblock.TimeBlock;
31  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
32  import org.kuali.rice.krad.service.KRADServiceLocator;
33  
34  public class TimeBlockDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TimeBlockDao {
35  
36      @SuppressWarnings("unused")
37      private static final Logger LOG = Logger.getLogger(TimeBlockDaoSpringOjbImpl.class);
38  
39      public void saveOrUpdate(TimeBlock timeBlock) {
40          KRADServiceLocator.getBusinessObjectService().save(timeBlock);
41      }
42  
43      public void saveOrUpdate(List<TimeBlock> timeBlockList) {
44          if (timeBlockList != null) {
45              for (TimeBlock timeBlock : timeBlockList) {
46                  this.getPersistenceBrokerTemplate().store(timeBlock);
47              }
48          }
49      }
50  
51      public TimeBlock getTimeBlock(String tkTimeBlockId) {
52          Criteria currentRecordCriteria = new Criteria();
53          currentRecordCriteria.addEqualTo("tkTimeBlockId", tkTimeBlockId);
54  
55          return (TimeBlock) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TimeBlock.class, currentRecordCriteria));
56      }
57  
58      @SuppressWarnings("unchecked")
59      public List<TimeBlock> getTimeBlocks(String documentId) {
60          Criteria currentRecordCriteria = new Criteria();
61          currentRecordCriteria.addEqualTo("documentId", documentId);
62          Query query = QueryFactory.newQuery(TimeBlock.class, currentRecordCriteria);
63          List<TimeBlock> timeBlocks = (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
64          return timeBlocks == null || timeBlocks.size() == 0 ? new LinkedList<TimeBlock>() : timeBlocks;
65      }
66  
67      @SuppressWarnings("unchecked")
68      public List<TimeBlock> getTimeBlocksForAssignment(Assignment assign) {
69          Criteria rootCriteria = new Criteria();
70          rootCriteria.addEqualTo("principalId", assign.getPrincipalId());
71          rootCriteria.addEqualTo("jobNumber", assign.getJobNumber());
72          rootCriteria.addEqualTo("task", assign.getTask());
73          rootCriteria.addEqualTo("workArea", assign.getWorkArea());
74          Query query = QueryFactory.newQuery(TimeBlock.class, rootCriteria);
75          List<TimeBlock> timeBlocks = (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
76          return timeBlocks == null || timeBlocks.isEmpty() ? new ArrayList<TimeBlock>() : timeBlocks;
77      }
78  
79      public void deleteTimeBlock(TimeBlock timeBlock) {
80          this.getPersistenceBrokerTemplate().delete(timeBlock);
81      }
82  
83  
84      public void deleteTimeBlocksAssociatedWithDocumentId(String documentId) {
85          Criteria crit = new Criteria();
86          crit.addEqualTo("documentId", documentId);
87          this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(TimeBlock.class, crit));
88  
89      }
90  
91      @SuppressWarnings("unchecked")
92      @Override
93      public List<TimeBlock> getTimeBlocksForClockLogEndId(String tkClockLogId) {
94          Criteria crit = new Criteria();
95          crit.addEqualTo("clockLogEndId", tkClockLogId);
96          return (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(TimeBlock.class, crit));
97      }
98  
99      @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 }