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