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.document.authorization;
17  
18  import java.sql.Timestamp;
19  
20  import javax.persistence.Column;
21  import javax.persistence.Entity;
22  import javax.persistence.GeneratedValue;
23  import javax.persistence.Id;
24  import javax.persistence.Table;
25  import javax.persistence.Transient;
26  
27  import org.kuali.rice.core.api.CoreApiServiceLocator;
28  import org.kuali.rice.kim.api.identity.Person;
29  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30  import org.kuali.rice.krad.UserSession;
31  import org.kuali.rice.krad.bo.BusinessObject;
32  import org.kuali.rice.krad.bo.PersistableBusinessObjectBaseAdapter;
33  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
34  
35  /**
36   * This is a business object used to lock a document pessimistically.
37   * Pessimistic locking is more strick than optimistic locking and assumes if a
38   * lock exists that a user should only have read-only access to a document. For
39   * more information see documentation pages.
40   * 
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   * 
43   */
44  @Entity
45  @Table(name="KRNS_PESSIMISTIC_LOCK_T")
46  public class PessimisticLock extends PersistableBusinessObjectBaseAdapter implements BusinessObject {
47      
48      private static final long serialVersionUID = -5210762282545093555L;
49      
50      public static final String DEFAULT_LOCK_DESCRIPTOR = null;
51      
52      // id is sequence number and primary key
53      @Id
54      @GeneratedValue(generator="KRNS_LOCK_S")
55      @PortableSequenceGenerator(name="KRNS_LOCK_S")
56  	@Column(name="PESSIMISTIC_LOCK_ID")
57      private Long id;
58      
59      @Column(name="PRNCPL_ID")
60      private String ownedByPrincipalIdentifier;
61      
62      @Column(name="LOCK_DESC_TXT")
63      private String lockDescriptor; // this will be defaulted to the value of DEFAULT_LOCK_DESCRIPTOR constant above
64      
65      @Column(name="GNRT_DT")
66      private Timestamp generatedTimestamp;
67      
68      @Column(name="DOC_HDR_ID")
69      private String documentNumber; // foreign key to document
70  
71      @Column(name="SESN_ID")
72      private String sessionId;
73      
74      @Transient
75      private Person ownedByUser;
76  
77      
78      /**
79       * This constructs an empty lock using the logged in user and default lock descriptor type
80       * but will NOT assign a document number or session id.  Use another constructor.
81       * @deprecated
82       */
83      @Deprecated
84      public PessimisticLock() {}
85      
86      /**
87       * This constructs a lock object using the logged in user and given lock type
88       */
89      public PessimisticLock(String documentNumber, String lockDescriptor, Person user, UserSession userSession) {
90          this.documentNumber = documentNumber;
91          this.ownedByPrincipalIdentifier = user.getPrincipalId();
92          this.lockDescriptor = lockDescriptor;  
93          this.generatedTimestamp = CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp();
94          this.sessionId = userSession.getKualiSessionId();
95      }
96      
97      public boolean isOwnedByUser(Person user) {
98          return user.getPrincipalId().equals(getOwnedByPrincipalIdentifier());
99      }
100     
101     /**
102      * @return the id
103      */
104     public Long getId() {
105         return this.id;
106     }
107 
108     /**
109      * @param id the id to set
110      */
111     public void setId(Long id) {
112         this.id = id;
113     }
114 
115     /**
116      * @return the ownedByPrincipalIdentifier
117      */
118     public String getOwnedByPrincipalIdentifier() {
119         return this.ownedByPrincipalIdentifier;
120     }
121 
122     /**
123      * @param ownedByPrincipalIdentifier the ownedByPrincipalIdentifier to set
124      */
125     public void setOwnedByPrincipalIdentifier(String ownedByPrincipalIdentifier) {
126         this.ownedByPrincipalIdentifier = ownedByPrincipalIdentifier;
127     }
128 
129     /**
130      * @return the lockDescriptor
131      */
132     public String getLockDescriptor() {
133         return this.lockDescriptor;
134     }
135 
136     /**
137      * @param lockDescriptor the lockDescriptor to set
138      */
139     public void setLockDescriptor(String lockDescriptor) {
140         this.lockDescriptor = lockDescriptor;
141     }
142 
143     /**
144      * @return the generatedTimestamp
145      */
146     public Timestamp getGeneratedTimestamp() {
147         return this.generatedTimestamp;
148     }
149 
150     /**
151      * @param generatedTimestamp the generatedTimestamp to set
152      */
153     public void setGeneratedTimestamp(Timestamp generatedTimestamp) {
154         this.generatedTimestamp = generatedTimestamp;
155     }
156 
157     /**
158      * @return the documentNumber
159      */
160     public String getDocumentNumber() {
161         return this.documentNumber;
162     }
163 
164     /**
165      * @param documentNumber the documentNumber to set
166      */
167     public void setDocumentNumber(String documentNumber) {
168         this.documentNumber = documentNumber;
169     }
170 
171     /**
172      * @return the sessionId
173      */
174     public String getSessionId() {
175         return this.sessionId;
176     }
177 
178     /**
179      * @param sessionId the sessionId to set
180      */
181     public void setSessionId(String sessionId) {
182         this.sessionId = sessionId;
183     }
184 
185 
186     /**
187      * @return the ownedByUser
188      */
189     public Person getOwnedByUser() {
190         ownedByUser = KimApiServiceLocator.getPersonService().updatePersonIfNecessary(ownedByPrincipalIdentifier, ownedByUser);
191         return ownedByUser;
192     }
193 
194     /**
195      * @param ownedByUser the ownedByUser to set
196      */
197     public void setOwnedByUser(Person ownedByUser) {
198         this.ownedByUser = ownedByUser;
199     }
200 }
201