KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > trans > LOBBatchQueryWrapper


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
21 package org.apache.cayenne.access.trans;
22
23 import java.sql.Types JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.cayenne.map.DbAttribute;
28 import org.apache.cayenne.query.BatchQuery;
29
30 /**
31  * Helper class to extract the information from BatchQueries, essential for LOB columns
32  * processing.
33  *
34  * @author Andrus Adamchik
35  */

36 public class LOBBatchQueryWrapper {
37
38     protected BatchQuery query;
39
40     protected List JavaDoc dbAttributes;
41
42     // attribute list decoders
43
protected boolean[] qualifierAttributes;
44     protected boolean[] allLOBAttributes;
45     protected Object JavaDoc[] updatedLOBAttributes;
46
47     protected boolean hasNext;
48
49     public LOBBatchQueryWrapper(BatchQuery query) {
50         this.query = query;
51         this.dbAttributes = query.getDbAttributes();
52
53         int len = dbAttributes.size();
54         this.qualifierAttributes = new boolean[len];
55         this.allLOBAttributes = new boolean[len];
56         this.updatedLOBAttributes = new Object JavaDoc[len];
57
58         indexQualifierAttributes();
59     }
60
61     public boolean next() {
62         hasNext = query.next();
63
64         if (hasNext) {
65             indexLOBAttributes();
66         }
67
68         return hasNext;
69     }
70
71     /**
72      * Indexes attributes
73      */

74     protected void indexQualifierAttributes() {
75         int len = this.dbAttributes.size();
76         for (int i = 0; i < len; i++) {
77             DbAttribute attribute = (DbAttribute) this.dbAttributes.get(i);
78             int type = attribute.getType();
79             qualifierAttributes[i] = attribute.isPrimaryKey();
80             allLOBAttributes[i] = (type == Types.BLOB || type == Types.CLOB);
81         }
82     }
83
84     /**
85      * Indexes attributes
86      */

87     protected void indexLOBAttributes() {
88         int len = updatedLOBAttributes.length;
89         for (int i = 0; i < len; i++) {
90             updatedLOBAttributes[i] = null;
91
92             if (allLOBAttributes[i]) {
93                 // skip null and empty LOBs
94
Object JavaDoc value = query.getValue(i);
95
96                 if (value == null) {
97                     continue;
98                 }
99
100                 if (((DbAttribute) dbAttributes.get(i)).getType() == Types.BLOB) {
101                     updatedLOBAttributes[i] = convertToBlobValue(value);
102                 }
103                 else {
104                     updatedLOBAttributes[i] = convertToClobValue(value);
105                 }
106             }
107         }
108     }
109
110     /**
111      * Converts value to byte[] if possible.
112      */

113     protected byte[] convertToBlobValue(Object JavaDoc value) {
114         if (value instanceof byte[]) {
115             byte[] bytes = (byte[]) value;
116             return bytes.length == 0 ? null : bytes;
117         }
118
119         return null;
120     }
121
122     /**
123      * Converts to char[] or String. Both are acceptable when writing CLOBs.
124      */

125     protected Object JavaDoc convertToClobValue(Object JavaDoc value) {
126
127         if (value instanceof char[]) {
128             char[] chars = (char[]) value;
129             return (chars.length == 0) ? null : chars;
130         }
131         else {
132             String JavaDoc strValue = value.toString();
133             return (strValue.length() == 0) ? null : strValue;
134         }
135     }
136
137     /**
138      * Returns a list of DbAttributes used in the qualifier of the query that selects a
139      * LOB row for LOB update.
140      */

141     public List JavaDoc getDbAttributesForLOBSelectQualifier() {
142
143         int len = qualifierAttributes.length;
144         List JavaDoc attributes = new ArrayList JavaDoc(len);
145
146         for (int i = 0; i < len; i++) {
147             if (this.qualifierAttributes[i]) {
148                 attributes.add(this.dbAttributes.get(i));
149             }
150         }
151         return attributes;
152     }
153
154     /**
155      * Returns a list of DbAttributes that correspond to the LOB columns updated in the
156      * current row in the batch query. The list will not include LOB attributes that are
157      * null or empty.
158      */

159     public List JavaDoc getDbAttributesForUpdatedLOBColumns() {
160         if (!hasNext) {
161             throw new IllegalStateException JavaDoc("No more rows in the BatchQuery.");
162         }
163
164         int len = updatedLOBAttributes.length;
165         List JavaDoc attributes = new ArrayList JavaDoc(len);
166
167         for (int i = 0; i < len; i++) {
168             if (this.updatedLOBAttributes[i] != null) {
169                 attributes.add(this.dbAttributes.get(i));
170             }
171         }
172         return attributes;
173     }
174
175     public List JavaDoc getValuesForLOBSelectQualifier() {
176         if (!hasNext) {
177             throw new IllegalStateException JavaDoc("No more rows in the BatchQuery.");
178         }
179
180         int len = this.qualifierAttributes.length;
181         List JavaDoc values = new ArrayList JavaDoc(len);
182         for (int i = 0; i < len; i++) {
183             if (this.qualifierAttributes[i]) {
184                 values.add(query.getValue(i));
185             }
186         }
187
188         return values;
189     }
190
191     public List JavaDoc getValuesForUpdatedLOBColumns() {
192         if (!hasNext) {
193             throw new IllegalStateException JavaDoc("No more rows in the BatchQuery.");
194         }
195
196         int len = this.updatedLOBAttributes.length;
197         List JavaDoc values = new ArrayList JavaDoc(len);
198         for (int i = 0; i < len; i++) {
199             if (this.updatedLOBAttributes[i] != null) {
200                 values.add(this.updatedLOBAttributes[i]);
201             }
202         }
203
204         return values;
205     }
206
207     /**
208      * Returns wrapped BatchQuery.
209      */

210     public BatchQuery getQuery() {
211         return query;
212     }
213 }
214
Popular Tags