KickJava   Java API By Example, From Geeks To Geeks.

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


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

44 class DataDomainUpdateBucket extends DataDomainSyncBucket {
45
46     DataDomainUpdateBucket(DataDomainFlushAction parent) {
47         super(parent);
48     }
49
50     void appendQueriesInternal(Collection JavaDoc queries) {
51
52         DataDomainDBDiffBuilder diffBuilder = new DataDomainDBDiffBuilder();
53         DataNodeSyncQualifierDescriptor qualifierBuilder = new DataNodeSyncQualifierDescriptor();
54
55         for (Iterator JavaDoc i = dbEntities.iterator(); i.hasNext();) {
56             DbEntity dbEntity = (DbEntity) i.next();
57             List JavaDoc objEntitiesForDbEntity = (List JavaDoc) descriptorsByDbEntity.get(dbEntity);
58             Map JavaDoc batches = new LinkedHashMap JavaDoc();
59
60             for (Iterator JavaDoc j = objEntitiesForDbEntity.iterator(); j.hasNext();) {
61                 ClassDescriptor descriptor = (ClassDescriptor) j.next();
62                 ObjEntity entity = descriptor.getEntity();
63
64                 diffBuilder.reset(entity, dbEntity);
65                 qualifierBuilder.reset(entity, dbEntity);
66                 boolean isRootDbEntity = entity.getDbEntity() == dbEntity;
67
68                 List JavaDoc objects = (List JavaDoc) objectsByDescriptor.get(descriptor);
69
70                 for (Iterator JavaDoc k = objects.iterator(); k.hasNext();) {
71                     Persistent o = (Persistent) k.next();
72                     ObjectDiff diff = parent.objectDiff(o.getObjectId());
73
74                     Map JavaDoc snapshot = diffBuilder.buildDBDiff(diff);
75
76                     // check whether MODIFIED object has real db-level modifications
77
if (snapshot == null) {
78
79                         if (isRootDbEntity) {
80                             k.remove();
81                             o.setPersistenceState(PersistenceState.COMMITTED);
82                         }
83
84                         continue;
85                     }
86
87                     // after we filtered out "fake" modifications, check if an
88
// attempt is made to modify a read only entity
89
checkReadOnly(entity);
90
91                     Map JavaDoc qualifierSnapshot = qualifierBuilder
92                             .createQualifierSnapshot(diff);
93
94                     // organize batches by the updated columns + nulls in qualifier
95
Set JavaDoc snapshotSet = snapshot.keySet();
96                     Set JavaDoc nullQualifierNames = new HashSet JavaDoc();
97                     Iterator JavaDoc it = qualifierSnapshot.entrySet().iterator();
98                     while (it.hasNext()) {
99                         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
100                         if (entry.getValue() == null) {
101                             nullQualifierNames.add(entry.getKey());
102                         }
103                     }
104
105                     List JavaDoc batchKey = Arrays.asList(new Object JavaDoc[] {
106                             snapshotSet, nullQualifierNames
107                     });
108
109                     UpdateBatchQuery batch = (UpdateBatchQuery) batches.get(batchKey);
110                     if (batch == null) {
111                         batch = new UpdateBatchQuery(
112                                 dbEntity,
113                                 qualifierBuilder.getAttributes(),
114                                 updatedAttributes(dbEntity, snapshot),
115                                 nullQualifierNames,
116                                 10);
117
118                         batch.setUsingOptimisticLocking(qualifierBuilder
119                                 .isUsingOptimisticLocking());
120                         batches.put(batchKey, batch);
121                     }
122
123                     batch.add(qualifierSnapshot, snapshot, o.getObjectId());
124
125                     // update replacement id with meaningful PK changes
126
if (isRootDbEntity) {
127                         Map JavaDoc replacementId = o.getObjectId().getReplacementIdMap();
128
129                         Iterator JavaDoc pkIt = dbEntity.getPrimaryKey().iterator();
130                         while (pkIt.hasNext()) {
131                             String JavaDoc name = ((DbAttribute) pkIt.next()).getName();
132                             if (snapshot.containsKey(name)
133                                     && !replacementId.containsKey(name)) {
134                                 replacementId.put(name, snapshot.get(name));
135                             }
136                         }
137                     }
138                 }
139             }
140
141             queries.addAll(batches.values());
142         }
143     }
144
145     /**
146      * Creates a list of DbAttributes that are updated in a snapshot
147      */

148     private List JavaDoc updatedAttributes(DbEntity entity, Map JavaDoc updatedSnapshot) {
149         List JavaDoc attributes = new ArrayList JavaDoc(updatedSnapshot.size());
150         Map JavaDoc entityAttributes = entity.getAttributeMap();
151
152         Iterator JavaDoc it = updatedSnapshot.keySet().iterator();
153         while (it.hasNext()) {
154             Object JavaDoc name = it.next();
155             attributes.add(entityAttributes.get(name));
156         }
157
158         return attributes;
159     }
160 }
161
Popular Tags