1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.service.impl; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.kuali.rice.core.api.util.RiceConstants; |
20 | |
import org.kuali.rice.kim.api.KimConstants.PermissionNames; |
21 | |
import org.kuali.rice.kim.api.identity.Person; |
22 | |
import org.kuali.rice.kim.api.identity.PersonService; |
23 | |
import org.kuali.rice.kim.api.permission.PermissionService; |
24 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
25 | |
import org.kuali.rice.krad.authorization.AuthorizationConstants; |
26 | |
import org.kuali.rice.krad.document.Document; |
27 | |
import org.kuali.rice.krad.document.authorization.PessimisticLock; |
28 | |
import org.kuali.rice.krad.exception.AuthorizationException; |
29 | |
import org.kuali.rice.krad.exception.PessimisticLockingException; |
30 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
31 | |
import org.kuali.rice.krad.service.DataDictionaryService; |
32 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
33 | |
import org.kuali.rice.krad.service.PessimisticLockService; |
34 | |
import org.kuali.rice.krad.util.GlobalVariables; |
35 | |
import org.kuali.rice.krad.util.KRADConstants; |
36 | |
import org.kuali.rice.krad.util.KRADPropertyConstants; |
37 | |
import org.kuali.rice.krad.util.ObjectUtils; |
38 | |
import org.springframework.transaction.annotation.Transactional; |
39 | |
|
40 | |
import java.util.ArrayList; |
41 | |
import java.util.Collections; |
42 | |
import java.util.HashMap; |
43 | |
import java.util.HashSet; |
44 | |
import java.util.Iterator; |
45 | |
import java.util.List; |
46 | |
import java.util.Map; |
47 | |
import java.util.Set; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
@Transactional |
56 | 0 | public class PessimisticLockServiceImpl implements PessimisticLockService { |
57 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PessimisticLockServiceImpl.class); |
58 | |
|
59 | |
private PersonService personService; |
60 | |
private BusinessObjectService businessObjectService; |
61 | |
private DataDictionaryService dataDictionaryService; |
62 | |
private PermissionService permissionService; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public void delete(String id) { |
68 | 0 | if (StringUtils.isBlank(id)) { |
69 | 0 | throw new IllegalArgumentException("An invalid blank id was passed to delete a Pessimistic Lock."); |
70 | |
} |
71 | 0 | Map<String,Object> primaryKeys = new HashMap<String,Object>(); |
72 | 0 | primaryKeys.put(KRADPropertyConstants.ID, Long.valueOf(id)); |
73 | 0 | PessimisticLock lock = (PessimisticLock) getBusinessObjectService().findByPrimaryKey(PessimisticLock.class, primaryKeys); |
74 | 0 | if (ObjectUtils.isNull(lock)) { |
75 | 0 | throw new IllegalArgumentException("Pessimistic Lock with id " + id + " cannot be found in the database."); |
76 | |
} |
77 | 0 | Person user = GlobalVariables.getUserSession().getPerson(); |
78 | 0 | if ( (!lock.isOwnedByUser(user)) && (!isPessimisticLockAdminUser(user)) ) { |
79 | 0 | throw new AuthorizationException(user.getName(),"delete", "Pessimistick Lock (id " + id + ")"); |
80 | |
} |
81 | 0 | delete(lock); |
82 | 0 | } |
83 | |
|
84 | |
private void delete(PessimisticLock lock) { |
85 | 0 | if ( LOG.isDebugEnabled() ) { |
86 | 0 | LOG.debug("Deleting lock: " + lock); |
87 | |
} |
88 | 0 | getBusinessObjectService().delete(lock); |
89 | 0 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public PessimisticLock generateNewLock(String documentNumber) { |
95 | 0 | return generateNewLock(documentNumber, GlobalVariables.getUserSession().getPerson()); |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public PessimisticLock generateNewLock(String documentNumber, String lockDescriptor) { |
102 | 0 | return generateNewLock(documentNumber, lockDescriptor, GlobalVariables.getUserSession().getPerson()); |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public PessimisticLock generateNewLock(String documentNumber, Person user) { |
109 | 0 | return generateNewLock(documentNumber, PessimisticLock.DEFAULT_LOCK_DESCRIPTOR, user); |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public PessimisticLock generateNewLock(String documentNumber, String lockDescriptor, Person user) { |
116 | 0 | PessimisticLock lock = new PessimisticLock(documentNumber, lockDescriptor, user); |
117 | 0 | lock = save(lock); |
118 | 0 | if ( LOG.isDebugEnabled() ) { |
119 | 0 | LOG.debug("Generated new lock: " + lock); |
120 | |
} |
121 | 0 | return lock; |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
@SuppressWarnings("unchecked") |
128 | |
public List<PessimisticLock> getPessimisticLocksForDocument(String documentNumber) { |
129 | 0 | Map fieldValues = new HashMap(); |
130 | 0 | fieldValues.put(KRADPropertyConstants.DOCUMENT_NUMBER, documentNumber); |
131 | 0 | return (List<PessimisticLock>) getBusinessObjectService().findMatching(PessimisticLock.class, fieldValues); |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public boolean isPessimisticLockAdminUser(Person user) { |
138 | 0 | return getPermissionService().isAuthorized( user.getPrincipalId(), KRADConstants.KRAD_NAMESPACE, PermissionNames.ADMIN_PESSIMISTIC_LOCKING, |
139 | |
Collections.<String, String>emptyMap(), Collections.<String, String>emptyMap() ); |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public void releaseAllLocksForUser(List<PessimisticLock> locks, Person user) { |
146 | 0 | for (Iterator<PessimisticLock> iterator = locks.iterator(); iterator.hasNext();) { |
147 | 0 | PessimisticLock lock = (PessimisticLock) iterator.next(); |
148 | 0 | if (lock.isOwnedByUser(user)) { |
149 | 0 | delete(lock); |
150 | |
} |
151 | 0 | } |
152 | 0 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
public void releaseAllLocksForUser(List<PessimisticLock> locks, Person user, String lockDescriptor) { |
158 | 0 | for (Iterator<PessimisticLock> iterator = locks.iterator(); iterator.hasNext();) { |
159 | 0 | PessimisticLock lock = (PessimisticLock) iterator.next(); |
160 | 0 | if ( (lock.isOwnedByUser(user)) && (lockDescriptor.equals(lock.getLockDescriptor())) ) { |
161 | 0 | delete(lock); |
162 | |
} |
163 | 0 | } |
164 | 0 | } |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
public PessimisticLock save(PessimisticLock lock) { |
170 | 0 | if ( LOG.isDebugEnabled() ) { |
171 | 0 | LOG.debug("Saving lock: " + lock); |
172 | |
} |
173 | 0 | return (PessimisticLock)getBusinessObjectService().save(lock); |
174 | |
} |
175 | |
|
176 | |
public BusinessObjectService getBusinessObjectService() { |
177 | 0 | return this.businessObjectService; |
178 | |
} |
179 | |
|
180 | |
public void setBusinessObjectService(BusinessObjectService businessObjectService) { |
181 | 0 | this.businessObjectService = businessObjectService; |
182 | 0 | } |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public Set getDocumentActions(Document document, Person user, Set<String> documentActions){ |
190 | 0 | if(documentActions.contains(KRADConstants.KUALI_ACTION_CAN_CANCEL) && !hasPreRouteEditAuthorization(document, user) ){ |
191 | 0 | documentActions.remove(KRADConstants.KUALI_ACTION_CAN_CANCEL); |
192 | |
} |
193 | 0 | if(documentActions.contains(KRADConstants.KUALI_ACTION_CAN_SAVE) && !hasPreRouteEditAuthorization(document, user)){ |
194 | 0 | documentActions.remove(KRADConstants.KUALI_ACTION_CAN_SAVE); |
195 | |
} |
196 | 0 | if(documentActions.contains(KRADConstants.KUALI_ACTION_CAN_ROUTE) && !hasPreRouteEditAuthorization(document, user)){ |
197 | 0 | documentActions.remove(KRADConstants.KUALI_ACTION_CAN_ROUTE); |
198 | |
} |
199 | 0 | if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_BLANKET_APPROVE) && !hasPreRouteEditAuthorization(document, user)){ |
200 | 0 | documentActions.remove(KRADConstants.KUALI_ACTION_CAN_BLANKET_APPROVE); |
201 | |
} |
202 | 0 | return documentActions; |
203 | |
} |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
protected boolean hasPreRouteEditAuthorization(Document document, Person user) { |
217 | 0 | if (document.getPessimisticLocks().isEmpty()) { |
218 | 0 | return true; |
219 | |
} |
220 | 0 | for (Iterator iterator = document.getPessimisticLocks().iterator(); iterator.hasNext();) { |
221 | 0 | PessimisticLock lock = (PessimisticLock) iterator.next(); |
222 | 0 | if (lock.isOwnedByUser(user)) { |
223 | 0 | return true; |
224 | |
} |
225 | 0 | } |
226 | 0 | return false; |
227 | |
} |
228 | |
|
229 | |
|
230 | |
protected boolean usesPessimisticLocking(Document document) { |
231 | 0 | return getDataDictionaryService().getDataDictionary().getDocumentEntry(document.getClass().getName()).getUsePessimisticLocking(); |
232 | |
} |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
public void establishWorkflowPessimisticLocking(Document document) { |
242 | 0 | PessimisticLock lock = createNewPessimisticLock(document, new HashMap(), getWorkflowPessimisticLockOwnerUser()); |
243 | 0 | document.addPessimisticLock(lock); |
244 | 0 | } |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
public void releaseWorkflowPessimisticLocking(Document document) { |
253 | 0 | releaseAllLocksForUser(document.getPessimisticLocks(), getWorkflowPessimisticLockOwnerUser()); |
254 | 0 | document.refreshPessimisticLocks(); |
255 | 0 | } |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
protected Person getWorkflowPessimisticLockOwnerUser() { |
267 | 0 | String networkId = KRADConstants.SYSTEM_USER; |
268 | 0 | return getPersonService().getPersonByPrincipalName(networkId); |
269 | |
} |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
public Map establishLocks(Document document, Map editMode, Person user) { |
285 | 0 | Map editModeMap = new HashMap(); |
286 | |
|
287 | 0 | List<String> givenUserLockDescriptors = new ArrayList<String>(); |
288 | |
|
289 | 0 | Map<String,Set<Person>> lockDescriptorUsers = new HashMap<String,Set<Person>>(); |
290 | |
|
291 | |
|
292 | 0 | for (PessimisticLock lock : document.getPessimisticLocks()) { |
293 | 0 | if (lock.isOwnedByUser(user)) { |
294 | |
|
295 | 0 | givenUserLockDescriptors.add(lock.getLockDescriptor()); |
296 | |
} else { |
297 | |
|
298 | 0 | if (!lockDescriptorUsers.containsKey(lock.getLockDescriptor())) { |
299 | 0 | lockDescriptorUsers.put(lock.getLockDescriptor(), new HashSet<Person>()); |
300 | |
} |
301 | 0 | ((Set<Person>) lockDescriptorUsers.get(lock.getLockDescriptor())).add(lock.getOwnedByUser()); |
302 | |
} |
303 | |
} |
304 | |
|
305 | |
|
306 | 0 | for (String givenUserLockDescriptor : givenUserLockDescriptors) { |
307 | 0 | if ( (lockDescriptorUsers.containsKey(givenUserLockDescriptor)) && (lockDescriptorUsers.get(givenUserLockDescriptor).size() > 0) ) { |
308 | 0 | Set<Person> users = lockDescriptorUsers.get(givenUserLockDescriptor); |
309 | 0 | if ( (users.size() != 1) || (!getWorkflowPessimisticLockOwnerUser().getPrincipalId().equals(users.iterator().next().getPrincipalId())) ) { |
310 | 0 | String descriptorText = (document.useCustomLockDescriptors()) ? " using lock descriptor '" + givenUserLockDescriptor + "'" : ""; |
311 | 0 | String errorMsg = "Found an invalid lock status on document number " + document.getDocumentNumber() + "with current user and other user both having locks" + descriptorText + " concurrently"; |
312 | 0 | LOG.debug(errorMsg); |
313 | 0 | throw new PessimisticLockingException(errorMsg); |
314 | |
} |
315 | 0 | } |
316 | |
} |
317 | |
|
318 | |
|
319 | 0 | if (givenUserLockDescriptors.isEmpty()) { |
320 | |
|
321 | 0 | if (lockDescriptorUsers.isEmpty()) { |
322 | |
|
323 | 0 | if (isLockRequiredByUser(document, editMode, user)) { |
324 | 0 | document.addPessimisticLock(createNewPessimisticLock(document, editMode, user)); |
325 | |
} |
326 | 0 | editModeMap.putAll(editMode); |
327 | |
} else { |
328 | |
|
329 | 0 | if (document.useCustomLockDescriptors()) { |
330 | |
|
331 | 0 | String customLockDescriptor = document.getCustomLockDescriptor(user); |
332 | 0 | if (lockDescriptorUsers.containsKey(customLockDescriptor)) { |
333 | |
|
334 | 0 | editModeMap = getEditModeWithEditableModesRemoved(editMode); |
335 | |
} else { |
336 | |
|
337 | 0 | if (isLockRequiredByUser(document, editMode, user)) { |
338 | 0 | document.addPessimisticLock(createNewPessimisticLock(document, editMode, user)); |
339 | |
} |
340 | 0 | editModeMap.putAll(editMode); |
341 | |
} |
342 | 0 | } else { |
343 | 0 | editModeMap = getEditModeWithEditableModesRemoved(editMode); |
344 | |
} |
345 | |
} |
346 | |
} else { |
347 | |
|
348 | 0 | if (document.useCustomLockDescriptors()) { |
349 | |
|
350 | 0 | String customLockDescriptor = document.getCustomLockDescriptor(user); |
351 | 0 | if (givenUserLockDescriptors.contains(customLockDescriptor)) { |
352 | |
|
353 | 0 | editModeMap.putAll(editMode); |
354 | |
} else { |
355 | |
|
356 | 0 | if (lockDescriptorUsers.containsKey(customLockDescriptor)) { |
357 | |
|
358 | 0 | editModeMap = getEditModeWithEditableModesRemoved(editMode); |
359 | |
} else { |
360 | |
|
361 | 0 | if (isLockRequiredByUser(document, editMode, user)) { |
362 | 0 | document.addPessimisticLock(createNewPessimisticLock(document, editMode, user)); |
363 | |
} |
364 | 0 | editModeMap.putAll(editMode); |
365 | |
} |
366 | |
} |
367 | 0 | } else { |
368 | |
|
369 | 0 | editModeMap.putAll(editMode); |
370 | |
} |
371 | |
} |
372 | |
|
373 | 0 | return editModeMap; |
374 | |
} |
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
protected boolean isLockRequiredByUser(Document document, Map editMode, Person user) { |
389 | |
|
390 | 0 | for (Iterator iterator = editMode.entrySet().iterator(); iterator.hasNext();) { |
391 | 0 | Map.Entry entry = (Map.Entry) iterator.next(); |
392 | 0 | if (isEntryEditMode(entry)) { |
393 | 0 | return true; |
394 | |
} |
395 | 0 | } |
396 | 0 | return false; |
397 | |
} |
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
protected Map getEditModeWithEditableModesRemoved(Map currentEditMode) { |
411 | 0 | Map editModeMap = new HashMap(); |
412 | 0 | for (Iterator iterator = currentEditMode.entrySet().iterator(); iterator.hasNext();) { |
413 | 0 | Map.Entry entry = (Map.Entry) iterator.next(); |
414 | 0 | if (isEntryEditMode(entry)) { |
415 | 0 | editModeMap.putAll(getEntryEditModeReplacementMode(entry)); |
416 | |
} else { |
417 | 0 | editModeMap.put(entry.getKey(), entry.getValue()); |
418 | |
} |
419 | 0 | } |
420 | 0 | return editModeMap; |
421 | |
} |
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
protected boolean isEntryEditMode(Map.Entry entry) { |
434 | |
|
435 | 0 | if (AuthorizationConstants.EditMode.FULL_ENTRY.equals(entry.getKey())) { |
436 | 0 | String fullEntryEditModeValue = (String)entry.getValue(); |
437 | 0 | return ( StringUtils.equalsIgnoreCase(KRADConstants.KUALI_DEFAULT_TRUE_VALUE, fullEntryEditModeValue) ); |
438 | |
} |
439 | 0 | return false; |
440 | |
} |
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
protected Map getEntryEditModeReplacementMode(Map.Entry entry) { |
449 | 0 | Map editMode = new HashMap(); |
450 | 0 | editMode.put(AuthorizationConstants.EditMode.VIEW_ONLY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE); |
451 | 0 | return editMode; |
452 | |
} |
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
protected PessimisticLock createNewPessimisticLock(Document document, Map editMode, Person user) { |
468 | 0 | if (document.useCustomLockDescriptors()) { |
469 | 0 | return generateNewLock(document.getDocumentNumber(), document.getCustomLockDescriptor(user), user); |
470 | |
} else { |
471 | 0 | return generateNewLock(document.getDocumentNumber(), user); |
472 | |
} |
473 | |
} |
474 | |
|
475 | |
public PersonService getPersonService() { |
476 | 0 | if ( personService == null ) { |
477 | 0 | personService = KimApiServiceLocator.getPersonService(); |
478 | |
} |
479 | 0 | return personService; |
480 | |
} |
481 | |
|
482 | |
public DataDictionaryService getDataDictionaryService() { |
483 | 0 | if ( dataDictionaryService == null ) { |
484 | 0 | dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService(); |
485 | |
} |
486 | 0 | return dataDictionaryService; |
487 | |
} |
488 | |
|
489 | |
public PermissionService getPermissionService() { |
490 | 0 | if ( permissionService == null ) { |
491 | 0 | permissionService = KimApiServiceLocator.getPermissionService(); |
492 | |
} |
493 | 0 | return permissionService; |
494 | |
} |
495 | |
|
496 | |
|
497 | |
|
498 | |
} |
499 | |
|