KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.cayenne.CayenneException;
26 import org.apache.cayenne.CayenneRuntimeException;
27 import org.apache.cayenne.DataRow;
28 import org.apache.cayenne.ObjectId;
29 import org.apache.cayenne.map.DbAttribute;
30 import org.apache.cayenne.query.BatchQuery;
31 import org.apache.cayenne.query.InsertBatchQuery;
32 import org.apache.cayenne.query.Query;
33 import org.apache.cayenne.util.Util;
34
35 /**
36  * Used as an observer for DataContext commit operations.
37  *
38  * @since 1.2
39  * @author Andrus Adamchik
40  */

41 class DataDomainFlushObserver implements OperationObserver {
42
43     public void nextQueryException(Query query, Exception JavaDoc ex) {
44         throw new CayenneRuntimeException("Raising from query exception.", Util
45                 .unwindException(ex));
46     }
47
48     public void nextGlobalException(Exception JavaDoc ex) {
49         throw new CayenneRuntimeException(
50                 "Raising from underlyingQueryEngine exception.",
51                 Util.unwindException(ex));
52     }
53
54     /**
55      * Processes generated keys.
56      *
57      * @since 1.2
58      */

59     public void nextGeneratedDataRows(Query query, ResultIterator keysIterator) {
60
61         // read and close the iterator before doing anything else
62
List JavaDoc keys;
63         try {
64             keys = keysIterator.dataRows(true);
65         }
66         catch (CayenneException ex) {
67             throw new CayenneRuntimeException("Error reading primary key", Util
68                     .unwindException(ex));
69         }
70
71         if (!(query instanceof InsertBatchQuery)) {
72             throw new CayenneRuntimeException(
73                     "Generated keys only supported for InsertBatchQuery, instead got "
74                             + query);
75         }
76
77         BatchQuery batch = (BatchQuery) query;
78
79         ObjectId id = batch.getObjectId();
80         if (id == null || !id.isTemporary()) {
81             // why would this happen?
82
return;
83         }
84
85         if (keys.size() != 1) {
86             throw new CayenneRuntimeException(
87                     "One and only one PK row is expected, instead got " + keys.size());
88         }
89
90         DataRow key = (DataRow) keys.get(0);
91
92         // empty key?
93
if (key.size() == 0) {
94             throw new CayenneRuntimeException("Empty key generated.");
95         }
96
97         // determine DbAttribute name...
98

99         // As of now (01/2005) all tested drivers don't provide decent descriptors of
100
// identity result sets, so a data row will contain garbage labels. Also most
101
// DBs only support one autogenerated key per table... So here we will have to
102
// infer the key name and currently will only support a single column...
103
if (key.size() > 1) {
104             throw new CayenneRuntimeException(
105                     "Only a single column autogenerated PK is supported. "
106                             + "Generated key: "
107                             + key);
108         }
109
110         Iterator JavaDoc it = batch.getDbEntity().getGeneratedAttributes().iterator();
111         while (it.hasNext()) {
112             DbAttribute attribute = (DbAttribute) it.next();
113
114             // batch can have generated attributes that are not PKs, e.g. columns with
115
// DB DEFAULT values. Ignore those.
116
if (attribute.isPrimaryKey()) {
117                 Object JavaDoc value = key.values().iterator().next();
118
119                 // I guess we should override any existing value,
120
// as generated key is the latest thing that exists in the DB.
121
id.getReplacementIdMap().put(attribute.getName(), value);
122                 break;
123             }
124         }
125     }
126
127     public void nextBatchCount(Query query, int[] resultCount) {
128     }
129
130     public void nextCount(Query query, int resultCount) {
131     }
132
133     public void nextDataRows(Query query, List JavaDoc dataRows) {
134     }
135
136     public void nextDataRows(Query q, ResultIterator it) {
137         throw new UnsupportedOperationException JavaDoc(
138                 "'nextDataRows(Query,ResultIterator)' is unsupported (and unexpected) on commit.");
139     }
140
141     public boolean isIteratedResult() {
142         return false;
143     }
144 }
145
Popular Tags