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            List<String> fields = Collections.singletonList("principalId");
050            root.addEqualTo("principalId", principalId);
051            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EmployeeOverride.class, asOfDate, fields, false));
052            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EmployeeOverride.class, fields, false));
053    
054            Criteria activeFilter = new Criteria(); // Inner Join For Activity
055            activeFilter.addEqualTo("active", true);
056            root.addAndCriteria(activeFilter);
057    
058            Query query = QueryFactory.newQuery(EmployeeOverride.class, root);
059            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
060    
061            if (c != null) {
062                    employeeOverrides.addAll(c);
063            }
064            return employeeOverrides;
065        }
066            
067            @Override
068            public EmployeeOverride getEmployeeOverride(String principalId, String leavePlan, String accrualCategory, String overrideType, Date asOfDate) {
069            Criteria root = new Criteria();
070    
071            root.addEqualTo("principalId", principalId);
072            root.addEqualTo("leavePlan", leavePlan);
073            root.addEqualTo("accrualCategory", accrualCategory);
074            root.addEqualTo("overrideType", overrideType);
075    
076            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EmployeeOverride.class, asOfDate, EQUAL_TO_FIELDS, false));
077            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EmployeeOverride.class, EQUAL_TO_FIELDS, false));
078    
079            Criteria activeFilter = new Criteria();
080            activeFilter.addEqualTo("active", true);
081            root.addAndCriteria(activeFilter);
082    
083            Query query = QueryFactory.newQuery(EmployeeOverride.class, root);
084            return (EmployeeOverride) getPersistenceBrokerTemplate().getObjectByQuery(query);
085            }
086    
087            @Override
088            public EmployeeOverride getEmployeeOverride(String lmEmployeeOverrideId) {
089                    Criteria crit = new Criteria();
090            crit.addEqualTo("lmEmployeeOverrideId", lmEmployeeOverrideId);
091            Query query = QueryFactory.newQuery(EmployeeOverride.class, crit);
092            return (EmployeeOverride) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
093            }
094    
095        @Override
096        @SuppressWarnings("unchecked")
097        public List<EmployeeOverride> getEmployeeOverrides(String principalId, String leavePlan, String accrualCategory, String overrideType,
098                                                           Date fromEffdt, Date toEffdt, String active) {
099    
100            List<EmployeeOverride> results = new ArrayList<EmployeeOverride>();
101            
102            Criteria root = new Criteria();
103    
104            if (StringUtils.isNotBlank(principalId)) {
105                    root.addEqualTo("principalId",principalId);
106            }
107            
108            if (StringUtils.isNotBlank(leavePlan)) {
109                    root.addEqualTo("leavePlan",leavePlan);
110            }
111            
112            if (StringUtils.isNotBlank(accrualCategory)) {
113                    root.addEqualTo("accrualCategory",accrualCategory);
114            }
115            
116            if (StringUtils.isNotBlank(overrideType)) {
117                    root.addEqualTo("overrideType",overrideType);
118            }
119            
120            Criteria effectiveDateFilter = new Criteria();
121            if (fromEffdt != null) {
122                effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
123            }
124            if (toEffdt != null) {
125                effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
126            }
127            if (fromEffdt == null && toEffdt == null) {
128                effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
129            }
130            root.addAndCriteria(effectiveDateFilter);
131    
132            if (StringUtils.isNotBlank(active)) {
133                Criteria activeFilter = new Criteria();
134                if (StringUtils.equals(active, "Y")) {
135                    activeFilter.addEqualTo("active", true);
136                } else if (StringUtils.equals(active, "N")) {
137                    activeFilter.addEqualTo("active", false);
138                }
139                root.addAndCriteria(activeFilter);
140            }
141    
142            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(EmployeeOverride.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
143            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EmployeeOverride.class, EQUAL_TO_FIELDS, false));
144    
145            Query query = QueryFactory.newQuery(EmployeeOverride.class, root);
146            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
147    
148            return results;
149        }
150    
151    }