View Javadoc

1   /*
2    * Copyright 2007-2008 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.kns.document.authorization;
17  
18  import java.sql.Timestamp;
19  import java.util.LinkedHashMap;
20  
21  import javax.persistence.Column;
22  import javax.persistence.Entity;
23  import javax.persistence.Id;
24  import javax.persistence.Table;
25  import javax.persistence.Transient;
26  
27  import org.kuali.rice.kim.bo.Person;
28  import org.kuali.rice.kim.bo.impl.PersonImpl;
29  import org.kuali.rice.kns.bo.PersistableBusinessObjectBase;
30  import org.kuali.rice.kns.service.KNSServiceLocator;
31  
32  /**
33   * This is a business object used to lock a document pessimistically.
34   * Pessimistic locking is more strick than optimistic locking and assumes if a
35   * lock exists that a user should only have read-only access to a document. For
36   * more information see documentation pages.
37   * 
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   * 
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      @Column(name="PESSIMISTIC_LOCK_ID")
53      private Long id;
54      
55      @Column(name="PRNCPL_ID")
56      private String ownedByPrincipalIdentifier;
57      
58      @Column(name="LOCK_DESC_TXT")
59      private String lockDescriptor; // this will be defaulted to the value of DEFAULT_LOCK_DESCRIPTOR constant above
60      
61      @Column(name="GNRT_DT")
62      private Timestamp generatedTimestamp;
63      
64      @Column(name="DOC_HDR_ID")
65      private String documentNumber; // foreign key to document
66      
67      @Transient
68      private Person ownedByUser;
69  
70      
71      /**
72       * This constructs an empty lock using the logged in user and default lock descriptor type
73       * but will NOT assign a document number.  Use another constructor.
74       * @deprecated
75       */
76      @Deprecated
77      public PessimisticLock() {}
78      
79      /**
80       * This constructs a lock object using the logged in user and given lock type
81       */
82      public PessimisticLock(String documentNumber, String lockDescriptor, Person user) {
83          this.documentNumber = documentNumber;
84          this.ownedByPrincipalIdentifier = user.getPrincipalId();
85          this.lockDescriptor = lockDescriptor;  
86          this.generatedTimestamp = KNSServiceLocator.getDateTimeService().getCurrentTimestamp();
87      }
88      
89      public boolean isOwnedByUser(Person user) {
90          return user.getPrincipalId().equals(getOwnedByPrincipalIdentifier());
91      }
92      
93      /**
94       * @return the id
95       */
96      public Long getId() {
97          return this.id;
98      }
99  
100     /**
101      * @param id the id to set
102      */
103     public void setId(Long id) {
104         this.id = id;
105     }
106 
107     /**
108      * @return the ownedByPrincipalIdentifier
109      */
110     public String getOwnedByPrincipalIdentifier() {
111         return this.ownedByPrincipalIdentifier;
112     }
113 
114     /**
115      * @param ownedByPrincipalIdentifier the ownedByPrincipalIdentifier to set
116      */
117     public void setOwnedByPrincipalIdentifier(String ownedByPrincipalIdentifier) {
118         this.ownedByPrincipalIdentifier = ownedByPrincipalIdentifier;
119     }
120 
121     /**
122      * @return the lockDescriptor
123      */
124     public String getLockDescriptor() {
125         return this.lockDescriptor;
126     }
127 
128     /**
129      * @param lockDescriptor the lockDescriptor to set
130      */
131     public void setLockDescriptor(String lockDescriptor) {
132         this.lockDescriptor = lockDescriptor;
133     }
134 
135     /**
136      * @return the generatedTimestamp
137      */
138     public Timestamp getGeneratedTimestamp() {
139         return this.generatedTimestamp;
140     }
141 
142     /**
143      * @param generatedTimestamp the generatedTimestamp to set
144      */
145     public void setGeneratedTimestamp(Timestamp generatedTimestamp) {
146         this.generatedTimestamp = generatedTimestamp;
147     }
148 
149     /**
150      * @return the documentNumber
151      */
152     public String getDocumentNumber() {
153         return this.documentNumber;
154     }
155 
156     /**
157      * @param documentNumber the documentNumber to set
158      */
159     public void setDocumentNumber(String documentNumber) {
160         this.documentNumber = documentNumber;
161     }
162 
163     /**
164      * @return the ownedByUser
165      */
166     public Person getOwnedByUser() {
167         ownedByUser = org.kuali.rice.kim.service.KIMServiceLocator.getPersonService().updatePersonIfNecessary(ownedByPrincipalIdentifier, ownedByUser);
168         return ownedByUser;
169     }
170 
171     /**
172      * @param ownedByUser the ownedByUser to set
173      */
174     public void setOwnedByUser(Person ownedByUser) {
175         this.ownedByUser = ownedByUser;
176     }
177 
178     /**
179      * This helper method used to define fields and field values to use
180      * in toString() method
181      * 
182      * @see org.kuali.rice.kns.bo.BusinessObjectBase#toStringMapper()
183      */
184     @Override
185     protected LinkedHashMap toStringMapper() {
186         LinkedHashMap m = new LinkedHashMap();
187         m.put("id", this.id);
188         m.put("ownedByPrincipalIdentifier", this.ownedByPrincipalIdentifier);
189         m.put("lockDescriptor", this.lockDescriptor);
190         m.put("generatedTimestamp", this.generatedTimestamp);
191         m.put("documentNumber", this.documentNumber);
192         return m;
193     }
194 }
195