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