KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.access.trans;
58
59 import java.sql.Types JavaDoc;
60 import java.util.ArrayList JavaDoc;
61 import java.util.List JavaDoc;
62
63 import org.apache.log4j.Level;
64 import org.objectstyle.cayenne.map.DbAttribute;
65 import org.objectstyle.cayenne.query.BatchQuery;
66
67 /**
68  * Helper class to extract the information from BatchQueries, essential for
69  * LOB columns processing.
70  *
71  * @author Andrei Adamchik
72  */

73 public class LOBBatchQueryWrapper {
74     protected BatchQuery query;
75
76     protected List JavaDoc dbAttributes;
77
78     // attribute list decoders
79
protected boolean[] qualifierAttributes;
80     protected boolean[] allLOBAttributes;
81     protected Object JavaDoc[] updatedLOBAttributes;
82
83     protected boolean hasNext;
84
85     public LOBBatchQueryWrapper(BatchQuery query) {
86         this.query = query;
87         this.dbAttributes = query.getDbAttributes();
88
89         int len = dbAttributes.size();
90         this.qualifierAttributes = new boolean[len];
91         this.allLOBAttributes = new boolean[len];
92         this.updatedLOBAttributes = new Object JavaDoc[len];
93
94         indexQualifierAttributes();
95     }
96
97     public boolean next() {
98         hasNext = query.next();
99
100         if (hasNext) {
101             indexLOBAttributes();
102         }
103
104         return hasNext;
105     }
106
107     /**
108       * Indexes attributes
109       */

110     protected void indexQualifierAttributes() {
111         int len = this.dbAttributes.size();
112         for (int i = 0; i < len; i++) {
113             DbAttribute attribute = (DbAttribute) this.dbAttributes.get(i);
114             int type = attribute.getType();
115             qualifierAttributes[i] = attribute.isPrimaryKey();
116             allLOBAttributes[i] = (type == Types.BLOB || type == Types.CLOB);
117         }
118     }
119
120     /**
121      * Indexes attributes
122      */

123     protected void indexLOBAttributes() {
124         int len = updatedLOBAttributes.length;
125         for (int i = 0; i < len; i++) {
126             updatedLOBAttributes[i] = null;
127
128             if (allLOBAttributes[i]) {
129                 // skip null and empty LOBs
130
Object JavaDoc value = query.getValue(i);
131
132                 if (value == null) {
133                     continue;
134                 }
135
136                 if (((DbAttribute) dbAttributes.get(i)).getType() == Types.BLOB) {
137                     updatedLOBAttributes[i] = convertToBlobValue(value);
138                 }
139                 else {
140                     updatedLOBAttributes[i] = convertToClobValue(value);
141                 }
142             }
143         }
144     }
145
146     /**
147      * Converts value to byte[] if possible.
148      */

149     protected byte[] convertToBlobValue(Object JavaDoc value) {
150         if (value instanceof byte[]) {
151             byte[] bytes = (byte[]) value;
152             return bytes.length == 0 ? null : bytes;
153         }
154
155         return null;
156     }
157
158     /**
159      * Converts to char[] or String. Both are acceptable when writing CLOBs.
160      */

161     protected Object JavaDoc convertToClobValue(Object JavaDoc value) {
162
163         if (value instanceof char[]) {
164             char[] chars = (char[]) value;
165             return (chars.length == 0) ? null : chars;
166         }
167         else {
168             String JavaDoc strValue = value.toString();
169             return (strValue.length() == 0) ? null : strValue;
170         }
171     }
172
173     /**
174      * Returns a list of DbAttributes used in the qualifier of the query
175      * that selects a LOB row for LOB update.
176      */

177     public List JavaDoc getDbAttributesForLOBSelectQualifier() {
178
179         int len = qualifierAttributes.length;
180         List JavaDoc attributes = new ArrayList JavaDoc(len);
181
182         for (int i = 0; i < len; i++) {
183             if (this.qualifierAttributes[i]) {
184                 attributes.add(this.dbAttributes.get(i));
185             }
186         }
187         return attributes;
188     }
189
190     /**
191      * Returns a list of DbAttributes that correspond to
192      * the LOB columns updated in the current row in the batch query.
193      * The list will not include LOB attributes that are null or empty.
194      */

195     public List JavaDoc getDbAttributesForUpdatedLOBColumns() {
196         if (!hasNext) {
197             throw new IllegalStateException JavaDoc("No more rows in the BatchQuery.");
198         }
199
200         int len = updatedLOBAttributes.length;
201         List JavaDoc attributes = new ArrayList JavaDoc(len);
202
203         for (int i = 0; i < len; i++) {
204             if (this.updatedLOBAttributes[i] != null) {
205                 attributes.add(this.dbAttributes.get(i));
206             }
207         }
208         return attributes;
209     }
210
211     public List JavaDoc getValuesForLOBSelectQualifier() {
212         if (!hasNext) {
213             throw new IllegalStateException JavaDoc("No more rows in the BatchQuery.");
214         }
215
216         int len = this.qualifierAttributes.length;
217         List JavaDoc values = new ArrayList JavaDoc(len);
218         for (int i = 0; i < len; i++) {
219             if (this.qualifierAttributes[i]) {
220                 values.add(query.getValue(i));
221             }
222         }
223         
224         return values;
225     }
226     
227     public List JavaDoc getValuesForUpdatedLOBColumns() {
228         if (!hasNext) {
229             throw new IllegalStateException JavaDoc("No more rows in the BatchQuery.");
230         }
231
232         int len = this.updatedLOBAttributes.length;
233         List JavaDoc values = new ArrayList JavaDoc(len);
234         for (int i = 0; i < len; i++) {
235             if (this.updatedLOBAttributes[i] != null) {
236                 values.add(this.updatedLOBAttributes[i]);
237             }
238         }
239         
240         return values;
241     }
242
243     /**
244      * Returns wrapped BatchQuery.
245      */

246     public BatchQuery getQuery() {
247         return query;
248     }
249     
250     public Level getLoggingLevel() {
251         return this.query.getLoggingLevel();
252     }
253 }
254
Popular Tags