Search in sources :

Example 96 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-rdbms by datanucleus.

the class EmbeddedMapping method setObject.

/**
 * Mutator for the embedded object in the datastore.
 * @param ec ExecutionContext
 * @param ps The Prepared Statement
 * @param param Param numbers in the PreparedStatement for the fields of this object
 * @param value The embedded object to use
 * @param ownerOP ObjectProvider of the owning object containing this embedded object
 * @param ownerFieldNumber Field number in the owning object where this is stored
 */
public void setObject(ExecutionContext ec, PreparedStatement ps, int[] param, Object value, ObjectProvider ownerOP, int ownerFieldNumber) {
    if (value == null) {
        int n = 0;
        String nullColumn = null;
        String nullValue = null;
        if (emd != null) {
            nullColumn = emd.getNullIndicatorColumn();
            nullValue = emd.getNullIndicatorValue();
        }
        if (discrimMapping != null) {
            discrimMapping.setObject(ec, ps, new int[] { param[n] }, null);
            n++;
        }
        for (int i = 0; i < javaTypeMappings.size(); i++) {
            JavaTypeMapping mapping = javaTypeMappings.get(i);
            int[] posMapping = new int[mapping.getNumberOfDatastoreMappings()];
            for (int j = 0; j < posMapping.length; j++) {
                posMapping[j] = param[n++];
            }
            // in which case apply the required value
            if (nullColumn != null && nullValue != null && mapping.getMemberMetaData().getColumnMetaData().length > 0 && mapping.getMemberMetaData().getColumnMetaData()[0].getName().equals(nullColumn)) {
                // Try to cater for user having an integer based column and value
                if (mapping instanceof IntegerMapping || mapping instanceof BigIntegerMapping || mapping instanceof LongMapping || mapping instanceof ShortMapping) {
                    Object convertedValue = null;
                    try {
                        if (mapping instanceof IntegerMapping || mapping instanceof ShortMapping) {
                            convertedValue = Integer.valueOf(nullValue);
                        } else if (mapping instanceof LongMapping || mapping instanceof BigIntegerMapping) {
                            convertedValue = Long.valueOf(nullValue);
                        }
                    } catch (Exception e) {
                    }
                    mapping.setObject(ec, ps, posMapping, convertedValue);
                } else {
                    mapping.setObject(ec, ps, posMapping, nullValue);
                }
            } else {
                if (mapping.getNumberOfDatastoreMappings() > 0) {
                    mapping.setObject(ec, ps, posMapping, null);
                }
            }
        }
    } else {
        ApiAdapter api = ec.getApiAdapter();
        if (!api.isPersistable(value)) {
            throw new NucleusException(Localiser.msg("041016", value.getClass(), value)).setFatal();
        }
        AbstractClassMetaData embCmd = ec.getMetaDataManager().getMetaDataForClass(value.getClass(), ec.getClassLoaderResolver());
        ObjectProvider embOP = ec.findObjectProvider(value);
        if (embOP == null || api.getExecutionContext(value) == null) {
            // Assign a StateManager to manage our embedded object
            embOP = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, value, false, ownerOP, ownerFieldNumber);
            embOP.setPcObjectType(objectType);
        }
        int n = 0;
        if (discrimMapping != null) {
            if (discrimMetaData.getStrategy() != DiscriminatorStrategy.NONE) {
                discrimMapping.setObject(ec, ps, new int[] { param[n] }, embCmd.getDiscriminatorValue());
            }
            n++;
        }
        for (int i = 0; i < javaTypeMappings.size(); i++) {
            JavaTypeMapping mapping = javaTypeMappings.get(i);
            int[] posMapping = new int[mapping.getNumberOfDatastoreMappings()];
            for (int j = 0; j < posMapping.length; j++) {
                posMapping[j] = param[n++];
            }
            // Retrieve value of member from Embedded StateManager
            int embAbsFieldNum = embCmd.getAbsolutePositionOfMember(mapping.getMemberMetaData().getName());
            if (embAbsFieldNum >= 0) {
                // Member is present in this embedded type
                Object fieldValue = embOP.provideField(embAbsFieldNum);
                if (mapping instanceof EmbeddedPCMapping) {
                    mapping.setObject(ec, ps, posMapping, fieldValue, embOP, embAbsFieldNum);
                } else {
                    if (mapping.getNumberOfDatastoreMappings() > 0) {
                        mapping.setObject(ec, ps, posMapping, fieldValue);
                    }
                }
            } else {
                mapping.setObject(ec, ps, posMapping, null);
            }
        }
    }
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ObjectProvider(org.datanucleus.state.ObjectProvider) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 97 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-rdbms by datanucleus.

the class EmbeddedPCMapping method getObjectProviderForEmbeddedObject.

/**
 * Accessor for the ObjectProvider of the embedded PC object when provided with the owner object.
 * @param ownerOP ObjectProvider of the owner
 * @return ObjectProvider of the embedded object
 */
private ObjectProvider getObjectProviderForEmbeddedObject(ObjectProvider ownerOP) {
    AbstractMemberMetaData theMmd = getRealMemberMetaData();
    // Owner (non-embedded) PC
    Object value = ownerOP.provideField(theMmd.getAbsoluteFieldNumber());
    TypeManager typeManager = ownerOP.getExecutionContext().getTypeManager();
    value = mmd.isSingleCollection() ? typeManager.getContainerAdapter(value).iterator().next() : value;
    if (value == null) {
        return null;
    }
    ExecutionContext ec = ownerOP.getExecutionContext();
    ObjectProvider thisOP = ec.findObjectProvider(value);
    if (thisOP == null) {
        // Assign a ObjectProvider to manage our embedded object
        thisOP = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, value, false, ownerOP, theMmd.getAbsoluteFieldNumber());
        thisOP.setPcObjectType(objectType);
    }
    return thisOP;
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) TypeManager(org.datanucleus.store.types.TypeManager) ObjectProvider(org.datanucleus.state.ObjectProvider) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 98 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-rdbms by datanucleus.

the class OracleArrayMapping method postInsert.

/**
 * Method to be called after the insert of the owner class element.
 * @param ownerOP ObjectProvider of the owner
 */
public void postInsert(ObjectProvider ownerOP) {
    if (containerIsStoredInSingleColumn()) {
        Object value = ownerOP.provideField(mmd.getAbsoluteFieldNumber());
        if (value == null) {
            return;
        }
        ExecutionContext ec = ownerOP.getExecutionContext();
        if (mmd.getArray().elementIsPersistent()) {
            // Make sure all persistable elements have ObjectProviders
            Object[] arrElements = (Object[]) value;
            for (Object elem : arrElements) {
                if (elem != null) {
                    ObjectProvider elemOP = ec.findObjectProvider(elem);
                    if (elemOP == null || ec.getApiAdapter().getExecutionContext(elem) == null) {
                        elemOP = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, elem, false, ownerOP, mmd.getAbsoluteFieldNumber());
                    }
                }
            }
        }
        // Generate the contents for the BLOB
        byte[] bytes = new byte[0];
        try {
            if (mmd.isSerialized()) {
                // Serialised field so just perform basic Java serialisation for retrieval
                if (!(value instanceof Serializable)) {
                    throw new NucleusDataStoreException(Localiser.msg("055005", value.getClass().getName()));
                }
                BlobImpl b = new BlobImpl(value);
                bytes = b.getBytes(0, (int) b.length());
            } else if (value instanceof boolean[]) {
                bytes = TypeConversionHelper.getByteArrayFromBooleanArray(value);
            } else if (value instanceof char[]) {
                bytes = TypeConversionHelper.getByteArrayFromCharArray(value);
            } else if (value instanceof double[]) {
                bytes = TypeConversionHelper.getByteArrayFromDoubleArray(value);
            } else if (value instanceof float[]) {
                bytes = TypeConversionHelper.getByteArrayFromFloatArray(value);
            } else if (value instanceof int[]) {
                bytes = TypeConversionHelper.getByteArrayFromIntArray(value);
            } else if (value instanceof long[]) {
                bytes = TypeConversionHelper.getByteArrayFromLongArray(value);
            } else if (value instanceof short[]) {
                bytes = TypeConversionHelper.getByteArrayFromShortArray(value);
            } else if (value instanceof Boolean[]) {
                bytes = TypeConversionHelper.getByteArrayFromBooleanObjectArray(value);
            } else if (value instanceof Byte[]) {
                bytes = TypeConversionHelper.getByteArrayFromByteObjectArray(value);
            } else if (value instanceof Character[]) {
                bytes = TypeConversionHelper.getByteArrayFromCharObjectArray(value);
            } else if (value instanceof Double[]) {
                bytes = TypeConversionHelper.getByteArrayFromDoubleObjectArray(value);
            } else if (value instanceof Float[]) {
                bytes = TypeConversionHelper.getByteArrayFromFloatObjectArray(value);
            } else if (value instanceof Integer[]) {
                bytes = TypeConversionHelper.getByteArrayFromIntObjectArray(value);
            } else if (value instanceof Long[]) {
                bytes = TypeConversionHelper.getByteArrayFromLongObjectArray(value);
            } else if (value instanceof Short[]) {
                bytes = TypeConversionHelper.getByteArrayFromShortObjectArray(value);
            } else if (value instanceof BigDecimal[]) {
                bytes = TypeConversionHelper.getByteArrayFromBigDecimalArray(value);
            } else if (value instanceof BigInteger[]) {
                bytes = TypeConversionHelper.getByteArrayFromBigIntegerArray(value);
            } else if (value instanceof byte[]) {
                bytes = (byte[]) value;
            } else if (value instanceof java.util.BitSet) {
                bytes = TypeConversionHelper.getByteArrayFromBooleanArray(TypeConversionHelper.getBooleanArrayFromBitSet((java.util.BitSet) value));
            } else {
                // Fall back to just perform Java serialisation for storage
                if (!(value instanceof Serializable)) {
                    throw new NucleusDataStoreException(Localiser.msg("055005", value.getClass().getName()));
                }
                BlobImpl b = new BlobImpl(value);
                bytes = b.getBytes(0, (int) b.length());
            }
        } catch (SQLException e) {
            throw new NucleusDataStoreException(Localiser.msg("055001", "Object", "" + value, mmd, e.getMessage()), e);
        } catch (IOException e1) {
        // Do nothing
        }
        // Update the BLOB
        OracleBlobRDBMSMapping.updateBlobColumn(ownerOP, getTable(), getDatastoreMapping(0), bytes);
    } else {
        super.postInsert(ownerOP);
    }
}
Also used : Serializable(java.io.Serializable) SQLException(java.sql.SQLException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) BlobImpl(org.datanucleus.store.rdbms.mapping.datastore.BlobImpl) IOException(java.io.IOException) BigInteger(java.math.BigInteger) ExecutionContext(org.datanucleus.ExecutionContext) BigInteger(java.math.BigInteger) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 99 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-rdbms by datanucleus.

the class OracleCollectionMapping method postUpdate.

/**
 * @see org.datanucleus.store.rdbms.mapping.MappingCallbacks#postUpdate(org.datanucleus.state.ObjectProvider)
 */
public void postUpdate(ObjectProvider ownerOP) {
    if (containerIsStoredInSingleColumn()) {
        ExecutionContext ec = ownerOP.getExecutionContext();
        Collection value = (Collection) ownerOP.provideField(mmd.getAbsoluteFieldNumber());
        if (value != null) {
            if (mmd.getCollection().elementIsPersistent()) {
                // Make sure all persistable elements have ObjectProviders
                Object[] collElements = value.toArray();
                for (Object elem : collElements) {
                    if (elem != null) {
                        ObjectProvider elemOP = ec.findObjectProvider(elem);
                        if (elemOP == null || ec.getApiAdapter().getExecutionContext(elem) == null) {
                            elemOP = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, elem, false, ownerOP, mmd.getAbsoluteFieldNumber());
                        }
                    }
                }
            }
        }
        postInsert(ownerOP);
    } else {
        super.postUpdate(ownerOP);
    }
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) Collection(java.util.Collection) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 100 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-rdbms by datanucleus.

the class SerialisedPCMapping method setObject.

/**
 * Method to populate parameter positions in a PreparedStatement with this object
 * @param ec The ExecutionContext
 * @param ps The Prepared Statement
 * @param exprIndex The parameter positions to populate
 * @param value The value of the PC to use in populating the parameter positions
 * @param ownerOP ObjectProvider for the owning object
 * @param fieldNumber field number of this object in the owning object
 */
public void setObject(ExecutionContext ec, PreparedStatement ps, int[] exprIndex, Object value, ObjectProvider ownerOP, int fieldNumber) {
    if (value != null) {
        // Assign a StateManager to the serialised object if none present
        ObjectProvider embSM = ec.findObjectProvider(value);
        if (embSM == null || ec.getApiAdapter().getExecutionContext(value) == null) {
            embSM = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, value, false, ownerOP, fieldNumber);
        }
    }
    ObjectProvider sm = null;
    if (value != null) {
        // Find SM for serialised object
        sm = ec.findObjectProvider(value);
    }
    if (sm != null) {
        sm.setStoringPC();
    }
    getDatastoreMapping(0).setObject(ps, exprIndex[0], value);
    if (sm != null) {
        sm.unsetStoringPC();
    }
}
Also used : ObjectProvider(org.datanucleus.state.ObjectProvider)

Aggregations

ObjectProvider (org.datanucleus.state.ObjectProvider)160 ExecutionContext (org.datanucleus.ExecutionContext)85 Iterator (java.util.Iterator)43 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)34 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)25 Map (java.util.Map)22 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)22 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)21 SQLException (java.sql.SQLException)17 Collection (java.util.Collection)16 ApiAdapter (org.datanucleus.api.ApiAdapter)16 NucleusObjectNotFoundException (org.datanucleus.exceptions.NucleusObjectNotFoundException)16 SCOCollectionIterator (org.datanucleus.store.types.SCOCollectionIterator)16 ArrayList (java.util.ArrayList)14 HashSet (java.util.HashSet)14 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)14 NucleusException (org.datanucleus.exceptions.NucleusException)13 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)13 ListIterator (java.util.ListIterator)12 SQLController (org.datanucleus.store.rdbms.SQLController)12