Search in sources :

Example 6 with XMLInverseReferenceMapping

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

the class PreLoginMappingAdapter method preLogin.

/* (non-Javadoc)
     * @see org.eclipse.persistence.internal.jaxb.SessionEventListener#preLogin(org.eclipse.persistence.sessions.SessionEvent)
     */
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void preLogin(SessionEvent event) {
    Project project = event.getSession().getProject();
    ClassLoader cl = jpaSession.getDatasourcePlatform().getConversionManager().getLoader();
    DefaultXMLNameTransformer xmlNameTransformer = new DefaultXMLNameTransformer();
    for (Object descriptorAlias : project.getAliasDescriptors().keySet()) {
        ClassDescriptor descriptor = project.getAliasDescriptors().get(descriptorAlias);
        if (!PersistenceWeavedRest.class.isAssignableFrom(descriptor.getJavaClass())) {
            continue;
        }
        if (descriptor.isXMLDescriptor()) {
            XMLDescriptor xmlDescriptor = (XMLDescriptor) project.getAliasDescriptors().get(descriptorAlias);
            if (null != xmlDescriptor) {
                if (null == xmlDescriptor.getDefaultRootElement()) {
                    // set root element
                    xmlDescriptor.setDefaultRootElement(xmlNameTransformer.transformRootElementName(xmlDescriptor.getJavaClass().getName()));
                    // set resultAlwaysXMLRoot to false, so that the elements are not wrapped in JAXBElements when unmarshalling.
                    xmlDescriptor.setResultAlwaysXMLRoot(false);
                }
            }
        }
        XMLCompositeCollectionMapping relationshipMapping = new XMLCompositeCollectionMapping();
        relationshipMapping.setAttributeName("_persistence_relationshipInfo");
        relationshipMapping.setGetMethodName("_persistence_getRelationships");
        relationshipMapping.setSetMethodName("_persistence_setRelationships");
        relationshipMapping.setDescriptor(descriptor);
        CollectionContainerPolicy containerPolicy = new CollectionContainerPolicy(ArrayList.class);
        relationshipMapping.setContainerPolicy(containerPolicy);
        relationshipMapping.setField(new XMLField("_relationships"));
        relationshipMapping.setReferenceClass(Link.class);
        XMLJavaTypeConverter converter = new XMLJavaTypeConverter(RelationshipLinkAdapter.class);
        converter.initialize(relationshipMapping, event.getSession());
        relationshipMapping.setConverter(converter);
        descriptor.addMapping(relationshipMapping);
        XMLCompositeObjectMapping hrefMapping = new XMLCompositeObjectMapping();
        hrefMapping.setAttributeName("_persistence_href");
        hrefMapping.setGetMethodName("_persistence_getHref");
        hrefMapping.setSetMethodName("_persistence_setHref");
        hrefMapping.setDescriptor(descriptor);
        hrefMapping.setField(new XMLField("_link"));
        hrefMapping.setReferenceClass(Link.class);
        hrefMapping.setXPath(".");
        descriptor.addMapping(hrefMapping);
        XMLCompositeObjectMapping itemLinksMapping = new XMLCompositeObjectMapping();
        itemLinksMapping.setAttributeName("_persistence_links");
        itemLinksMapping.setGetMethodName("_persistence_getLinks");
        itemLinksMapping.setSetMethodName("_persistence_setLinks");
        itemLinksMapping.setDescriptor(descriptor);
        itemLinksMapping.setReferenceClass(ItemLinks.class);
        itemLinksMapping.setXPath(".");
        descriptor.addMapping(itemLinksMapping);
        ClassDescriptor jpaDescriptor = jpaSession.getDescriptorForAlias(descriptor.getAlias());
        Vector<DatabaseMapping> descriptorMappings = (Vector<DatabaseMapping>) descriptor.getMappings().clone();
        for (DatabaseMapping mapping : descriptorMappings) {
            if (mapping.isXMLMapping()) {
                if (mapping.isAbstractCompositeObjectMapping() || mapping.isAbstractCompositeCollectionMapping()) {
                    if (mapping.isAbstractCompositeCollectionMapping()) {
                        XMLInverseReferenceMapping inverseMapping = ((XMLCompositeCollectionMapping) mapping).getInverseReferenceMapping();
                        if (inverseMapping != null) {
                            break;
                        }
                    } else if (mapping.isAbstractCompositeObjectMapping()) {
                        XMLInverseReferenceMapping inverseMapping = ((XMLCompositeObjectMapping) mapping).getInverseReferenceMapping();
                        if (inverseMapping != null) {
                            break;
                        }
                    }
                    if (jpaDescriptor != null) {
                        DatabaseMapping dbMapping = jpaDescriptor.getMappingForAttributeName(mapping.getAttributeName());
                        if ((dbMapping != null) && (dbMapping instanceof ForeignReferenceMapping)) {
                            ForeignReferenceMapping jpaMapping = (ForeignReferenceMapping) dbMapping;
                            if (jpaMapping.getMappedBy() != null) {
                                ClassDescriptor inverseDescriptor = project.getDescriptorForAlias(jpaMapping.getReferenceDescriptor().getAlias());
                                if (inverseDescriptor != null) {
                                    DatabaseMapping inverseMapping = inverseDescriptor.getMappingForAttributeName(jpaMapping.getMappedBy());
                                    if (inverseMapping != null) {
                                        convertMappingToXMLInverseReferenceMapping(inverseDescriptor, inverseMapping, jpaMapping);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        InheritancePolicy inheritancePolicy = descriptor.getInheritancePolicyOrNull();
        if ((inheritancePolicy != null) && (inheritancePolicy.isRootParentDescriptor())) {
            boolean isAbstract = Modifier.isAbstract(descriptor.getJavaClass().getModifiers());
            if (isAbstract) {
                Class subClassToInstantiate = null;
                Map<?, ?> classIndicatorMapping = inheritancePolicy.getClassIndicatorMapping();
                // get one of subclasses that extends this abstract class
                for (Map.Entry<?, ?> entry : classIndicatorMapping.entrySet()) {
                    Object value = entry.getValue();
                    if (value instanceof Class) {
                        subClassToInstantiate = (Class) value;
                        isAbstract = Modifier.isAbstract(subClassToInstantiate.getModifiers());
                        if (!isAbstract) {
                            InstantiationPolicy instantiationPolicy = new InstantiationPolicy();
                            instantiationPolicy.useFactoryInstantiationPolicy(new ConcreteSubclassFactory(subClassToInstantiate), "createConcreteSubclass");
                            descriptor.setInstantiationPolicy(instantiationPolicy);
                            break;
                        }
                    }
                }
            }
        }
    }
    for (Object descriptorAlias : project.getAliasDescriptors().keySet()) {
        ClassDescriptor descriptor = project.getAliasDescriptors().get(descriptorAlias);
        ClassDescriptor jpaDescriptor = jpaSession.getDescriptorForAlias(descriptor.getAlias());
        Vector<DatabaseMapping> descriptorMappings = (Vector<DatabaseMapping>) descriptor.getMappings().clone();
        for (DatabaseMapping mapping : descriptorMappings) {
            if (mapping.isXMLMapping()) {
                if (mapping.isAbstractCompositeObjectMapping() || mapping.isAbstractCompositeCollectionMapping()) {
                    if (jpaDescriptor != null) {
                        DatabaseMapping dbMapping = jpaDescriptor.getMappingForAttributeName(mapping.getAttributeName());
                        if ((dbMapping instanceof ForeignReferenceMapping)) {
                            ForeignReferenceMapping jpaMapping = (ForeignReferenceMapping) dbMapping;
                            ClassDescriptor jaxbDescriptor = project.getDescriptorForAlias(jpaMapping.getDescriptor().getAlias());
                            convertMappingToXMLChoiceMapping(jaxbDescriptor, jpaMapping, cl, jpaSession);
                        }
                    } else if (mapping instanceof XMLCompositeObjectMapping) {
                        // Fix for Bug 403113 - JPA-RS Isn't Serializing an Embeddable defined in an ElementCollection to JSON Correctly
                        // add choice mapping for one-to-one relationships within embeddables
                        // Based on (http://wiki.eclipse.org/EclipseLink/Examples/JPA/NoSQL#Step_2_:_Map_the_data),
                        // the mappedBy option on relationships is not supported for NoSQL data, so no need to add inverse mapping
                        XMLCompositeObjectMapping jpaMapping = (XMLCompositeObjectMapping) mapping;
                        ClassDescriptor jaxbDescriptor = project.getDescriptorForAlias(jpaMapping.getDescriptor().getAlias());
                        if (jaxbDescriptor != null) {
                            Class clazz = jpaMapping.getReferenceClass();
                            if (clazz != null) {
                                if ((jpaSession.getDescriptor(clazz) != null) && (jpaSession.getDescriptor(clazz).isEISDescriptor()))
                                    convertMappingToXMLChoiceMapping(jaxbDescriptor, jpaMapping, cl, jpaSession);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) InstantiationPolicy(org.eclipse.persistence.internal.descriptors.InstantiationPolicy) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) Vector(java.util.Vector) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter) XMLField(org.eclipse.persistence.oxm.XMLField) PersistenceWeavedRest(org.eclipse.persistence.internal.jpa.rs.weaving.PersistenceWeavedRest) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Project(org.eclipse.persistence.sessions.Project) InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) DefaultXMLNameTransformer(org.eclipse.persistence.jaxb.DefaultXMLNameTransformer) Map(java.util.Map) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 7 with XMLInverseReferenceMapping

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

the class PreLoginMappingAdapter method convertMappingToXMLInverseReferenceMapping.

/**
 * Build an XMLInverseMapping based on a particular mapping and replace that mapping with
 * the newly created XMLInverseMapping in jaxbDescriptor
 */
private static void convertMappingToXMLInverseReferenceMapping(ClassDescriptor jaxbDescriptor, DatabaseMapping mapping, ForeignReferenceMapping jpaMapping) {
    if ((mapping != null) && (jaxbDescriptor != null)) {
        if (!(mapping.isXMLMapping())) {
            return;
        }
        if ((jpaMapping.isAggregateCollectionMapping()) || (jpaMapping.isAggregateMapping())) {
            return;
        }
        XMLInverseReferenceMapping jaxbInverseMapping = new XMLInverseReferenceMapping();
        copyAccessorToMapping(mapping, jaxbInverseMapping);
        jaxbInverseMapping.setProperties(mapping.getProperties());
        jaxbInverseMapping.setIsReadOnly(mapping.isReadOnly());
        jaxbInverseMapping.setMappedBy(jpaMapping.getAttributeName());
        if (mapping.isAbstractCompositeCollectionMapping()) {
            jaxbInverseMapping.setContainerPolicy(mapping.getContainerPolicy());
            jaxbInverseMapping.setReferenceClass(((XMLCompositeCollectionMapping) mapping).getReferenceClass());
        } else if (mapping.isAbstractCompositeObjectMapping()) {
            jaxbInverseMapping.setReferenceClass(((XMLCompositeObjectMapping) mapping).getReferenceClass());
        }
        jaxbDescriptor.removeMappingForAttributeName(mapping.getAttributeName());
        jaxbDescriptor.addMapping(jaxbInverseMapping);
    }
}
Also used : XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 8 with XMLInverseReferenceMapping

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

the class XmlInverseReferenceMappingTestCases method testAddressContainerType.

public void testAddressContainerType() {
    XMLDescriptor xDesc = xmlContext.getDescriptor(new QName("address"));
    assertNotNull("No descriptor was generated for Address.", xDesc);
    DatabaseMapping mapping = xDesc.getMappingForAttributeName("emp");
    assertNotNull("No mapping exists on Address for attribute [emp].", mapping);
    assertTrue("Expected an XMLInverseReferenceMapping for attribute [emp], but was [" + mapping.toString() + "].", mapping instanceof XMLInverseReferenceMapping);
    assertTrue("Expected container class [java.util.LinkedList] but was [" + mapping.getContainerPolicy().getContainerClassName() + "]", mapping.getContainerPolicy().getContainerClassName().equals("java.util.LinkedList"));
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) QName(javax.xml.namespace.QName) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping)

Example 9 with XMLInverseReferenceMapping

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

the class JAXBValueStore method unsetDeclaredProperty.

/**
 * For isMany=false properties set the value to null. For isMany=true set
 * the value to an empty container of the appropriate type.
 */
@Override
public void unsetDeclaredProperty(int propertyIndex) {
    SDOProperty declaredProperty = (SDOProperty) dataObject.getType().getDeclaredProperties().get(propertyIndex);
    Mapping mapping = this.getJAXBMappingForProperty(declaredProperty);
    if (declaredProperty.isMany()) {
        ContainerMapping containerMapping = (ContainerMapping) mapping;
        ContainerPolicy containerPolicy = containerMapping.getContainerPolicy();
        // OLD VALUE
        if (mapping.isAbstractCompositeCollectionMapping()) {
            XMLCompositeCollectionMapping compositeMapping = (XMLCompositeCollectionMapping) mapping;
            XMLInverseReferenceMapping inverseReferenceMapping = compositeMapping.getInverseReferenceMapping();
            if (inverseReferenceMapping != null && inverseReferenceMapping.getAttributeAccessor() != null) {
                Object oldContainer = mapping.getAttributeValueFromObject(entity);
                if (oldContainer != null) {
                    AbstractSession session = ((JAXBContext) jaxbHelperContext.getJAXBContext()).getXMLContext().getSession(entity);
                    Object iterator = containerPolicy.iteratorFor(oldContainer);
                    while (containerPolicy.hasNext(iterator)) {
                        Object oldValue = containerPolicy.next(iterator, session);
                        inverseReferenceMapping.getAttributeAccessor().setAttributeValueInObject(oldValue, null);
                    }
                }
            }
        }
        // NEW VALUE
        Object container = containerPolicy.containerInstance();
        mapping.getAttributeAccessor().setAttributeValueInObject(entity, container);
    } else {
        // OLD VALUE
        Object oldValue = mapping.getAttributeAccessor().getAttributeValueFromObject(entity);
        if (mapping.isAbstractCompositeObjectMapping()) {
            XMLCompositeObjectMapping compositeMapping = (XMLCompositeObjectMapping) mapping;
            final XMLInverseReferenceMapping inverseReferenceMapping = compositeMapping.getInverseReferenceMapping();
            if (inverseReferenceMapping != null && inverseReferenceMapping.getAttributeAccessor() != null) {
                if (oldValue != null) {
                    inverseReferenceMapping.getAttributeAccessor().setAttributeValueInObject(oldValue, null);
                }
            }
        }
        // NEW VALUE
        mapping.getAttributeAccessor().setAttributeValueInObject(entity, null);
    }
}
Also used : CoreContainerPolicy(org.eclipse.persistence.internal.core.queries.CoreContainerPolicy) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) ContainerMapping(org.eclipse.persistence.mappings.ContainerMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) DataObject(commonj.sdo.DataObject) ContainerMapping(org.eclipse.persistence.mappings.ContainerMapping) SDOProperty(org.eclipse.persistence.sdo.SDOProperty) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 10 with XMLInverseReferenceMapping

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

the class DynamicTestProject method addAddressDescriptor.

private void addAddressDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClassName("org.eclipse.persistence.testing.oxm.dynamic.XAddress");
    XMLDirectMapping street = new XMLDirectMapping();
    street.setAttributeName("street");
    street.setXPath("street/text()");
    descriptor.addMapping(street);
    XMLInverseReferenceMapping owningEmployee = new XMLInverseReferenceMapping();
    owningEmployee.setReferenceClassName("org.eclipse.persistence.testing.oxm.dynamic.XEmployee");
    owningEmployee.setMappedBy("address");
    owningEmployee.setAttributeName("owningEmployee");
    owningEmployee.setSetMethodName("setOwningEmployee");
    owningEmployee.setGetMethodName("getOwningEmployee");
    descriptor.addMapping(owningEmployee);
    this.addDescriptor(descriptor);
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping)

Aggregations

XMLInverseReferenceMapping (org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping)15 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)9 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)9 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)7 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)4 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)4 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)3 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)3 DataObject (commonj.sdo.DataObject)2 FetchGroupManager (org.eclipse.persistence.descriptors.FetchGroupManager)2 PersistenceWeavedRest (org.eclipse.persistence.internal.jpa.rs.weaving.PersistenceWeavedRest)2 Mapping (org.eclipse.persistence.internal.oxm.mappings.Mapping)2 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)2 ContainerMapping (org.eclipse.persistence.mappings.ContainerMapping)2 XMLObjectReferenceMapping (org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping)2 FetchGroup (org.eclipse.persistence.queries.FetchGroup)2 FetchGroupTracker (org.eclipse.persistence.queries.FetchGroupTracker)2 SDODataObject (org.eclipse.persistence.sdo.SDODataObject)2 SDOProperty (org.eclipse.persistence.sdo.SDOProperty)2 List (java.util.List)1