001/**
002 * Copyright 2004-2014 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 */
016package org.kuali.kpme.tklm.leave.block.dao;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.Collections;
021import java.util.List;
022
023import org.apache.commons.lang.StringUtils;
024import org.apache.log4j.Logger;
025import org.apache.ojb.broker.query.Criteria;
026import org.apache.ojb.broker.query.Query;
027import org.apache.ojb.broker.query.QueryFactory;
028import org.joda.time.LocalDate;
029import org.kuali.kpme.core.util.HrConstants;
030import org.kuali.kpme.tklm.common.LMConstants;
031import org.kuali.kpme.tklm.leave.block.LeaveBlock;
032import org.kuali.kpme.tklm.leave.block.LeaveBlockHistory;
033import org.kuali.kpme.tklm.time.service.TkServiceLocator;
034import org.kuali.kpme.tklm.time.workflow.TimesheetDocumentHeader;
035import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
036
037public class LeaveBlockHistoryDaoOjbImpl extends PlatformAwareDaoBaseOjb implements LeaveBlockHistoryDao {
038
039        @SuppressWarnings("unused")
040        private static final Logger LOG = Logger
041                        .getLogger(LeaveBlockHistoryDaoOjbImpl.class);
042
043        @Override
044        public void saveOrUpdate(LeaveBlockHistory leaveBlockHistory) {
045                this.getPersistenceBrokerTemplate().store(leaveBlockHistory);
046        }
047
048        @Override
049        public void saveOrUpdate(List<LeaveBlockHistory> leaveBlockHistoryList) {
050                if (leaveBlockHistoryList != null) {
051                        for (LeaveBlockHistory leaveBlockHistory : leaveBlockHistoryList) {
052                                this.getPersistenceBrokerTemplate().store(leaveBlockHistory);
053                        }
054
055                }
056        }
057
058        @Override
059        public List<LeaveBlockHistory> getLeaveBlockHistoryByLmLeaveBlockId(
060                        String lmLeaveBlockId) {
061                Criteria recordCriteria = new Criteria();
062                recordCriteria.addEqualTo("lmLeaveBlockId", lmLeaveBlockId);
063                Query query = QueryFactory.newQuery(LeaveBlockHistory.class,
064                                recordCriteria);
065                return (List<LeaveBlockHistory>) this.getPersistenceBrokerTemplate()
066                                .getCollectionByQuery(query);
067        }
068
069        @Override
070        public List<LeaveBlockHistory> getLeaveBlockHistories(String principalId,
071                        List<String> requestStatus) {
072                Criteria recordCriteria = new Criteria();
073                recordCriteria.addEqualTo("principalId", principalId);
074                if (requestStatus != null) {
075                        recordCriteria.addIn("requestStatus", requestStatus);
076                }
077                Query query = QueryFactory.newQuery(LeaveBlockHistory.class,
078                                recordCriteria);
079                return (List<LeaveBlockHistory>) this.getPersistenceBrokerTemplate()
080                                .getCollectionByQuery(query);
081        }
082        
083        @Override
084        public List<LeaveBlockHistory> getLeaveBlockHistories(String principalId,String requestStatus, String action, LocalDate currentDate) {
085                Criteria recordCriteria = new Criteria();
086                recordCriteria.addEqualTo("principalId", principalId);
087                if (requestStatus != null) {
088                        recordCriteria.addEqualTo("requestStatus", requestStatus);
089                }
090                if(currentDate != null) {
091                        recordCriteria.addGreaterThan("leaveDate", currentDate.toDate());
092                }
093                if(action != null) {
094                        recordCriteria.addEqualTo("action", action);
095                }
096                Query query = QueryFactory.newQuery(LeaveBlockHistory.class,
097                                recordCriteria);
098                return (List<LeaveBlockHistory>) this.getPersistenceBrokerTemplate()
099                                .getCollectionByQuery(query);
100        }
101
102        @Override
103        public List<LeaveBlockHistory> getLeaveBlockHistoriesForLeaveDisplay(String principalId,
104                        LocalDate beginDate, LocalDate endDate, boolean considerModifiedUser) {
105                
106                List<LeaveBlockHistory> leaveBlockHistories = new ArrayList<LeaveBlockHistory>();
107                
108                Criteria root = new Criteria();
109                root.addEqualTo("principalId", principalId);
110                root.addGreaterOrEqualThan("leaveDate", beginDate.toDate());
111                root.addLessOrEqualThan("leaveDate", endDate.toDate());
112                root.addEqualTo("action",HrConstants.ACTION.MODIFIED);
113                if(considerModifiedUser) {
114                        root.addNotEqualTo("principalIdModified", principalId);
115                } 
116                
117                Criteria root1 = new Criteria();
118                root1.addEqualTo("principalId", principalId);
119                root1.addGreaterOrEqualThan("leaveDate", beginDate.toDate());
120                root1.addLessOrEqualThan("leaveDate", endDate.toDate());
121                root1.addEqualTo("action",HrConstants.ACTION.DELETE);
122                if(considerModifiedUser) {
123                        root1.addNotEqualTo("principalIdDeleted", principalId);
124                } 
125                
126                root.addOrCriteria(root1);
127                
128                Query query = QueryFactory.newQuery(LeaveBlockHistory.class, root);
129                Collection c = this.getPersistenceBrokerTemplate()
130                                .getCollectionByQuery(query);
131
132                if (c != null) {
133                        leaveBlockHistories.addAll(c);
134                }
135                return leaveBlockHistories;
136        }
137
138        @Override
139        public List<LeaveBlockHistory> getLeaveBlockHistoriesForLookup(String documentId,
140                        String principalId, String userPrincipalId, LocalDate fromDate,
141                        LocalDate toDate) {
142                List<LeaveBlockHistory> leaveBlocks = new ArrayList<LeaveBlockHistory>();
143        Criteria criteria = new Criteria();
144
145        //document id....
146        //get document, and cal entry, fill in query data
147        if (StringUtils.isNotBlank(documentId)) {
148            TimesheetDocumentHeader tsdh = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(documentId);
149            if (tsdh == null) {
150                return Collections.emptyList();
151            }
152            criteria.addGreaterOrEqualThan("beginTimestamp", tsdh.getBeginDate());
153            criteria.addLessOrEqualThan("endTimestamp",tsdh.getEndDate());
154            criteria.addEqualTo("principalId", tsdh.getPrincipalId());
155        }
156
157
158        if(fromDate != null) {
159                criteria.addGreaterOrEqualThan("beginTimestamp", fromDate.toDate());
160        }
161        if(toDate != null) {
162                criteria.addLessOrEqualThan("endTimestamp",toDate.toDate());
163        }
164        if(StringUtils.isNotBlank(principalId)) {
165                criteria.addEqualTo("principalId", principalId);
166        }
167        if(StringUtils.isNotBlank(userPrincipalId)) {
168                criteria.addEqualTo("principalIdModified", userPrincipalId);
169        }
170        criteria.addEqualTo("leaveBlockType",LMConstants.LEAVE_BLOCK_TYPE.TIME_CALENDAR);
171        Query query = QueryFactory.newQuery(LeaveBlockHistory.class, criteria);
172        Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
173        if (c != null) {
174                leaveBlocks.addAll(c);
175        }
176        return leaveBlocks;
177        }
178        @Override
179        public List<LeaveBlockHistory> getLeaveBlockHistoriesForLookup(String documentId,
180                        String principalId, String userPrincipalId, LocalDate fromDate,
181                        LocalDate toDate, String leaveBlockType) {
182                List<LeaveBlockHistory> leaveBlocks = new ArrayList<LeaveBlockHistory>();
183        Criteria criteria = new Criteria();
184
185        //document id....
186        //get document, and cal entry, fill in query data
187        if (StringUtils.isNotBlank(documentId)) {
188            TimesheetDocumentHeader tsdh = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(documentId);
189            if (tsdh == null) {
190                return Collections.emptyList();
191            }
192            criteria.addGreaterOrEqualThan("beginTimestamp", tsdh.getBeginDate());
193            criteria.addLessOrEqualThan("endTimestamp",tsdh.getEndDate());
194            criteria.addEqualTo("principalId", tsdh.getPrincipalId());
195        }
196
197
198        if(fromDate != null) {
199                criteria.addGreaterOrEqualThan("beginTimestamp", fromDate.toDate());
200        }
201        if(toDate != null) {
202                criteria.addLessOrEqualThan("endTimestamp",toDate.toDate());
203        }
204        if(StringUtils.isNotBlank(principalId)) {
205                criteria.addEqualTo("principalId", principalId);
206        }
207        if(StringUtils.isNotBlank(userPrincipalId)) {
208                criteria.addEqualTo("principalIdModified", userPrincipalId);
209        }
210        criteria.addEqualTo("leaveBlockType",leaveBlockType);
211        Query query = QueryFactory.newQuery(LeaveBlockHistory.class, criteria);
212        Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
213        if (c != null) {
214                leaveBlocks.addAll(c);
215        }
216        return leaveBlocks;
217        }
218}