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.lm.workflow.dao;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.Date;
021    import java.util.List;
022    
023    import org.apache.log4j.Logger;
024    import org.apache.ojb.broker.query.Criteria;
025    import org.apache.ojb.broker.query.Query;
026    import org.apache.ojb.broker.query.QueryByCriteria;
027    import org.apache.ojb.broker.query.QueryFactory;
028    import org.apache.ojb.broker.query.ReportQueryByCriteria;
029    import org.kuali.hr.lm.workflow.LeaveCalendarDocumentHeader;
030    import org.kuali.hr.time.util.TkConstants;
031    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
032    
033    public class LeaveCalendarDocumentHeaderDaoImpl extends PlatformAwareDaoBaseOjb implements LeaveCalendarDocumentHeaderDao {
034        private static final Logger LOG = Logger.getLogger(LeaveCalendarDocumentHeaderDaoImpl.class);
035    
036        @Override
037        public LeaveCalendarDocumentHeader getLeaveCalendarDocumentHeader(String documentId) {
038            Criteria crit = new Criteria();
039            crit.addEqualTo("documentId", documentId);
040            return (LeaveCalendarDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(LeaveCalendarDocumentHeader.class, crit));
041        }
042    
043        @Override
044        public LeaveCalendarDocumentHeader getLeaveCalendarDocumentHeader(String principalId, Date beginDate, Date endDate) {
045            Criteria crit = new Criteria();
046            crit.addEqualTo("principalId", principalId);
047            crit.addEqualTo("beginDate", beginDate);
048            crit.addEqualTo("endDate", endDate);
049    
050            return (LeaveCalendarDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(LeaveCalendarDocumentHeader.class, crit));
051        }
052    
053        @Override
054        public void saveOrUpdate(LeaveCalendarDocumentHeader leaveCalendarDocumentHeader) {
055            this.getPersistenceBrokerTemplate().store(leaveCalendarDocumentHeader);
056        }
057        
058        /**
059         * Document header IDs are ordered, so an ID less than the current will
060         * always be previous to current.
061         */
062        public LeaveCalendarDocumentHeader getPreviousDocumentHeader(String principalId, Date beginDate) {
063            Criteria crit = new Criteria();
064            crit.addEqualTo("principalId", principalId);
065            // the pay begin date is the end date of the previous pay period
066            crit.addEqualTo("endDate", beginDate);
067            QueryByCriteria query = new QueryByCriteria(LeaveCalendarDocumentHeader.class, crit);
068            query.addOrderByDescending("documentId");
069            query.setStartAtIndex(0);
070            query.setEndAtIndex(1);
071    
072            return (LeaveCalendarDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
073        }
074    
075        @Override
076        public LeaveCalendarDocumentHeader getNextDocumentHeader(String principalId, Date endDate) {
077            Criteria crit = new Criteria();
078            crit.addEqualTo("principalId", principalId);
079            // the pay end date is the begin date of the next pay period
080            crit.addEqualTo("beginDate", endDate);
081            QueryByCriteria query = new QueryByCriteria(LeaveCalendarDocumentHeader.class, crit);
082            query.setStartAtIndex(0);
083            query.setEndAtIndex(1);
084    
085            return (LeaveCalendarDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
086        }
087        
088        @Override
089        public List<LeaveCalendarDocumentHeader> getDocumentHeaders(Date beginDate, Date endDate) {
090            Criteria crit = new Criteria();
091            List<LeaveCalendarDocumentHeader> lstDocumentHeaders = new ArrayList<LeaveCalendarDocumentHeader>();
092    
093            crit.addEqualTo("beginDate", beginDate);
094            crit.addEqualTo("endDate", endDate);
095            QueryByCriteria query = new QueryByCriteria(LeaveCalendarDocumentHeader.class, crit);
096            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
097            if (c != null) {
098                lstDocumentHeaders.addAll(c);
099            }
100            
101            return lstDocumentHeaders;
102        }
103        
104        @Override
105        public LeaveCalendarDocumentHeader getMaxEndDateApprovedLeaveCalendar(String principalId) {
106            Criteria root = new Criteria();
107            Criteria crit = new Criteria();
108            
109            crit.addEqualTo("principalId", principalId);
110            crit.addEqualTo("documentStatus", TkConstants.ROUTE_STATUS.FINAL);
111            ReportQueryByCriteria endDateSubQuery = QueryFactory.newReportQuery(LeaveCalendarDocumentHeader.class, crit);
112            endDateSubQuery.setAttributes(new String[]{"max(endDate)"});
113    
114            root.addEqualTo("principalId", principalId);
115            root.addEqualTo("documentStatus", TkConstants.ROUTE_STATUS.FINAL);
116            root.addEqualTo("endDate", endDateSubQuery);
117    
118            Query query = QueryFactory.newQuery(LeaveCalendarDocumentHeader.class, root);
119            return (LeaveCalendarDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
120        }
121        
122        @Override
123            public LeaveCalendarDocumentHeader getMinBeginDatePendingLeaveCalendar(String principalId) {
124            Criteria root = new Criteria();
125            Criteria crit = new Criteria();
126            List<String> pendingStatuses = new ArrayList<String>();
127            pendingStatuses.add(TkConstants.ROUTE_STATUS.ENROUTE);
128            pendingStatuses.add(TkConstants.ROUTE_STATUS.INITIATED);
129            pendingStatuses.add(TkConstants.ROUTE_STATUS.SAVED);        
130            
131            crit.addEqualTo("principalId", principalId);
132            crit.addIn("documentStatus", pendingStatuses);
133            ReportQueryByCriteria startDateSubQuery = QueryFactory.newReportQuery(LeaveCalendarDocumentHeader.class, crit);
134            startDateSubQuery.setAttributes(new String[]{"min(beginDate)"});
135    
136            root.addEqualTo("principalId", principalId);
137            root.addIn("documentStatus", pendingStatuses);
138            root.addEqualTo("beginDate", startDateSubQuery);
139    
140            Query query = QueryFactory.newQuery(LeaveCalendarDocumentHeader.class, root);
141            return (LeaveCalendarDocumentHeader) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
142            }
143        @Override
144        public List<LeaveCalendarDocumentHeader> getAllDocumentHeadersForPricipalId(String principalId) {
145                    Criteria crit = new Criteria();
146            List<LeaveCalendarDocumentHeader> lstDocumentHeaders = new ArrayList<LeaveCalendarDocumentHeader>();
147    
148            crit.addEqualTo("principalId", principalId);
149            QueryByCriteria query = new QueryByCriteria(LeaveCalendarDocumentHeader.class, crit);
150            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
151            if (c != null) {
152                lstDocumentHeaders.addAll(c);
153            }
154            return lstDocumentHeaders;
155        }
156        
157        @Override
158        public List<LeaveCalendarDocumentHeader> getSubmissionDelinquentDocumentHeaders(String principalId, Date beforeDate) {
159            Criteria crit = new Criteria();
160            List<LeaveCalendarDocumentHeader> lstDocumentHeaders = new ArrayList<LeaveCalendarDocumentHeader>();
161    
162            crit.addEqualTo("principalId", principalId);
163            crit.addLessThan("endDate", beforeDate);
164            crit.addNotEqualTo("documentStatus", TkConstants.ROUTE_STATUS.INITIATED);
165            crit.addNotEqualTo("documentStatus", TkConstants.ROUTE_STATUS.ENROUTE);
166            crit.addNotEqualTo("documentStatus", TkConstants.ROUTE_STATUS.FINAL);
167            QueryByCriteria query = new QueryByCriteria(LeaveCalendarDocumentHeader.class, crit);
168            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
169            if (c != null) {
170                lstDocumentHeaders.addAll(c);
171            }
172            return lstDocumentHeaders;
173        }
174        
175        @Override
176        public List<LeaveCalendarDocumentHeader> getApprovalDelinquentDocumentHeaders(String principalId) {
177            Criteria crit = new Criteria();
178            List<LeaveCalendarDocumentHeader> lstDocumentHeaders = new ArrayList<LeaveCalendarDocumentHeader>();
179    
180            crit.addEqualTo("principalId", principalId);
181            crit.addNotEqualTo("documentStatus", TkConstants.ROUTE_STATUS.INITIATED);
182            crit.addNotEqualTo("documentStatus", TkConstants.ROUTE_STATUS.FINAL);
183            QueryByCriteria query = new QueryByCriteria(LeaveCalendarDocumentHeader.class, crit);
184            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
185            if (c != null) {
186                lstDocumentHeaders.addAll(c);
187            }
188            return lstDocumentHeaders;
189        }
190    
191        public void deleteLeaveCalendarHeader(String documentId){
192            Criteria crit = new Criteria();
193            crit.addEqualTo("documentId", documentId);
194            this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(LeaveCalendarDocumentHeader.class, crit));
195        }
196        
197        @Override
198        public List<LeaveCalendarDocumentHeader> getAllDocumentHeadersInRangeForPricipalId(String principalId, Date startDate, Date endDate) {
199            Criteria root = new Criteria();
200            List<LeaveCalendarDocumentHeader> lstDocumentHeaders = new ArrayList<LeaveCalendarDocumentHeader>();
201    
202            Criteria beginRoot = new Criteria();
203            beginRoot.addEqualTo("principalId", principalId);
204            beginRoot.addLessOrEqualThan("beginDate", startDate);
205            beginRoot.addGreaterOrEqualThan("endDate", startDate);  
206            
207            Criteria endRoot = new Criteria();
208            endRoot.addEqualTo("principalId", principalId);
209            endRoot.addLessOrEqualThan("beginDate", endDate);
210            endRoot.addGreaterOrEqualThan("endDate", endDate); 
211            
212            root.addEqualTo("principalId", principalId);
213            root.addGreaterOrEqualThan("beginDate", startDate);
214            root.addLessOrEqualThan("beginDate", endDate);
215            root.addGreaterOrEqualThan("endDate", startDate);
216            root.addLessOrEqualThan("endDate", endDate);
217            root.addOrCriteria(beginRoot);
218            root.addOrCriteria(endRoot);
219            
220            QueryByCriteria query = new QueryByCriteria(LeaveCalendarDocumentHeader.class, root);
221            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
222            if (c != null) {
223                lstDocumentHeaders.addAll(c);
224            }
225            return lstDocumentHeaders;
226        }
227    }