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.employeeoverride.dao;
017    
018    import java.sql.Date;
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.List;
022    
023    import com.google.common.collect.ImmutableList;
024    import edu.emory.mathcs.backport.java.util.Collections;
025    import org.apache.commons.lang.StringUtils;
026    import org.apache.ojb.broker.query.Criteria;
027    import org.apache.ojb.broker.query.Query;
028    import org.apache.ojb.broker.query.QueryFactory;
029    import org.apache.ojb.broker.query.ReportQueryByCriteria;
030    import org.kuali.hr.core.util.OjbSubQueryUtil;
031    import org.kuali.hr.lm.employeeoverride.EmployeeOverride;
032    import org.kuali.hr.time.util.TKUtils;
033    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
034    
035    public class EmployeeOverrideDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements EmployeeOverrideDao{
036        private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
037                .add("principalId")
038                .add("leavePlan")
039                .add("accrualCategory")
040                .add("overrideType")
041                .build();
042    
043            @SuppressWarnings("unchecked")
044            @Override
045            public List<EmployeeOverride> getEmployeeOverrides(String principalId, Date asOfDate) {
046            List<EmployeeOverride> employeeOverrides = new ArrayList<EmployeeOverride>();
047            Criteria root = new Criteria();
048    
049            root.addEqualTo("principalId", principalId);
050            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EmployeeOverride.class, asOfDate, EQUAL_TO_FIELDS, false));
051            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EmployeeOverride.class, EQUAL_TO_FIELDS, false));
052    
053            Criteria activeFilter = new Criteria(); // Inner Join For Activity
054            activeFilter.addEqualTo("active", true);
055            root.addAndCriteria(activeFilter);
056    
057            Query query = QueryFactory.newQuery(EmployeeOverride.class, root);
058            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
059    
060            if (c != null) {
061                    employeeOverrides.addAll(c);
062            }
063            return employeeOverrides;
064        }
065            
066            @Override
067            public EmployeeOverride getEmployeeOverride(String principalId, String leavePlan, String accrualCategory, String overrideType, Date asOfDate) {
068            Criteria root = new Criteria();
069    
070            root.addEqualTo("principalId", principalId);
071            root.addEqualTo("leavePlan", leavePlan);
072            root.addEqualTo("accrualCategory", accrualCategory);
073            root.addEqualTo("overrideType", overrideType);
074    
075            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EmployeeOverride.class, asOfDate, EQUAL_TO_FIELDS, false));
076            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EmployeeOverride.class, EQUAL_TO_FIELDS, false));
077    
078            Criteria activeFilter = new Criteria();
079            activeFilter.addEqualTo("active", true);
080            root.addAndCriteria(activeFilter);
081    
082            Query query = QueryFactory.newQuery(EmployeeOverride.class, root);
083            return (EmployeeOverride) getPersistenceBrokerTemplate().getObjectByQuery(query);
084            }
085    
086            @Override
087            public EmployeeOverride getEmployeeOverride(String lmEmployeeOverrideId) {
088                    Criteria crit = new Criteria();
089            crit.addEqualTo("lmEmployeeOverrideId", lmEmployeeOverrideId);
090            Query query = QueryFactory.newQuery(EmployeeOverride.class, crit);
091            return (EmployeeOverride) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
092            }
093    
094        @Override
095        @SuppressWarnings("unchecked")
096        public List<EmployeeOverride> getEmployeeOverrides(String principalId, String leavePlan, String accrualCategory, String overrideType,
097                                                           Date fromEffdt, Date toEffdt, String active) {
098    
099            List<EmployeeOverride> results = new ArrayList<EmployeeOverride>();
100            
101            Criteria root = new Criteria();
102    
103            if (StringUtils.isNotBlank(principalId)) {
104                    root.addEqualTo("principalId",principalId);
105            }
106            
107            if (StringUtils.isNotBlank(leavePlan)) {
108                    root.addEqualTo("leavePlan",leavePlan);
109            }
110            
111            if (StringUtils.isNotBlank(accrualCategory)) {
112                    root.addEqualTo("accrualCategory",accrualCategory);
113            }
114            
115            if (StringUtils.isNotBlank(overrideType)) {
116                    root.addEqualTo("overrideType",overrideType);
117            }
118            
119            Criteria effectiveDateFilter = new Criteria();
120            if (fromEffdt != null) {
121                effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
122            }
123            if (toEffdt != null) {
124                effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
125            }
126            if (fromEffdt == null && toEffdt == null) {
127                effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
128            }
129            root.addAndCriteria(effectiveDateFilter);
130    
131            if (StringUtils.isNotBlank(active)) {
132                Criteria activeFilter = new Criteria();
133                if (StringUtils.equals(active, "Y")) {
134                    activeFilter.addEqualTo("active", true);
135                } else if (StringUtils.equals(active, "N")) {
136                    activeFilter.addEqualTo("active", false);
137                }
138                root.addAndCriteria(activeFilter);
139            }
140    
141            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(EmployeeOverride.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
142            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EmployeeOverride.class, EQUAL_TO_FIELDS, false));
143    
144            Query query = QueryFactory.newQuery(EmployeeOverride.class, root);
145            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
146    
147            return results;
148        }
149    
150    }