KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.cayenne.map.DbEntity;
29 import org.apache.cayenne.query.DeleteBatchQuery;
30 import org.apache.cayenne.query.InsertBatchQuery;
31
32 /**
33  * A sync bucket that holds flattened queries.
34  *
35  * @since 1.2
36  * @author Andrus Adamchik
37  */

38 class DataDomainFlattenedBucket {
39
40     final DataDomainFlushAction parent;
41     final Map JavaDoc flattenedInsertQueries;
42     final Map JavaDoc flattenedDeleteQueries;
43
44     DataDomainFlattenedBucket(DataDomainFlushAction parent) {
45         this.parent = parent;
46         this.flattenedInsertQueries = new HashMap JavaDoc();
47         this.flattenedDeleteQueries = new HashMap JavaDoc();
48     }
49
50     boolean isEmpty() {
51         return flattenedInsertQueries.isEmpty() && flattenedDeleteQueries.isEmpty();
52     }
53
54     void addFlattenedInsert(DbEntity flattenedEntity, FlattenedArcKey flattenedInsertInfo) {
55
56         InsertBatchQuery relationInsertQuery = (InsertBatchQuery) flattenedInsertQueries
57                 .get(flattenedEntity);
58
59         if (relationInsertQuery == null) {
60             relationInsertQuery = new InsertBatchQuery(flattenedEntity, 50);
61             flattenedInsertQueries.put(flattenedEntity, relationInsertQuery);
62         }
63
64         DataNode node = parent.getDomain().lookupDataNode(flattenedEntity.getDataMap());
65         Map JavaDoc flattenedSnapshot = flattenedInsertInfo.buildJoinSnapshotForInsert(node);
66         relationInsertQuery.add(flattenedSnapshot);
67     }
68
69     void addFlattenedDelete(DbEntity flattenedEntity, FlattenedArcKey flattenedDeleteInfo) {
70
71         DeleteBatchQuery relationDeleteQuery = (DeleteBatchQuery) flattenedDeleteQueries
72                 .get(flattenedEntity);
73         if (relationDeleteQuery == null) {
74             boolean optimisticLocking = false;
75             relationDeleteQuery = new DeleteBatchQuery(flattenedEntity, 50);
76             relationDeleteQuery.setUsingOptimisticLocking(optimisticLocking);
77             flattenedDeleteQueries.put(flattenedEntity, relationDeleteQuery);
78         }
79
80         DataNode node = parent.getDomain().lookupDataNode(flattenedEntity.getDataMap());
81         List JavaDoc flattenedSnapshots = flattenedDeleteInfo.buildJoinSnapshotsForDelete(node);
82         if (!flattenedSnapshots.isEmpty()) {
83             Iterator JavaDoc snapsIt = flattenedSnapshots.iterator();
84             while (snapsIt.hasNext()) {
85                 relationDeleteQuery.add((Map JavaDoc) snapsIt.next());
86             }
87         }
88     }
89
90     void appendInserts(Collection JavaDoc queries) {
91         if (!flattenedInsertQueries.isEmpty()) {
92             queries.addAll(flattenedInsertQueries.values());
93         }
94     }
95
96     void appendDeletes(Collection JavaDoc queries) {
97         if (!flattenedDeleteQueries.isEmpty()) {
98             queries.addAll(flattenedDeleteQueries.values());
99         }
100     }
101 }
102
Popular Tags