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.earncodesec.dao;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.Date;
021    import java.util.HashSet;
022    import java.util.LinkedList;
023    import java.util.List;
024    import java.util.Set;
025    
026    import com.google.common.collect.ImmutableList;
027    import org.apache.commons.lang.StringUtils;
028    import org.apache.log4j.Logger;
029    import org.apache.ojb.broker.query.Criteria;
030    import org.apache.ojb.broker.query.Query;
031    import org.apache.ojb.broker.query.QueryFactory;
032    import org.apache.ojb.broker.query.ReportQueryByCriteria;
033    import org.kuali.hr.core.util.OjbSubQueryUtil;
034    import org.kuali.hr.lm.earncodesec.EarnCodeSecurity;
035    import org.kuali.hr.time.util.TKUtils;
036    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
037    
038    public class EarnCodeSecurityDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements EarnCodeSecurityDao {
039        private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
040                .add("dept")
041                .add("hrSalGroup")
042                .add("earnCode")
043                .add("location")
044                .build();
045    
046            @SuppressWarnings("unused")
047            private static final Logger LOG = Logger.getLogger(EarnCodeSecurityDaoSpringOjbImpl.class);
048    
049            public void saveOrUpdate(EarnCodeSecurity earnCodeSec) {
050                    this.getPersistenceBrokerTemplate().store(earnCodeSec);
051            }
052    
053            public void saveOrUpdate(List<EarnCodeSecurity> ernCdSecList) {
054                    if (ernCdSecList != null) {
055                            for (EarnCodeSecurity ernCdSec : ernCdSecList) {
056                                    this.getPersistenceBrokerTemplate().store(ernCdSec);
057                            }
058                    }
059            }
060    
061            @SuppressWarnings({ "unchecked", "deprecation" })
062            @Override
063            public List<EarnCodeSecurity> getEarnCodeSecurities(String department, String hrSalGroup, String location, Date asOfDate) {
064                    List<EarnCodeSecurity> decs = new LinkedList<EarnCodeSecurity>();
065    
066                    Criteria root = new Criteria();
067                    
068                    Criteria deptCrit = new Criteria();
069                    Criteria salGroupCrit = new Criteria();
070                    Criteria locationCrit = new Criteria();
071                    
072                    deptCrit.addEqualTo("dept", "%");
073                    salGroupCrit.addEqualTo("hrSalGroup", "%");
074                    locationCrit.addEqualTo("location", "%");
075                    
076                    Criteria deptCrit2 = new Criteria();
077                    deptCrit2.addEqualTo("dept", department);
078                    deptCrit2.addOrCriteria(deptCrit);
079                    root.addAndCriteria(deptCrit2);
080                    
081                    Criteria salGroupCrit2 = new Criteria();
082                    salGroupCrit2.addEqualTo("hrSalGroup", hrSalGroup);
083                    salGroupCrit2.addOrCriteria(salGroupCrit);
084                    root.addAndCriteria(salGroupCrit2);
085                    
086                    Criteria locationCrit2 = new Criteria();
087                    if ( !location.trim().isEmpty() ){
088                            locationCrit2.addEqualTo("location", location);
089                            locationCrit2.addOrCriteria(locationCrit);
090                            root.addAndCriteria(locationCrit2);
091                    }
092                    
093                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
094                    activeFilter.addEqualTo("active", true);
095                    root.addAndCriteria(activeFilter);
096            ImmutableList.Builder<String> fields = new ImmutableList.Builder<String>()
097                    .add("dept")
098                    .add("hrSalGroup")
099                    .add("earnCode");
100            if ( !location.trim().isEmpty() ){
101                fields.add("location");
102            }
103            java.sql.Date effDate = null;
104            if (asOfDate != null) {
105                effDate = new java.sql.Date(asOfDate.getTime());
106            }
107            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EarnCodeSecurity.class, effDate, fields.build(), false));
108            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EarnCodeSecurity.class, fields.build(), false));
109                    
110                    root.addOrderBy("earnCode", true);
111                    root.addOrderBy("dept",false);
112                    root.addOrderBy("hrSalGroup",false);
113                    
114                    
115                    Query query = QueryFactory.newQuery(EarnCodeSecurity.class, root);
116                    
117                    Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
118                    
119                    if (c != null) {
120                            decs.addAll(c);
121                    }
122                    
123                    //Now we can have duplicates so remove any that match more than once
124                    Set<String> aSet = new HashSet<String>();
125                    List<EarnCodeSecurity> aList = new ArrayList<EarnCodeSecurity>();
126                    for(EarnCodeSecurity dec : decs){
127                            if(!aSet.contains(dec.getEarnCode())){
128                                    aList.add(dec);
129                                    aSet.add(dec.getEarnCode());
130                            } 
131                    }
132                    return aList;
133            }
134    
135            @Override
136            public EarnCodeSecurity getEarnCodeSecurity(String hrEarnCodeSecId) {
137                    Criteria crit = new Criteria();
138                    crit.addEqualTo("hrEarnCodeSecurityId", hrEarnCodeSecId);
139                    
140                    Query query = QueryFactory.newQuery(EarnCodeSecurity.class, crit);
141                    return (EarnCodeSecurity)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
142            }
143            
144            @Override
145            @SuppressWarnings("unchecked")
146            public List<EarnCodeSecurity> searchEarnCodeSecurities(String dept, String salGroup, String earnCode, String location, Date fromEffdt, Date toEffdt, 
147                                                                                                                       String active, String showHistory) {
148                    
149                    List<EarnCodeSecurity> results = new ArrayList<EarnCodeSecurity>();
150    
151            Criteria root = new Criteria();
152    
153            if (StringUtils.isNotBlank(dept)) {
154                root.addLike("dept", dept);
155            }
156    
157            if (StringUtils.isNotBlank(salGroup)) {
158                root.addLike("hrSalGroup", salGroup);
159            }
160    
161            if (StringUtils.isNotBlank(earnCode)) {
162                root.addLike("earnCode", earnCode);
163            }
164    
165            if (StringUtils.isNotBlank(location)) {
166                root.addLike("location", location);
167            }
168    
169            Criteria effectiveDateFilter = new Criteria();
170            if (fromEffdt != null) {
171                effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
172            }
173    
174            if (toEffdt != null) {
175                effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
176            }
177            if (fromEffdt == null && toEffdt == null) {
178                effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
179            }
180            root.addAndCriteria(effectiveDateFilter);
181    
182            if (StringUtils.isNotBlank(active)) {
183                    Criteria activeFilter = new Criteria();
184                if (StringUtils.equals(active, "Y")) {
185                    activeFilter.addEqualTo("active", true);
186                } else if (StringUtils.equals(active, "N")) {
187                    activeFilter.addEqualTo("active", false);
188                }
189                root.addAndCriteria(activeFilter);
190            }
191    
192            if (StringUtils.equals(showHistory, "N")) {
193                root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(EarnCodeSecurity.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
194                root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EarnCodeSecurity.class, EQUAL_TO_FIELDS, false));
195            }
196            
197            Query query = QueryFactory.newQuery(EarnCodeSecurity.class, root);
198            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
199    
200            return results;
201            }
202            
203            @Override
204            public int getEarnCodeSecurityCount(String dept, String salGroup, String earnCode, String employee, String approver, String location,
205                            String active, java.sql.Date effdt,String hrDeptEarnCodeId) {
206                    Criteria crit = new Criteria();
207          crit.addEqualTo("dept", dept);
208          crit.addEqualTo("hrSalGroup", salGroup);
209          crit.addEqualTo("earnCode", earnCode);
210          crit.addEqualTo("employee", employee);
211          crit.addEqualTo("approver", approver);
212          crit.addEqualTo("location", location);
213          crit.addEqualTo("active", active);
214          crit.addEqualTo("effectiveDate", effdt);
215          if(hrDeptEarnCodeId != null) {
216              crit.addEqualTo("hrEarnCodeSecurityId", hrDeptEarnCodeId);
217          }
218          Query query = QueryFactory.newQuery(EarnCodeSecurity.class, crit);
219          return this.getPersistenceBrokerTemplate().getCount(query);
220            }
221            @Override
222            public int getNewerEarnCodeSecurityCount(String earnCode, Date effdt) {
223                    Criteria crit = new Criteria();
224                    crit.addEqualTo("earnCode", earnCode);
225                    crit.addEqualTo("active", "Y");
226                    crit.addGreaterThan("effectiveDate", effdt);
227                    Query query = QueryFactory.newQuery(EarnCodeSecurity.class, crit);
228            return this.getPersistenceBrokerTemplate().getCount(query);
229            }
230    }