KickJava   Java API By Example, From Geeks To Geeks.

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


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 delete query.
71  *
72  * @author Andriy Shapochka
73  */

74 public class DeleteBatchQuery extends BatchQuery {
75     private List JavaDoc qualifierSnapshots;
76     private List JavaDoc dbAttributes;
77     private Iterator JavaDoc qualifierIterator = IteratorUtils.EMPTY_ITERATOR;
78     private Map JavaDoc currentQualifier = Collections.EMPTY_MAP;
79
80     private boolean usingOptimisticLocking;
81     private List JavaDoc qualifierAttributes;
82     private Collection JavaDoc nullQualifierNames;
83
84     /**
85      * Creates new DeleteBatchQuery.
86      *
87      * @param dbEntity Table or view to delete.
88      * @param qualifierAttributes DbAttributes used in the WHERE clause.
89      * @param nullQualifierNames DbAttribute names in the WHERE clause that have null values.
90      * @param batchCapacity Estimated size of the batch.
91      */

92     public DeleteBatchQuery(DbEntity objectEntity,
93         List JavaDoc qualifierAttributes,
94         Collection JavaDoc nullQualifierNames,
95         int batchCapacity) {
96         
97         super(objectEntity);
98         
99         this.qualifierAttributes = qualifierAttributes;
100         this.nullQualifierNames =
101             nullQualifierNames != null ? nullQualifierNames : Collections.EMPTY_SET;
102         
103         qualifierSnapshots = new ArrayList JavaDoc(batchCapacity);
104         dbAttributes = new ArrayList JavaDoc(qualifierAttributes.size());
105         dbAttributes.addAll(qualifierAttributes);
106         // dbAttributes = getDbEntity().getPrimaryKey();
107
}
108
109     /**
110      * Creates new DeleteBatchQuery.
111      * Used by ContextCommit.categorizeFlattenedDeletesAndCreateBatches for deleting flattenned relationships.
112      *
113      * @param dbEntity Table or view to delete.
114      * @param batchCapacity Estimated size of the batch.
115      */

116     public DeleteBatchQuery(DbEntity dbEntity, int batchCapacity) {
117         this(dbEntity, dbEntity.getPrimaryKey(), Collections.EMPTY_SET, batchCapacity);
118     }
119
120
121     /**
122      * Returns true if a given attribute always has a null value
123      * in the batch.
124      *
125      * @since 1.2
126      */

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

136     public boolean isUsingOptimisticLocking() {
137         return usingOptimisticLocking;
138     }
139
140     /**
141      * @since 1.2
142      */

143     public void setUsingOptimisticLocking(boolean usingOptimisticLocking) {
144         this.usingOptimisticLocking = usingOptimisticLocking;
145     }
146
147     /**
148      * @since 1.2
149      */

150     public List JavaDoc getQualifierAttributes() {
151         return Collections.unmodifiableList(qualifierAttributes);
152     }
153
154     public void reset() {
155         qualifierIterator = qualifierSnapshots.iterator();
156         currentQualifier = Collections.EMPTY_MAP;
157     }
158
159     public boolean next() {
160         if (!qualifierIterator.hasNext())
161             return false;
162         currentQualifier = (Map JavaDoc) qualifierIterator.next();
163         currentQualifier = (currentQualifier != null ? currentQualifier : Collections.EMPTY_MAP);
164         return true;
165     }
166
167     public Object JavaDoc getValue(int dbAttributeIndex) {
168         DbAttribute attribute =
169             (DbAttribute) dbAttributes.get(dbAttributeIndex);
170         return currentQualifier.get(attribute.getName());
171     }
172
173     public void add(Map JavaDoc dataObjectId) {
174         qualifierSnapshots.add(dataObjectId);
175     }
176
177     public int size() {
178         return qualifierSnapshots.size();
179     }
180
181     public List JavaDoc getDbAttributes() {
182         return dbAttributes;
183     }
184
185     /**
186      * Returns a snapshot of the current qualifier values.
187      *
188      * @since 1.2
189      */

190     public Map JavaDoc getCurrentQualifier() {
191         return currentQualifier;
192     }
193 }
194
Popular Tags