View Javadoc

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