Search in sources :

Example 11 with XMLChoiceObjectMapping

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

the class PreLoginMappingAdapter method convertMappingToXMLChoiceMapping.

/**
 * Build an XMLChoiceObjectMapping based on a particular mapping and replace that mapping with
 * the newly created XMLChoiceObjectMapping in jaxbDescriptor.
 * @param jaxbDescriptor the jaxb descriptor
 * @param jpaMapping the jpa mapping
 * @param cl the classloader
 */
@SuppressWarnings("rawtypes")
private static void convertMappingToXMLChoiceMapping(ClassDescriptor jaxbDescriptor, DatabaseMapping jpaMapping, ClassLoader cl, Session jpaSession) {
    if ((jpaMapping != null) && (jaxbDescriptor != null)) {
        if ((jpaMapping instanceof ForeignReferenceMapping) && ((jpaMapping.isAggregateCollectionMapping()) || (jpaMapping.isAggregateMapping()))) {
            // Aggregates don't have identity to create links, thus no weaved REST adapters to insert choice mappings
            return;
        }
        String attributeName = jpaMapping.getAttributeName();
        DatabaseMapping jaxbMapping = jaxbDescriptor.getMappingForAttributeName(jpaMapping.getAttributeName());
        if (!(jaxbMapping.isXMLMapping() && (jaxbMapping.isAbstractCompositeCollectionMapping() || jaxbMapping.isAbstractCompositeObjectMapping()))) {
            return;
        }
        ClassDescriptor refDesc = null;
        if (jpaMapping instanceof ForeignReferenceMapping) {
            Class clazz = ((ForeignReferenceMapping) jpaMapping).getReferenceClass();
            refDesc = jpaSession.getDescriptor(clazz);
        } else if (jpaMapping instanceof XMLCompositeObjectMapping) {
            Class clazz = ((XMLCompositeObjectMapping) jpaMapping).getReferenceClass();
            refDesc = jpaSession.getDescriptor(clazz);
        }
        if (refDesc == null) {
            return;
        }
        String adapterClassName = RestAdapterClassWriter.constructClassNameForReferenceAdapter(refDesc.getJavaClassName());
        if (adapterClassName != null) {
            try {
                if (jaxbMapping.isAbstractCompositeObjectMapping()) {
                    XMLChoiceObjectMapping xmlChoiceMapping = new XMLChoiceObjectMapping();
                    xmlChoiceMapping.setAttributeName(attributeName);
                    copyAccessorToMapping(jaxbMapping, xmlChoiceMapping);
                    xmlChoiceMapping.setProperties(jaxbMapping.getProperties());
                    XMLCompositeObjectMapping compositeMapping = (XMLCompositeObjectMapping) jaxbMapping;
                    xmlChoiceMapping.addChoiceElement(compositeMapping.getXPath(), Link.class);
                    xmlChoiceMapping.addChoiceElement(compositeMapping.getXPath(), refDesc.getJavaClass());
                    xmlChoiceMapping.setConverter(new XMLJavaTypeConverter((Class<? extends XmlAdapter<?, ?>>) Class.forName(adapterClassName, true, cl)));
                    jaxbDescriptor.removeMappingForAttributeName(jaxbMapping.getAttributeName());
                    jaxbDescriptor.addMapping(xmlChoiceMapping);
                } else if (jaxbMapping.isAbstractCompositeCollectionMapping()) {
                    XMLChoiceCollectionMapping xmlChoiceMapping = new XMLChoiceCollectionMapping();
                    xmlChoiceMapping.setAttributeName(attributeName);
                    copyAccessorToMapping(jaxbMapping, xmlChoiceMapping);
                    xmlChoiceMapping.setProperties(jaxbMapping.getProperties());
                    XMLCompositeCollectionMapping compositeMapping = (XMLCompositeCollectionMapping) jaxbMapping;
                    xmlChoiceMapping.addChoiceElement(compositeMapping.getXPath(), Link.class);
                    xmlChoiceMapping.addChoiceElement(compositeMapping.getXPath(), refDesc.getJavaClass());
                    xmlChoiceMapping.setContainerPolicy(jaxbMapping.getContainerPolicy());
                    xmlChoiceMapping.setConverter(new XMLJavaTypeConverter((Class<? extends XmlAdapter<?, ?>>) Class.forName(adapterClassName, true, cl)));
                    jaxbDescriptor.removeMappingForAttributeName(jaxbMapping.getAttributeName());
                    jaxbDescriptor.addMapping(xmlChoiceMapping);
                }
            } catch (Exception ex) {
                throw JPARSException.exceptionOccurred(ex);
            }
        }
    }
}
Also used : XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException) ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) XmlAdapter(jakarta.xml.bind.annotation.adapters.XmlAdapter) Link(org.eclipse.persistence.internal.jpa.rs.metadata.model.Link) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter)

Example 12 with XMLChoiceObjectMapping

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

the class TestModelProject method addRootDescriptor.

public void addRootDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClassName("org.persistence.testing.jaxb.dynamic.xxx.Root");
    descriptor.setNamespaceResolver(nsResolver);
    XMLCompositeObjectMapping compObj = new XMLCompositeObjectMapping();
    compObj.setAttributeName("compObj");
    compObj.setXPath("comp-obj");
    compObj.setReferenceClassName("org.persistence.testing.jaxb.dynamic.xxx.CompositeObjectTarget");
    descriptor.addMapping(compObj);
    XMLCompositeCollectionMapping compColl = new XMLCompositeCollectionMapping();
    compColl.setAttributeName("compColl");
    compColl.setXPath("comp-coll/item");
    compColl.setReferenceClassName("org.persistence.testing.jaxb.dynamic.xxx.CompositeCollectionTarget");
    compColl.useCollectionClass(ArrayList.class);
    compColl.setContainerPolicy(ContainerPolicy.buildPolicyFor(ArrayList.class));
    descriptor.addMapping(compColl);
    XMLCompositeDirectCollectionMapping compDirColl = new XMLCompositeDirectCollectionMapping();
    compDirColl.setAttributeName("compDirColl");
    compDirColl.setXPath("comp-dir-coll");
    descriptor.addMapping(compDirColl);
    XMLBinaryDataMapping binData = new XMLBinaryDataMapping();
    binData.setAttributeName("binData");
    XMLField binDataField = new XMLField("bin-data");
    binDataField.setSchemaType(XMLConstants.BASE_64_BINARY_QNAME);
    binData.setField(binDataField);
    binData.setShouldInlineBinaryData(true);
    descriptor.addMapping(binData);
    XMLBinaryDataCollectionMapping binDataColl = new XMLBinaryDataCollectionMapping();
    binDataColl.setAttributeName("binDataColl");
    XMLField binDataCollField = new XMLField("bin-data-coll");
    binDataCollField.setSchemaType(XMLConstants.BASE_64_BINARY_QNAME);
    binDataColl.setField(binDataCollField);
    binDataColl.setShouldInlineBinaryData(true);
    descriptor.addMapping(binDataColl);
    XMLAnyObjectMapping anyObj = new XMLAnyObjectMapping();
    anyObj.setAttributeName("anyObj");
    anyObj.setField(new XMLField("any-obj"));
    descriptor.addMapping(anyObj);
    XMLCompositeObjectMapping anyAtt = new XMLCompositeObjectMapping();
    anyAtt.setAttributeName("anyAtt");
    anyAtt.setXPath("any-att");
    anyAtt.setReferenceClassName("org.persistence.testing.jaxb.dynamic.xxx.AnyAttributeTarget");
    descriptor.addMapping(anyAtt);
    XMLTransformationMapping transform = new XMLTransformationMapping();
    transform.setAttributeName("transform");
    transform.setAttributeTransformer(new AttributeTransformer());
    transform.addFieldTransformer("transform/first-val/text()", new FirstFieldTransformer());
    transform.addFieldTransformer("transform/second-val/text()", new SecondFieldTransformer());
    descriptor.addMapping(transform);
    XMLFragmentMapping frag = new XMLFragmentMapping();
    frag.setAttributeName("frag");
    frag.setXPath("frag");
    descriptor.addMapping(frag);
    XMLFragmentCollectionMapping fragColl = new XMLFragmentCollectionMapping();
    fragColl.setAttributeName("fragColl");
    fragColl.setXPath("frag-coll");
    fragColl.useCollectionClass(ArrayList.class);
    descriptor.addMapping(fragColl);
    XMLObjectReferenceMapping objRef = new XMLObjectReferenceMapping();
    objRef.setAttributeName("objRef");
    objRef.setReferenceClassName("org.persistence.testing.jaxb.dynamic.xxx.ObjectReferenceTarget");
    objRef.addSourceToTargetKeyFieldAssociation("obj-ref-id/text()", "@id");
    descriptor.addMapping(objRef);
    XMLCollectionReferenceMapping collRef = new XMLCollectionReferenceMapping();
    collRef.setAttributeName("collRef");
    collRef.setReferenceClassName("org.persistence.testing.jaxb.dynamic.xxx.CollectionReferenceTarget");
    collRef.addSourceToTargetKeyFieldAssociation("coll-ref-id/text()", "@id");
    descriptor.addMapping(collRef);
    XMLChoiceObjectMapping choice = new XMLChoiceObjectMapping();
    choice.setAttributeName("choice");
    choice.addChoiceElement("choice-int/text()", Integer.class);
    choice.addChoiceElement("choice-float/text()", Float.class);
    descriptor.addMapping(choice);
    XMLChoiceCollectionMapping choiceColl = new XMLChoiceCollectionMapping();
    choiceColl.setAttributeName("choiceColl");
    choiceColl.addChoiceElement("choice-coll-double/text()", Double.class);
    choiceColl.addChoiceElement("choice-coll-string/text()", String.class);
    choiceColl.addChoiceElement("choice-coll-boolean/text()", Boolean.class);
    descriptor.addMapping(choiceColl);
    this.addDescriptor(descriptor);
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) XMLCollectionReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping) ArrayList(java.util.ArrayList) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) XMLCompositeDirectCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) SecondFieldTransformer(org.eclipse.persistence.testing.jaxb.dynamic.util.SecondFieldTransformer) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) XMLFragmentCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLFragmentCollectionMapping) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) FirstFieldTransformer(org.eclipse.persistence.testing.jaxb.dynamic.util.FirstFieldTransformer) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) AttributeTransformer(org.eclipse.persistence.testing.jaxb.dynamic.util.AttributeTransformer) XMLBinaryDataCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataCollectionMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLFragmentMapping(org.eclipse.persistence.oxm.mappings.XMLFragmentMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) XMLAnyObjectMapping(org.eclipse.persistence.oxm.mappings.XMLAnyObjectMapping)

Aggregations

XMLChoiceObjectMapping (org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping)12 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)7 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)7 XMLField (org.eclipse.persistence.oxm.XMLField)6 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)6 XMLChoiceCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping)5 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)5 ArrayList (java.util.ArrayList)4 XMLBinaryDataMapping (org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping)4 XMLJavaTypeConverter (org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter)3 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)3 XMLBinaryDataCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLBinaryDataCollectionMapping)3 XMLCompositeDirectCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping)3 XMLMapping (org.eclipse.persistence.oxm.mappings.XMLMapping)3 XMLObjectReferenceMapping (org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping)3 CoreConverter (org.eclipse.persistence.core.mappings.converters.CoreConverter)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)2 JAXBElementConverter (org.eclipse.persistence.internal.jaxb.JAXBElementConverter)2 JAXBElementRootConverter (org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter)2