001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.jdbc.meta.strats;
020:
021: import java.sql.ResultSet;
022: import java.sql.SQLException;
023:
024: import org.apache.openjpa.jdbc.kernel.JDBCStore;
025: import org.apache.openjpa.jdbc.sql.DBDictionary;
026: import org.apache.openjpa.jdbc.sql.Row;
027: import org.apache.openjpa.jdbc.sql.SQLExceptions;
028: import org.apache.openjpa.kernel.OpenJPAStateManager;
029: import org.apache.openjpa.lib.util.Localizer;
030: import org.apache.openjpa.meta.JavaTypes;
031: import org.apache.openjpa.util.MetaDataException;
032:
033: /**
034: * Mapping for serialized fields on a dictionary that has a maximum embedded
035: * BLOB size.
036: *
037: * @author Abe White
038: * @nojavadoc
039: * @since 0.4.0
040: */
041: public class MaxEmbeddedBlobFieldStrategy extends
042: MaxEmbeddedLobFieldStrategy {
043:
044: private static final Localizer _loc = Localizer
045: .forPackage(MaxEmbeddedBlobFieldStrategy.class);
046:
047: private int _maxSize = 0;
048:
049: protected int getExpectedJavaType() {
050: return JavaTypes.OBJECT;
051: }
052:
053: protected void update(OpenJPAStateManager sm, Row row)
054: throws SQLException {
055: byte[] b = (byte[]) sm.getImplData(field.getIndex());
056: if (b == null || b.length > _maxSize)
057: row.setNull(field.getColumns()[0], true);
058: else {
059: sm.setImplData(field.getIndex(), null);
060: DBDictionary.SerializedData dat = new DBDictionary.SerializedData(
061: b);
062: row.setObject(field.getColumns()[0], dat);
063: }
064: }
065:
066: protected Boolean isCustom(OpenJPAStateManager sm, JDBCStore store) {
067: // have we already stored our serialized data?
068: byte[] b = (byte[]) sm.getImplData(field.getIndex());
069: if (b == null) {
070: Object o = sm.fetch(field.getIndex());
071: if (o == null)
072: return Boolean.FALSE;
073:
074: // serialize field value
075: DBDictionary dict = field.getMappingRepository()
076: .getDBDictionary();
077: try {
078: b = dict.serialize(o, store);
079: } catch (SQLException se) {
080: throw SQLExceptions.getStore(se, dict);
081: }
082:
083: // set in impl data so that we don't have to re-serialize on store
084: sm.setImplData(field.getIndex(), b);
085: }
086: return (b.length > _maxSize) ? null : Boolean.FALSE;
087: }
088:
089: protected void putData(OpenJPAStateManager sm, ResultSet rs,
090: DBDictionary dict) throws SQLException {
091: byte[] b = (byte[]) sm.setImplData(field.getIndex(), null);
092: Object blob = rs.getBlob(1);
093: dict.putBytes(blob, b);
094: }
095:
096: public void map(boolean adapt) {
097: if (!field.isSerialized())
098: throw new MetaDataException(_loc.get("not-serialized",
099: field));
100: super .map(adapt);
101: }
102:
103: public void initialize() {
104: DBDictionary dict = field.getMappingRepository()
105: .getDBDictionary();
106: _maxSize = dict.maxEmbeddedBlobSize;
107: field.setUsesImplData(Boolean.TRUE);
108: }
109: }
|