Search in sources :

Example 1 with ObjectReferenceMapping

use of org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping in project eclipselink by eclipse-ee4j.

the class JsonSchemaGenerator method generateProperty.

private Property generateProperty(Mapping next, XMLDescriptor descriptor, Map<String, Property> properties) {
    Property prop = null;
    if (next.isCollectionMapping()) {
        if (next instanceof CollectionReferenceMapping) {
            CollectionReferenceMapping mapping = (CollectionReferenceMapping) next;
            Set<XMLField> sourceFields = mapping.getSourceToTargetKeyFieldAssociations().keySet();
            XMLDescriptor reference = (XMLDescriptor) mapping.getReferenceDescriptor();
            for (XMLField nextField : sourceFields) {
                XPathFragment frag = nextField.getXPathFragment();
                String propertyName = getNameForFragment(frag);
                XMLField targetField = (XMLField) mapping.getSourceToTargetKeyFieldAssociations().get(nextField);
                Class<?> type = CoreClassConstants.STRING;
                if (reference != null) {
                    type = getTypeForTargetField(targetField, reference);
                }
                prop = properties.get(propertyName);
                if (prop == null) {
                    prop = new Property();
                    prop.setName(propertyName);
                }
                Property nestedProperty = getNestedPropertyForFragment(frag, prop);
                nestedProperty.setType(JsonType.ARRAY);
                nestedProperty.setItem(new Property());
                nestedProperty.getItem().setType(getJsonTypeForJavaType(type));
                if (!properties.containsKey(prop.getName())) {
                    properties.put(prop.getName(), prop);
                }
            }
            return prop;
        } else if (next.isAbstractCompositeCollectionMapping()) {
            CompositeCollectionMapping mapping = (CompositeCollectionMapping) next;
            XMLField field = (XMLField) mapping.getField();
            XPathFragment frag = field.getXPathFragment();
            String propName = getNameForFragment(frag);
            // for paths, there may already be an existing property
            prop = properties.get(propName);
            if (prop == null) {
                prop = new Property();
                prop.setName(propName);
            }
            Property nestedProperty = getNestedPropertyForFragment(frag, prop);
            nestedProperty.setType(JsonType.ARRAY);
            nestedProperty.setItem(new Property());
            XMLDescriptor referenceDescriptor = (XMLDescriptor) mapping.getReferenceDescriptor();
            if (referenceDescriptor != null && referenceDescriptor.hasInheritance()) {
                // handle inheritence
                // schema.setAnyOf(new Property[descriptor.getInheritancePolicy().getAllChildDescriptors().size()]);
                List<ClassDescriptor> descriptors = getAllDescriptorsForInheritance(referenceDescriptor);
                Property[] props = new Property[descriptors.size()];
                for (int i = 0; i < props.length; i++) {
                    XMLDescriptor nextDescriptor = null;
                    nextDescriptor = (XMLDescriptor) descriptors.get(i);
                    Property ref = new Property();
                    ref.setRef(getReferenceForDescriptor(nextDescriptor, true));
                    props[i] = ref;
                }
                nestedProperty.getItem().setAnyOf(props);
            } else {
                nestedProperty.getItem().setRef(getReferenceForDescriptor(referenceDescriptor, false));
            // populateProperties(nestedProperty.getItem().getProperties(), (XMLDescriptor)mapping.getReferenceDescriptor());
            }
        } else if (next.isAbstractCompositeDirectCollectionMapping()) {
            DirectCollectionMapping mapping = (DirectCollectionMapping) next;
            XMLField field = (XMLField) mapping.getField();
            XPathFragment frag = field.getXPathFragment();
            List<String> enumeration = null;
            if (mapping.getValueConverter() instanceof JAXBEnumTypeConverter) {
                enumeration = getEnumeration((DatabaseMapping) next);
            }
            String propertyName = getNameForFragment(frag);
            if (frag.nameIsText()) {
                propertyName = (String) this.contextProperties.get(MarshallerProperties.JSON_VALUE_WRAPPER);
            }
            if (frag.isAttribute() && this.attributePrefix != null) {
                propertyName = attributePrefix + propertyName;
            }
            prop = properties.get(propertyName);
            if (prop == null) {
                prop = new Property();
                prop.setName(propertyName);
            }
            Property nestedProperty = getNestedPropertyForFragment(frag, prop);
            nestedProperty.setType(JsonType.ARRAY);
            nestedProperty.setItem(new Property());
            if (enumeration != null) {
                nestedProperty.getItem().setEnumeration(enumeration);
            }
            Class<?> type = mapping.getAttributeElementClass();
            if (type == null) {
                type = CoreClassConstants.STRING;
            }
            nestedProperty.getItem().setType(getJsonTypeForJavaType(type));
            return prop;
        } else if (next instanceof BinaryDataCollectionMapping) {
            BinaryDataCollectionMapping mapping = (BinaryDataCollectionMapping) next;
            XMLField field = (XMLField) mapping.getField();
            XPathFragment frag = field.getXPathFragment();
            String propertyName = getNameForFragment(frag);
            if (frag.isSelfFragment()) {
                propertyName = Constants.VALUE_WRAPPER;
                if (this.contextProperties != null) {
                    String valueWrapper = (String) this.contextProperties.get(JAXBContextProperties.JSON_VALUE_WRAPPER);
                    if (valueWrapper != null) {
                        propertyName = valueWrapper;
                    }
                }
            }
            if (frag.isAttribute() && this.attributePrefix != null) {
                propertyName = attributePrefix + propertyName;
            }
            prop = properties.get(propertyName);
            if (prop == null) {
                prop = new Property();
                prop.setName(propertyName);
            }
            Property nestedProperty = getNestedPropertyForFragment(frag, prop);
            nestedProperty.setType(JsonType.ARRAY);
            nestedProperty.setItem(new Property());
            if (mapping.shouldInlineBinaryData()) {
                nestedProperty.getItem().setType(JsonType.STRING);
            } else {
                nestedProperty.getItem().setAnyOf(getXopIncludeProperties());
            }
            return prop;
        }
    } else {
        if (next.isAbstractDirectMapping()) {
            // handle direct mapping
            DirectMapping directMapping = (DirectMapping) next;
            XMLField field = (XMLField) directMapping.getField();
            XPathFragment frag = field.getXPathFragment();
            List<String> enumeration = null;
            if (directMapping.getConverter() instanceof JAXBEnumTypeConverter) {
                enumeration = getEnumeration((DatabaseMapping) directMapping);
            }
            String propertyName = getNameForFragment(frag);
            if (frag.nameIsText()) {
                propertyName = Constants.VALUE_WRAPPER;
                if (this.contextProperties != null) {
                    String valueWrapper = (String) this.contextProperties.get(JAXBContextProperties.JSON_VALUE_WRAPPER);
                    if (valueWrapper != null) {
                        propertyName = valueWrapper;
                    }
                }
            }
            if (frag.isAttribute() && this.attributePrefix != null) {
                propertyName = attributePrefix + propertyName;
            }
            prop = properties.get(propertyName);
            if (prop == null) {
                prop = new Property();
                prop.setName(propertyName);
            }
            Property nestedProperty = getNestedPropertyForFragment(frag, prop);
            if (enumeration != null) {
                nestedProperty.setEnumeration(enumeration);
            }
            if (directMapping instanceof BinaryDataMapping) {
                BinaryDataMapping binaryMapping = (BinaryDataMapping) directMapping;
                if (binaryMapping.shouldInlineBinaryData() || binaryMapping.isSwaRef()) {
                    nestedProperty.setType(JsonType.STRING);
                } else {
                    if (this.xopIncludeProp == null) {
                        initXopIncludeProp();
                    }
                    nestedProperty.setAnyOf(this.xopIncludeProp);
                }
            } else {
                nestedProperty.setType(getJsonTypeForJavaType(directMapping.getAttributeClassification()));
            }
            return prop;
        } else if (next instanceof ObjectReferenceMapping) {
            ObjectReferenceMapping mapping = (ObjectReferenceMapping) next;
            Set<XMLField> sourceFields = mapping.getSourceToTargetKeyFieldAssociations().keySet();
            XMLDescriptor reference = (XMLDescriptor) mapping.getReferenceDescriptor();
            for (XMLField nextField : sourceFields) {
                XPathFragment frag = nextField.getXPathFragment();
                String propName = getNameForFragment(frag);
                XMLField targetField = (XMLField) mapping.getSourceToTargetKeyFieldAssociations().get(nextField);
                Class<?> type = CoreClassConstants.STRING;
                if (reference != null) {
                    type = getTypeForTargetField(targetField, reference);
                }
                prop = properties.get(propName);
                if (prop == null) {
                    prop = new Property();
                    prop.setName(propName);
                }
                Property nestedProperty = getNestedPropertyForFragment(frag, prop);
                // nestedProperty.setType(JsonType.ARRAY);
                // nestedProperty.setItem(new Property());
                nestedProperty.setType(getJsonTypeForJavaType(type));
                if (!properties.containsKey(prop.getName())) {
                    properties.put(prop.getName(), prop);
                }
            }
            return prop;
        } else if (next.isAbstractCompositeObjectMapping()) {
            CompositeObjectMapping mapping = (CompositeObjectMapping) next;
            XMLDescriptor nextDescriptor = (XMLDescriptor) mapping.getReferenceDescriptor();
            XMLField field = (XMLField) mapping.getField();
            XPathFragment firstFragment = field.getXPathFragment();
            if (firstFragment.isSelfFragment() || firstFragment.nameIsText()) {
                if (nextDescriptor != null) {
                    populateProperties(properties, nextDescriptor);
                }
            } else {
                String propName = getNameForFragment(firstFragment);
                prop = properties.get(propName);
                if (prop == null) {
                    prop = new Property();
                    prop.setName(propName);
                }
                // prop.setType(JsonType.OBJECT);
                prop.setName(propName);
                Property nestedProperty = getNestedPropertyForFragment(firstFragment, prop);
                XMLDescriptor referenceDescriptor = (XMLDescriptor) mapping.getReferenceDescriptor();
                if (referenceDescriptor != null && referenceDescriptor.hasInheritance()) {
                    // handle inheritence
                    // schema.setAnyOf(new Property[descriptor.getInheritancePolicy().getAllChildDescriptors().size()]);
                    List<ClassDescriptor> descriptors = getAllDescriptorsForInheritance(referenceDescriptor);
                    Property[] props = new Property[descriptors.size()];
                    for (int i = 0; i < props.length; i++) {
                        XMLDescriptor nextDesc = (XMLDescriptor) descriptors.get(i);
                        Property ref = new Property();
                        ref.setRef(getReferenceForDescriptor(nextDesc, true));
                        props[i] = ref;
                    }
                    nestedProperty.setAnyOf(props);
                } else {
                    nestedProperty.setRef(getReferenceForDescriptor(referenceDescriptor, false));
                // populateProperties(nestedProperty.getItem().getProperties(), (XMLDescriptor)mapping.getReferenceDescriptor());
                }
            // populateProperties(nestedProperty.getProperties(), nextDescriptor);
            }
        } else if (next instanceof BinaryDataMapping) {
            BinaryDataMapping binaryMapping = (BinaryDataMapping) next;
            XMLField field = (XMLField) binaryMapping.getField();
            XPathFragment frag = field.getXPathFragment();
            String propertyName = getNameForFragment(frag);
            if (frag.isSelfFragment()) {
                propertyName = Constants.VALUE_WRAPPER;
                if (this.contextProperties != null) {
                    String valueWrapper = (String) this.contextProperties.get(MarshallerProperties.JSON_VALUE_WRAPPER);
                    if (valueWrapper != null) {
                        propertyName = valueWrapper;
                    }
                }
            }
            if (frag.isAttribute() && this.attributePrefix != null) {
                propertyName = attributePrefix + propertyName;
            }
            prop = properties.get(propertyName);
            if (prop == null) {
                prop = new Property();
                prop.setName(propertyName);
            }
            Property nestedProperty = getNestedPropertyForFragment(frag, prop);
            if (binaryMapping.shouldInlineBinaryData() || binaryMapping.isSwaRef()) {
                nestedProperty.setType(JsonType.STRING);
            } else {
                if (this.xopIncludeProp == null) {
                    initXopIncludeProp();
                }
                nestedProperty.setAnyOf(this.xopIncludeProp);
            }
            return prop;
        }
    }
    return prop;
}
Also used : Set(java.util.Set) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) CollectionReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping) ArrayList(java.util.ArrayList) List(java.util.List) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) JAXBEnumTypeConverter(org.eclipse.persistence.jaxb.JAXBEnumTypeConverter) Property(org.eclipse.persistence.internal.jaxb.json.schema.model.Property) XMLField(org.eclipse.persistence.oxm.XMLField) ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping)

Example 2 with ObjectReferenceMapping

use of org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping in project eclipselink by eclipse-ee4j.

the class SchemaModelGenerator method processMapping.

/**
 * Process a given mapping.
 */
protected void processMapping(CoreMapping mapping, Sequence seq, ComplexType ct, HashMap<String, Schema> schemaForNamespace, Schema workingSchema, SchemaModelGeneratorProperties properties, List<Descriptor> descriptors) {
    if (mapping instanceof BinaryDataMapping) {
        processXMLBinaryDataMapping((BinaryDataMapping) mapping, seq, ct, schemaForNamespace, workingSchema, properties);
    } else if (mapping instanceof BinaryDataCollectionMapping) {
        processXMLBinaryDataCollectionMapping((BinaryDataCollectionMapping) mapping, seq, ct, schemaForNamespace, workingSchema, properties);
    } else if (mapping instanceof DirectMapping) {
        processXMLDirectMapping((DirectMapping) mapping, seq, ct, schemaForNamespace, workingSchema, properties);
    } else if (mapping instanceof DirectCollectionMapping) {
        processXMLCompositeDirectCollectionMapping((DirectCollectionMapping) mapping, seq, ct, schemaForNamespace, workingSchema, properties);
    } else if (mapping instanceof CompositeCollectionMapping) {
        processXMLCompositeMapping((CompositeCollectionMapping) mapping, seq, ct, schemaForNamespace, workingSchema, properties, descriptors, true);
    } else if (mapping instanceof CompositeObjectMapping) {
        processXMLCompositeMapping((CompositeObjectMapping) mapping, seq, ct, schemaForNamespace, workingSchema, properties, descriptors, false);
    } else if (mapping instanceof AnyAttributeMapping) {
        AnyAttribute anyAttribute = new AnyAttribute();
        anyAttribute.setProcessContents(AnyAttribute.LAX);
        ct.setAnyAttribute(anyAttribute);
    } else if (mapping instanceof AnyObjectMapping) {
        processAnyMapping(seq, false);
    } else if (mapping instanceof AnyCollectionMapping) {
        processAnyMapping(seq, true);
    } else if (mapping instanceof ChoiceObjectMapping) {
        processXMLChoiceObjectMapping((ChoiceObjectMapping) mapping, seq, ct, schemaForNamespace, workingSchema, properties, descriptors);
    } else if (mapping instanceof ChoiceCollectionMapping) {
        processXMLChoiceCollectionMapping((ChoiceCollectionMapping) mapping, seq, ct, schemaForNamespace, workingSchema, properties, descriptors);
    } else if (mapping instanceof CollectionReferenceMapping) {
        processXMLObjectReferenceMapping((CollectionReferenceMapping) mapping, seq, ct, schemaForNamespace, workingSchema, properties, descriptors, true);
    } else if (mapping instanceof ObjectReferenceMapping) {
        processXMLObjectReferenceMapping((ObjectReferenceMapping) mapping, seq, ct, schemaForNamespace, workingSchema, properties, descriptors, false);
    }
}
Also used : AnyAttribute(org.eclipse.persistence.internal.oxm.schema.model.AnyAttribute) ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) AnyObjectMapping(org.eclipse.persistence.internal.oxm.mappings.AnyObjectMapping) AnyCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.AnyCollectionMapping) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) ChoiceCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceCollectionMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) CollectionReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) AnyAttributeMapping(org.eclipse.persistence.internal.oxm.mappings.AnyAttributeMapping) ChoiceObjectMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceObjectMapping)

Example 3 with ObjectReferenceMapping

use of org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping in project eclipselink by eclipse-ee4j.

the class SchemaModelGenerator method processXMLObjectReferenceMapping.

/**
 * Process a given XMLObjectReferenceMapping.  In the case of an XMLCollectionReferenceMapping,
 * i.e. the isCollection flag is set to true, maxOccurs will be set to 'unbounded' on any
 * source elements
 */
protected void processXMLObjectReferenceMapping(ObjectReferenceMapping mapping, Sequence seq, ComplexType ct, HashMap<String, Schema> schemaForNamespace, Schema workingSchema, SchemaModelGeneratorProperties properties, List<Descriptor> descriptors, boolean isCollection) {
    String tgtClassName = mapping.getReferenceClassName();
    Descriptor tgtDesc = getDescriptorByName(tgtClassName, descriptors);
    if (tgtDesc == null) {
        throw DescriptorException.descriptorIsMissing(tgtClassName, (DatabaseMapping) mapping);
    }
    // get the target mapping(s) to determine the appropriate type(s)
    String schemaTypeString = null;
    Map<Field, Field> associations = mapping.getSourceToTargetKeyFieldAssociations();
    for (Entry<Field, Field> entry : associations.entrySet()) {
        Field tgtField = entry.getValue();
        Vector mappings = tgtDesc.getMappings();
        // schemaTypeString = Constants.SCHEMA_PREFIX + COLON + IDREF;
        for (Enumeration mappingsNum = mappings.elements(); mappingsNum.hasMoreElements(); ) {
            Mapping nextMapping = (Mapping) mappingsNum.nextElement();
            if (nextMapping.getField() != null && nextMapping.getField() instanceof Field) {
                Field xFld = (Field) nextMapping.getField();
                if (xFld == tgtField) {
                    schemaTypeString = getSchemaTypeForElement(tgtField, nextMapping.getAttributeClassification(), workingSchema);
                }
            }
        }
        if (schemaTypeString == null) {
            schemaTypeString = getSchemaTypeString(Constants.STRING_QNAME, workingSchema);
        }
        XPathFragment frag = entry.getKey().getXPathFragment();
        if (frag.isAttribute()) {
            Attribute attr = buildAttribute(frag, schemaTypeString);
            ct.getOrderedAttributes().add(attr);
        } else {
            Element elem = buildElement(frag, schemaTypeString, Occurs.ZERO, null);
            if (isCollection) {
                elem.setMaxOccurs(Occurs.UNBOUNDED);
            }
            seq.addElement(elem);
        }
    }
}
Also used : Field(org.eclipse.persistence.internal.oxm.mappings.Field) Enumeration(java.util.Enumeration) AnyAttribute(org.eclipse.persistence.internal.oxm.schema.model.AnyAttribute) Attribute(org.eclipse.persistence.internal.oxm.schema.model.Attribute) Element(org.eclipse.persistence.internal.oxm.schema.model.Element) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) AnyObjectMapping(org.eclipse.persistence.internal.oxm.mappings.AnyObjectMapping) CoreMapping(org.eclipse.persistence.core.mappings.CoreMapping) AnyCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.AnyCollectionMapping) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) ChoiceCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceCollectionMapping) ChoiceObjectMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceObjectMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) AnyAttributeMapping(org.eclipse.persistence.internal.oxm.mappings.AnyAttributeMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) CollectionReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) Vector(java.util.Vector)

Example 4 with ObjectReferenceMapping

use of org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping 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);
            }
        }
    }
}
Also used : InverseReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.InverseReferenceMapping) ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CoreAttributeAccessor(org.eclipse.persistence.core.mappings.CoreAttributeAccessor) Callable(java.util.concurrent.Callable) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) ConversionException(org.eclipse.persistence.exceptions.ConversionException) CollectionReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CoreContainerPolicy(org.eclipse.persistence.internal.core.queries.CoreContainerPolicy)

Example 5 with ObjectReferenceMapping

use of org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping 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());
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) CoreAttributeAccessor(org.eclipse.persistence.core.mappings.CoreAttributeAccessor) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) OrmAttributeAccessor(org.eclipse.persistence.internal.oxm.accessor.OrmAttributeAccessor) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

ObjectReferenceMapping (org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping)6 CollectionReferenceMapping (org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping)5 CompositeCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping)5 CompositeObjectMapping (org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping)5 BinaryDataCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping)4 BinaryDataMapping (org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping)4 DirectCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping)4 DirectMapping (org.eclipse.persistence.internal.oxm.mappings.DirectMapping)4 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)3 AnyAttributeMapping (org.eclipse.persistence.internal.oxm.mappings.AnyAttributeMapping)3 AnyCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.AnyCollectionMapping)3 AnyObjectMapping (org.eclipse.persistence.internal.oxm.mappings.AnyObjectMapping)3 ChoiceCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.ChoiceCollectionMapping)3 ChoiceObjectMapping (org.eclipse.persistence.internal.oxm.mappings.ChoiceObjectMapping)3 Mapping (org.eclipse.persistence.internal.oxm.mappings.Mapping)3 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2