View Javadoc
1   /**
2    * Copyright 2005-2015 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.uif.view;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.util.RiceConstants;
20  import org.kuali.rice.core.api.util.RiceKeyConstants;
21  import org.kuali.rice.kim.api.identity.Person;
22  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
23  import org.kuali.rice.krad.document.Document;
24  import org.kuali.rice.krad.document.authorization.PessimisticLock;
25  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
26  import org.kuali.rice.krad.service.PessimisticLockService;
27  import org.kuali.rice.krad.uif.UifConstants;
28  import org.kuali.rice.krad.util.GlobalVariables;
29  import org.kuali.rice.krad.util.KRADConstants;
30  import org.kuali.rice.krad.web.form.TransactionalDocumentFormBase;
31  import org.kuali.rice.krad.uif.util.LifecycleElement;
32  
33  /**
34   * View type for Transactional documents.
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  @BeanTag(name = "transactionalDocumentView", parent = "Uif-TransactionalDocumentView")
39  public class TransactionalDocumentView extends DocumentView {
40  
41      private static final long serialVersionUID = 4375336878804984171L;
42  
43      private PessimisticLockService pessimisticLockService;
44  
45      public TransactionalDocumentView() {
46          super();
47  
48          setViewTypeName(UifConstants.ViewType.TRANSACTIONAL);
49      }
50  
51      /**
52       * {@inheritDoc}
53       *
54       * <p>
55       * Locks the document if pessimistic locking is turned on.
56       * </p>
57       */
58      @Override
59      public void performFinalize(Object model, LifecycleElement parent) {
60          super.performFinalize(model, parent);
61  
62          if (getDocumentEntryForView().getUsePessimisticLocking()) {
63              TransactionalDocumentFormBase form = (TransactionalDocumentFormBase) model;
64  
65              generatePessimisticLockMessages(form);
66              setupPessimisticLockingTimeout(form);
67          }
68      }
69  
70      /**
71       * Generates the messages that warn users that the document has been locked for editing by another user.
72       *
73       * @param form form instance containing the transactional document data
74       */
75      protected void generatePessimisticLockMessages(TransactionalDocumentFormBase form) {
76          Document document = form.getDocument();
77          Person user = GlobalVariables.getUserSession().getPerson();
78  
79          for (PessimisticLock lock : document.getPessimisticLocks()) {
80              if (!lock.isOwnedByUser(user)) {
81                  String lockDescriptor = StringUtils.defaultIfBlank(lock.getLockDescriptor(), "full");
82                  String lockOwner = lock.getOwnedByUser().getName();
83                  String lockTime = RiceConstants.getDefaultTimeFormat().format(lock.getGeneratedTimestamp());
84                  String lockDate = RiceConstants.getDefaultDateFormat().format(lock.getGeneratedTimestamp());
85  
86                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS,
87                          RiceKeyConstants.ERROR_TRANSACTIONAL_LOCKED, lockDescriptor, lockOwner, lockTime, lockDate);
88              }
89          }
90      }
91  
92      /**
93       * Enables the session timeout warning if any pessimistic locks exist.
94       *
95       * @param form form instance containing the transactional document data
96       */
97      protected void setupPessimisticLockingTimeout(TransactionalDocumentFormBase form) {
98          Document document = form.getDocument();
99  
100         if (!document.getPessimisticLocks().isEmpty()) {
101             form.getView().getSessionPolicy().setEnableTimeoutWarning(true);
102         }
103     }
104 
105     protected PessimisticLockService getPessimisticLockService() {
106         if (pessimisticLockService == null) {
107             pessimisticLockService = KRADServiceLocatorWeb.getPessimisticLockService();
108         }
109 
110         return pessimisticLockService;
111     }
112 
113     protected void setPessimisticLockService(PessimisticLockService pessimisticLockService) {
114         this.pessimisticLockService = pessimisticLockService;
115     }
116 
117 }