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.principal.dao;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.Collections;
021    import java.util.Date;
022    import java.util.HashSet;
023    import java.util.Iterator;
024    import java.util.List;
025    import java.util.Set;
026    
027    import com.google.common.collect.ImmutableList;
028    import org.apache.commons.collections.CollectionUtils;
029    import org.apache.commons.lang.StringUtils;
030    import org.apache.ojb.broker.query.Criteria;
031    import org.apache.ojb.broker.query.Query;
032    import org.apache.ojb.broker.query.QueryFactory;
033    import org.apache.ojb.broker.query.ReportQueryByCriteria;
034    import org.kuali.hr.core.util.OjbSubQueryUtil;
035    import org.kuali.hr.time.principal.PrincipalHRAttributes;
036    import org.kuali.hr.time.util.TKUtils;
037    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
038    
039    public class PrincipalHRAttributesDaoImpl extends PlatformAwareDaoBaseOjb implements PrincipalHRAttributesDao {
040    
041            @Override
042            public PrincipalHRAttributes getPrincipalCalendar(String principalId,
043                            java.util.Date asOfDate) {
044                    PrincipalHRAttributes pc = null;
045    
046                    Criteria root = new Criteria();
047    
048            ImmutableList<String> fields = new ImmutableList.Builder<String>()
049                    .add("principalId")
050                    .build();
051    
052                    root.addEqualTo("principalId", principalId);
053            java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
054            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false));
055            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
056    
057                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
058                    activeFilter.addEqualTo("active", true);
059                    root.addAndCriteria(activeFilter);
060                    
061                    Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
062                    Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
063    
064                    if (obj != null) {
065                            pc = (PrincipalHRAttributes) obj;
066                    }
067    
068                    return pc;
069            }
070    
071            @Override
072            public void saveOrUpdate(PrincipalHRAttributes principalCalendar) {
073                    this.getPersistenceBrokerTemplate().store(principalCalendar);
074                    
075            }
076    
077            @Override
078            public void saveOrUpdate(List<PrincipalHRAttributes> lstPrincipalCalendar) {
079                    if(lstPrincipalCalendar != null){
080                            for(PrincipalHRAttributes principalCal : lstPrincipalCalendar){
081                                    this.getPersistenceBrokerTemplate().store(principalCal);
082                            }
083                    }
084                    
085            }
086    
087        @SuppressWarnings({"rawtypes", "unchecked"})
088        public List<PrincipalHRAttributes> getActiveEmployeesForPayCalendar(String payCalendarName, java.util.Date asOfDate) {
089            List<PrincipalHRAttributes> principalHRAttributes = new ArrayList<PrincipalHRAttributes>();
090            Criteria root = new Criteria();
091            
092            root.addEqualTo("payCalendar", payCalendarName);
093            ImmutableList<String> fields = new ImmutableList.Builder<String>()
094                    .add("payCalendar")
095                    .add("principalId")
096                    .build();
097    
098            Criteria activeFilter = new Criteria();
099            activeFilter.addEqualTo("active", true);
100            root.addAndCriteria(activeFilter);
101            java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
102            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false));
103            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
104    
105            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
106            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
107    
108            if (c != null) {
109                    principalHRAttributes.addAll(c);
110            }
111    
112            return principalHRAttributes;
113        }
114        
115        @SuppressWarnings({"rawtypes", "unchecked"})
116        public List<PrincipalHRAttributes> getActiveEmployeesForLeaveCalendar(String leaveCalendarName, Date asOfDate) {
117            List<PrincipalHRAttributes> principalHRAttributes = new ArrayList<PrincipalHRAttributes>();
118            Criteria root = new Criteria();
119    
120    
121            root.addEqualTo("leaveCalendar", leaveCalendarName);
122    
123            ImmutableList<String> fields = new ImmutableList.Builder<String>()
124                    .add("leaveCalendar")
125                    .add("principalId")
126                    .build();
127            java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
128            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false));
129            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
130    
131            Criteria activeFilter = new Criteria();
132            activeFilter.addEqualTo("active", true);
133            root.addAndCriteria(activeFilter);
134    
135            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
136            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
137    
138            if (c != null) {
139                    principalHRAttributes.addAll(c);
140            }
141    
142            return principalHRAttributes;
143        }
144        
145        public List<String> getActiveEmployeesIdForLeaveCalendarAndIdList(String leaveCalendarName, List<String> pidList, Date asOfDate) {
146            List<PrincipalHRAttributes> principalHRAttributes = new ArrayList<PrincipalHRAttributes>();
147            Criteria root = new Criteria();
148            
149            root.addEqualTo("leaveCalendar", leaveCalendarName);
150            root.addIn("principalId", pidList);
151    
152            ImmutableList<String> fields = new ImmutableList.Builder<String>()
153                    .add("leaveCalendar")
154                    .add("principalId")
155                    .build();
156            java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
157            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class,effDate, fields, false));
158            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
159    
160            Criteria activeFilter = new Criteria();
161            activeFilter.addEqualTo("active", true);
162            root.addAndCriteria(activeFilter);
163    
164            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
165            Collection c = getPersistenceBrokerTemplate().getCollectionByQuery(query);
166            if (c != null) {
167                    principalHRAttributes.addAll(c);
168            }
169            Set<String> pids = new HashSet<String>();
170            for(PrincipalHRAttributes phra : principalHRAttributes) {
171                    if(phra != null) {
172                    pids.add(phra.getPrincipalId());
173                    }
174            }
175            List<String> ids = new ArrayList<String>();
176            ids.addAll(pids);
177            
178            return ids;
179        }
180        
181        public List<String> getActiveEmployeesIdForTimeCalendarAndIdList(String timeCalendarName, List<String> pidList, Date asOfDate) {
182            List<PrincipalHRAttributes> principalHRAttributes = new ArrayList<PrincipalHRAttributes>();
183            Criteria root = new Criteria();
184            
185            root.addEqualTo("payCalendar", timeCalendarName);
186            root.addIn("principalId", pidList);
187    
188            ImmutableList<String> fields = new ImmutableList.Builder<String>()
189                    .add("payCalendar")
190                    .add("principalId")
191                    .build();
192            java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
193            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false));
194            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
195    
196            Criteria activeFilter = new Criteria();
197            activeFilter.addEqualTo("active", true);
198            root.addAndCriteria(activeFilter);
199    
200            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
201            Collection c = getPersistenceBrokerTemplate().getCollectionByQuery(query);
202            if (c != null) {
203                    principalHRAttributes.addAll(c);
204            }
205            Set<String> pids = new HashSet<String>();
206            for(PrincipalHRAttributes phra : principalHRAttributes) {
207                    if(phra != null) {
208                    pids.add(phra.getPrincipalId());
209                    }
210            }
211            List<String> ids = new ArrayList<String>();
212            ids.addAll(pids);
213            
214            return ids;
215        }
216            
217        // KPME-1250 Kagata
218        @SuppressWarnings({"rawtypes", "unchecked"})
219        public List<PrincipalHRAttributes> getActiveEmployeesForLeavePlan(String leavePlan, java.util.Date asOfDate) {
220    
221            List<PrincipalHRAttributes> principals = new ArrayList<PrincipalHRAttributes>();
222            Criteria root = new Criteria();
223    
224            ImmutableList<String> fields = new ImmutableList.Builder<String>()
225                    .add("leavePlan")
226                    .add("principalId")
227                    .build();
228            java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
229            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false));
230            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
231    
232            root.addEqualTo("leavePlan", leavePlan);
233            root.addEqualTo("active", true);
234    
235            Criteria activeFilter = new Criteria(); // Inner Join For Activity
236            activeFilter.addEqualTo("active", true);
237            root.addAndCriteria(activeFilter);
238    
239            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
240            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
241    
242            if (c != null) {
243                    principals.addAll(c);
244            }
245    
246            return principals;
247        }
248    
249        @Override
250        public List<String> getUniqueLeavePayGroupsForPrincipalIds(List<String> principalIds) {
251            if (CollectionUtils.isEmpty(principalIds)) {
252                return Collections.emptyList();
253            }
254            List<String> leaveCalendars = new ArrayList<String>();
255            Criteria crit = new Criteria();
256            crit.addEqualTo("active", true);
257            crit.addIn("principalId", principalIds);
258            ReportQueryByCriteria q = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit, true);
259            q.setDistinct(true);
260            q.setAttributes(new String[] {"leaveCalendar"});
261            Iterator iter = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
262            while (iter.hasNext()) {
263                Object[] values = (Object[]) iter.next();
264                String leaveCalendar = (String)values[0];
265                if (StringUtils.isNotBlank(leaveCalendar)) {
266                    leaveCalendars.add(leaveCalendar);
267                }
268            }
269            return leaveCalendars;
270        }
271        
272        @SuppressWarnings("rawtypes")
273            @Override
274        public List<String> getUniqueTimePayGroups() {
275            List<String> payCalendars = new ArrayList<String>();
276            Criteria crit = new Criteria();
277            crit.addEqualTo("active", true);
278            ReportQueryByCriteria q = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit, true);
279            q.setDistinct(true);
280            q.setAttributes(new String[] {"pay_calendar"});
281            Iterator iter = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
282            while (iter.hasNext()) {
283                Object[] values = (Object[]) iter.next();
284                String leaveCalendar = (String)values[0];
285                if (StringUtils.isNotBlank(leaveCalendar)) {
286                    payCalendars.add(leaveCalendar);
287                }
288            }
289            return payCalendars;
290        }
291    
292    //    @Override
293    //      public PrincipalHRAttributes getPrincipalHRAttributes(String principalId) {
294    //              Criteria crit = new Criteria();
295    //              crit.addEqualTo("principalId", principalId);
296    //              Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit);
297    //              return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query);              
298    //      }
299        
300        @Override
301        public PrincipalHRAttributes getInactivePrincipalHRAttributes(String principalId, java.util.Date asOfDate) {
302            PrincipalHRAttributes pc = null;
303    
304                    Criteria root = new Criteria();
305                    Criteria effdt = new Criteria();
306    
307            effdt.addGreaterOrEqualThan("effectiveDate", asOfDate);
308            ImmutableList<String> fields = new ImmutableList.Builder<String>()
309                    .add("leavePlan")
310                    .add("principalId")
311                    .build();
312            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(PrincipalHRAttributes.class, effdt, fields, false));
313            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
314    
315                    root.addEqualTo("principalId", principalId);
316    
317                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
318                    activeFilter.addEqualTo("active", false);
319                    root.addAndCriteria(activeFilter);
320                    
321                    Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
322                    Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
323    
324                    if (obj != null) {
325                            pc = (PrincipalHRAttributes) obj;
326                    }
327    
328                    return pc;
329        }
330        
331        @Override
332        public PrincipalHRAttributes getPrincipalHRAttributes(String hrPrincipalAttributeId) {
333            Criteria crit = new Criteria();
334                    crit.addEqualTo("hrPrincipalAttributeId", hrPrincipalAttributeId);
335                    
336                    Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit);
337                    return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
338        }
339        
340        @Override
341        public List<PrincipalHRAttributes> getAllActivePrincipalHrAttributesForPrincipalId(String principalId, java.util.Date asOfDate) {
342            
343            List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>();
344            Criteria root = new Criteria();
345            root.addEqualTo("principalId", principalId);
346            root.addLessOrEqualThan("effectiveDate", asOfDate);
347            root.addEqualTo("active", true);
348            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
349            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
350            if (c != null) {
351                phaList.addAll(c);
352            }
353            return phaList;        
354        }
355        
356        @Override
357        public List<PrincipalHRAttributes> getAllInActivePrincipalHrAttributesForPrincipalId(String principalId, java.util.Date asOfDate) {
358            List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>();
359            Criteria root = new Criteria();
360            root.addEqualTo("principalId", principalId);
361            root.addLessOrEqualThan("effectiveDate", asOfDate);
362            root.addEqualTo("active", false);
363    
364            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
365            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
366            if (c != null) {
367                phaList.addAll(c);
368            }
369            return phaList;  
370        }
371        @Override
372        public PrincipalHRAttributes getMaxTimeStampPrincipalHRAttributes(String principalId) {
373            Criteria root = new Criteria();
374            Criteria crit = new Criteria();
375            
376            crit.addEqualTo("principalId", principalId);
377            ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit);
378            timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
379    
380            root.addEqualTo("principalId", principalId);
381            root.addEqualTo("timestamp", timestampSubQuery);
382    
383            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
384            return (PrincipalHRAttributes) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
385        }
386        
387        @Override
388        public List<PrincipalHRAttributes> getActivePrincipalHrAttributesForRange(String principalId, java.util.Date startDate, java.util.Date endDate) {
389            List<PrincipalHRAttributes> activeList = new ArrayList<PrincipalHRAttributes>();
390            Criteria root = new Criteria();
391            root.addEqualTo("principalId", principalId);
392            root.addGreaterOrEqualThan("effectiveDate", startDate);
393            root.addLessOrEqualThan("effectiveDate", endDate);
394            root.addEqualTo("active", true);
395            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
396            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
397            if (c != null) {
398                    activeList.addAll(c);
399            }
400            List<PrincipalHRAttributes> aList = new ArrayList<PrincipalHRAttributes>();
401            aList.addAll(activeList);
402            for(PrincipalHRAttributes aPha : aList) {
403                    List<PrincipalHRAttributes> inactivePhas = this.getInactivePrincipalHRAttributesForRange(principalId, aPha.getEffectiveDate(), endDate);
404                    if(CollectionUtils.isNotEmpty(inactivePhas)) {
405                            for(PrincipalHRAttributes inactive : inactivePhas) {
406                                    if(inactive.getTimestamp().after(aPha.getTimestamp())) {
407                                            activeList.remove(aPha);
408                                    }
409                            }
410                    }
411            }
412            
413            return activeList;   
414        }
415        
416        @Override
417        public List<PrincipalHRAttributes> getInactivePrincipalHRAttributesForRange(String principalId, java.util.Date startDate, java.util.Date endDate) {
418            List<PrincipalHRAttributes> inactiveList = new ArrayList<PrincipalHRAttributes>();
419            Criteria root = new Criteria();
420            root.addEqualTo("principalId", principalId);
421            root.addGreaterOrEqualThan("effectiveDate", startDate);
422            root.addLessOrEqualThan("effectiveDate", endDate);
423            root.addEqualTo("active", false);
424            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
425            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
426            if (c != null) {
427                    inactiveList.addAll(c);
428            }
429            return inactiveList;
430        }
431       
432            @Override
433        @SuppressWarnings("unchecked")
434        public List<PrincipalHRAttributes> getPrincipalHrAtributes(String principalId, String leavePlan, java.sql.Date fromEffdt, java.sql.Date toEffdt, String active, String showHistory) {
435            List<PrincipalHRAttributes> results = new ArrayList<PrincipalHRAttributes>();
436            
437            Criteria root = new Criteria();
438            
439            if (StringUtils.isNotBlank(principalId)) {
440                root.addLike("principalId", principalId);
441            }
442    
443            if (StringUtils.isNotBlank(leavePlan)) {
444                root.addLike("leavePlan", leavePlan);
445            }
446    
447            Criteria effectiveDateFilter = new Criteria();
448            if (fromEffdt != null) {
449                effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
450            }
451            if (toEffdt != null) {
452                effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
453            }
454            if (fromEffdt == null && toEffdt == null) {
455                effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
456            }
457            root.addAndCriteria(effectiveDateFilter);
458    
459            if (StringUtils.isNotBlank(active)) {
460                    Criteria activeFilter = new Criteria();
461                if (StringUtils.equals(active, "Y")) {
462                    activeFilter.addEqualTo("active", true);
463                } else if (StringUtils.equals(active, "N")) {
464                    activeFilter.addEqualTo("active", false);
465                }
466                root.addAndCriteria(activeFilter);
467            }
468            
469            if (StringUtils.equals(showHistory, "N")) {
470                ImmutableList<String> fields = new ImmutableList.Builder<String>()
471                        .add("principalId")
472                        .add("leavePlan")
473                        .build();
474                root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(PrincipalHRAttributes.class, effectiveDateFilter, fields, false));
475                root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false));
476           }
477            
478           Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
479           results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
480           
481           return results;
482        }
483    
484    }