View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.service.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.ojb.broker.OptimisticLockException;
29  import org.kuali.rice.core.api.criteria.QueryByCriteria;
30  import org.kuali.rice.core.api.util.RiceConstants;
31  import org.kuali.rice.kim.api.KimConstants.PermissionNames;
32  import org.kuali.rice.kim.api.identity.Person;
33  import org.kuali.rice.kim.api.identity.PersonService;
34  import org.kuali.rice.kim.api.permission.PermissionService;
35  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
36  import org.kuali.rice.kns.authorization.AuthorizationConstants;
37  import org.kuali.rice.krad.data.DataObjectService;
38  import org.kuali.rice.krad.document.Document;
39  import org.kuali.rice.krad.document.authorization.PessimisticLock;
40  import org.kuali.rice.krad.exception.AuthorizationException;
41  import org.kuali.rice.krad.exception.PessimisticLockingException;
42  import org.kuali.rice.krad.service.DataDictionaryService;
43  import org.kuali.rice.krad.service.PessimisticLockService;
44  import org.kuali.rice.krad.util.GlobalVariables;
45  import org.kuali.rice.krad.util.KRADConstants;
46  import org.kuali.rice.krad.util.KRADPropertyConstants;
47  import org.springframework.beans.factory.annotation.Required;
48  import org.springframework.transaction.annotation.Transactional;
49  
50  /**
51   * Service implementation for pessimistic locking
52   *
53   * @author Kuali Rice Team (rice.collab@kuali.org)
54   */
55  @Transactional
56  public class PessimisticLockServiceImpl implements PessimisticLockService {
57      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PessimisticLockServiceImpl.class);
58  
59      protected DataObjectService dataObjectService;
60      protected DataDictionaryService dataDictionaryService;
61      protected PermissionService permissionService;
62      protected PersonService personService;
63  
64  	/**
65       * @see org.kuali.rice.krad.service.PessimisticLockService#delete(java.lang.String)
66       */
67      @Override
68  	public void delete(String id) {
69          if (StringUtils.isBlank(id)) {
70              throw new IllegalArgumentException("An invalid blank id was passed to delete a Pessimistic Lock.");
71          }
72          PessimisticLock lock = (PessimisticLock) dataObjectService.find(PessimisticLock.class, Long.valueOf(id));
73          if ( lock == null ) {
74              throw new IllegalArgumentException("Pessimistic Lock with id " + id + " cannot be found in the database.");
75          }
76          Person user = GlobalVariables.getUserSession().getPerson();
77          if ( (!lock.isOwnedByUser(user)) && (!isPessimisticLockAdminUser(user)) ) {
78              throw new AuthorizationException(user.getName(),"delete", "Pessimistick Lock (id " + id + ")");
79          }
80          delete(lock);
81      }
82  
83      private void delete(PessimisticLock lock) {
84      	if ( LOG.isDebugEnabled() ) {
85      		LOG.debug("Deleting lock: " + lock);
86      	}
87          dataObjectService.delete(lock);
88      }
89  
90      /**
91       * @see org.kuali.rice.krad.service.PessimisticLockService#generateNewLock(String)
92       */
93      @Override
94  	public PessimisticLock generateNewLock(String documentNumber) {
95          return generateNewLock(documentNumber, GlobalVariables.getUserSession().getPerson());
96      }
97  
98      /**
99       * @see org.kuali.rice.krad.service.PessimisticLockService#generateNewLock(java.lang.String)
100      */
101     @Override
102 	public PessimisticLock generateNewLock(String documentNumber, String lockDescriptor) {
103         return generateNewLock(documentNumber, lockDescriptor, GlobalVariables.getUserSession().getPerson());
104     }
105 
106     /**
107      * @see org.kuali.rice.krad.service.PessimisticLockService#generateNewLock(java.lang.String, org.kuali.rice.kim.api.identity.Person)
108      */
109     @Override
110 	public PessimisticLock generateNewLock(String documentNumber, Person user) {
111         return generateNewLock(documentNumber, PessimisticLock.DEFAULT_LOCK_DESCRIPTOR, user);
112     }
113 
114     /**
115      * @see org.kuali.rice.krad.service.PessimisticLockService#generateNewLock(java.lang.String, java.lang.String, org.kuali.rice.kim.api.identity.Person)
116      */
117     @Override
118 	public PessimisticLock generateNewLock(String documentNumber, String lockDescriptor, Person user) {
119         PessimisticLock lock = new PessimisticLock(documentNumber, lockDescriptor, user, GlobalVariables.getUserSession());
120         lock = save(lock);
121         if ( LOG.isDebugEnabled() ) {
122         	LOG.debug("Generated new lock: " + lock);
123         }
124         return lock;
125     }
126 
127     /**
128      * @see org.kuali.rice.krad.service.PessimisticLockService#getPessimisticLocksForDocument(java.lang.String)
129      */
130     @Override
131 	public List<PessimisticLock> getPessimisticLocksForDocument(String documentNumber) {
132         return new ArrayList<PessimisticLock>( dataObjectService.findMatching(PessimisticLock.class,
133         		QueryByCriteria.Builder.forAttribute(KRADPropertyConstants.DOCUMENT_NUMBER, documentNumber)
134         		.build()).getResults() );
135     }
136 
137     /**
138      * @see org.kuali.rice.krad.service.PessimisticLockService#getPessimisticLocksForSession(java.lang.String)
139      */
140     @Override
141 	public List<PessimisticLock> getPessimisticLocksForSession(String sessionId) {
142         return new ArrayList<PessimisticLock>( dataObjectService.findMatching(PessimisticLock.class,
143         		QueryByCriteria.Builder.forAttribute(KRADPropertyConstants.SESSION_ID, sessionId)
144         		.build()).getResults() );
145     }
146 
147     /**
148      * @see org.kuali.rice.krad.service.PessimisticLockService#isPessimisticLockAdminUser(org.kuali.rice.kim.api.identity.Person)
149      */
150     @Override
151 	public boolean isPessimisticLockAdminUser(Person user) {
152     	return getPermissionService().isAuthorized( user.getPrincipalId(), KRADConstants.KNS_NAMESPACE, PermissionNames.ADMIN_PESSIMISTIC_LOCKING,
153                 Collections.<String, String>emptyMap() );
154     }
155 
156     /**
157      * @see org.kuali.rice.krad.service.PessimisticLockService#releaseAllLocksForUser(java.util.List, org.kuali.rice.kim.api.identity.Person)
158      */
159     @Override
160 	public void releaseAllLocksForUser(List<PessimisticLock> locks, Person user) {
161         for (Iterator<PessimisticLock> iterator = locks.iterator(); iterator.hasNext();) {
162             PessimisticLock lock = (PessimisticLock) iterator.next();
163             if (lock.isOwnedByUser(user)) {
164                 try {
165                     delete(lock);
166                 } catch ( RuntimeException ex ) {
167                     if ( ex.getCause() instanceof OptimisticLockException) {
168                         LOG.warn( "Suppressing Optimistic Lock Exception. Document Num: " +  lock.getDocumentNumber());
169                     } else {
170                         throw ex;
171                     }
172                 }
173             }
174         }
175     }
176 
177     /**
178      * @see org.kuali.rice.krad.service.PessimisticLockService#releaseAllLocksForUser(java.util.List, org.kuali.rice.kim.api.identity.Person, java.lang.String)
179      */
180     @Override
181 	public void releaseAllLocksForUser(List<PessimisticLock> locks, Person user, String lockDescriptor) {
182         for (Iterator<PessimisticLock> iterator = locks.iterator(); iterator.hasNext();) {
183             PessimisticLock lock = (PessimisticLock) iterator.next();
184             if ( (lock.isOwnedByUser(user)) && (lockDescriptor.equals(lock.getLockDescriptor())) ) {
185                 try {
186                     delete(lock);
187                 } catch ( RuntimeException ex ) {
188                     if ( ex.getCause() instanceof OptimisticLockException ) {
189                         LOG.warn( "Suppressing Optimistic Lock Exception. Document Num: " +  lock.getDocumentNumber());
190                     } else {
191                         throw ex;
192                     }
193                 }
194             }
195         }
196     }
197 
198     /**
199      * @see org.kuali.rice.krad.service.PessimisticLockService#save(org.kuali.rice.krad.document.authorization.PessimisticLock)
200      */
201     @Override
202 	public PessimisticLock save(PessimisticLock lock) {
203     	if ( LOG.isDebugEnabled() ) {
204     		LOG.debug("Saving lock: " + lock);
205     	}
206         return dataObjectService.save(lock);
207     }
208 
209     /**
210      * @param document
211      * @param user
212      * @return Set of actions are permitted the given user on the given document
213      */
214     @Override
215 	public Set getDocumentActions(Document document, Person user, Set<String> documentActions){
216     	if(documentActions.contains(KRADConstants.KUALI_ACTION_CAN_CANCEL) && !hasPreRouteEditAuthorization(document, user) ){
217     		documentActions.remove(KRADConstants.KUALI_ACTION_CAN_CANCEL);
218     	}
219     	if(documentActions.contains(KRADConstants.KUALI_ACTION_CAN_SAVE)  && !hasPreRouteEditAuthorization(document, user)){
220     		documentActions.remove(KRADConstants.KUALI_ACTION_CAN_SAVE);
221     	}
222         if(documentActions.contains(KRADConstants.KUALI_ACTION_CAN_ROUTE) && !hasPreRouteEditAuthorization(document, user)){
223         	documentActions.remove(KRADConstants.KUALI_ACTION_CAN_ROUTE);
224         }
225         if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_BLANKET_APPROVE) && !hasPreRouteEditAuthorization(document, user)){
226         	documentActions.remove(KRADConstants.KUALI_ACTION_CAN_BLANKET_APPROVE);
227         }
228     	return documentActions;
229     }
230 
231 
232     /**
233      * This method checks to see that the given user has a lock on the document and return true if one is found.
234      *
235      * @param document - document to check
236      * @param user - current user
237      * @return true if the document is using Pessimistic Locking, the user has initiate authorization (see
238      *         {@link #hasInitiateAuthorization(Document, Person)}), and the document has a lock owned by the given
239      *         user. If the document is not using Pessimistic Locking the value returned will be that returned by
240      *         {@link #hasInitiateAuthorization(Document, Person)}.
241      */
242     protected boolean hasPreRouteEditAuthorization(Document document, Person user) {
243     	if (document.getPessimisticLocks().isEmpty()) {
244     		return true;
245     	}
246     	for (Iterator<PessimisticLock> iterator = document.getPessimisticLocks().iterator(); iterator.hasNext();) {
247     		PessimisticLock lock = iterator.next();
248     		if (lock.isOwnedByUser(user)) {
249     			return true;
250             }
251         }
252         return false;
253     }
254 
255 
256     protected boolean usesPessimisticLocking(Document document) {
257         return getDataDictionaryService().getDataDictionary().getDocumentEntry(document.getClass().getName()).getUsePessimisticLocking();
258     }
259 
260 
261     /**
262      * This method creates a new {@link PessimisticLock} when Workflow processing requires one
263      *
264      * @param document - the document to create the lock against and add the lock to
265      * @see org.kuali.rice.kns.document.authorization.DocumentAuthorizer#establishWorkflowPessimisticLocking(org.kuali.rice.krad.document.Document)
266      */
267     @Override
268 	public void establishWorkflowPessimisticLocking(Document document) {
269         PessimisticLock lock = createNewPessimisticLock(document, new HashMap(), getWorkflowPessimisticLockOwnerUser());
270         document.addPessimisticLock(lock);
271     }
272 
273     /**
274      * This method releases locks created via the {@link #establishWorkflowPessimisticLocking(Document)} method for the given document
275      *
276      * @param document - document to release locks from
277      * @see org.kuali.rice.kns.document.authorization.DocumentAuthorizer#releaseWorkflowPessimisticLocking(org.kuali.rice.krad.document.Document)
278      */
279     @Override
280 	public void releaseWorkflowPessimisticLocking(Document document) {
281         releaseAllLocksForUser(document.getPessimisticLocks(), getWorkflowPessimisticLockOwnerUser());
282         document.refreshPessimisticLocks();
283     }
284 
285     /**
286      * This method identifies the user that should be used to create and clear {@link PessimisticLock} objects required by
287      * Workflow.<br>
288      * <br>
289      * The default is the Kuali system user defined by {@link RiceConstants#SYSTEM_USER}. This method can be overriden by
290      * implementing documents if another user is needed.
291      *
292      * @return a valid {@link Person} object
293      */
294     protected Person getWorkflowPessimisticLockOwnerUser() {
295         String networkId = KRADConstants.SYSTEM_USER;
296         return getPersonService().getPersonByPrincipalName(networkId);
297     }
298 
299     /**
300      * This implementation will check the given document, editMode map, and user object to verify Pessimistic Locking. If the
301      * given edit mode map contains an 'entry type' edit mode then the system will check the locks already in existence on
302      * the document. If a valid lock for the given user is found the system will return the given edit mode map. If a valid
303      * lock is found but is owned by another user the edit mode map returned will have any 'entry type' edit modes removed. If the
304      * given document has no locks and the edit mode map passed in has at least one 'entry type' mode then a new
305      * {@link PessimisticLock} object will be created and set on the document for the given user.<br>
306      * <br>
307      * NOTE: This method is only called if the document uses pessimistic locking as described in the data dictionary file.
308      *
309      * @see org.kuali.rice.kns.document.authorization.DocumentAuthorizer#establishLocks(org.kuali.rice.krad.document.Document,
310      *      java.util.Map, org.kuali.rice.kim.api.identity.Person)
311      */
312     @Override
313 	public Map establishLocks(Document document, Map editMode, Person user) {
314         Map editModeMap = new HashMap();
315         // givenUserLockDescriptors is a list of lock descriptors currently held on the document by the given user
316         List<String> givenUserLockDescriptors = new ArrayList<String>();
317         // lockDescriptorUsers is a map with lock descriptors as keys and users other than the given user who hold a lock of each descriptor
318         Map<String,Set<Person>> lockDescriptorUsers = new HashMap<String,Set<Person>>();
319 
320         // build the givenUserLockDescriptors set and the lockDescriptorUsers map
321         for (PessimisticLock lock : document.getPessimisticLocks()) {
322             if (lock.isOwnedByUser(user)) {
323                 // lock is owned by given user
324                 givenUserLockDescriptors.add(lock.getLockDescriptor());
325             } else {
326                 // lock is not owned by the given user
327                 if (!lockDescriptorUsers.containsKey(lock.getLockDescriptor())) {
328                     lockDescriptorUsers.put(lock.getLockDescriptor(), new HashSet<Person>());
329                 }
330                 ((Set<Person>) lockDescriptorUsers.get(lock.getLockDescriptor())).add(lock.getOwnedByUser());
331             }
332         }
333 
334         // verify that no locks held by current user exist for any other user
335         for (String givenUserLockDescriptor : givenUserLockDescriptors) {
336             if ( (lockDescriptorUsers.containsKey(givenUserLockDescriptor)) && (lockDescriptorUsers.get(givenUserLockDescriptor).size() > 0) ) {
337                 Set<Person> users = lockDescriptorUsers.get(givenUserLockDescriptor);
338                 if ( (users.size() != 1) || (!getWorkflowPessimisticLockOwnerUser().getPrincipalId().equals(users.iterator().next().getPrincipalId())) ) {
339                     String descriptorText = (document.useCustomLockDescriptors()) ? " using lock descriptor '" + givenUserLockDescriptor + "'" : "";
340                     String errorMsg = "Found an invalid lock status on document number " + document.getDocumentNumber() + "with current user and other user both having locks" + descriptorText + " concurrently";
341                     LOG.debug(errorMsg);
342                     throw new PessimisticLockingException(errorMsg);
343                 }
344             }
345         }
346 
347         // check to see if the given user has any locks in the system at all
348         if (givenUserLockDescriptors.isEmpty()) {
349             // given user has no locks... check for other user locks
350             if (lockDescriptorUsers.isEmpty()) {
351                 // no other user has any locks... set up locks for given user if user has edit privileges
352                 if (isLockRequiredByUser(document, editMode, user)) {
353                     document.addPessimisticLock(createNewPessimisticLock(document, editMode, user));
354                 }
355                 editModeMap.putAll(editMode);
356             } else {
357                 // at least one other user has at least one other lock... adjust edit mode for read only
358                 if (document.useCustomLockDescriptors()) {
359                     // check to see if the custom lock descriptor is already in use
360                     String customLockDescriptor = document.getCustomLockDescriptor(user);
361                     if (lockDescriptorUsers.containsKey(customLockDescriptor)) {
362                         // at least one other user has this descriptor locked... remove editable edit modes
363                         editModeMap = getEditModeWithEditableModesRemoved(editMode);
364                     } else {
365                         // no other user has a lock with this descriptor
366                         if (isLockRequiredByUser(document, editMode, user)) {
367                             document.addPessimisticLock(createNewPessimisticLock(document, editMode, user));
368                         }
369                         editModeMap.putAll(editMode);
370                     }
371                 } else {
372                     editModeMap = getEditModeWithEditableModesRemoved(editMode);
373                 }
374             }
375         } else {
376             // given user already has at least one lock descriptor
377             if (document.useCustomLockDescriptors()) {
378                 // get the custom lock descriptor and check to see if if the given user has a lock with that descriptor
379                 String customLockDescriptor = document.getCustomLockDescriptor(user);
380                 if (givenUserLockDescriptors.contains(customLockDescriptor)) {
381                     // user already has lock that is required
382                     editModeMap.putAll(editMode);
383                 } else {
384                     // user does not have lock for descriptor required
385                     if (lockDescriptorUsers.containsKey(customLockDescriptor)) {
386                         // another user has the lock descriptor that the given user requires... disallow lock and alter edit modes to have read only
387                         editModeMap = getEditModeWithEditableModesRemoved(editMode);
388                     } else {
389                         // no other user has a lock with this descriptor... check if this user needs a lock
390                         if (isLockRequiredByUser(document, editMode, user)) {
391                             document.addPessimisticLock(createNewPessimisticLock(document, editMode, user));
392                         }
393                         editModeMap.putAll(editMode);
394                     }
395                 }
396             } else {
397                 // user already has lock and no descriptors are being used... use the existing edit modes
398                 editModeMap.putAll(editMode);
399             }
400         }
401 
402         return editModeMap;
403     }
404 
405     /**
406      * This method is used to check if the given parameters warrant a new lock to be created for the given user. This method
407      * utilizes the {@link #isEntryEditMode(java.util.Map.Entry)} method.
408      *
409      * @param document -
410      *            document to verify lock creation against
411      * @param editMode -
412      *            edit modes list to check for 'entry type' edit modes
413      * @param user -
414      *            user the lock will be 'owned' by
415      * @return true if the given edit mode map has at least one 'entry type' edit mode... false otherwise
416      */
417     protected boolean isLockRequiredByUser(Document document, Map editMode, Person user) {
418         // check for entry edit mode
419         for (Iterator iterator = editMode.entrySet().iterator(); iterator.hasNext();) {
420             Map.Entry entry = (Map.Entry) iterator.next();
421             if (isEntryEditMode(entry)) {
422                 return true;
423             }
424         }
425         return false;
426     }
427 
428    /**
429      * This method is used to remove edit modes from the given map that allow the user to edit data on the document. This
430      * method utilizes the {@link #isEntryEditMode(java.util.Map.Entry)} method to identify if an edit mode is defined as an
431      * 'entry type' edit mode. It also uses the {@link #getEntryEditModeReplacementMode(java.util.Map.Entry)} method to replace
432      * any 'entry type' edit modes it finds.
433      *
434      * @param currentEditMode -
435      *            current set of edit modes the user has assigned to them
436      * @return an adjusted edit mode map where 'entry type' edit modes have been removed or replaced using the
437      *         {@link #getEntryEditModeReplacementMode} method
438      */
439     protected Map getEditModeWithEditableModesRemoved(Map currentEditMode) {
440         Map editModeMap = new HashMap();
441         for (Iterator iterator = currentEditMode.entrySet().iterator(); iterator.hasNext();) {
442             Map.Entry entry = (Map.Entry) iterator.next();
443             if (isEntryEditMode(entry)) {
444                 editModeMap.putAll(getEntryEditModeReplacementMode(entry));
445             } else {
446                 editModeMap.put(entry.getKey(), entry.getValue());
447             }
448         }
449         return editModeMap;
450     }
451 
452     /**
453      * This method is used to check if the given {@link Map.Entry} is an 'entry type' edit mode and that the value is set to
454      * signify that this user has that edit mode available to them
455      *
456      * @param entry -
457      *            the {@link Map.Entry} object that contains an edit mode such as the ones returned but
458      *            {@link #getEditMode(Document, Person)}
459      * @return true if the given entry has a key signifying an 'entry type' edit mode and the value is equal to
460      *         {@link #EDIT_MODE_DEFAULT_TRUE_VALUE}... false if not
461      */
462     protected boolean isEntryEditMode(Map.Entry entry) {
463     	// check for FULL_ENTRY edit mode set to default true value
464     	if (AuthorizationConstants.EditMode.FULL_ENTRY.equals(entry.getKey())) {
465     		String fullEntryEditModeValue = (String)entry.getValue();
466     		return ( StringUtils.equalsIgnoreCase(KRADConstants.KUALI_DEFAULT_TRUE_VALUE, fullEntryEditModeValue) );
467     	}
468     	return false;
469     }
470 
471     /**
472      * This method is used to return values needed to replace the given 'entry type' edit mode {@link Map.Entry} with one that will not allow the user to enter data on the document
473      *
474      * @param entry - the current 'entry type' edit mode to replace
475      * @return a Map of edit modes that will be used to replace this edit mode (represented by the given entry parameter)
476      */
477     protected Map getEntryEditModeReplacementMode(Map.Entry entry) {
478         Map editMode = new HashMap();
479         editMode.put(AuthorizationConstants.EditMode.VIEW_ONLY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
480         return editMode;
481     }
482 
483     /**
484      * This method creates a new {@link PessimisticLock} object using the given document and user. If the document's
485      * useCustomLockDescriptors() method returns true then the new lock will also have a custom lock descriptor
486      * value set to the return value of the document's getCustomLockDescriptor(Person) method.
487      *
488      * @param document -
489      *            document to place the lock on
490      * @param editMode -
491      *            current edit modes for given user
492      * @param user -
493      *            user who will 'own' the new lock object
494      * @return the newly created lock object
495      */
496     protected PessimisticLock createNewPessimisticLock(Document document, Map editMode, Person user) {
497         if (document.useCustomLockDescriptors()) {
498             return generateNewLock(document.getDocumentNumber(), document.getCustomLockDescriptor(user), user);
499         } else {
500             return generateNewLock(document.getDocumentNumber(), user);
501         }
502     }
503 
504     public PersonService getPersonService() {
505         if ( personService == null ) {
506             personService = KimApiServiceLocator.getPersonService();
507         }
508         return personService;
509     }
510 
511 	public DataDictionaryService getDataDictionaryService() {
512 		return dataDictionaryService;
513 	}
514 
515 	@Required
516 	public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
517 		this.dataDictionaryService = dataDictionaryService;
518 	}
519 
520 	public PermissionService getPermissionService() {
521         if ( permissionService == null ) {
522         	permissionService = KimApiServiceLocator.getPermissionService();
523         }
524 		return permissionService;
525 	}
526 
527 
528 	@Required
529     public void setDataObjectService(DataObjectService dataObjectService) {
530 		this.dataObjectService = dataObjectService;
531 	}
532 }
533