KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > query > UpdateBatchQuery


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.query;
57
58 import java.util.ArrayList JavaDoc;
59 import java.util.Collection JavaDoc;
60 import java.util.Collections JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.Map JavaDoc;
64
65 import org.apache.commons.collections.IteratorUtils;
66 import org.objectstyle.cayenne.map.DbAttribute;
67 import org.objectstyle.cayenne.map.DbEntity;
68
69 /**
70  * Batched UPDATE query.
71  *
72  * @author Andriy Shapochka
73  */

74 public class UpdateBatchQuery extends BatchQuery {
75     private List JavaDoc qualifierSnapshots;
76     private List JavaDoc updateSnapshots;
77
78     private List JavaDoc updatedAttributes;
79     private List JavaDoc qualifierAttributes;
80     private Collection JavaDoc nullQualifierNames;
81
82     private List JavaDoc dbAttributes;
83
84     private Iterator JavaDoc qualifierIterator = IteratorUtils.EMPTY_ITERATOR;
85     private Iterator JavaDoc updateIterator = IteratorUtils.EMPTY_ITERATOR;
86     private Map JavaDoc currentUpdate = Collections.EMPTY_MAP;
87     private Map JavaDoc currentQualifier = Collections.EMPTY_MAP;
88
89     private boolean usingOptimisticLocking;
90
91     /**
92      * Creates new UpdateBatchQuery.
93      *
94      * @param dbEntity Table or view to update.
95      * @param qualifierAttributes DbAttributes used in the WHERE clause.
96      * @param nullQualifierNames DbAttribute names in the WHERE clause that have null values.
97      * @param updatedAttribute DbAttributes describing updated columns.
98      * @param batchCapacity Estimated size of the batch.
99      */

100     public UpdateBatchQuery(
101         DbEntity dbEntity,
102         List JavaDoc qualifierAttributes,
103         List JavaDoc updatedAttribute,
104         Collection JavaDoc nullQualifierNames,
105         int batchCapacity) {
106
107         super(dbEntity);
108
109         this.updatedAttributes = updatedAttribute;
110         this.qualifierAttributes = qualifierAttributes;
111         this.nullQualifierNames =
112             nullQualifierNames != null ? nullQualifierNames : Collections.EMPTY_SET;
113
114         qualifierSnapshots = new ArrayList JavaDoc(batchCapacity);
115         updateSnapshots = new ArrayList JavaDoc(batchCapacity);
116
117         dbAttributes =
118             new ArrayList JavaDoc(updatedAttributes.size() + qualifierAttributes.size());
119         dbAttributes.addAll(updatedAttributes);
120         dbAttributes.addAll(qualifierAttributes);
121     }
122
123     /**
124      * Returns true if a given attribute always has a null value
125      * in the batch.
126      *
127      * @since 1.1
128      */

129     public boolean isNull(DbAttribute attribute) {
130         return nullQualifierNames.contains(attribute.getName());
131     }
132
133     /**
134      * Returns true if the batch query uses optimistic locking.
135      *
136      * @since 1.1
137      */

138     public boolean isUsingOptimisticLocking() {
139         return usingOptimisticLocking;
140     }
141
142     /**
143      * @since 1.1
144      */

145     public void setUsingOptimisticLocking(boolean usingOptimisticLocking) {
146         this.usingOptimisticLocking = usingOptimisticLocking;
147     }
148
149     public void reset() {
150         qualifierIterator = qualifierSnapshots.iterator();
151         updateIterator = updateSnapshots.iterator();
152         currentQualifier = Collections.EMPTY_MAP;
153         currentUpdate = Collections.EMPTY_MAP;
154     }
155
156     public boolean next() {
157         if (!qualifierIterator.hasNext()) {
158             return false;
159         }
160
161         currentQualifier = (Map JavaDoc) qualifierIterator.next();
162         currentQualifier =
163             (currentQualifier != null) ? currentQualifier : Collections.EMPTY_MAP;
164         currentUpdate = (Map JavaDoc) updateIterator.next();
165         currentUpdate = (currentUpdate != null) ? currentUpdate : Collections.EMPTY_MAP;
166         return true;
167     }
168
169     public Object JavaDoc getValue(int dbAttributeIndex) {
170         DbAttribute attribute = (DbAttribute) dbAttributes.get(dbAttributeIndex);
171         String JavaDoc name = attribute.getName();
172
173         // take value either from updated values or id's,
174
// depending on the index
175
return (dbAttributeIndex < updatedAttributes.size())
176             ? currentUpdate.get(name)
177             : currentQualifier.get(name);
178     }
179
180     /**
181      * Adds a parameter row to the batch.
182      *
183      * @param qualifierSnapshot describes WHERE clause of the update; includes PK values and
184      * any attributes used in optimistic locking.
185      * @param updateSnapshot describes updated columns.
186      */

187     public void add(Map JavaDoc qualifierSnapshot, Map JavaDoc updateSnapshot) {
188         qualifierSnapshots.add(qualifierSnapshot);
189         updateSnapshots.add(updateSnapshot);
190     }
191
192     public int size() {
193         return qualifierSnapshots.size();
194     }
195
196     public List JavaDoc getDbAttributes() {
197         return dbAttributes;
198     }
199
200     /**
201      * @since 1.1
202      */

203     public List JavaDoc getUpdatedAttributes() {
204         return Collections.unmodifiableList(updatedAttributes);
205     }
206
207     /**
208      * @since 1.1
209      */

210     public List JavaDoc getQualifierAttributes() {
211         return Collections.unmodifiableList(qualifierAttributes);
212     }
213
214     /**
215      * Returns a snapshot of the current qualifier values.
216      *
217      * @since 1.1
218      */

219     public Map JavaDoc getCurrentQualifier() {
220         return currentQualifier;
221     }
222 }
223
Popular Tags