KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > DataDomainDeleteBucket


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.access;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedHashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import org.apache.cayenne.Persistent;
32 import org.apache.cayenne.map.DbEntity;
33 import org.apache.cayenne.map.EntitySorter;
34 import org.apache.cayenne.query.DeleteBatchQuery;
35 import org.apache.cayenne.reflect.ClassDescriptor;
36
37 /**
38  * @since 1.2
39  * @author Andrus Adamchik
40  */

41 class DataDomainDeleteBucket extends DataDomainSyncBucket {
42
43     DataDomainDeleteBucket(DataDomainFlushAction parent) {
44         super(parent);
45     }
46
47     void postprocess() {
48
49         if (!objectsByDescriptor.isEmpty()) {
50
51             Collection JavaDoc deletedIds = parent.getResultDeletedIds();
52
53             Iterator JavaDoc it = objectsByDescriptor.values().iterator();
54             while (it.hasNext()) {
55                 Iterator JavaDoc objects = ((Collection JavaDoc) it.next()).iterator();
56                 while (objects.hasNext()) {
57                     Persistent object = (Persistent) objects.next();
58                     deletedIds.add(object.getObjectId());
59                 }
60             }
61         }
62     }
63
64     void appendQueriesInternal(Collection JavaDoc queries) {
65
66         DataNodeSyncQualifierDescriptor qualifierBuilder = new DataNodeSyncQualifierDescriptor();
67
68         EntitySorter sorter = parent.getDomain().getEntitySorter();
69         sorter.sortDbEntities(dbEntities, true);
70
71         for (Iterator JavaDoc i = dbEntities.iterator(); i.hasNext();) {
72             DbEntity dbEntity = (DbEntity) i.next();
73             List JavaDoc objEntitiesForDbEntity = (List JavaDoc) descriptorsByDbEntity.get(dbEntity);
74             Map JavaDoc batches = new LinkedHashMap JavaDoc();
75
76             for (Iterator JavaDoc j = objEntitiesForDbEntity.iterator(); j.hasNext();) {
77                 ClassDescriptor descriptor = (ClassDescriptor) j.next();
78
79                 qualifierBuilder.reset(descriptor.getEntity(), dbEntity);
80
81                 boolean isRootDbEntity = (descriptor.getEntity().getDbEntity() == dbEntity);
82
83                 // remove object set for dependent entity, so that it does not show up
84
// on post processing
85
List JavaDoc objects = (List JavaDoc) objectsByDescriptor.get(descriptor);
86
87                 if (objects.isEmpty()) {
88                     continue;
89                 }
90
91                 checkReadOnly(descriptor.getEntity());
92
93                 if (isRootDbEntity) {
94                     sorter.sortObjectsForEntity(descriptor.getEntity(), objects, true);
95                 }
96
97                 for (Iterator JavaDoc k = objects.iterator(); k.hasNext();) {
98                     Persistent o = (Persistent) k.next();
99                     ObjectDiff diff = parent.objectDiff(o.getObjectId());
100
101                     Map JavaDoc qualifierSnapshot = qualifierBuilder
102                             .createQualifierSnapshot(diff);
103
104                     // organize batches by the nulls in qualifier
105
Set JavaDoc nullQualifierNames = new HashSet JavaDoc();
106                     Iterator JavaDoc it = qualifierSnapshot.entrySet().iterator();
107                     while (it.hasNext()) {
108                         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
109                         if (entry.getValue() == null) {
110                             nullQualifierNames.add(entry.getKey());
111                         }
112                     }
113
114                     List JavaDoc batchKey = Arrays.asList(new Object JavaDoc[] {
115                         nullQualifierNames
116                     });
117
118                     DeleteBatchQuery batch = (DeleteBatchQuery) batches.get(batchKey);
119                     if (batch == null) {
120                         batch = new DeleteBatchQuery(dbEntity, qualifierBuilder
121                                 .getAttributes(), nullQualifierNames, 27);
122                         batch.setUsingOptimisticLocking(qualifierBuilder
123                                 .isUsingOptimisticLocking());
124                         batches.put(batchKey, batch);
125                     }
126
127                     batch.add(qualifierSnapshot);
128                 }
129             }
130
131             queries.addAll(batches.values());
132         }
133     }
134 }
135
Popular Tags