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;
}
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()));
}
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;
}
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);
}
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);
}
Aggregations