/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.openjpa.jdbc.meta.strats;

import java.io.InputStream;
import java.io.ObjectOutput;
import java.io.Reader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;

import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.enhance.StateManager;
import org.apache.openjpa.jdbc.kernel.EagerFetchModes;
import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration;
import org.apache.openjpa.jdbc.kernel.JDBCStore;
import org.apache.openjpa.jdbc.meta.ClassMapping;
import org.apache.openjpa.jdbc.meta.Embeddable;
import org.apache.openjpa.jdbc.meta.FieldMapping;
import org.apache.openjpa.jdbc.meta.JavaSQLTypes;
import org.apache.openjpa.jdbc.meta.Joinable;
import org.apache.openjpa.jdbc.meta.RelationId;
import org.apache.openjpa.jdbc.meta.ValueMappingInfo;
import org.apache.openjpa.jdbc.schema.Column;
import org.apache.openjpa.jdbc.schema.ColumnIO;
import org.apache.openjpa.jdbc.schema.ForeignKey;
import org.apache.openjpa.jdbc.schema.Table;
import org.apache.openjpa.jdbc.sql.Joins;
import org.apache.openjpa.jdbc.sql.Result;
import org.apache.openjpa.jdbc.sql.Row;
import org.apache.openjpa.jdbc.sql.RowManager;
import org.apache.openjpa.jdbc.sql.SQLBuffer;
import org.apache.openjpa.jdbc.sql.Select;
import org.apache.openjpa.kernel.FetchConfiguration;
import org.apache.openjpa.kernel.OpenJPAStateManager;
import org.apache.openjpa.kernel.PCState;
import org.apache.openjpa.kernel.StateManagerImpl;
import org.apache.openjpa.kernel.StoreContext;
import org.apache.openjpa.lib.util.Localizer;
import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.meta.FetchGroup;
import org.apache.openjpa.meta.FieldMetaData;
import org.apache.openjpa.meta.JavaTypes;
import org.apache.openjpa.meta.MetaDataModes;
import org.apache.openjpa.meta.ValueMetaData;
import org.apache.openjpa.util.InternalException;
import org.apache.openjpa.util.MetaDataException;

/**
 * Mapping for an embedded persistent object.
 *
 * @author Abe White
 * @since 0.4.0
 */
public class EmbedFieldStrategy
    extends AbstractFieldStrategy
    implements Embeddable {

    
    private static final long serialVersionUID = 1L;
    private static final int INSERT = 0;
    private static final int UPDATE = 1;
    private static final int DELETE = 2;

    private static final Localizer _loc = Localizer.forPackage
        (EmbedFieldStrategy.class);

    private boolean _synthetic = false;

    @Override
    public void map(boolean adapt) {
        if (field.getEmbeddedMetaData() == null)
            throw new MetaDataException(_loc.get("not-embed", field));
        assertNotMappedBy();

        // map join key (if any)
        field.mapJoin(adapt, false);
        field.getKeyMapping().getValueInfo().assertNoSchemaComponents
            (field.getKey(), !adapt);
        field.getElementMapping().getValueInfo().assertNoSchemaComponents
            (field.getElement(), !adapt);

        ValueMappingInfo vinfo = field.getValueInfo();
        vinfo.assertNoJoin(field, true);
        vinfo.assertNoForeignKey(field, !adapt);
        vinfo.assertNoUnique(field, !adapt);
        vinfo.assertNoIndex(field, !adapt);

        // before we map the null indicator column, we need to make sure our
        // value is mapped so we can tell whether the column is synthetic
        field.getValueMapping().resolve(MetaDataModes.MODE_META | MetaDataModes.MODE_MAPPING);
        Column col = vinfo.getNullIndicatorColumn(field, field.getName(),
            field.getTable(), adapt);
        if (col != null) {
            field.setColumns(new Column[]{ col });
            field.setColumnIO(vinfo.getColumnIO());
        }

        field.mapPrimaryKey(adapt);
    }

    @Override
    public void initialize() {
        Column[] cols = field.getColumns();
        if (cols.length != 1)
            _synthetic = false;
        else {
            // do any of the embedded field mappings use this column?
            // if not, consider it synthetic
            Column col = cols[0];
            boolean found = false;
            FieldMapping[] fields = field.getEmbeddedMapping().
                getFieldMappings();
            for (int i = 0; !found && i < fields.length; i++) {
                cols = fields[i].getColumns();
                for (Column column : cols)
                    if (column.equals(col))
                        found = true;
            }
            _synthetic = !found;
        }
    }

    @Override
    public void insert(OpenJPAStateManager sm, JDBCStore store, RowManager rm)
        throws SQLException {
        Row row = field.getRow(sm, store, rm, Row.ACTION_INSERT);
        if (row == null)
            return;

        OpenJPAStateManager em = store.getContext().getStateManager
            (sm.fetchObject(field.getIndex()));
        insert(sm, em, store, rm, row);
    }

    /**
     * Insert an embedded object.
     *
     * @param owner the owning state manager
     * @param sm the embedded state manager, or null if the value is null
     * @param rm the row manager
     * @param row expected row for this embedded value
     */
    public void insert(OpenJPAStateManager owner, OpenJPAStateManager sm,
        JDBCStore store, RowManager rm, Row row)
        throws SQLException {
        OpenJPAStateManager em = sm;
        if (em == null)
            em = new NullEmbeddedStateManager(owner, field);
        rm = new EmbeddedRowManager(rm, row);
        FieldMapping[] fields = field.getEmbeddedMapping().getFieldMappings();
        for (FieldMapping fieldMapping : fields)
            if (!Boolean.TRUE.equals(fieldMapping.isCustomInsert(em, store)))
                if (!fieldMapping.isPrimaryKey())
                    fieldMapping.insert(em, store, rm);

        if (field.getColumnIO().isInsertable(0, true))
            setNullIndicatorColumn(sm, row);
    }

    @Override
    public void update(OpenJPAStateManager sm, JDBCStore store, RowManager rm)
        throws SQLException {
        OpenJPAStateManager em = store.getContext().getStateManager
            (sm.fetchObject(field.getIndex()));
        boolean newVal = em == null || em.getPCState() == PCState.ECOPY;

        Row row = null;
        if (newVal && field.getJoinForeignKey() != null
            && field.isJoinOuter()) {
            // if our record is in an outer-joined related table and we're not
            // updating an existing value, delete the old one
            Row del = rm.getRow(field.getTable(), Row.ACTION_DELETE, sm, true);
            del.whereForeignKey(field.getJoinForeignKey(), sm);
            delete(sm, null, store, rm, del);

            // insert the new value
            row = rm.getRow(field.getTable(), Row.ACTION_INSERT, sm,
                em != null);
        } else
            row = rm.getRow(field.getTable(), Row.ACTION_UPDATE, sm, true);
        if (row == null)
            return;

        if (row.getAction() == Row.ACTION_INSERT)
            insert(sm, em, store, rm, row);
        else
            update(sm, em, store, rm, row);
    }

    /**
     * Update an embedded object.
     *
     * @param owner the owning state manager
     * @param sm the embedded state manager, or null if the value is null
     * @param rm the row manager
     * @param row expected row for this embedded value
     */
    public void update(OpenJPAStateManager owner, OpenJPAStateManager sm,
        JDBCStore store, RowManager rm, Row row)
        throws SQLException {
        OpenJPAStateManager em = sm;
        if (em == null)
            em = new NullEmbeddedStateManager(owner, field);
        rm = new EmbeddedRowManager(rm, row);
        FieldMapping[] fields = field.getEmbeddedMapping().getFieldMappings();
        for (int i = 0; i < fields.length; i++)
            if (em.getDirty().get(i)
                && !em.getFlushed().get(i)
                && !Boolean.TRUE.equals(fields[i].isCustomUpdate(em, store)))
                fields[i].update(em, store, rm);

        if (field.getColumnIO().isUpdatable(0, true))
            setNullIndicatorColumn(sm, row);
    }

    @Override
    public void delete(OpenJPAStateManager sm, JDBCStore store, RowManager rm)
        throws SQLException {
        OpenJPAStateManager em = null;
        if (sm.getLoaded().get(field.getIndex()))
            em = store.getContext().getStateManager(sm.fetchObject
                (field.getIndex()));
        Row row = field.getRow(sm, store, rm, Row.ACTION_DELETE);
        delete(sm, em, store, rm, row);
    }

    /**
     * Delete an embedded object record.
     *
     * @param owner the owning state manager
     * @param sm the embedded state manager, or null if not known
     * @param rm the row manager
     * @param row expected row for this embedded value
     */
    public void delete(OpenJPAStateManager owner, OpenJPAStateManager sm,
        JDBCStore store, RowManager rm, Row row)
        throws SQLException {
        rm = new EmbeddedRowManager(rm, row);
        if (sm == null)
            sm = new NullEmbeddedStateManager(owner, field);
        FieldMapping[] fields = field.getEmbeddedMapping().getFieldMappings();
        for (FieldMapping fieldMapping : fields)
            if (!Boolean.TRUE.equals(fieldMapping.isCustomDelete(sm, store)))
                fieldMapping.delete(sm, store, rm);
    }

    /**
     * Set the proper null indicator value into the given row.
     */
    private void setNullIndicatorColumn(OpenJPAStateManager sm, Row row)
        throws SQLException {
        if (!_synthetic)
            return;

        Column col = field.getColumns()[0];
        Object val = ((EmbeddedClassStrategy) field.getEmbeddedMapping().
            getStrategy()).getNullIndicatorValue(sm);
        if (val == null)
            row.setNull(col, true);
        else
            row.setObject(col, val);
    }

    @Override
    public Boolean isCustomInsert(OpenJPAStateManager sm, JDBCStore store) {
        OpenJPAStateManager em = sm.getContext().getStateManager(sm.fetchObject
            (field.getIndex()));
        Boolean custom = isCustom(INSERT, sm, em, store);
        if (Boolean.TRUE.equals(custom) && _synthetic)
            return null;
        return custom;
    }

    @Override
    public Boolean isCustomUpdate(OpenJPAStateManager sm, JDBCStore store) {
        OpenJPAStateManager em = sm.getContext().getStateManager(sm.fetchObject
            (field.getIndex()));
        Boolean custom = isCustom(UPDATE, sm, em, store);
        if (Boolean.TRUE.equals(custom) && _synthetic)
            return null;
        return custom;
    }

    @Override
    public Boolean isCustomDelete(OpenJPAStateManager sm, JDBCStore store) {
        OpenJPAStateManager em = sm.getContext().getStateManager(sm.fetchObject
            (field.getIndex()));
        return isCustom(DELETE, sm, em, store);
    }

    /**
     * Return whether the given action requires custom handling for any fields.
     */
    private Boolean isCustom(int action, OpenJPAStateManager owner,
        OpenJPAStateManager sm, JDBCStore store) {
        if (sm == null)
            sm = new NullEmbeddedStateManager(owner, field);
        boolean hasCustom = false;
        boolean hasStd = false;

        FieldMapping[] fields = field.getEmbeddedMapping().getFieldMappings();
        Boolean custom = null;
        for (FieldMapping fieldMapping : fields) {
            switch (action) {
                case INSERT:
                    custom = fieldMapping.isCustomInsert(sm, store);
                    break;
                case UPDATE:
                    custom = fieldMapping.isCustomUpdate(sm, store);
                    break;
                case DELETE:
                    custom = fieldMapping.isCustomDelete(sm, store);
                    break;
            }

            if (!Boolean.FALSE.equals(custom))
                hasCustom = true;
            if (!Boolean.TRUE.equals(custom))
                hasStd = true;
        }

        if (hasCustom && hasStd)
            return null;
        return (hasCustom) ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public void customInsert(OpenJPAStateManager sm, JDBCStore store)
        throws SQLException {
        OpenJPAStateManager em = store.getContext().getStateManager
            (sm.fetchObject(field.getIndex()));
        if (em == null)
            em = new NullEmbeddedStateManager(sm, field);
        FieldMapping[] fields = field.getEmbeddedMapping().getFieldMappings();
        for (FieldMapping fieldMapping : fields)
            if (!Boolean.FALSE.equals(fieldMapping.isCustomInsert(em, store)))
                fieldMapping.customInsert(em, store);
    }

    @Override
    public void customUpdate(OpenJPAStateManager sm, JDBCStore store)
        throws SQLException {
        OpenJPAStateManager em = store.getContext().getStateManager
            (sm.fetchObject(field.getIndex()));
        if (em == null)
            em = new NullEmbeddedStateManager(sm, field);
        FieldMapping[] fields = field.getEmbeddedMapping().getFieldMappings();
        for (int i = 0; i < fields.length; i++)
            if (em.getDirty().get(i)
                && !em.getFlushed().get(i)
                && !Boolean.FALSE.equals(fields[i].isCustomUpdate(em, store)))
                fields[i].customUpdate(em, store);
    }

    @Override
    public void customDelete(OpenJPAStateManager sm, JDBCStore store)
        throws SQLException {
        OpenJPAStateManager em = store.getContext().getStateManager
            (sm.fetchObject(field.getIndex()));
        if (em == null)
            em = new NullEmbeddedStateManager(sm, field);
        FieldMapping[] fields = field.getEmbeddedMapping().getFieldMappings();
        for (FieldMapping fieldMapping : fields)
            if (!Boolean.FALSE.equals(fieldMapping.isCustomDelete(em, store)))
                fieldMapping.customDelete(em, store);
    }

    @Override
    public int supportsSelect(Select sel, int type, OpenJPAStateManager sm,
        JDBCStore store, JDBCFetchConfiguration fetch) {
        if (type == Select.TYPE_JOINLESS && sel.isSelected(field.getTable()))
            return 1;
        return 0;
    }

    @Override
    public int select(Select sel, OpenJPAStateManager sm, JDBCStore store,
        JDBCFetchConfiguration fetch, int eagerMode) {
        Joins joins = field.join(sel);
        sel.select(field.getColumns(), joins); // null indicator

        // limit eager mode to join b/c at this point the select has been
        // modified and an attempt to clone it for a to-many eager select can
        // result in a clone that produces invalid SQL
        eagerMode = Math.min(eagerMode, EagerFetchModes.EAGER_JOIN);
        sel.select(field.getEmbeddedMapping(), Select.SUBS_EXACT, store,
            fetch, eagerMode, joins);
        return 1;
    }

    @Override
    public void load(OpenJPAStateManager sm, JDBCStore store,
        JDBCFetchConfiguration fetch, Result res)
        throws SQLException {
        Boolean isNull = indicatesNull(res);
        if (isNull == null)
            return;

        if (Boolean.TRUE.equals(isNull)) {
            sm.storeObject(field.getIndex(), null);
            return;
        }

        // handling of lazy embeddables.  if the embedded field is not part of any
        // fetch group and the result does not contain any embeddable columns,
        // do not load the embeddable.
        if (fetch.requiresFetch(field) == FetchConfiguration.FETCH_NONE &&
            !containsEmbeddedResult(fetch, res)) {
            return;
        }

        //### note: without a null indicator column, the above indicatesNull()
        //### call will always return false, meaning we always have to assume
        //### we selected the embedded object fields and load the object
        //### immediately; this will be inefficient when the embedded object
        //### was not selected after all
        StoreContext ctx = store.getContext();
        OpenJPAStateManager em = ctx.embed(null, null, sm, field);
        sm.storeObject(field.getIndex(), em.getManagedInstance());
        boolean needsLoad = loadFields(em, store, fetch, res);

        // After loading everything from result, load the rest of the
        // configured fields if anything is missing.
        if (needsLoad &&
            fetch.requiresFetch(field.getFieldMetaData()) ==
                FetchConfiguration.FETCH_LOAD) {
          em.load(fetch);
        }
    }

    /*
     * finds an eager fetch field and searches for it in the result.
     * if the result does not contain it, assume that it contains no embeddable
     * column data.  this is a fairly safe assumption given that the entire
     * embeddable was marked lazy.
     */
    private boolean containsEmbeddedResult(FetchConfiguration fetch, Result res) {
        FieldMapping[] fields = field.getEmbeddedMapping().getFieldMappings();
        boolean containsUnloadedEagerField = false;

        for (FieldMapping fieldMapping : fields) {
            boolean inResultSet = checkResult(fieldMapping, res);
            if (inResultSet) {
                // At least one of the embeddable's field is in the ResultSet.
                return true;
            }

            if (fetch.requiresFetch(fieldMapping) == FetchConfiguration.FETCH_LOAD) {
                containsUnloadedEagerField = true;
            }
        }

        // A field expected to be loaded eagerly was missing from the ResultSet.
        if (containsUnloadedEagerField) {
            return false;
        }

        // if all fields are lazy and in the default fetch group, populate the embeddable
        // so its attributes can be loaded when accessed.
        return fetch.hasFetchGroup(FetchGroup.NAME_DEFAULT);
    }

    private boolean checkResult(FieldMapping fm, Result res) {
        if (fm.getStrategy() instanceof Joinable) {
            Joinable strat = (Joinable)fm.getStrategy();
            Column[] cols = strat.getColumns();
            for (Column col : cols) {
                try {
                    if (res.contains(col)) {
                        return true;
                    }
                } catch (Exception e) {
                    return false;
                }
            }
        }
        // if the field is a collection, also check for an eager result which could result from
        // a non-lazy relationship in the embeddable
        int type = fm.getTypeCode();
        if ((type == JavaTypes.ARRAY ||
             type == JavaTypes.COLLECTION ||
             type == JavaTypes.MAP)
            && res.getEager(fm) != null) {
            return true;
        }
        return false;
    }

    private boolean loadFields(OpenJPAStateManager em, JDBCStore store,
        JDBCFetchConfiguration fetch, Result res)
        throws SQLException {
        FieldMapping[] fields = field.getEmbeddedMapping().getFieldMappings();
        Object eres, processed;
        boolean needsLoad = false;
        for (int i = 0; i < fields.length; i++) {
            eres = res.getEager(fields[i]);
            res.startDataRequest(fields[i]);
            try {
                if (eres == res)
                    fields[i].loadEagerJoin(em, store, fetch, res);
                else if (eres != null) {
                    processed =
                        fields[i].loadEagerParallel(em, store, fetch, eres);
                    if (processed != eres)
                        res.putEager(fields[i], processed);
                } else {
                    fields[i].load(em, store, fetch, res);
                }
                needsLoad = needsLoad || (!em.getLoaded().get(i) &&
                    fetch.requiresFetch(fields[i])
                        == FetchConfiguration.FETCH_LOAD);
            } finally {
                res.endDataRequest();
            }
        }
        return needsLoad;
    }

    /**
     * Return whether the given result indicates that the embedded value is
     * null.
     *
     * @return {@link Boolean#TRUE} if the value is null, {@link Boolean#FALSE}
     * if it is not, or <code>null</code> if the result does not
     * contain the embedded value
     */
    private Boolean indicatesNull(Result res)
        throws SQLException {
        Column[] cols = field.getColumns();
        if (cols.length != 1)
            return Boolean.FALSE;
        if (!res.contains(cols[0]))
            return null;

        Object obj = res.getObject(cols[0], -1, (Object) null);
        if (((EmbeddedClassStrategy) field.getEmbeddedMapping().getStrategy()).
            indicatesNull(obj))
            return Boolean.TRUE;
        return Boolean.FALSE;
    }

    @Override
    public Object toDataStoreValue(Object val, JDBCStore store) {
        ClassMapping type = field.getTypeMapping();
        return type.toDataStoreValue(val, type.getPrimaryKeyColumns(), store);
    }

    @Override
    public void appendIsNull(SQLBuffer sql, Select sel, Joins joins) {
        appendTestNull(sql, sel, joins, true);
    }

    @Override
    public void appendIsNotNull(SQLBuffer sql, Select sel, Joins joins) {
        appendTestNull(sql, sel, joins, false);
    }

    /**
     * Append SQL to test whether the embedded object is null.
     */
    private void appendTestNull(SQLBuffer sql, Select sel, Joins joins,
        boolean isNull) {
        Column[] cols = field.getColumns();
        if (cols.length != 1) {
            if (isNull)
                sql.append("1 <> 1");
            else
                sql.append("1 = 1");
            return;
        }

        Object val;
        if (cols[0].isNotNull()) {
            if (_synthetic || cols[0].getDefaultString() == null)
                val = JavaSQLTypes.getEmptyValue(cols[0].getJavaType());
            else
                val = cols[0].getDefault();
        } else
            val = null;

        joins = join(joins, false);
        sql.append(sel.getColumnAlias(cols[0], joins));
        if (isNull && val == null)
            sql.append(" IS ");
        else if (isNull)
            sql.append(" = ");
        else if (val == null)
            sql.append(" IS NOT ");
        else
            sql.append(" <> ");
        sql.appendValue(val, cols[0]);
    }

    @Override
    public Joins join(Joins joins, boolean forceOuter) {
        return field.join(joins, forceOuter, false);
    }

    /**
     * Loading embed object without instantiating owner entity
     */
    @Override
    public Object loadProjection(JDBCStore store, JDBCFetchConfiguration fetch,
        Result res, Joins joins)
        throws SQLException {
        Boolean isNull = indicatesNull(res);
        if (isNull == null)
            return null;

        StoreContext ctx = store.getContext();
        // load primary key of owner entity
        Object owner = field.getDefiningMapping().getObjectId(store, res,
            null, true, joins);
        OpenJPAStateManager em = ctx.embed(null, null, null, field);
        // set owner id
        ((StateManagerImpl) em).setOwner(owner);
        boolean needsLoad = loadFields(em, store, fetch, res);

        // After loading everything from result, load the rest of the
        // configured fields if anything is missing.
        if (needsLoad &&
            fetch.requiresFetch(field.getFieldMetaData()) ==
                FetchConfiguration.FETCH_LOAD) {
          em.load(fetch);
        }

        return em.getManagedInstance();
    }

    /////////////////////////////
    // Embeddable implementation
    /////////////////////////////

    @Override
    public Column[] getColumns() {
        return field.getColumns();
    }

    @Override
    public ColumnIO getColumnIO() {
        return field.getColumnIO();
    }

    @Override
    public Object[] getResultArguments() {
        return null;
    }

    @Override
    public Object toEmbeddedDataStoreValue(Object val, JDBCStore store) {
        return toDataStoreValue(val, store);
    }

    @Override
    public Object toEmbeddedObjectValue(Object val) {
        //return UNSUPPORTED;
        return null;
    }

    @Override
    public void loadEmbedded(OpenJPAStateManager sm, JDBCStore store,
        JDBCFetchConfiguration fetch, Object val)
        throws SQLException {
        if (val != null)
            sm.storeObject(field.getIndex(), val);
        else
            sm.storeObject(field.getIndex(), null);
    }


    /**
     * State manager that represents a null embedded object.
     * Used to update embedded fields when the embedded value is null. This
     * state manager doesn't require access to the old embedded value, because
     * all updates are flushed before all deletes, so all foreign keys will
     * be properly nulled before the related object is deleted. However, this
     * state manager should not be used if possible when the parent object is
     * being deleted; in that case having the original embedded state manager
     * is preferable so that it can set foreign key relations to order DELETE
     * statements to meet constraints.
     */
    private static class NullEmbeddedStateManager
        implements OpenJPAStateManager {

        private static final BitSet EMPTY_BITSET = new BitSet();

        private final OpenJPAStateManager _owner;
        private final ValueMetaData _vmd;
        private BitSet _full = null;

        /**
         * Constructor; supply the owner of the null/deleted embedded value,
         * and the value metadata.
         */
        public NullEmbeddedStateManager(OpenJPAStateManager owner,
            ValueMetaData vmd) {
            _owner = owner;
            _vmd = vmd;
        }

        @Override
        public void initialize(Class forType, PCState state) {
            throw new InternalException();
        }

        @Override
        public void load(FetchConfiguration fetch) {
            throw new InternalException();
        }

        @Override
        public boolean assignObjectId(boolean flush) {
            throw new InternalException();
        }

        @Override
        public Object getManagedInstance() {
            return null;
        }

        @Override
        public PersistenceCapable getPersistenceCapable() {
            return null;
        }

        @Override
        public ClassMetaData getMetaData() {
            return _vmd.getEmbeddedMetaData();
        }

        @Override
        public OpenJPAStateManager getOwner() {
            return _owner;
        }

        @Override
        public int getOwnerIndex() {
            return _vmd.getFieldMetaData().getIndex();
        }

        @Override
        public boolean isEmbedded() {
            return true;
        }

        @Override
        public boolean isTransactional() {
            return true;
        }

        @Override
        public boolean isPersistent() {
            return true;
        }

        @Override
        public boolean isNew() {
            return _owner.isNew();
        }

        @Override
        public boolean isDeleted() {
            return _owner.isDeleted();
        }

        @Override
        public boolean isDetached() {
            return _owner.isDetached();
        }

        @Override
        public boolean isVersionUpdateRequired() {
            return _owner.isVersionUpdateRequired();
        }

        @Override
        public boolean isVersionCheckRequired() {
            return _owner.isVersionCheckRequired();
        }

        @Override
        public boolean isDirty() {
            return true;
        }

        @Override
        public boolean isFlushed() {
            return _owner.isFlushed();
        }

        @Override
        public boolean isFlushedDirty() {
            return isFlushed();
        }

        @Override
        public boolean isProvisional() {
            return _owner.isProvisional();
        }

        @Override
        public BitSet getLoaded() {
            // consider everything loaded
            if (_full == null) {
                FieldMetaData[] fmds = _vmd.getEmbeddedMetaData().getFields();
                _full = new BitSet(fmds.length);
                for (int i = 0; i < fmds.length; i++)
                    _full.set(i);
            }
            return _full;
        }

        @Override
        public BitSet getDirty() {
            // consider everything dirty
            if (_full == null) {
                FieldMetaData[] fmds = _vmd.getEmbeddedMetaData().getFields();
                _full = new BitSet(fmds.length);
                for (int i = 0; i < fmds.length; i++)
                    _full.set(i);
            }
            return _full;
        }

        @Override
        public BitSet getFlushed() {
            return EMPTY_BITSET;
        }

        @Override
        public BitSet getUnloaded(FetchConfiguration fetch) {
            throw new InternalException();
        }

        @Override
        public Object newProxy(int field) {
            throw new InternalException();
        }

        @Override
        public Object newFieldProxy(int field) {
            throw new InternalException();
        }

        @Override
        public boolean isDefaultValue(int field) {
            return true;
        }

        @Override
        public StoreContext getContext() {
            return _owner.getContext();
        }

        @Override
        public Object getId() {
            return _owner.getId();
        }

        @Override
        public Object getObjectId() {
            return _owner.getObjectId();
        }

        @Override
        public void setObjectId(Object oid) {
            throw new InternalException();
        }

        @Override
        public Object getLock() {
            return null;
        }

        @Override
        public void setLock(Object lock) {
            throw new InternalException();
        }

        @Override
        public Object getVersion() {
            return null;
        }

        @Override
        public void setVersion(Object version) {
            throw new InternalException();
        }

        @Override
        public void setNextVersion(Object version) {
            throw new InternalException();
        }

        @Override
        public PCState getPCState() {
            return (_owner.isDeleted()) ? PCState.EDELETED : PCState.ECOPY;
        }

        @Override
        public Object getImplData() {
            return null;
        }

        @Override
        public Object setImplData(Object data, boolean cacheable) {
            throw new InternalException();
        }

        @Override
        public boolean isImplDataCacheable() {
            return false;
        }

        @Override
        public Object getImplData(int field) {
            return null;
        }

        @Override
        public Object setImplData(int field, Object data) {
            throw new InternalException();
        }

        @Override
        public boolean isImplDataCacheable(int field) {
            return false;
        }

        @Override
        public Object getIntermediate(int field) {
            return null;
        }

        @Override
        public void setIntermediate(int field, Object value) {
            throw new InternalException();
        }

        @Override
        public boolean fetchBooleanField(int field) {
            return false;
        }

        @Override
        public byte fetchByteField(int field) {
            return (byte) 0;
        }

        @Override
        public char fetchCharField(int field) {
            return (char) 0;
        }

        @Override
        public double fetchDoubleField(int field) {
            return 0D;
        }

        @Override
