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