001    /**
002     * Copyright 2004-2012 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.Date;
021    import java.util.List;
022    
023    import org.apache.commons.collections.CollectionUtils;
024    import org.apache.ojb.broker.query.Criteria;
025    import org.apache.ojb.broker.query.Query;
026    import org.apache.ojb.broker.query.QueryFactory;
027    import org.apache.ojb.broker.query.ReportQueryByCriteria;
028    import org.kuali.hr.time.principal.PrincipalHRAttributes;
029    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
030    
031    public class PrincipalHRAttributesDaoImpl extends PlatformAwareDaoBaseOjb implements PrincipalHRAttributesDao {
032    
033            @Override
034            public PrincipalHRAttributes getPrincipalCalendar(String principalId,
035                            Date asOfDate) {
036                    PrincipalHRAttributes pc = null;
037    
038                    Criteria root = new Criteria();
039                    Criteria effdt = new Criteria();
040                    Criteria timestamp = new Criteria();
041    
042                    effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
043                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
044                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, effdt);
045                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
046    
047                    timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
048                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
049                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, timestamp);
050                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
051    
052                    root.addEqualTo("principalId", principalId);
053                    root.addEqualTo("effectiveDate", effdtSubQuery);
054                    root.addEqualTo("timestamp", timestampSubQuery);
055    
056                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
057                    activeFilter.addEqualTo("active", true);
058                    root.addAndCriteria(activeFilter);
059                    
060                    Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
061                    Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
062    
063                    if (obj != null) {
064                            pc = (PrincipalHRAttributes) obj;
065                    }
066    
067                    return pc;
068            }
069    
070            @Override
071            public void saveOrUpdate(PrincipalHRAttributes principalCalendar) {
072                    this.getPersistenceBrokerTemplate().store(principalCalendar);
073                    
074            }
075    
076            @Override
077            public void saveOrUpdate(List<PrincipalHRAttributes> lstPrincipalCalendar) {
078                    if(lstPrincipalCalendar != null){
079                            for(PrincipalHRAttributes principalCal : lstPrincipalCalendar){
080                                    this.getPersistenceBrokerTemplate().store(principalCal);
081                            }
082                    }
083                    
084            }
085    
086    
087    //    @Override
088    //      public PrincipalHRAttributes getPrincipalHRAttributes(String principalId) {
089    //              Criteria crit = new Criteria();
090    //              crit.addEqualTo("principalId", principalId);
091    //              Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit);
092    //              return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query);              
093    //      }
094        
095        @Override
096        public PrincipalHRAttributes getInactivePrincipalHRAttributes(String principalId, Date asOfDate) {
097            PrincipalHRAttributes pc = null;
098    
099                    Criteria root = new Criteria();
100                    Criteria effdt = new Criteria();
101                    Criteria timestamp = new Criteria();
102    
103                    effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
104                    effdt.addGreaterOrEqualThan("effectiveDate", asOfDate);
105                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, effdt);
106                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
107    
108                    timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
109                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
110                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, timestamp);
111                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
112    
113                    root.addEqualTo("principalId", principalId);
114                    root.addEqualTo("effectiveDate", effdtSubQuery);
115                    root.addEqualTo("timestamp", timestampSubQuery);
116    
117                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
118                    activeFilter.addEqualTo("active", false);
119                    root.addAndCriteria(activeFilter);
120                    
121                    Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
122                    Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
123    
124                    if (obj != null) {
125                            pc = (PrincipalHRAttributes) obj;
126                    }
127    
128                    return pc;
129        }
130        
131        @Override
132        public PrincipalHRAttributes getPrincipalHRAttributes(String hrPrincipalAttributeId) {
133            Criteria crit = new Criteria();
134                    crit.addEqualTo("hrPrincipalAttributeId", hrPrincipalAttributeId);
135                    
136                    Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit);
137                    return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
138        }
139        
140        @Override
141        public List<PrincipalHRAttributes> getAllActivePrincipalHrAttributesForPrincipalId(String principalId, Date asOfDate) {
142            
143            List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>();
144            Criteria root = new Criteria();
145            root.addEqualTo("principalId", principalId);
146            root.addLessOrEqualThan("effectiveDate", asOfDate);
147            root.addEqualTo("active", true);
148            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
149            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
150            if (c != null) {
151                phaList.addAll(c);
152            }
153            return phaList;        
154        }
155        
156        @Override
157        public List<PrincipalHRAttributes> getAllInActivePrincipalHrAttributesForPrincipalId(String principalId, Date asOfDate) {
158            List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>();
159            Criteria root = new Criteria();
160            root.addEqualTo("principalId", principalId);
161            root.addLessOrEqualThan("effectiveDate", asOfDate);
162            root.addEqualTo("active", false);
163    
164            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
165            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
166            if (c != null) {
167                phaList.addAll(c);
168            }
169            return phaList;  
170        }
171        @Override
172        public PrincipalHRAttributes getMaxTimeStampPrincipalHRAttributes(String principalId) {
173            Criteria root = new Criteria();
174            Criteria crit = new Criteria();
175            
176            crit.addEqualTo("principalId", principalId);
177            ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit);
178            timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
179    
180            root.addEqualTo("principalId", principalId);
181            root.addEqualTo("timestamp", timestampSubQuery);
182    
183            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
184            return (PrincipalHRAttributes) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
185        }
186        
187        @Override
188        public List<PrincipalHRAttributes> getActivePrincipalHrAttributesForRange(String principalId, Date startDate, Date endDate) {
189            List<PrincipalHRAttributes> activeList = new ArrayList<PrincipalHRAttributes>();
190            Criteria root = new Criteria();
191            root.addEqualTo("principalId", principalId);
192            root.addGreaterOrEqualThan("effectiveDate", startDate);
193            root.addLessOrEqualThan("effectiveDate", endDate);
194            root.addEqualTo("active", true);
195            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
196            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
197            if (c != null) {
198                    activeList.addAll(c);
199            }
200            List<PrincipalHRAttributes> aList = new ArrayList<PrincipalHRAttributes>();
201            aList.addAll(activeList);
202            for(PrincipalHRAttributes aPha : aList) {
203                    List<PrincipalHRAttributes> inactivePhas = this.getInactivePrincipalHRAttributesForRange(principalId, aPha.getEffectiveDate(), endDate);
204                    if(CollectionUtils.isNotEmpty(inactivePhas)) {
205                            for(PrincipalHRAttributes inactive : inactivePhas) {
206                                    if(inactive.getTimestamp().after(aPha.getTimestamp())) {
207                                            activeList.remove(aPha);
208                                    }
209                            }
210                    }
211            }
212            
213            return activeList;   
214        }
215        
216        @Override
217        public List<PrincipalHRAttributes> getInactivePrincipalHRAttributesForRange(String principalId, Date startDate, Date endDate) {
218            List<PrincipalHRAttributes> inactiveList = new ArrayList<PrincipalHRAttributes>();
219            Criteria root = new Criteria();
220            root.addEqualTo("principalId", principalId);
221            root.addGreaterOrEqualThan("effectiveDate", startDate);
222            root.addLessOrEqualThan("effectiveDate", endDate);
223            root.addEqualTo("active", false);
224            Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root);
225            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
226            if (c != null) {
227                    inactiveList.addAll(c);
228            }
229            return inactiveList;
230        }
231    }