Search in sources :

Example 1 with ValueHolder

use of org.eclipse.persistence.indirection.ValueHolder in project eclipselink by eclipse-ee4j.

the class AbstractEntityResource method checkIdempotenceOnRelationships.

/**
 * This method maintains idempotence on PUT by disallowing sequencing in relationships.
 *
 * @param descriptor    descriptor of the entity passed in 'entity' parameter.
 * @param entity        entity to process.
 * @return true if check is passed (no sequencing)
 */
private boolean checkIdempotenceOnRelationships(ClassDescriptor descriptor, Object entity) {
    final List<DatabaseMapping> mappings = descriptor.getMappings();
    if ((mappings != null) && (!mappings.isEmpty())) {
        for (DatabaseMapping mapping : mappings) {
            if (mapping instanceof ForeignReferenceMapping) {
                final ForeignReferenceMapping fkMapping = (ForeignReferenceMapping) mapping;
                if ((fkMapping.isCascadePersist()) || (fkMapping.isCascadeMerge())) {
                    final ClassDescriptor referenceDescriptor = fkMapping.getReferenceDescriptor();
                    if (referenceDescriptor != null) {
                        if (referenceDescriptor instanceof RelationalDescriptor) {
                            final RelationalDescriptor relDesc = (RelationalDescriptor) referenceDescriptor;
                            final AbstractDirectMapping relSequenceMapping = relDesc.getObjectBuilder().getSequenceMapping();
                            if (relSequenceMapping != null) {
                                final Object value = mapping.getAttributeAccessor().getAttributeValueFromObject(entity);
                                if (value != null) {
                                    if (value instanceof ValueHolder) {
                                        final ValueHolder<?> holder = (ValueHolder<?>) value;
                                        if (holder.getValue() != null) {
                                            return false;
                                        }
                                    } else if (value instanceof Collection) {
                                        if (!(((Collection<?>) value).isEmpty())) {
                                            return false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return true;
}
Also used : RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) AbstractDirectMapping(org.eclipse.persistence.mappings.foundation.AbstractDirectMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Collection(java.util.Collection) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) ValueHolder(org.eclipse.persistence.indirection.ValueHolder)

Example 2 with ValueHolder

use of org.eclipse.persistence.indirection.ValueHolder in project eclipselink by eclipse-ee4j.

the class NullDelegateInValueHolderTest method setup.

@Override
public void setup() {
    String customerName = "ACME, Inc.";
    if (indirectCollectionClass.equals(IndirectList.class)) {
        testOrder = new Order(customerName);
    } else if (indirectCollectionClass.equals(IndirectMap.class)) {
        testOrder = new MappedOrder(customerName);
    } else if (indirectCollectionClass.equals(IndirectSet.class)) {
        testOrder = new SetOrder(customerName);
    }
    ClassDescriptor descriptor = getSession().getDescriptor(testOrder);
    ForeignReferenceMapping mapping = (ForeignReferenceMapping) descriptor.getMappingForAttributeName("salesReps");
    IndirectionPolicy policy = mapping.getIndirectionPolicy();
    // replace indirect container's valueholder with a new ValueHolder instance
    mapping.setAttributeValueInObject(testOrder, policy.buildIndirectObject(new ValueHolder()));
}
Also used : ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) IndirectionPolicy(org.eclipse.persistence.internal.indirection.IndirectionPolicy) IndirectMap(org.eclipse.persistence.indirection.IndirectMap) ValueHolder(org.eclipse.persistence.indirection.ValueHolder)

Example 3 with ValueHolder

use of org.eclipse.persistence.indirection.ValueHolder in project eclipselink by eclipse-ee4j.

the class JoinedAttributeTestHelper method compareAttributes.

protected static String compareAttributes(Object obj1, Object obj2, DatabaseMapping mapping, AbstractSession session, Map processed) {
    String errorMsg = "";
    if (mapping.isForeignReferenceMapping()) {
        ForeignReferenceMapping frm = (ForeignReferenceMapping) mapping;
        Object value1 = frm.getAttributeValueFromObject(obj1);
        Object value2 = frm.getAttributeValueFromObject(obj2);
        boolean isInstantiated1 = frm.getIndirectionPolicy().objectIsInstantiated(value1);
        boolean isInstantiated2 = frm.getIndirectionPolicy().objectIsInstantiated(value2);
        if (!isInstantiated1 && !isInstantiated2) {
            return "";
        } else if (isInstantiated1 && !isInstantiated2) {
            if (frm.isOneToOneMapping() && value1 instanceof ValueHolder && ((ValueHolder) (value1)).getValue() == null) {
            // In OneToOne case if the foreign key of the read object is null then ValueHolder (which is always instantiated) with value null is created
            } else {
                errorMsg = ":  indirection instantiated != indirection NOT instantiated; ";
            }
        } else if (!isInstantiated1 && isInstantiated2) {
            if (frm.isOneToOneMapping() && value2 instanceof ValueHolder && ((ValueHolder) (value2)).getValue() == null) {
            // In OneToOne case if the foreign key of the read object is null then ValueHolder (which is always instantiated) with value null is created
            } else {
                errorMsg = ": indirection NOT instantiated != indirection instantiated; ";
            }
        } else {
            value1 = frm.getRealAttributeValueFromObject(obj1, session);
            value2 = frm.getRealAttributeValueFromObject(obj2, session);
            if (frm.isCollectionMapping()) {
                Class<?> containerClass = frm.getContainerPolicy().getContainerClass();
                if (Collection.class.isAssignableFrom(containerClass)) {
                    errorMsg += compareCollections((Collection) value1, (Collection) value2, frm.getReferenceDescriptor(), session, processed);
                } else if (Map.class.isAssignableFrom(containerClass)) {
                    errorMsg += compareMaps((Map) value1, (Map) value2, session, processed);
                } else {
                    errorMsg += mapping.toString() + " container class implements neither Collection nor Map - can't processl; ";
                }
            } else {
                errorMsg += compareObjects(value1, value2, session, processed);
            }
        }
    } else if (!mapping.compareObjects(obj1, obj2, session)) {
        Object value1 = mapping.getAttributeValueFromObject(obj1);
        if (value1 == null) {
            value1 = new String("null");
        }
        Object value2 = mapping.getAttributeValueFromObject(obj2);
        if (value2 == null) {
            value2 = new String("null");
        }
        errorMsg = ": " + value1.toString() + "!=" + value2.toString() + "; ";
    }
    if (errorMsg.length() > 0) {
        errorMsg = "." + mapping.getAttributeName() + errorMsg;
    }
    return errorMsg;
}
Also used : ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) Collection(java.util.Collection) ValueHolder(org.eclipse.persistence.indirection.ValueHolder) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ValueHolder

use of org.eclipse.persistence.indirection.ValueHolder in project eclipselink by eclipse-ee4j.

the class DynamicPropertiesInitializatonPolicy method initializeDefaultValue.

/**
 * Initialize the default value handling primitives, collections and
 * indirection.
 */
private void initializeDefaultValue(DatabaseMapping mapping, DynamicEntityImpl entity) {
    Object value = null;
    if (mapping.isDirectToFieldMapping() && mapping.getAttributeClassification().isPrimitive()) {
        Class<?> primClass = mapping.getAttributeClassification();
        if (primClass == ClassConstants.PBOOLEAN) {
            value = false;
        } else if (primClass == ClassConstants.PINT) {
            value = 0;
        } else if (primClass == ClassConstants.PLONG) {
            value = 0L;
        } else if (primClass == ClassConstants.PCHAR) {
            value = Character.MIN_VALUE;
        } else if (primClass == ClassConstants.PDOUBLE) {
            value = 0.0d;
        } else if (primClass == ClassConstants.PFLOAT) {
            value = 0.0f;
        } else if (primClass == ClassConstants.PSHORT) {
            value = Short.MIN_VALUE;
        } else if (primClass == ClassConstants.PBYTE) {
            value = Byte.MIN_VALUE;
        }
    } else if (mapping.isForeignReferenceMapping()) {
        ForeignReferenceMapping refMapping = (ForeignReferenceMapping) mapping;
        if (refMapping.usesIndirection() && refMapping.getIndirectionPolicy() instanceof BasicIndirectionPolicy) {
            value = new ValueHolder<>(value);
        } else if (refMapping.isCollectionMapping()) {
            value = refMapping.getContainerPolicy().containerInstance();
        }
    } else if (mapping.isAggregateObjectMapping()) {
        value = mapping.getReferenceDescriptor().getObjectBuilder().buildNewInstance();
    }
    PropertyWrapper propertyWrapper = entity.getPropertiesMap().get(mapping.getAttributeName());
    // NB - only the value is set, not the 'isSet' boolean
    propertyWrapper.setValue(value);
}
Also used : PropertyWrapper(org.eclipse.persistence.internal.dynamic.DynamicEntityImpl.PropertyWrapper) ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) BasicIndirectionPolicy(org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy) ValueHolder(org.eclipse.persistence.indirection.ValueHolder)

Example 5 with ValueHolder

use of org.eclipse.persistence.indirection.ValueHolder in project eclipselink by eclipse-ee4j.

the class OrmAttributeAccessor method setAttributeValueInObject.

@Override
public void setAttributeValueInObject(Object object, Object value) {
    if (isChangeTracking) {
        Object oldValue = getAttributeValueFromObject(object);
        PropertyChangeListener listener = ((ChangeTracker) object)._persistence_getPropertyChangeListener();
        if (listener != null) {
            listener.propertyChange(new PropertyChangeEvent(object, oxmAccessor.getAttributeName(), value, oldValue));
        }
    }
    if (isValueHolderProperty) {
        @SuppressWarnings({ "unchecked" }) ValueHolderInterface<Object> vh = (ValueHolderInterface<Object>) ormAccessor.getAttributeValueFromObject(object);
        if (vh == null) {
            vh = new ValueHolder<>();
            ((ValueHolder<?>) vh).setIsNewlyWeavedValueHolder(true);
        }
        vh.setValue(value);
        ormAccessor.setAttributeValueInObject(object, vh);
    }
    oxmAccessor.setAttributeValueInObject(object, value);
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) WeavedAttributeValueHolderInterface(org.eclipse.persistence.indirection.WeavedAttributeValueHolderInterface) ValueHolderInterface(org.eclipse.persistence.indirection.ValueHolderInterface) ValueHolder(org.eclipse.persistence.indirection.ValueHolder) ChangeTracker(org.eclipse.persistence.descriptors.changetracking.ChangeTracker)

Aggregations

ValueHolder (org.eclipse.persistence.indirection.ValueHolder)20 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)5 ForeignReferenceMapping (org.eclipse.persistence.mappings.ForeignReferenceMapping)5 Collection (java.util.Collection)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)4 ChangeTracker (org.eclipse.persistence.descriptors.changetracking.ChangeTracker)4 ObjectChangeSet (org.eclipse.persistence.internal.sessions.ObjectChangeSet)4 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)4 ObjectChangeListener (org.eclipse.persistence.internal.descriptors.changetracking.ObjectChangeListener)3 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)3 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)3 ReadQuery (org.eclipse.persistence.queries.ReadQuery)3 PropertyChangeEvent (java.beans.PropertyChangeEvent)2 Iterator (java.util.Iterator)2 IndirectCollection (org.eclipse.persistence.indirection.IndirectCollection)2 ValueHolderInterface (org.eclipse.persistence.indirection.ValueHolderInterface)2 AttributeChangeListener (org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener)2 PropertyWrapper (org.eclipse.persistence.internal.dynamic.DynamicEntityImpl.PropertyWrapper)2