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 com.google.common.collect.ImmutableList;
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
029 import org.kuali.hr.core.util.OjbSubQueryUtil;
030 import org.kuali.hr.time.department.Department;
031 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
032
033 public class DepartmentDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements DepartmentDao {
034 private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
035 .add("dept")
036 .build();
037
038 @Override
039 public void saveOrUpdate(Department dept) {
040 this.getPersistenceBrokerTemplate().store(dept);
041 }
042
043 @Override
044 public Department getDepartment(String department, Date asOfDate) {
045 Criteria root = new Criteria();
046
047 root.addEqualTo("dept", department);
048 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Department.class, asOfDate, EQUAL_TO_FIELDS, false));
049 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, EQUAL_TO_FIELDS, false));
050
051 Criteria activeFilter = new Criteria(); // Inner Join For Activity
052 activeFilter.addEqualTo("active", true);
053 root.addAndCriteria(activeFilter);
054
055 Query query = QueryFactory.newQuery(Department.class, root);
056
057 Department d = (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
058
059 return d;
060 }
061
062 @Override
063 public List<Department> getDepartments(String location, Date asOfDate) {
064 Criteria root = new Criteria();
065
066 root.addEqualTo("location", location);
067 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Department.class, asOfDate, EQUAL_TO_FIELDS, false));
068 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, EQUAL_TO_FIELDS, false));
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(Department.class, root);
076
077 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
078 List<Department> d = new ArrayList<Department>(c.size());
079 d.addAll(c);
080
081 return d;
082 }
083
084 @Override
085 @SuppressWarnings("unchecked")
086 public List<Department> getDepartments(String dept, String location, String departmentDescr, String active, String showHistory) {
087 List<Department> results = new ArrayList<Department>();
088
089 Criteria root = new Criteria();
090
091 if (StringUtils.isNotBlank(dept)) {
092 root.addLike("dept", dept);
093 }
094
095 if (StringUtils.isNotBlank(location)) {
096 root.addLike("location", location);
097 }
098
099 if (StringUtils.isNotBlank(departmentDescr)) {
100 root.addLike("description", departmentDescr);
101 }
102
103 if (StringUtils.isNotBlank(active)) {
104 Criteria activeFilter = new Criteria();
105 if (StringUtils.equals(active, "Y")) {
106 activeFilter.addEqualTo("active", true);
107 } else if (StringUtils.equals(active, "N")) {
108 activeFilter.addEqualTo("active", false);
109 }
110 root.addAndCriteria(activeFilter);
111 }
112
113 if (StringUtils.equals(showHistory, "N")) {
114 ImmutableList<String> fields = new ImmutableList.Builder<String>()
115 .add("dept")
116 .add("location")
117 .add("description")
118 .build();
119 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(Department.class, EQUAL_TO_FIELDS, false));
120 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, EQUAL_TO_FIELDS, false));
121 }
122
123 Query query = QueryFactory.newQuery(Department.class, root);
124 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
125
126 return results;
127 }
128
129 @Override
130 public Department getDepartment(String hrDeptId) {
131 Criteria crit = new Criteria();
132 crit.addEqualTo("hrDeptId", hrDeptId);
133
134 Query query = QueryFactory.newQuery(Department.class, crit);
135 return (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
136 }
137
138 @Override
139 public List<Department> getDepartmentByLocation(String location) {
140 Criteria crit = new Criteria();
141 crit.addEqualTo("location", location);
142
143 Query query = QueryFactory.newQuery(Department.class, crit);
144
145 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
146 List<Department> d = new ArrayList<Department>(c.size());
147 d.addAll(c);
148
149 return d;
150 }
151
152 @Override
153 public int getDepartmentCount(String department) {
154 Criteria crit = new Criteria();
155 crit.addEqualTo("dept", department);
156 Query query = QueryFactory.newQuery(Department.class, crit);
157 return this.getPersistenceBrokerTemplate().getCount(query);
158 }
159 }