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.dept.lunch.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 org.apache.commons.collections.CollectionUtils;
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.time.dept.lunch.DeptLunchRule;
032    import org.kuali.hr.time.service.base.TkServiceLocator;
033    import org.kuali.hr.time.util.TKUtils;
034    import org.kuali.hr.time.workarea.WorkArea;
035    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
036    
037    public class DepartmentLunchRuleDaoImpl  extends PlatformAwareDaoBaseOjb implements DepartmentLunchRuleDao {
038        private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
039                .add("dept")
040                .add("workArea")
041                .add("principalId")
042                .add("jobNumber")
043                .build();
044    
045            @Override
046            public DeptLunchRule getDepartmentLunchRule(String dept, Long workArea, String principalId, 
047                                                                                                    Long jobNumber, Date asOfDate) {
048                    Criteria root = new Criteria();
049    
050                    root.addEqualTo("dept", dept);
051                    root.addEqualTo("workArea", workArea);
052                    root.addEqualTo("principalId", principalId);
053                    root.addEqualTo("jobNumber", jobNumber);
054            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(DeptLunchRule.class, asOfDate, EQUAL_TO_FIELDS, false));
055            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(DeptLunchRule.class, EQUAL_TO_FIELDS, false));
056    //              root.addEqualTo("active", true);
057    
058                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
059                    activeFilter.addEqualTo("active", true);
060                    root.addAndCriteria(activeFilter);
061                    
062                    
063                    Query query = QueryFactory.newQuery(DeptLunchRule.class, root);
064                    return (DeptLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
065                    
066    
067            }
068    
069            @Override
070            public DeptLunchRule getDepartmentLunchRule(String tkDeptLunchRuleId) {
071                    Criteria crit = new Criteria();
072                    crit.addEqualTo("tkDeptLunchRuleId", tkDeptLunchRuleId);
073                    
074                    Query query = QueryFactory.newQuery(DeptLunchRule.class, crit);
075                    return (DeptLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
076            }
077    
078            @Override
079        @SuppressWarnings("unchecked")
080        public List<DeptLunchRule> getDepartmentLunchRules(String dept, String workArea, String principalId, String jobNumber, String active) {
081            List<DeptLunchRule> results = new ArrayList<DeptLunchRule>();
082            
083            Criteria root = new Criteria();
084    
085            if (StringUtils.isNotBlank(dept)) {
086                root.addLike("dept", dept);
087            }
088            
089            if (StringUtils.isNotBlank(principalId)) {
090                root.addLike("principalId", principalId);
091            }
092            
093            if (StringUtils.isNotBlank(jobNumber)) {
094                root.addLike("jobNumber", jobNumber);
095            }
096            
097            if (StringUtils.isNotBlank(dept)) {
098                Criteria workAreaCriteria = new Criteria();
099                Date asOfDate = TKUtils.getCurrentDate();
100                Collection<WorkArea> workAreasForDept = TkServiceLocator.getWorkAreaService().getWorkAreas(dept,asOfDate);
101                if (CollectionUtils.isNotEmpty(workAreasForDept)) {
102                    List<Long> longWorkAreas = new ArrayList<Long>();
103                    for(WorkArea cwa : workAreasForDept){
104                        longWorkAreas.add(cwa.getWorkArea());
105                    }
106                    workAreaCriteria.addIn("workArea", longWorkAreas);
107                }
108                root.addAndCriteria(workAreaCriteria);
109            }
110            
111            if (StringUtils.isNotBlank(workArea)) {
112                root.addLike("workArea", workArea);
113            }
114            
115            if (StringUtils.isNotBlank(active)) {
116                    Criteria activeFilter = new Criteria();
117                if (StringUtils.equals(active, "Y")) {
118                    activeFilter.addEqualTo("active", true);
119                } else if (StringUtils.equals(active, "N")) {
120                    activeFilter.addEqualTo("active", false);
121                }
122                root.addAndCriteria(activeFilter);
123            }
124            
125            Query query = QueryFactory.newQuery(DeptLunchRule.class, root);
126            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
127    
128            return results;
129        }
130    
131    }