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.department.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.lang.StringUtils; 024 import org.apache.ojb.broker.query.Criteria; 025 import org.apache.ojb.broker.query.Query; 026 import org.apache.ojb.broker.query.QueryFactory; 027 import org.apache.ojb.broker.query.ReportQueryByCriteria; 028 import org.kuali.hr.time.department.Department; 029 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb; 030 031 public class DepartmentDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements DepartmentDao { 032 033 @Override 034 public void saveOrUpdate(Department dept) { 035 this.getPersistenceBrokerTemplate().store(dept); 036 } 037 038 @Override 039 public Department getDepartment(String department, 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.addLessOrEqualThan("effectiveDate", asOfDate); 046 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Department.class, effdt); 047 effdtSubQuery.setAttributes(new String[] { "max(effdt)" }); 048 049 timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept"); 050 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate"); 051 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Department.class, timestamp); 052 timestampSubQuery.setAttributes(new String[] { "max(timestamp)" }); 053 054 root.addEqualTo("dept", department); 055 root.addEqualTo("effectiveDate", effdtSubQuery); 056 root.addEqualTo("timestamp", timestampSubQuery); 057 058 Criteria activeFilter = new Criteria(); // Inner Join For Activity 059 activeFilter.addEqualTo("active", true); 060 root.addAndCriteria(activeFilter); 061 062 Query query = QueryFactory.newQuery(Department.class, root); 063 064 Department d = (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query); 065 066 return d; 067 } 068 069 @Override 070 public List<Department> getDepartments(String location, Date asOfDate) { 071 Criteria root = new Criteria(); 072 Criteria effdt = new Criteria(); 073 Criteria timestamp = new Criteria(); 074 075 effdt.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location"); 076 effdt.addLessOrEqualThan("effectiveDate", asOfDate); 077 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Department.class, effdt); 078 effdtSubQuery.setAttributes(new String[] { "max(effdt)" }); 079 080 timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept"); 081 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate"); 082 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Department.class, timestamp); 083 timestampSubQuery.setAttributes(new String[] { "max(timestamp)" }); 084 085 root.addEqualTo("location", location); 086 root.addEqualTo("effectiveDate", effdtSubQuery); 087 root.addEqualTo("timestamp", timestampSubQuery); 088 089 Criteria activeFilter = new Criteria(); // Inner Join For Activity 090 activeFilter.addEqualTo("active", true); 091 root.addAndCriteria(activeFilter); 092 093 094 Query query = QueryFactory.newQuery(Department.class, root); 095 096 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 097 List<Department> d = new ArrayList<Department>(c.size()); 098 d.addAll(c); 099 100 return d; 101 } 102 103 @Override 104 @SuppressWarnings("unchecked") 105 public List<Department> getDepartments(String dept, String location, String departmentDescr, String active) { 106 List<Department> results = new ArrayList<Department>(); 107 108 Criteria root = new Criteria(); 109 110 if (StringUtils.isNotBlank(dept)) { 111 root.addLike("dept", dept); 112 } 113 114 if (StringUtils.isNotBlank(location)) { 115 root.addLike("location", location); 116 } 117 118 if (StringUtils.isNotBlank(departmentDescr)) { 119 root.addLike("description", departmentDescr); 120 } 121 122 if (StringUtils.isNotBlank(active)) { 123 Criteria activeFilter = new Criteria(); 124 if (StringUtils.equals(active, "Y")) { 125 activeFilter.addEqualTo("active", true); 126 } else if (StringUtils.equals(active, "N")) { 127 activeFilter.addEqualTo("active", false); 128 } 129 root.addAndCriteria(activeFilter); 130 } 131 132 Query query = QueryFactory.newQuery(Department.class, root); 133 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query)); 134 135 return results; 136 } 137 138 @Override 139 public Department getDepartment(String hrDeptId) { 140 Criteria crit = new Criteria(); 141 crit.addEqualTo("hrDeptId", hrDeptId); 142 143 Query query = QueryFactory.newQuery(Department.class, crit); 144 return (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query); 145 } 146 147 @Override 148 public List<Department> getDepartmentByLocation(String location) { 149 Criteria crit = new Criteria(); 150 crit.addEqualTo("location", location); 151 152 Query query = QueryFactory.newQuery(Department.class, crit); 153 154 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 155 List<Department> d = new ArrayList<Department>(c.size()); 156 d.addAll(c); 157 158 return d; 159 } 160 161 @Override 162 public int getDepartmentCount(String department) { 163 Criteria crit = new Criteria(); 164 crit.addEqualTo("dept", department); 165 Query query = QueryFactory.newQuery(Department.class, crit); 166 return this.getPersistenceBrokerTemplate().getCount(query); 167 } 168 }