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.kpme.tklm.time.timeblock.dao;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.LinkedList;
21  import java.util.List;
22  
23  import org.apache.commons.lang.StringUtils;
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.joda.time.DateTime;
30  import org.joda.time.LocalDate;
31  import org.kuali.kpme.core.assignment.Assignment;
32  import org.kuali.kpme.tklm.time.timeblock.TimeBlock;
33  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
34  
35  public class TimeBlockDaoOjbImpl extends PlatformAwareDaoBaseOjb implements TimeBlockDao {
36  
37      @SuppressWarnings("unused")
38      private static final Logger LOG = Logger.getLogger(TimeBlockDaoOjbImpl.class);
39  
40      public void saveOrUpdate(TimeBlock timeBlock) {
41          this.getPersistenceBrokerTemplate().store(timeBlock);
42      }
43  
44      public void saveOrUpdate(List<TimeBlock> timeBlockList) {
45          if (timeBlockList != null) {
46              for (TimeBlock timeBlock : timeBlockList) {
47                  this.getPersistenceBrokerTemplate().store(timeBlock);
48              }
49          }
50      }
51  
52      public TimeBlock getTimeBlock(String tkTimeBlockId) {
53          Criteria currentRecordCriteria = new Criteria();
54          currentRecordCriteria.addEqualTo("tkTimeBlockId", tkTimeBlockId);
55  
56          return (TimeBlock) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TimeBlock.class, currentRecordCriteria));
57      }
58  
59      @SuppressWarnings("unchecked")
60      public List<TimeBlock> getTimeBlocks(String documentId) {
61          Criteria currentRecordCriteria = new Criteria();
62          currentRecordCriteria.addEqualTo("documentId", documentId);
63          Query query = QueryFactory.newQuery(TimeBlock.class, currentRecordCriteria);
64          List<TimeBlock> timeBlocks = (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
65          return timeBlocks == null || timeBlocks.size() == 0 ? new LinkedList<TimeBlock>() : timeBlocks;
66      }
67  
68      @SuppressWarnings("unchecked")
69      public List<TimeBlock> getTimeBlocksForAssignment(Assignment assign) {
70          Criteria rootCriteria = new Criteria();
71          rootCriteria.addEqualTo("principalId", assign.getPrincipalId());
72          rootCriteria.addEqualTo("jobNumber", assign.getJobNumber());
73          rootCriteria.addEqualTo("task", assign.getTask());
74          rootCriteria.addEqualTo("workArea", assign.getWorkArea());
75          Query query = QueryFactory.newQuery(TimeBlock.class, rootCriteria);
76          List<TimeBlock> timeBlocks = (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
77          return timeBlocks == null || timeBlocks.isEmpty() ? new ArrayList<TimeBlock>() : timeBlocks;
78      }
79  
80      public void deleteTimeBlock(TimeBlock timeBlock) {
81          this.getPersistenceBrokerTemplate().delete(timeBlock);
82      }
83  
84  
85      public void deleteTimeBlocksAssociatedWithDocumentId(String documentId) {
86          Criteria crit = new Criteria();
87          crit.addEqualTo("documentId", documentId);
88          this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(TimeBlock.class, crit));
89  
90      }
91  
92      @SuppressWarnings("unchecked")
93      @Override
94      public List<TimeBlock> getTimeBlocksForClockLogEndId(String tkClockLogId) {
95          Criteria crit = new Criteria();
96          crit.addEqualTo("clockLogEndId", tkClockLogId);
97          return (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(TimeBlock.class, crit));
98      }
99  
100     @SuppressWarnings("unchecked")
101     @Override
102     public List<TimeBlock> getTimeBlocksForClockLogBeginId(String tkClockLogId) {
103         Criteria crit = new Criteria();
104         crit.addEqualTo("clockLogBeginId", tkClockLogId);
105         return (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(TimeBlock.class, crit));
106     }
107 
108     @Override
109     public List<TimeBlock> getLatestEndTimestampForEarnCode(String earnCode) { //KPME937
110         List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>();
111         Criteria root = new Criteria();
112         Criteria crit = new Criteria();
113         crit.addEqualTo("earnCode", earnCode);
114         ReportQueryByCriteria endTimestampSubQuery = QueryFactory.newReportQuery(TimeBlock.class, crit);
115         endTimestampSubQuery.setAttributes(new String[]{"max(endTimestamp)"});
116 
117         root.addEqualTo("endTimestamp", endTimestampSubQuery);
118         root.addEqualTo("earnCode", earnCode);
119         Query query = QueryFactory.newQuery(TimeBlock.class, root);
120         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
121 
122         if (c != null) {
123             timeBlocks.addAll(c);
124         }
125 
126         return timeBlocks;
127     }
128 
129     @Override
130     public List<TimeBlock> getOvernightTimeBlocks(String clockLogEndId) {
131         List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>();
132         Criteria root = new Criteria();
133 
134         root.addEqualTo("clockLogEndId", clockLogEndId);
135 
136         Query query = QueryFactory.newQuery(TimeBlock.class, root);
137         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
138 
139         if (c != null) {
140             timeBlocks.addAll(c);
141         }
142 
143         return timeBlocks;
144     }
145     
146     @SuppressWarnings("unchecked")
147 	@Override
148     public List<TimeBlock> getTimeBlocksWithEarnCode(String earnCode, DateTime effDate) {
149     	 Criteria root = new Criteria();
150          root.addEqualTo("earnCode", earnCode);
151          root.addGreaterOrEqualThan("beginTimestamp", effDate.toDate());
152          return (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(TimeBlock.class, root));
153     }
154 
155 	@Override
156 	public List<TimeBlock> getTimeBlocksForLookup(String documentId,
157 			String principalId, String userPrincipalId, LocalDate fromDate,
158 			LocalDate toDate) {
159         Criteria criteria = new Criteria();
160         if(StringUtils.isNotBlank(documentId)) {
161         	criteria.addEqualTo("documentId", documentId);
162         }
163         if(fromDate != null) {
164         	criteria.addGreaterOrEqualThan("beginTimestamp", fromDate.toDate());
165         }
166         if(toDate != null) {
167         	criteria.addLessOrEqualThan("endTimestamp",toDate.toDate());
168         }
169         if(StringUtils.isNotBlank(principalId)) {
170         	criteria.addEqualTo("principalId", principalId);
171         }
172         if(StringUtils.isNotBlank(userPrincipalId)) {
173         	criteria.addEqualTo("userPrincipalId", userPrincipalId);
174         }
175         Query query = QueryFactory.newQuery(TimeBlock.class, criteria);
176         List<TimeBlock> timeBlocks = (List<TimeBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
177         return timeBlocks == null || timeBlocks.size() == 0 ? new LinkedList<TimeBlock>() : timeBlocks;
178 	}
179 
180 }