KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.cayenne.ObjectId;
29 import org.apache.cayenne.map.DbAttribute;
30 import org.apache.cayenne.map.DbEntity;
31
32 /**
33  * Batched UPDATE query.
34  *
35  * @author Andriy Shapochka
36  */

37 public class UpdateBatchQuery extends BatchQuery {
38
39     /**
40      * @since 1.2
41      */

42     protected List JavaDoc objectIds;
43
44     protected List JavaDoc qualifierSnapshots;
45     protected List JavaDoc updateSnapshots;
46
47     protected boolean usingOptimisticLocking;
48
49     private List JavaDoc updatedAttributes;
50     private List JavaDoc qualifierAttributes;
51     private Collection JavaDoc nullQualifierNames;
52     private List JavaDoc dbAttributes;
53
54     /**
55      * Creates new UpdateBatchQuery.
56      *
57      * @param dbEntity Table or view to update.
58      * @param qualifierAttributes DbAttributes used in the WHERE clause.
59      * @param nullQualifierNames DbAttribute names in the WHERE clause that have null
60      * values.
61      * @param updatedAttribute DbAttributes describing updated columns.
62      * @param batchCapacity Estimated size of the batch.
63      */

64     public UpdateBatchQuery(DbEntity dbEntity, List JavaDoc qualifierAttributes,
65             List JavaDoc updatedAttribute, Collection JavaDoc nullQualifierNames, int batchCapacity) {
66
67         super(dbEntity);
68
69         this.updatedAttributes = updatedAttribute;
70         this.qualifierAttributes = qualifierAttributes;
71         this.nullQualifierNames = nullQualifierNames != null
72                 ? nullQualifierNames
73                 : Collections.EMPTY_SET;
74
75         qualifierSnapshots = new ArrayList JavaDoc(batchCapacity);
76         updateSnapshots = new ArrayList JavaDoc(batchCapacity);
77         objectIds = new ArrayList JavaDoc(batchCapacity);
78
79         dbAttributes = new ArrayList JavaDoc(updatedAttributes.size()
80                 + qualifierAttributes.size());
81         dbAttributes.addAll(updatedAttributes);
82         dbAttributes.addAll(qualifierAttributes);
83     }
84
85     /**
86      * Returns true if a given attribute always has a null value in the batch.
87      *
88      * @since 1.1
89      */

90     public boolean isNull(DbAttribute attribute) {
91         return nullQualifierNames.contains(attribute.getName());
92     }
93
94     /**
95      * Returns true if the batch query uses optimistic locking.
96      *
97      * @since 1.1
98      */

99     public boolean isUsingOptimisticLocking() {
100         return usingOptimisticLocking;
101     }
102
103     /**
104      * @since 1.1
105      */

106     public void setUsingOptimisticLocking(boolean usingOptimisticLocking) {
107         this.usingOptimisticLocking = usingOptimisticLocking;
108     }
109
110     public Object JavaDoc getValue(int dbAttributeIndex) {
111         DbAttribute attribute = (DbAttribute) dbAttributes.get(dbAttributeIndex);
112
113         // take value either from updated values or id's,
114
// depending on the index
115
Object JavaDoc snapshot = (dbAttributeIndex < updatedAttributes.size()) ? updateSnapshots
116                 .get(batchIndex) : qualifierSnapshots.get(batchIndex);
117         return getValue((Map JavaDoc) snapshot, attribute);
118     }
119
120     /**
121      * Adds a parameter row to the batch.
122      */

123     public void add(Map JavaDoc qualifierSnapshot, Map JavaDoc updateSnapshot) {
124         add(qualifierSnapshot, updateSnapshot, null);
125     }
126
127     /**
128      * Adds a parameter row to the batch.
129      *
130      * @since 1.2
131      */

132     public void add(Map JavaDoc qualifierSnapshot, Map JavaDoc updateSnapshot, ObjectId id) {
133         qualifierSnapshots.add(qualifierSnapshot);
134         updateSnapshots.add(updateSnapshot);
135         objectIds.add(id);
136     }
137
138     public int size() {
139         return qualifierSnapshots.size();
140     }
141
142     public List JavaDoc getDbAttributes() {
143         return dbAttributes;
144     }
145
146     /**
147      * @since 1.1
148      */

149     public List JavaDoc getUpdatedAttributes() {
150         return Collections.unmodifiableList(updatedAttributes);
151     }
152
153     /**
154      * @since 1.1
155      */

156     public List JavaDoc getQualifierAttributes() {
157         return Collections.unmodifiableList(qualifierAttributes);
158     }
159
160     /**
161      * Returns a snapshot of the current qualifier values.
162      *
163      * @since 1.1
164      */

165     public Map JavaDoc getCurrentQualifier() {
166         return (Map JavaDoc) qualifierSnapshots.get(batchIndex);
167     }
168
169     /**
170      * Returns an ObjectId associated with the current batch iteration. Used internally by
171      * Cayenne to match current iteration with a specific object and assign it generated
172      * keys.
173      *
174      * @since 1.2
175      */

176     public ObjectId getObjectId() {
177         return (ObjectId) objectIds.get(batchIndex);
178     }
179 }
180
Popular Tags