001    /**
002     * Copyright 2005-2012 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     */
016    package org.kuali.rice.krad.document.authorization;
017    
018    import org.hibernate.annotations.GenericGenerator;
019    import org.hibernate.annotations.Parameter;
020    import org.kuali.rice.core.api.CoreApiServiceLocator;
021    import org.kuali.rice.kim.api.identity.Person;
022    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
023    import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
024    
025    import javax.persistence.Column;
026    import javax.persistence.Entity;
027    import javax.persistence.GeneratedValue;
028    import javax.persistence.Id;
029    import javax.persistence.Table;
030    import javax.persistence.Transient;
031    import java.sql.Timestamp;
032    
033    /**
034     * This is a business object used to lock a document pessimistically.
035     * Pessimistic locking is more strick than optimistic locking and assumes if a
036     * lock exists that a user should only have read-only access to a document. For
037     * more information see documentation pages.
038     * 
039     * @author Kuali Rice Team (rice.collab@kuali.org)
040     * 
041     */
042    
043    @Entity
044    @Table(name="KRNS_PESSIMISTIC_LOCK_T")
045    public 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        @Transient
074        private Person ownedByUser;
075    
076        
077        /**
078         * This constructs an empty lock using the logged in user and default lock descriptor type
079         * but will NOT assign a document number.  Use another constructor.
080         * @deprecated
081         */
082        @Deprecated
083        public PessimisticLock() {}
084        
085        /**
086         * This constructs a lock object using the logged in user and given lock type
087         */
088        public PessimisticLock(String documentNumber, String lockDescriptor, Person user) {
089            this.documentNumber = documentNumber;
090            this.ownedByPrincipalIdentifier = user.getPrincipalId();
091            this.lockDescriptor = lockDescriptor;  
092            this.generatedTimestamp = CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp();
093        }
094        
095        public boolean isOwnedByUser(Person user) {
096            return user.getPrincipalId().equals(getOwnedByPrincipalIdentifier());
097        }
098        
099        /**
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 ownedByUser
171         */
172        public Person getOwnedByUser() {
173            ownedByUser = KimApiServiceLocator.getPersonService().updatePersonIfNecessary(ownedByPrincipalIdentifier, ownedByUser);
174            return ownedByUser;
175        }
176    
177        /**
178         * @param ownedByUser the ownedByUser to set
179         */
180        public void setOwnedByUser(Person ownedByUser) {
181            this.ownedByUser = ownedByUser;
182        }
183    }
184