KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.cayenne.ObjectId;
27 import org.apache.cayenne.map.DbAttribute;
28 import org.apache.cayenne.map.DbEntity;
29
30 /**
31  * Batched INSERT query. Allows inserting multiple object snapshots (DataRows) for a given
32  * DbEntity in a single query. InsertBatchQuery normally is not used directly. Rather
33  * DataContext creates one internally when committing DataObjects.
34  *
35  * @author Andriy Shapochka
36  */

37 public class InsertBatchQuery extends BatchQuery {
38
39     /**
40      * @since 1.2
41      */

42     protected List JavaDoc objectIds;
43
44     protected List JavaDoc objectSnapshots;
45     protected List JavaDoc dbAttributes;
46
47     /**
48      * Creates new InsertBatchQuery for a given DbEntity and estimated capacity.
49      */

50     public InsertBatchQuery(DbEntity entity, int batchCapacity) {
51         super(entity);
52
53         this.objectSnapshots = new ArrayList JavaDoc(batchCapacity);
54         this.objectIds = new ArrayList JavaDoc(batchCapacity);
55         this.dbAttributes = new ArrayList JavaDoc(getDbEntity().getAttributes());
56     }
57
58     public Object JavaDoc getValue(int dbAttributeIndex) {
59         DbAttribute attribute = (DbAttribute) dbAttributes.get(dbAttributeIndex);
60         Map JavaDoc currentSnapshot = (Map JavaDoc) objectSnapshots.get(batchIndex);
61         return getValue(currentSnapshot, attribute);
62     }
63
64     /**
65      * Adds a snapshot to batch. A shortcut for "add(snapshot, null)".
66      */

67     public void add(Map JavaDoc snapshot) {
68         add(snapshot, null);
69     }
70
71     /**
72      * Adds a snapshot to batch. Optionally stores the object id for the snapshot. Note
73      * that snapshot can hold either the real values or the instances of
74      * org.apache.commons.collections.Factory that will be resolved to the actual value on
75      * the spot, thus allowing deferred propagated keys resolution.
76      *
77      * @since 1.2
78      */

79     public void add(Map JavaDoc snapshot, ObjectId id) {
80         objectSnapshots.add(snapshot);
81         objectIds.add(id);
82     }
83
84     public int size() {
85         return objectSnapshots.size();
86     }
87
88     public List JavaDoc getDbAttributes() {
89         return dbAttributes;
90     }
91
92     /**
93      * Returns an ObjectId associated with the current batch iteration. Used internally by
94      * Cayenne to match current iteration with a specific object and assign it generated
95      * keys.
96      *
97      * @since 1.2
98      */

99     public ObjectId getObjectId() {
100         return (ObjectId) objectIds.get(batchIndex);
101     }
102 }
103
Popular Tags