KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > OptimisticLockException


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access;
21
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.cayenne.CayenneRuntimeException;
28 import org.apache.cayenne.exp.Expression;
29 import org.apache.cayenne.exp.ExpressionFactory;
30 import org.apache.cayenne.map.DbAttribute;
31 import org.apache.cayenne.map.DbEntity;
32 import org.apache.cayenne.query.SelectQuery;
33
34 /**
35  * An exception thrown on optimistic lock failure.
36  *
37  * @since 1.1
38  * @author Andrus Adamchik
39  */

40 public class OptimisticLockException extends CayenneRuntimeException {
41
42     protected String JavaDoc querySQL;
43     protected DbEntity rootEntity;
44     protected Map JavaDoc qualifierSnapshot;
45
46     public OptimisticLockException(DbEntity rootEntity, String JavaDoc querySQL,
47             Map JavaDoc qualifierSnapshot) {
48         super("Optimistic Lock Failure");
49
50         this.rootEntity = rootEntity;
51         this.querySQL = querySQL;
52         this.qualifierSnapshot = (qualifierSnapshot != null)
53                 ? qualifierSnapshot
54                 : Collections.EMPTY_MAP;
55     }
56
57     public Map JavaDoc getQualifierSnapshot() {
58         return qualifierSnapshot;
59     }
60
61     public String JavaDoc getQuerySQL() {
62         return querySQL;
63     }
64
65     /**
66      * Retrieves fresh snapshot for the failed row. Null row indicates that it was
67      * deleted.
68      */

69     // TODO: andrus, 5/30/2006 - use DataChannel instead of QE as a parameter after 1.2
70
public Map JavaDoc getFreshSnapshot(QueryEngine engine) {
71
72         // extract PK from the qualifierSnapshot and fetch a row
73
// for PK, ignoring other locking attributes...
74

75         Expression qualifier = null;
76         Iterator JavaDoc it = rootEntity.getPrimaryKey().iterator();
77         while (it.hasNext()) {
78             DbAttribute attribute = (DbAttribute) it.next();
79             Expression attributeQualifier = ExpressionFactory.matchDbExp(attribute
80                     .getName(), qualifierSnapshot.get(attribute.getName()));
81
82             qualifier = (qualifier != null)
83                     ? qualifier.andExp(attributeQualifier)
84                     : attributeQualifier;
85         }
86
87         SelectQuery query = new SelectQuery(rootEntity, qualifier);
88         query.setFetchingDataRows(true);
89         QueryResult observer = new QueryResult();
90         engine.performQueries(Collections.singletonList(query), observer);
91         List JavaDoc results = observer.getFirstRows(query);
92
93         if (results == null || results.isEmpty()) {
94             return null;
95         }
96         else if (results.size() > 1) {
97             throw new CayenneRuntimeException("More than one row for ObjectId.");
98         }
99         else {
100             return (Map JavaDoc) results.get(0);
101         }
102     }
103
104     /**
105      * Returns descriptive message for this exception.
106      */

107     public String JavaDoc getMessage() {
108         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(super.getMessage());
109
110         if (querySQL != null) {
111             buffer.append(", SQL: [").append(querySQL.trim()).append("]");
112         }
113
114         if (!qualifierSnapshot.isEmpty()) {
115             buffer.append(", WHERE clause bindings: [");
116             Iterator JavaDoc it = qualifierSnapshot.entrySet().iterator();
117             while (it.hasNext()) {
118                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
119                 buffer.append(entry.getKey()).append("=");
120                 QueryLogger.sqlLiteralForObject(buffer, entry.getValue());
121
122                 if (it.hasNext()) {
123                     buffer.append(", ");
124                 }
125             }
126             buffer.append("]");
127         }
128
129         return buffer.toString();
130     }
131 }
132
Popular Tags