KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > exceptions > OptimisticLockException


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.exceptions;
23
24 import java.util.Vector JavaDoc;
25 import oracle.toplink.essentials.queryframework.*;
26 import oracle.toplink.essentials.exceptions.i18n.ExceptionMessageGenerator;
27 import oracle.toplink.essentials.sessions.SessionProfiler;
28
29 /**
30  * <p><b>Purpose</b>: This exception is used when TopLink's optimistic locking feature is used.
31  * It will be raised if the object being updated or deleted was changed or deleted from the database since
32  * it as last read.
33  */

34 public class OptimisticLockException extends TopLinkException {
35
36     /** Store the query that raised the optimistic violation. */
37     protected transient ObjectLevelModifyQuery query;
38
39     // ERROR CODES
40
public final static int NO_VERSION_NUMBER_WHEN_DELETING = 5001;
41     public final static int OBJECT_CHANGED_SINCE_LAST_READ_WHEN_DELETING = 5003;
42     public final static int NO_VERSION_NUMBER_WHEN_UPDATING = 5004;
43     public final static int OBJECT_CHANGED_SINCE_LAST_READ_WHEN_UPDATING = 5006;
44     public final static int MUST_HAVE_MAPPING_WHEN_IN_OBJECT = 5007;
45     public final static int NEED_TO_MAP_JAVA_SQL_TIMESTAMP = 5008;
46     public final static int UNWRAPPING_OBJECT_DELETED_SINCE_LAST_READ = 5009;
47     public final static int OBJECT_CHANGED_SINCE_LAST_MERGE = 5010;
48
49     /**
50      * INTERNAL:
51      * TopLink exceptions should only be thrown by TopLink.
52      */

53     protected OptimisticLockException(String JavaDoc theMessage) {
54         super(theMessage);
55     }
56
57     /**
58      * INTERNAL:
59      * TopLink exceptions should only be thrown by TopLink.
60      */

61     protected OptimisticLockException(String JavaDoc theMessage, ObjectLevelModifyQuery query) {
62         super(theMessage);
63         this.query = query;
64         query.getSession().incrementProfile(SessionProfiler.OptimisticLockException);
65
66     }
67
68     /**
69      * PUBLIC:
70      * Return the object for which the problem was detected.
71      */

72     public Object JavaDoc getObject() {
73         return getQuery().getObject();
74     }
75
76     /**
77      * PUBLIC:
78      * Return the query in which the problem was detected.
79      */

80     public ObjectLevelModifyQuery getQuery() {
81         return query;
82     }
83
84     public static OptimisticLockException mustHaveMappingWhenStoredInObject(Class JavaDoc aClass) {
85         Object JavaDoc[] args = { aClass };
86
87         OptimisticLockException optimisticLockException = new OptimisticLockException(ExceptionMessageGenerator.buildMessage(OptimisticLockException.class, MUST_HAVE_MAPPING_WHEN_IN_OBJECT, args));
88         optimisticLockException.setErrorCode(MUST_HAVE_MAPPING_WHEN_IN_OBJECT);
89         return optimisticLockException;
90
91     }
92
93     public static OptimisticLockException noVersionNumberWhenDeleting(Object JavaDoc object, ObjectLevelModifyQuery query) {
94         Vector JavaDoc key = new Vector JavaDoc();
95         if (query.getSession() != null) {
96             key = query.getSession().keyFromObject(object);
97         }
98         Object JavaDoc[] args = { object, object.getClass().getName(), key, CR };
99
100         OptimisticLockException optimisticLockException = new OptimisticLockException(ExceptionMessageGenerator.buildMessage(OptimisticLockException.class, NO_VERSION_NUMBER_WHEN_DELETING, args), query);
101         optimisticLockException.setErrorCode(NO_VERSION_NUMBER_WHEN_DELETING);
102         return optimisticLockException;
103     }
104
105     public static OptimisticLockException noVersionNumberWhenUpdating(Object JavaDoc object, ObjectLevelModifyQuery query) {
106         Vector JavaDoc key = new Vector JavaDoc();
107         if (query.getSession() != null) {
108             key = query.getSession().keyFromObject(object);
109         }
110         Object JavaDoc[] args = { object, object.getClass().getName(), key, CR };
111
112         OptimisticLockException optimisticLockException = new OptimisticLockException(ExceptionMessageGenerator.buildMessage(OptimisticLockException.class, NO_VERSION_NUMBER_WHEN_UPDATING, args), query);
113         optimisticLockException.setErrorCode(NO_VERSION_NUMBER_WHEN_UPDATING);
114         return optimisticLockException;
115     }
116
117     public static OptimisticLockException objectChangedSinceLastReadWhenDeleting(Object JavaDoc object, ObjectLevelModifyQuery query) {
118         Vector JavaDoc key = new Vector JavaDoc();
119         if (query.getSession() != null) {
120             key = query.getSession().keyFromObject(object);
121         }
122         Object JavaDoc[] args = { object, object.getClass().getName(), key, CR };
123
124         OptimisticLockException optimisticLockException = new OptimisticLockException(ExceptionMessageGenerator.buildMessage(OptimisticLockException.class, OBJECT_CHANGED_SINCE_LAST_READ_WHEN_DELETING, args), query);
125         optimisticLockException.setErrorCode(OBJECT_CHANGED_SINCE_LAST_READ_WHEN_DELETING);
126         return optimisticLockException;
127     }
128
129     public static OptimisticLockException objectChangedSinceLastReadWhenUpdating(Object JavaDoc object, ObjectLevelModifyQuery query) {
130         Vector JavaDoc key = new Vector JavaDoc();
131         if (query.getSession() != null) {
132             key = query.getSession().keyFromObject(object);
133         }
134         Object JavaDoc[] args = { object, object.getClass().getName(), key, CR };
135
136         OptimisticLockException optimisticLockException = new OptimisticLockException(ExceptionMessageGenerator.buildMessage(OptimisticLockException.class, OBJECT_CHANGED_SINCE_LAST_READ_WHEN_UPDATING, args), query);
137         optimisticLockException.setErrorCode(OBJECT_CHANGED_SINCE_LAST_READ_WHEN_UPDATING);
138         return optimisticLockException;
139     }
140     
141     public static OptimisticLockException objectChangedSinceLastMerge(Object JavaDoc object) {
142         Object JavaDoc[] args = { object, object.getClass().getName(), CR };
143
144         OptimisticLockException optimisticLockException = new OptimisticLockException(ExceptionMessageGenerator.buildMessage(OptimisticLockException.class, OBJECT_CHANGED_SINCE_LAST_MERGE, args));
145         optimisticLockException.setErrorCode(OBJECT_CHANGED_SINCE_LAST_MERGE);
146         return optimisticLockException;
147     }
148
149     public static OptimisticLockException unwrappingObjectDeletedSinceLastRead(Vector JavaDoc pkVector, String JavaDoc className) {
150         Object JavaDoc[] args = { pkVector, className };
151
152         OptimisticLockException optimisticLockException = new OptimisticLockException(ExceptionMessageGenerator.buildMessage(OptimisticLockException.class, UNWRAPPING_OBJECT_DELETED_SINCE_LAST_READ, args));
153         optimisticLockException.setErrorCode(UNWRAPPING_OBJECT_DELETED_SINCE_LAST_READ);
154         return optimisticLockException;
155     }
156
157     //For CR#2281
158
public static OptimisticLockException needToMapJavaSqlTimestampWhenStoredInObject() {
159         Object JavaDoc[] args = { };
160
161         OptimisticLockException optimisticLockException = new OptimisticLockException(ExceptionMessageGenerator.buildMessage(OptimisticLockException.class, NEED_TO_MAP_JAVA_SQL_TIMESTAMP, args));
162         optimisticLockException.setErrorCode(NEED_TO_MAP_JAVA_SQL_TIMESTAMP);
163         return optimisticLockException;
164     }
165
166     /**
167      * INTERNAL:
168      * Set the query in which the problem was detected.
169      */

170     public void setQuery(ObjectLevelModifyQuery query) {
171         this.query = query;
172     }
173 }
174
Popular Tags