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.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  
44  @Entity
45  @Table(name="KRNS_PESSIMISTIC_LOCK_T")
46  public class PessimisticLock extends PersistableBusinessObjectBase {
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  	@GenericGenerator(name="KRNS_LOCK_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
56  			@Parameter(name="sequence_name",value="KRNS_LOCK_S"),
57  			@Parameter(name="value_column",value="id")
58  	})
59      @Column(name="PESSIMISTIC_LOCK_ID")
60      private Long id;
61      
62      @Column(name="PRNCPL_ID")
63      private String ownedByPrincipalIdentifier;
64      
65      @Column(name="LOCK_DESC_TXT")
66      private String lockDescriptor; // this will be defaulted to the value of DEFAULT_LOCK_DESCRIPTOR constant above
67      
68      @Column(name="GNRT_DT")
69      private Timestamp generatedTimestamp;
70      
71      @Column(name="DOC_HDR_ID")
72      private String documentNumber; // foreign key to document
73  
74      @Column(name="SESN_ID")
75      private String sessionId;
76      
77      @Transient
78      private Person ownedByUser;
79  
80      
81      /**
82       * This constructs an empty lock using the logged in user and default lock descriptor type
83       * but will NOT assign a document number or session id.  Use another constructor.
84       * @deprecated
85       */
86      @Deprecated
87      public PessimisticLock() {}
88      
89      /**
90       * This constructs a lock object using the logged in user and given lock type
91       */
92      public PessimisticLock(String documentNumber, String lockDescriptor, Person user, UserSession userSession) {
93          this.documentNumber = documentNumber;
94          this.ownedByPrincipalIdentifier = user.getPrincipalId();
95          this.lockDescriptor = lockDescriptor;  
96          this.generatedTimestamp = CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp();
97          this.sessionId = userSession.getKualiSessionId();
98      }
99      
100     public boolean isOwnedByUser(Person user) {
101         return user.getPrincipalId().equals(getOwnedByPrincipalIdentifier());
102     }
103     
104     /**
105      * @return the id
106      */
107     public Long getId() {
108         return this.id;
109     }
110 
111     /**
112      * @param id the id to set
113      */
114     public void setId(Long id) {
115         this.id = id;
116     }
117 
118     /**
119      * @return the ownedByPrincipalIdentifier
120      */
121     public String getOwnedByPrincipalIdentifier() {
122         return this.ownedByPrincipalIdentifier;
123     }
124 
125     /**
126      * @param ownedByPrincipalIdentifier the ownedByPrincipalIdentifier to set
127      */
128     public void setOwnedByPrincipalIdentifier(String ownedByPrincipalIdentifier) {
129         this.ownedByPrincipalIdentifier = ownedByPrincipalIdentifier;
130     }
131 
132     /**
133      * @return the lockDescriptor
134      */
135     public String getLockDescriptor() {
136         return this.lockDescriptor;
137     }
138 
139     /**
140      * @param lockDescriptor the lockDescriptor to set
141      */
142     public void setLockDescriptor(String lockDescriptor) {
143         this.lockDescriptor = lockDescriptor;
144     }
145 
146     /**
147      * @return the generatedTimestamp
148      */
149     public Timestamp getGeneratedTimestamp() {
150         return this.generatedTimestamp;
151     }
152 
153     /**
154      * @param generatedTimestamp the generatedTimestamp to set
155      */
156     public void setGeneratedTimestamp(Timestamp generatedTimestamp) {
157         this.generatedTimestamp = generatedTimestamp;
158     }
159 
160     /**
161      * @return the documentNumber
162      */
163     public String getDocumentNumber() {
164         return this.documentNumber;
165     }
166 
167     /**
168      * @param documentNumber the documentNumber to set
169      */
170     public void setDocumentNumber(String documentNumber) {
171         this.documentNumber = documentNumber;
172     }
173 
174     /**
175      * @return the sessionId
176      */
177     public String getSessionId() {
178         return this.sessionId;
179     }
180 
181     /**
182      * @param sessionId the sessionId to set
183      */
184     public void setSessionId(String sessionId) {
185         this.sessionId = sessionId;
186     }
187 
188 
189     /**
190      * @return the ownedByUser
191      */
192     public Person getOwnedByUser() {
193         ownedByUser = KimApiServiceLocator.getPersonService().updatePersonIfNecessary(ownedByPrincipalIdentifier, ownedByUser);
194         return ownedByUser;
195     }
196 
197     /**
198      * @param ownedByUser the ownedByUser to set
199      */
200     public void setOwnedByUser(Person ownedByUser) {
201         this.ownedByUser = ownedByUser;
202     }
203 }
204