use of org.eclipse.persistence.core.mappings.CoreAttributeAccessor in project eclipselink by eclipse-ee4j.
the class JAXBValueStore method copy.
@Override
public ValueStore copy() {
AbstractSession session = ((JAXBContext) jaxbHelperContext.getJAXBContext()).getXMLContext().getSession(entity);
Object originalEntity = entity;
entity = descriptor.getInstantiationPolicy().buildNewInstance();
for (SDOProperty sdoProperty : (List<SDOProperty>) dataObject.getType().getProperties()) {
if (!sdoProperty.getType().isChangeSummaryType()) {
Mapping mapping = getJAXBMappingForProperty(sdoProperty);
CoreAttributeAccessor attributeAccessor = mapping.getAttributeAccessor();
Object attributeValue = attributeAccessor.getAttributeValueFromObject(originalEntity);
if (mapping.isCollectionMapping()) {
Object containerCopy = null;
SDOChangeSummary sdoChangeSummary = dataObject.getChangeSummary();
if (null != sdoChangeSummary) {
List list = listWrappers.get(sdoProperty);
containerCopy = sdoChangeSummary.getOriginalElements().get(list);
}
if (null == containerCopy) {
CoreContainerPolicy containerPolicy = mapping.getContainerPolicy();
containerCopy = containerPolicy.containerInstance();
if (null != attributeValue) {
Object attributeValueIterator = containerPolicy.iteratorFor(attributeValue);
while (containerPolicy.hasNext(attributeValueIterator)) {
containerPolicy.addInto(containerPolicy.nextEntry(attributeValueIterator), containerCopy, session);
}
}
}
attributeAccessor.setAttributeValueInObject(entity, containerCopy);
} else {
attributeAccessor.setAttributeValueInObject(entity, attributeValue);
}
}
}
return new JAXBValueStore(jaxbHelperContext, originalEntity, descriptor, dataObject, listWrappers);
}
use of org.eclipse.persistence.core.mappings.CoreAttributeAccessor in project eclipselink by eclipse-ee4j.
the class ReferenceResolver method perform.
/**
* Add java doc if you understand this code.
*/
private void perform(final CoreAbstractSession session, final IDResolver userSpecifiedResolver, final ErrorHandler handler, final Reference reference) {
final Object referenceSourceObject = reference.getSourceObject();
if (reference.getMapping() instanceof CollectionReferenceMapping) {
final CollectionReferenceMapping mapping = (CollectionReferenceMapping) reference.getMapping();
final CoreContainerPolicy cPolicy = mapping.getContainerPolicy();
// container should never be null
final Object container = reference.getContainer();
// create vectors of primary key values - one vector per reference instance
createPKVectorsFromMap(reference, mapping);
// if the we could not generate the primary key for the reference, it will not resolve - skip it
if (reference.getPrimaryKey() == null) {
return;
}
// loop over each pk vector and get object from cache - then add to collection and set on object
Object value = null;
if (!mapping.isWriteOnly()) {
for (Object o : ((Vector) reference.getPrimaryKey())) {
final CacheId primaryKey = (CacheId) o;
if (userSpecifiedResolver != null) {
final Callable c;
try {
if (primaryKey.getPrimaryKey().length > 1) {
final Map<String, Object> idWrapper = new HashMap<>();
for (int y = 0; y < primaryKey.getPrimaryKey().length; y++) {
final ObjectReferenceMapping refMapping = (ObjectReferenceMapping) reference.getMapping();
final String idName = (String) refMapping.getReferenceDescriptor().getPrimaryKeyFieldNames().get(y);
final Object idValue = primaryKey.getPrimaryKey()[y];
idWrapper.put(idName, idValue);
}
c = userSpecifiedResolver.resolve(idWrapper, reference.getTargetClass());
} else {
c = userSpecifiedResolver.resolve(primaryKey.getPrimaryKey()[0], reference.getTargetClass());
}
if (c != null) {
value = c.call();
}
} catch (Exception e) {
throw XMLMarshalException.unmarshalException(e);
}
} else {
value = getValue(session, reference, primaryKey, handler);
}
if (value != null) {
cPolicy.addInto(value, container, session);
}
}
}
// for each reference, get the source object and add it to the container policy
// when finished, set the policy on the mapping
mapping.setAttributeValueInObject(referenceSourceObject, container);
final InverseReferenceMapping inverseReferenceMapping = mapping.getInverseReferenceMapping();
if (inverseReferenceMapping != null && value != null) {
final CoreAttributeAccessor backpointerAccessor = inverseReferenceMapping.getAttributeAccessor();
final CoreContainerPolicy backpointerContainerPolicy = inverseReferenceMapping.getContainerPolicy();
if (backpointerContainerPolicy == null) {
backpointerAccessor.setAttributeValueInObject(value, referenceSourceObject);
} else {
Object backpointerContainer = backpointerAccessor.getAttributeValueFromObject(value);
if (backpointerContainer == null) {
backpointerContainer = backpointerContainerPolicy.containerInstance();
backpointerAccessor.setAttributeValueInObject(value, backpointerContainer);
}
backpointerContainerPolicy.addInto(referenceSourceObject, backpointerContainer, session);
}
}
} else if (reference.getMapping() instanceof ObjectReferenceMapping) {
final CacheId primaryKey = (CacheId) reference.getPrimaryKey();
Object value = null;
if (userSpecifiedResolver != null) {
final Callable c;
try {
if (primaryKey.getPrimaryKey().length > 1) {
final Map<String, Object> idWrapper = new HashMap<>();
for (int y = 0; y < primaryKey.getPrimaryKey().length; y++) {
final ObjectReferenceMapping refMapping = (ObjectReferenceMapping) reference.getMapping();
final String idName = (String) refMapping.getReferenceDescriptor().getPrimaryKeyFieldNames().get(y);
final Object idValue = primaryKey.getPrimaryKey()[y];
idWrapper.put(idName, idValue);
}
c = userSpecifiedResolver.resolve(idWrapper, reference.getTargetClass());
} else {
c = userSpecifiedResolver.resolve(primaryKey.getPrimaryKey()[0], reference.getTargetClass());
}
if (c != null) {
value = c.call();
}
} catch (Exception e) {
throw XMLMarshalException.unmarshalException(e);
}
} else {
value = getValue(session, reference, primaryKey, handler);
}
ObjectReferenceMapping mapping = (ObjectReferenceMapping) reference.getMapping();
if (value != null) {
mapping.setAttributeValueInObject(reference.getSourceObject(), value);
}
if (null != reference.getSetting()) {
reference.getSetting().setValue(value);
}
InverseReferenceMapping inverseReferenceMapping = mapping.getInverseReferenceMapping();
if (inverseReferenceMapping != null) {
CoreAttributeAccessor backpointerAccessor = inverseReferenceMapping.getAttributeAccessor();
CoreContainerPolicy backpointerContainerPolicy = inverseReferenceMapping.getContainerPolicy();
if (backpointerContainerPolicy == null) {
backpointerAccessor.setAttributeValueInObject(value, referenceSourceObject);
} else {
Object backpointerContainer = backpointerAccessor.getAttributeValueFromObject(value);
if (backpointerContainer == null) {
backpointerContainer = backpointerContainerPolicy.containerInstance();
backpointerAccessor.setAttributeValueInObject(value, backpointerContainer);
}
backpointerContainerPolicy.addInto(reference.getSourceObject(), backpointerContainer, session);
}
}
}
}
use of org.eclipse.persistence.core.mappings.CoreAttributeAccessor in project eclipselink by eclipse-ee4j.
the class XMLContext method applyORMMetadata.
/**
* ADVANCED:
* Adjust the OXM metadata to take into account ORM mapping metadata,
*/
public void applyORMMetadata(AbstractSession ormSession) {
// Iterate over the ORM descriptors and check for matching OXM descriptors
Iterator<ClassDescriptor> ormDescriptors = ormSession.getDescriptors().values().iterator();
while (ormDescriptors.hasNext()) {
ClassDescriptor ormDescriptor = ormDescriptors.next();
Class<?> javaClass = ormDescriptor.getJavaClass();
AbstractSession oxmSession = null;
try {
oxmSession = this.getSession(javaClass);
} catch (XMLMarshalException ex) {
// if we couldn't find a session for this class, we
// don't have an OX descriptor for it.
}
if (oxmSession != null) {
ClassDescriptor oxmDescriptor = oxmSession.getDescriptor(javaClass);
// If we have an oxmDescriptor for this ORM descriptor, iterate over
// mappings, and update the required OXM mappings attribute accessors
Iterator<DatabaseMapping> ormMappings = ormDescriptor.getMappings().iterator();
while (ormMappings.hasNext()) {
DatabaseMapping ormMapping = ormMappings.next();
Mapping oxmMapping = (Mapping) oxmDescriptor.getMappingForAttributeName(ormMapping.getAttributeName());
if (oxmMapping != null) {
CoreAttributeAccessor oxmAccessor = oxmMapping.getAttributeAccessor();
OrmAttributeAccessor newAccessor = new OrmAttributeAccessor(ormMapping.getAttributeAccessor(), oxmAccessor);
if (ormMapping.isOneToOneMapping() && ((OneToOneMapping) ormMapping).usesIndirection()) {
newAccessor.setValueHolderProperty(true);
}
newAccessor.setChangeTracking(ormDescriptor.getObjectChangePolicy().isAttributeChangeTrackingPolicy());
oxmMapping.setAttributeAccessor(newAccessor);
// check to see if we need to deal with containerAccessor
CoreAttributeAccessor containerAccessor = null;
Class<?> containerClass = null;
if (oxmMapping.isAbstractCompositeObjectMapping()) {
containerAccessor = ((CompositeObjectMapping) oxmMapping).getInverseReferenceMapping().getAttributeAccessor();
containerClass = ((CompositeObjectMapping) oxmMapping).getReferenceClass();
} else if (oxmMapping.isAbstractCompositeCollectionMapping()) {
containerAccessor = ((CompositeCollectionMapping) oxmMapping).getInverseReferenceMapping().getAttributeAccessor();
containerClass = ((CompositeCollectionMapping) oxmMapping).getReferenceClass();
}
if (containerAccessor != null) {
ClassDescriptor containerDescriptor = ormSession.getDescriptor(containerClass);
if (containerDescriptor != null) {
DatabaseMapping ormContainerMapping = containerDescriptor.getMappingForAttributeName(containerAccessor.getAttributeName());
if (ormContainerMapping != null) {
// Check for indirection on the container mapping
OrmAttributeAccessor ormAccessor = new OrmAttributeAccessor(ormContainerMapping.getAttributeAccessor(), containerAccessor);
ormAccessor.setChangeTracking(containerDescriptor.getObjectChangePolicy().isAttributeChangeTrackingPolicy());
ormAccessor.setValueHolderProperty(ormContainerMapping instanceof OneToOneMapping && ((OneToOneMapping) ormContainerMapping).usesIndirection());
if (oxmMapping.isAbstractCompositeObjectMapping()) {
((CompositeObjectMapping) oxmMapping).getInverseReferenceMapping().setAttributeAccessor(ormAccessor);
} else if (oxmMapping.isAbstractCompositeCollectionMapping()) {
((CompositeCollectionMapping) oxmMapping).getInverseReferenceMapping().setAttributeAccessor(ormAccessor);
}
}
}
}
}
}
Iterator<DatabaseMapping> oxmMappingsIterator = oxmDescriptor.getMappings().iterator();
while (oxmMappingsIterator.hasNext()) {
// iterate over the oxm mappings. Any ReferenceMappings that have a
// collection as a backpointer, check to see if the container policy
// needs to be matched with the ORM project
DatabaseMapping nextMapping = oxmMappingsIterator.next();
if (nextMapping instanceof ObjectReferenceMapping) {
ObjectReferenceMapping refMapping = (ObjectReferenceMapping) nextMapping;
if (refMapping.getInverseReferenceMapping().getAttributeAccessor() != null && refMapping.getInverseReferenceMapping().getContainerPolicy() != null) {
ClassDescriptor refDescriptor = ormSession.getClassDescriptor(refMapping.getReferenceClass());
if (refDescriptor != null) {
DatabaseMapping backpointerMapping = refDescriptor.getMappingForAttributeName(refMapping.getInverseReferenceMapping().getAttributeName());
if (backpointerMapping != null && backpointerMapping.isCollectionMapping()) {
refMapping.getInverseReferenceMapping().getContainerPolicy().setContainerClass(backpointerMapping.getContainerPolicy().getContainerClass());
}
}
}
}
}
}
}
}
use of org.eclipse.persistence.core.mappings.CoreAttributeAccessor in project eclipselink by eclipse-ee4j.
the class XMLVariableXPathMappingNodeValue method setXPathInObject.
public void setXPathInObject(String uri, String localName, Object childObject) {
CoreAttributeAccessor variableAttributeAccessor = this.getMapping().getVariableAttributeAccessor();
if (!variableAttributeAccessor.isWriteOnly()) {
Object value = null;
if (getMapping().getVariableAttributeAccessor().getAttributeClass() == CoreClassConstants.QNAME) {
if (uri != null && uri.length() > 0) {
value = new QName(uri, localName);
} else {
value = new QName(localName);
}
} else {
value = localName;
}
variableAttributeAccessor.setAttributeValueInObject(childObject, value);
}
}
Aggregations