Search in sources :

Example 16 with XMLField

use of org.eclipse.persistence.oxm.XMLField 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 17 with XMLField

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

the class SDOProperty method buildXMLDirectMapping.

private DatabaseMapping buildXMLDirectMapping(String mappingUri) {
    XMLDirectMapping mapping = new XMLDirectMapping();
    mapping.setNullValueMarshalled(true);
    mapping.setAttributeName(getName());
    String xpath = getQualifiedXPath(mappingUri, true);
    mapping.setXPath(xpath);
    if (getXsdType() != null) {
        ((XMLField) mapping.getField()).setSchemaType(getXsdType());
    }
    if (getType().getInstanceClass() != null) {
        if (shouldAddInstanceClassConverter()) {
            InstanceClassConverter converter = new InstanceClassConverter();
            converter.setCustomClass(getType().getInstanceClass());
            mapping.setConverter(converter);
        }
    }
    // Use NullPolicy or IsSetNullPolicy
    if (nullable) {
        // elements only
        setIsSetNillablePolicyOnMapping(mapping, propertyName);
    } else {
        // elements or attributes
        setIsSetOptionalPolicyOnMapping(mapping, propertyName);
    }
    if (this.isDefaultSet()) {
        mapping.setNullValue(getDefault());
        mapping.getNullPolicy().setNullRepresentedByEmptyNode(false);
    } else {
        mapping.getNullPolicy().setNullRepresentedByEmptyNode(true);
    }
    return mapping;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) InstanceClassConverter(org.eclipse.persistence.sdo.helper.InstanceClassConverter)

Example 18 with XMLField

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

the class SDOProperty method buildXMLCompositeCollectionMapping.

private DatabaseMapping buildXMLCompositeCollectionMapping(String mappingUri) {
    XMLCompositeCollectionMapping mapping = new XMLCompositeCollectionMapping();
    mapping.setAttributeName(getName());
    String xpath = getQualifiedXPath(mappingUri, false);
    mapping.setXPath(xpath);
    if (!getType().isDataObjectType()) {
        QName schemaContext = getType().getXmlDescriptor().getSchemaReference().getSchemaContextAsQName(getType().getXmlDescriptor().getNamespaceResolver());
        ((XMLField) mapping.getField()).setLeafElementType(schemaContext);
        mapping.setReferenceClassName(getType().getImplClassName());
        mapping.setReferenceClass(getType().getImplClass());
    } else {
        if (getXsdType() != null) {
            ((XMLField) mapping.getField()).setLeafElementType(getXsdType());
        }
    }
    mapping.useCollectionClass(ListWrapper.class);
    // Set null policy on mapping for xsi:nil support:
    // - aNullPolicy.setNullRepresentedByEmptyNode(false);
    // - aNullPolicy.setNullRepresentedByXsiNil(true);
    setIsSetNillablePolicyOnMapping(mapping, propertyName);
    return mapping;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) QName(javax.xml.namespace.QName) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)

Example 19 with XMLField

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

the class SDOProperty method buildXMLCompositeObjectMapping.

private DatabaseMapping buildXMLCompositeObjectMapping(String mappingUri) {
    XMLCompositeObjectMapping mapping = new XMLCompositeObjectMapping();
    mapping.setAttributeName(getName());
    String xpath = getQualifiedXPath(mappingUri, false);
    mapping.setXPath(xpath);
    if (!getType().isDataObjectType()) {
        QName schemaContext = getType().getXmlDescriptor().getSchemaReference().getSchemaContextAsQName(getType().getXmlDescriptor().getNamespaceResolver());
        ((XMLField) mapping.getField()).setLeafElementType(schemaContext);
        mapping.setReferenceClassName(getType().getImplClassName());
        mapping.setReferenceClass(getType().getImplClass());
    } else {
        if (getXsdType() != null) {
            ((XMLField) mapping.getField()).setLeafElementType(getXsdType());
        }
    }
    // Handle nillable element support via the nullable property
    if (nullable) {
        setIsSetNillablePolicyOnMapping(mapping, propertyName);
    } else {
        // elements or attributes
        setIsSetOptionalPolicyOnMapping(mapping, propertyName);
    }
    return mapping;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) QName(javax.xml.namespace.QName) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 20 with XMLField

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

the class SDOProperty method buildXMLTransformationMapping.

private DatabaseMapping buildXMLTransformationMapping(String mappingUri) {
    XMLTransformationMapping mapping = new XMLTransformationMapping();
    mapping.setAttributeName(getName());
    String xpath = getQualifiedXPath(mappingUri, true);
    String xpathMinusText;
    int indexOfTextXPath = xpath.lastIndexOf("/text()");
    if (indexOfTextXPath < 0) {
        xpathMinusText = xpath;
    } else {
        xpathMinusText = xpath.substring(0, indexOfTextXPath);
    }
    QNameTransformer transformer = new QNameTransformer(xpath);
    mapping.setAttributeTransformer(transformer);
    mapping.addFieldTransformer(xpath, transformer);
    NamespaceResolver nsr = new NamespaceResolver();
    nsr.put(javax.xml.XMLConstants.XMLNS_ATTRIBUTE, javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
    XMLField field = new XMLField();
    field.setNamespaceResolver(nsr);
    field.setXPath(xpathMinusText + "/@" + javax.xml.XMLConstants.XMLNS_ATTRIBUTE + ":" + QNameTransformer.QNAME_NAMESPACE_PREFIX);
    mapping.addFieldTransformer(field, new NamespaceURITransformer());
    return mapping;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) QNameTransformer(org.eclipse.persistence.sdo.helper.metadata.QNameTransformer) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) NamespaceURITransformer(org.eclipse.persistence.sdo.helper.metadata.NamespaceURITransformer)

Aggregations

XMLField (org.eclipse.persistence.oxm.XMLField)302 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)141 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)99 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)48 QName (javax.xml.namespace.QName)43 Element (org.w3c.dom.Element)41 NodeList (org.w3c.dom.NodeList)39 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)36 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)30 ArrayList (java.util.ArrayList)27 Node (org.w3c.dom.Node)25 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)22 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)21 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)17 XMLRecord (org.eclipse.persistence.oxm.record.XMLRecord)17 XPathFragment (org.eclipse.persistence.internal.oxm.XPathFragment)15 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)15 XMLCompositeDirectCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping)15 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)14 Field (org.eclipse.persistence.internal.oxm.mappings.Field)13