KickJava   Java API By Example, From Geeks To Geeks.

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


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.map.DbAttribute;
29 import org.apache.cayenne.map.DbEntity;
30
31 /**
32  * Batched delete query.
33  *
34  * @author Andriy Shapochka
35  */

36 public class DeleteBatchQuery extends BatchQuery {
37
38     protected List JavaDoc qualifierSnapshots;
39     protected List JavaDoc dbAttributes;
40     protected boolean usingOptimisticLocking;
41
42     private List JavaDoc qualifierAttributes;
43     private Collection JavaDoc nullQualifierNames;
44
45     /**
46      * Creates new DeleteBatchQuery. Used by
47      * ContextCommit.categorizeFlattenedDeletesAndCreateBatches for deleting flattenned
48      * relationships.
49      *
50      * @param dbEntity Table or view to delete.
51      * @param batchCapacity Estimated size of the batch.
52      */

53     public DeleteBatchQuery(DbEntity dbEntity, int batchCapacity) {
54         this(dbEntity, dbEntity.getPrimaryKey(), Collections.EMPTY_SET, batchCapacity);
55     }
56
57     /**
58      * Creates new DeleteBatchQuery.
59      *
60      * @param dbEntity Table or view to delete.
61      * @param qualifierAttributes DbAttributes used in the WHERE clause.
62      * @param nullQualifierNames DbAttribute names in the WHERE clause that have null
63      * values.
64      * @param batchCapacity Estimated size of the batch.
65      */

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

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

96     public boolean isUsingOptimisticLocking() {
97         return usingOptimisticLocking;
98     }
99
100     /**
101      * @since 1.2
102      */

103     public void setUsingOptimisticLocking(boolean usingOptimisticLocking) {
104         this.usingOptimisticLocking = usingOptimisticLocking;
105     }
106
107     /**
108      * @since 1.2
109      */

110     public List JavaDoc getQualifierAttributes() {
111         return Collections.unmodifiableList(qualifierAttributes);
112     }
113
114     public Object JavaDoc getValue(int dbAttributeIndex) {
115         DbAttribute attribute = (DbAttribute) dbAttributes.get(dbAttributeIndex);
116         return getCurrentQualifier().get(attribute.getName());
117     }
118
119     public void add(Map JavaDoc dataObjectId) {
120         qualifierSnapshots.add(dataObjectId);
121     }
122
123     public int size() {
124         return qualifierSnapshots.size();
125     }
126
127     public List JavaDoc getDbAttributes() {
128         return dbAttributes;
129     }
130
131     /**
132      * Returns a snapshot of the current qualifier values.
133      *
134      * @since 1.2
135      */

136     public Map JavaDoc getCurrentQualifier() {
137         return (Map JavaDoc) qualifierSnapshots.get(batchIndex);
138     }
139 }
140
Popular Tags