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.roles.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.job.Job;
032 import org.kuali.hr.time.roles.TkRole;
033 import org.kuali.hr.time.service.base.TkServiceLocator;
034 import org.kuali.hr.time.util.TKUtils;
035 import org.kuali.hr.time.util.TkConstants;
036 import org.kuali.hr.time.workarea.WorkArea;
037 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
038 import org.kuali.rice.krad.service.KRADServiceLocator;
039
040 public class TkRoleDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TkRoleDao {
041 private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
042 .add("roleName")
043 .add("positionNumber")
044 .add("principalId")
045 .add("workArea")
046 .add("department")
047 .add("chart")
048 .build();
049
050
051 public List<TkRole> findAllRoles(String principalId, Date asOfDate) {
052 return findRoles(principalId, asOfDate, null, null, null, null);
053 }
054
055 @SuppressWarnings("unchecked")
056 @Override
057 public List<TkRole> findPositionRoles(String positionNumber, Date asOfDate, String roleName, Long workArea, String department, String chart) {
058 List<TkRole> roles = new ArrayList<TkRole>();
059
060 Criteria root = new Criteria();
061 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, asOfDate, EQUAL_TO_FIELDS, false));
062 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TkRole.class, EQUAL_TO_FIELDS, false));
063
064 // Optional ROOT criteria added :
065 if (workArea != null)
066 root.addEqualTo("workArea", workArea);
067 if (StringUtils.isNotEmpty(department))
068 root.addEqualTo("department", department);
069 if (chart != null)
070 root.addEqualTo("chart", chart);
071 if (roleName != null)
072 root.addEqualTo("roleName", roleName);
073 if (positionNumber != null)
074 root.addEqualTo("positionNumber", positionNumber);
075
076 // Filter for ACTIVE = 'Y'
077 Criteria activeFilter = new Criteria();
078 activeFilter.addEqualTo("active", true);
079 root.addAndCriteria(activeFilter);
080
081 Query query = QueryFactory.newQuery(TkRole.class, root);
082 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
083
084 if (c != null) {
085 roles.addAll(c);
086 }
087
088 return roles;
089 }
090
091 @SuppressWarnings("unchecked")
092 @Override
093 public List<TkRole> findRoles(String principalId, Date asOfDate, String roleName, Long workArea, String department, String chart) {
094 List<TkRole> roles = new ArrayList<TkRole>();
095
096 Criteria root = new Criteria();
097
098 if (StringUtils.isNotEmpty(principalId)) {
099 root.addEqualTo("principalId", principalId);
100 }
101
102 if (asOfDate != null) {
103 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, asOfDate, EQUAL_TO_FIELDS, false));
104 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TkRole.class, EQUAL_TO_FIELDS, false));
105 }
106
107 if (StringUtils.isNotEmpty(roleName)) {
108 root.addEqualTo("roleName", roleName);
109 }
110
111 if (workArea != null) {
112 root.addEqualTo("workArea", workArea);
113 }
114 if (StringUtils.isNotEmpty(department)) {
115 Criteria departmentCriteria = new Criteria();
116 departmentCriteria.addEqualTo("department", department);
117 Collection<WorkArea> collectionWorkAreas = TkServiceLocator.getWorkAreaService().getWorkAreas(department, asOfDate);
118 if (CollectionUtils.isNotEmpty(collectionWorkAreas)) {
119 List<Long> longWorkAreas = new ArrayList<Long>();
120 for(WorkArea cwa : collectionWorkAreas){
121 longWorkAreas.add(cwa.getWorkArea());
122 }
123 Criteria workAreaCriteria = new Criteria();
124 workAreaCriteria.addIn("workArea", longWorkAreas);
125 departmentCriteria.addOrCriteria(workAreaCriteria);
126 }
127 root.addAndCriteria(departmentCriteria);
128 }
129
130 if (StringUtils.isNotEmpty(chart)) {
131 root.addEqualTo("chart", chart);
132 }
133
134 root.addEqualTo("active", true);
135
136 Query query = QueryFactory.newQuery(TkRole.class, root);
137 // limit the number of the resultset
138 // TODO: hard coding the limits? probably not the most user friendly of ways to do this
139 query.setStartAtIndex(0);
140 query.setEndAtIndex(299);
141 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
142
143 if (c != null) {
144 roles.addAll(c);
145 }
146
147 if (StringUtils.isNotBlank(principalId)) {
148 //Fetch all the jobs and grab any position roles for this persons jobs
149 List<Job> lstActiveJobs = TkServiceLocator.getJobService().getJobs(principalId, asOfDate);
150 for (Job job : lstActiveJobs) {
151 if (job.getPositionNumber() != null) {
152 List<TkRole> lstRoles = findPositionRoles(job.getPositionNumber(),
153 asOfDate, roleName, workArea, department, chart);
154 roles.addAll(lstRoles);
155 }
156 }
157 } else if (workArea != null) {
158 List<TkRole> lstPosRoles = getPositionRolesForWorkArea(workArea, asOfDate);
159 for (TkRole tkRole : lstPosRoles) {
160 if (!roles.contains(tkRole)) {
161 roles.add(tkRole);
162 }
163 }
164 }
165 return roles;
166 }
167
168 @SuppressWarnings("unchecked")
169 @Override
170 public List<TkRole> findInActiveRoles(String principalId, Date asOfDate, String roleName, Long workArea, String department, String chart) {
171 List<TkRole> roles = new ArrayList<TkRole>();
172
173 Criteria root = new Criteria();
174
175 if (StringUtils.isNotEmpty(principalId)) {
176 root.addEqualTo("principalId", principalId);
177 }
178
179 if (asOfDate != null) {
180 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, asOfDate, EQUAL_TO_FIELDS, false));
181 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TkRole.class, EQUAL_TO_FIELDS, false));
182 }
183
184
185 if (StringUtils.isNotEmpty(roleName)) {
186 root.addEqualTo("roleName", roleName);
187 }
188
189 if (workArea != null) {
190 root.addEqualTo("workArea", workArea);
191 }
192
193 if (StringUtils.isNotEmpty(department)) {
194 root.addEqualTo("department", department);
195 }
196
197 if (StringUtils.isNotEmpty(chart)) {
198 root.addEqualTo("chart", chart);
199 }
200
201 root.addEqualTo("active", false);
202
203 Query query = QueryFactory.newQuery(TkRole.class, root);
204 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
205
206 if (c != null) {
207 roles.addAll(c);
208 }
209
210 return roles;
211 }
212
213 @Override
214 public void saveOrUpdateRole(TkRole role) {
215 KRADServiceLocator.getBusinessObjectService().save(role);
216 }
217
218 @Override
219 public void saveOrUpdateRoles(List<TkRole> roles) {
220 if (roles != null) {
221 for (TkRole role : roles) {
222 saveOrUpdateRole(role);
223 }
224 }
225 }
226
227 @Override
228 public TkRole getRole(String tkRoleId) {
229 Criteria currentRecordCriteria = new Criteria();
230 currentRecordCriteria.addEqualTo("hrRolesId", tkRoleId);
231
232 return (TkRole) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TkRole.class, currentRecordCriteria));
233 }
234
235 @Override
236 public TkRole getRolesByPosition(String positionNumber) {
237 Criteria currentRecordCriteria = new Criteria();
238 currentRecordCriteria.addEqualTo("positionNumber", positionNumber);
239
240 currentRecordCriteria.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, TKUtils.getCurrentDate(), EQUAL_TO_FIELDS, false));
241
242 // Filter for ACTIVE = 'Y'
243 Criteria activeFilter = new Criteria();
244 activeFilter.addEqualTo("active", true);
245 currentRecordCriteria.addAndCriteria(activeFilter);
246
247
248 TkRole tkRole = (TkRole) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TkRole.class, currentRecordCriteria));
249 return tkRole;
250 }
251
252 @Override
253 public TkRole getInactiveRolesByPosition(String positionNumber) {
254 Criteria currentRecordCriteria = new Criteria();
255 currentRecordCriteria.addEqualTo("positionNumber", positionNumber);
256
257 currentRecordCriteria.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, TKUtils.getCurrentDate(), EQUAL_TO_FIELDS, false));
258
259 // Filter for ACTIVE = 'N'
260 Criteria activeFilter = new Criteria();
261 activeFilter.addEqualTo("active", false);
262 currentRecordCriteria.addAndCriteria(activeFilter);
263
264
265 TkRole tkRole = (TkRole) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TkRole.class, currentRecordCriteria));
266 return tkRole;
267 }
268
269
270 @Override
271 public List<TkRole> getPositionRolesForWorkArea(Long workArea, Date asOfDate) {
272 List<TkRole> roles = new ArrayList<TkRole>();
273
274 Criteria root = new Criteria();
275
276 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, asOfDate, EQUAL_TO_FIELDS, false));
277 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TkRole.class, EQUAL_TO_FIELDS, false));
278
279 // Optional ROOT criteria added :
280 if (workArea != null)
281 root.addEqualTo("workArea", workArea);
282 root.addEqualTo("roleName", TkConstants.ROLE_TK_APPROVER);
283
284 // Filter for ACTIVE = 'Y'
285 Criteria activeFilter = new Criteria();
286 activeFilter.addEqualTo("active", true);
287 root.addAndCriteria(activeFilter);
288
289 Query query = QueryFactory.newQuery(TkRole.class, root);
290 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
291
292 if (c != null) {
293 roles.addAll(c);
294 }
295
296 return roles;
297 }
298 }