001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.document.authorization;
017
018import org.hibernate.annotations.GenericGenerator;
019import org.hibernate.annotations.Parameter;
020import org.kuali.rice.core.api.CoreApiServiceLocator;
021import org.kuali.rice.kim.api.identity.Person;
022import org.kuali.rice.kim.api.services.KimApiServiceLocator;
023import org.kuali.rice.krad.UserSession;
024import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
025
026import javax.persistence.Column;
027import javax.persistence.Entity;
028import javax.persistence.GeneratedValue;
029import javax.persistence.Id;
030import javax.persistence.Table;
031import javax.persistence.Transient;
032import java.sql.Timestamp;
033
034/**
035 * This is a business object used to lock a document pessimistically.
036 * Pessimistic locking is more strick than optimistic locking and assumes if a
037 * lock exists that a user should only have read-only access to a document. For
038 * more information see documentation pages.
039 * 
040 * @author Kuali Rice Team (rice.collab@kuali.org)
041 * 
042 */
043@Entity
044@Table(name="KRNS_PESSIMISTIC_LOCK_T")
045public class PessimisticLock extends PersistableBusinessObjectBase {
046    
047    private static final long serialVersionUID = -5210762282545093555L;
048    
049    public static final String DEFAULT_LOCK_DESCRIPTOR = null;
050    
051    // id is sequence number and primary key
052    @Id
053    @GeneratedValue(generator="KRNS_LOCK_S")
054        @GenericGenerator(name="KRNS_LOCK_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
055                        @Parameter(name="sequence_name",value="KRNS_LOCK_S"),
056                        @Parameter(name="value_column",value="id")
057        })
058    @Column(name="PESSIMISTIC_LOCK_ID")
059    private Long id;
060    
061    @Column(name="PRNCPL_ID")
062    private String ownedByPrincipalIdentifier;
063    
064    @Column(name="LOCK_DESC_TXT")
065    private String lockDescriptor; // this will be defaulted to the value of DEFAULT_LOCK_DESCRIPTOR constant above
066    
067    @Column(name="GNRT_DT")
068    private Timestamp generatedTimestamp;
069    
070    @Column(name="DOC_HDR_ID")
071    private String documentNumber; // foreign key to document
072
073    @Column(name="SESN_ID")
074    private String sessionId;
075    
076    @Transient
077    private Person ownedByUser;
078
079    
080    /**
081     * This constructs an empty lock using the logged in user and default lock descriptor type
082     * but will NOT assign a document number or session id.  Use another constructor.
083     * @deprecated
084     */
085    @Deprecated
086    public PessimisticLock() {}
087    
088    /**
089     * This constructs a lock object using the logged in user and given lock type
090     */
091    public PessimisticLock(String documentNumber, String lockDescriptor, Person user, UserSession userSession) {
092        this.documentNumber = documentNumber;
093        this.ownedByPrincipalIdentifier = user.getPrincipalId();
094        this.lockDescriptor = lockDescriptor;  
095        this.generatedTimestamp = CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp();
096        this.sessionId = userSession.getKualiSessionId();
097    }
098    
099    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