01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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: package org.apache.openjpa.jdbc.meta.strats;
20:
21: import java.lang.reflect.Array;
22: import java.sql.ResultSet;
23: import java.sql.SQLException;
24:
25: import org.apache.openjpa.jdbc.kernel.JDBCStore;
26: import org.apache.openjpa.jdbc.meta.JavaSQLTypes;
27: import org.apache.openjpa.jdbc.schema.Column;
28: import org.apache.openjpa.jdbc.sql.DBDictionary;
29: import org.apache.openjpa.jdbc.sql.Joins;
30: import org.apache.openjpa.jdbc.sql.Result;
31: import org.apache.openjpa.jdbc.sql.Row;
32: import org.apache.openjpa.kernel.OpenJPAStateManager;
33: import org.apache.openjpa.lib.util.Localizer;
34: import org.apache.openjpa.util.MetaDataException;
35:
36: /**
37: * Mapping for byte array fields on a dictionary that has a maximum embedded
38: * BLOB size.
39: *
40: * @author Abe White
41: * @nojavadoc
42: * @since 0.4.0
43: */
44: public class MaxEmbeddedByteArrayFieldStrategy extends
45: MaxEmbeddedLobFieldStrategy {
46:
47: private static final Localizer _loc = Localizer
48: .forPackage(MaxEmbeddedByteArrayFieldStrategy.class);
49:
50: private int _maxSize = 0;
51:
52: protected int getExpectedJavaType() {
53: return JavaSQLTypes.BYTES;
54: }
55:
56: protected void update(OpenJPAStateManager sm, Row row)
57: throws SQLException {
58: byte[] b = PrimitiveWrapperArrays.toByteArray(sm
59: .fetchObject(field.getIndex()));
60: if (b == null || b.length > _maxSize)
61: row.setBytes(field.getColumns()[0], null);
62: else
63: row.setBytes(field.getColumns()[0], b);
64: }
65:
66: protected Boolean isCustom(OpenJPAStateManager sm, JDBCStore store) {
67: Object val = sm.fetchObject(field.getIndex());
68: return (val != null && Array.getLength(val) > _maxSize) ? null
69: : Boolean.FALSE;
70: }
71:
72: protected void putData(OpenJPAStateManager sm, ResultSet rs,
73: DBDictionary dict) throws SQLException {
74: Object blob = rs.getBlob(1);
75: dict.putBytes(blob, PrimitiveWrapperArrays.toByteArray(sm
76: .fetchObject(field.getIndex())));
77: }
78:
79: protected Object load(Column col, Result res, Joins joins)
80: throws SQLException {
81: return PrimitiveWrapperArrays.toObjectValue(field, (byte[]) res
82: .getBytes(col, joins));
83: }
84:
85: public void map(boolean adapt) {
86: if (field.getType() != byte[].class
87: && field.getType() != Byte[].class)
88: throw new MetaDataException(_loc.get("not-bytes", field));
89: super .map(adapt);
90: }
91:
92: public void initialize() {
93: DBDictionary dict = field.getMappingRepository()
94: .getDBDictionary();
95: _maxSize = dict.maxEmbeddedBlobSize;
96: }
97: }
|