KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > query > BatchQuery


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.query;
21
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.cayenne.CayenneRuntimeException;
26 import org.apache.cayenne.ObjectId;
27 import org.apache.cayenne.map.DbAttribute;
28 import org.apache.cayenne.map.DbEntity;
29 import org.apache.cayenne.map.EntityResolver;
30 import org.apache.commons.collections.Factory;
31
32 /**
33  * BatchQuery and its descendants allow to group similar data for the batch database
34  * modifications, including inserts, updates and deletes. Single BatchQuery corresponds to
35  * a parameterized PreparedStatement and a matrix of values.
36  *
37  * @author Andriy Shapochka
38  * @author Andrus Adamchik
39  */

40 public abstract class BatchQuery implements Query {
41
42     /**
43      * @since 1.2
44      */

45     protected int batchIndex;
46
47     /**
48      * @since 1.2
49      */

50     protected DbEntity dbEntity;
51
52     protected String JavaDoc name;
53
54     public BatchQuery(DbEntity dbEntity) {
55         this.dbEntity = dbEntity;
56         this.batchIndex = -1;
57     }
58
59     public String JavaDoc getName() {
60         return name;
61     }
62
63     public void setName(String JavaDoc name) {
64         this.name = name;
65     }
66
67     /**
68      * Returns default select parameters.
69      *
70      * @since 1.2
71      */

72     public QueryMetadata getMetaData(EntityResolver resolver) {
73         return new DefaultQueryMetadata() {
74
75             public DbEntity getDbEntity() {
76                 return dbEntity;
77             }
78         };
79     }
80
81     /**
82      * @since 1.2
83      */

84     public void route(QueryRouter router, EntityResolver resolver, Query substitutedQuery) {
85         router.route(
86                 router.engineForDataMap(dbEntity.getDataMap()),
87                 this,
88                 substitutedQuery);
89     }
90
91     /**
92      * Calls "batchAction" on the visitor.
93      *
94      * @since 1.2
95      */

96     public SQLAction createSQLAction(SQLActionVisitor visitor) {
97         return visitor.batchAction(this);
98     }
99
100     /**
101      * Returns true if the batch query uses optimistic locking.
102      *
103      * @since 1.1
104      */

105     public boolean isUsingOptimisticLocking() {
106         return false;
107     }
108
109     /**
110      * Returns a DbEntity associated with this batch.
111      */

112     public DbEntity getDbEntity() {
113         return dbEntity;
114     }
115
116     /**
117      * Returns <code>true</code> if this batch query has no parameter rows.
118      */

119     public boolean isEmpty() {
120         return size() == 0;
121     }
122
123     /**
124      * Returns a list of DbAttributes describing batch parameters.
125      */

126     public abstract List JavaDoc getDbAttributes();
127
128     /**
129      * Rewinds batch to the first parameter row.
130      */

131     public void reset() {
132         batchIndex = -1;
133     }
134
135     /**
136      * Repositions batch to the next object, so that subsequent calls to getObject(int)
137      * would return the values of the next batch object. Returns <code>true</code> if
138      * batch has more objects to iterate over, <code>false</code> otherwise.
139      */

140     public boolean next() {
141         batchIndex++;
142         return size() > batchIndex;
143     }
144
145     /**
146      * Returns a value at a given index for the current batch iteration.
147      *
148      * @since 1.2
149      */

150     public abstract Object JavaDoc getValue(int valueIndex);
151
152     /**
153      * Returns the number of parameter rows in a batch.
154      */

155     public abstract int size();
156
157     /**
158      * A helper method used by subclasses to resolve deferred values on demand. This is
159      * useful when a certain value comes from a generated key of another master object.
160      *
161      * @since 1.2
162      */

163     protected Object JavaDoc getValue(Map JavaDoc valueMap, DbAttribute attribute) {
164
165         Object JavaDoc value = valueMap.get(attribute.getName());
166
167         // if a value is a Factory, resolve it here...
168
// slight chance that a normal value will implement Factory interface???
169
if (value instanceof Factory) {
170             value = ((Factory) value).create();
171
172             // update replacement id
173
if (attribute.isPrimaryKey()) {
174                 // sanity check
175
if (value == null) {
176                     String JavaDoc name = attribute.getEntity() != null ? attribute
177                             .getEntity()
178                             .getName() : "<null>";
179                     throw new CayenneRuntimeException("Failed to generate PK: "
180                             + name
181                             + "."
182                             + attribute.getName());
183                 }
184
185                 ObjectId id = getObjectId();
186                 if (id != null) {
187                     // always override with fresh value as this is what's in the DB
188
id.getReplacementIdMap().put(attribute.getName(), value);
189                 }
190             }
191
192             // update snapshot
193
valueMap.put(attribute.getName(), value);
194         }
195
196         return value;
197     }
198
199     /**
200      * Returns an ObjectId associated with the current batch iteration. Used internally by
201      * Cayenne to match current iteration with a specific object and assign it generated
202      * keys.
203      * <p>
204      * Default implementation simply returns null.
205      * </p>
206      *
207      * @since 1.2
208      */

209     public ObjectId getObjectId() {
210         return null;
211     }
212 }
213
Popular Tags