Search in sources :

Example 1 with XMLChoiceObjectMapping

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

the class MappingsGenerator method generateChoiceMapping.

public ChoiceObjectMapping generateChoiceMapping(Property property, Descriptor descriptor, NamespaceInfo namespace) {
    XMLChoiceObjectMapping mapping = new XMLChoiceObjectMapping();
    initializeXMLMapping(mapping, property);
    boolean isIdRef = property.isXmlIdRef();
    Iterator<Property> choiceProperties = property.getChoiceProperties().iterator();
    while (choiceProperties.hasNext()) {
        Property next = choiceProperties.next();
        JavaClass type = next.getType();
        JavaClass originalType = next.getType();
        Converter converter = null;
        TypeInfo info = typeInfo.get(type.getName());
        if (info != null) {
            XmlJavaTypeAdapter adapter = info.getXmlJavaTypeAdapter();
            if (adapter != null) {
                String adapterValue = adapter.getValue();
                JavaClass adapterClass = helper.getJavaClass(adapterValue);
                JavaClass theClass = CompilerHelper.getTypeFromAdapterClass(adapterClass, helper);
                type = theClass;
                converter = new XMLJavaTypeConverter(adapterClass.getQualifiedName());
            }
        }
        if (next.getXmlJoinNodes() != null) {
            // handle XmlJoinNodes
            List<XMLField> srcFlds = new ArrayList<>();
            List<XMLField> tgtFlds = new ArrayList<>();
            for (XmlJoinNode xmlJoinNode : next.getXmlJoinNodes().getXmlJoinNode()) {
                srcFlds.add(new XMLField(xmlJoinNode.getXmlPath()));
                tgtFlds.add(new XMLField(xmlJoinNode.getReferencedXmlPath()));
            }
            mapping.addChoiceElement(srcFlds, type.getQualifiedName(), tgtFlds);
        } else if (isIdRef) {
            // handle IDREF
            String tgtXPath = null;
            TypeInfo referenceType = typeInfo.get(type.getQualifiedName());
            if (null != referenceType && referenceType.isIDSet()) {
                Property prop = referenceType.getIDProperty();
                tgtXPath = getXPathForField(prop, namespace, !prop.isAttribute(), false).getXPath();
            }
            // if the XPath is set (via xml-path) use it, otherwise figure it out
            Field srcXPath;
            if (next.getXmlPath() != null) {
                srcXPath = new XMLField(next.getXmlPath());
            } else {
                srcXPath = getXPathForField(next, namespace, true, false);
            }
            mapping.addChoiceElement(srcXPath.getXPath(), type.getQualifiedName(), tgtXPath);
        } else {
            XMLField xpath;
            if (next.getXmlPath() != null) {
                xpath = new XMLField(next.getXmlPath());
            } else {
                xpath = (XMLField) getXPathForField(next, namespace, (!(this.typeInfo.containsKey(type.getQualifiedName()))) || next.isMtomAttachment() || type.isEnum(), false);
            }
            mapping.addChoiceElement(xpath, type.getQualifiedName());
            if (!originalType.getQualifiedName().equals(type.getQualifiedName())) {
                mapping.getClassNameToFieldMappings().putIfAbsent(originalType.getQualifiedName(), xpath);
                mapping.addConverter(xpath, converter);
            }
            XMLMapping nestedMapping = mapping.getChoiceElementMappings().get(xpath);
            if (nestedMapping instanceof BinaryDataMapping) {
                ((BinaryDataMapping) nestedMapping).getNullPolicy().setNullRepresentedByEmptyNode(false);
            }
            if (type.isEnum()) {
                if (nestedMapping.isAbstractDirectMapping()) {
                    ((DirectMapping) nestedMapping).setConverter(buildJAXBEnumTypeConverter(nestedMapping, (EnumTypeInfo) info));
                }
            }
        }
    }
    return mapping;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) XMLMapping(org.eclipse.persistence.oxm.mappings.XMLMapping) XmlJavaTypeAdapter(org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter) ArrayList(java.util.ArrayList) XmlJoinNode(org.eclipse.persistence.jaxb.xmlmodel.XmlJoinNodes.XmlJoinNode) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) XMLField(org.eclipse.persistence.oxm.XMLField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Field(org.eclipse.persistence.internal.oxm.mappings.Field) JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) CoreConverter(org.eclipse.persistence.core.mappings.converters.CoreConverter) JAXBEnumTypeConverter(org.eclipse.persistence.jaxb.JAXBEnumTypeConverter) JAXBElementConverter(org.eclipse.persistence.internal.jaxb.JAXBElementConverter) XMLListConverter(org.eclipse.persistence.oxm.mappings.converters.XMLListConverter) XMLConverter(org.eclipse.persistence.oxm.mappings.converters.XMLConverter) Converter(org.eclipse.persistence.mappings.converters.Converter) DefaultElementConverter(org.eclipse.persistence.internal.jaxb.DefaultElementConverter) DomHandlerConverter(org.eclipse.persistence.internal.jaxb.DomHandlerConverter) JAXBElementRootConverter(org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter)

Example 2 with XMLChoiceObjectMapping

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

the class DBWSModelProject method buildQueryDescriptor.

// query
// 
// name,parameter*,result,named-query|sql|jpql|stored-procedure|stored-function
protected XMLDescriptor buildQueryDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(QueryOperation.class);
    descriptor.setDefaultRootElement("query");
    XMLDirectMapping name = new XMLDirectMapping();
    name.setAttributeName("name");
    name.setXPath("name/text()");
    descriptor.addMapping(name);
    XMLCompositeCollectionMapping parameters = new XMLCompositeCollectionMapping();
    parameters.setAttributeName("parameters");
    parameters.setReferenceClass(Parameter.class);
    parameters.setXPath("parameter");
    descriptor.addMapping(parameters);
    XMLCompositeObjectMapping resultMapping = new XMLCompositeObjectMapping();
    resultMapping.setAttributeName("result");
    resultMapping.setReferenceClass(Result.class);
    resultMapping.setXPath("result");
    descriptor.addMapping(resultMapping);
    XMLChoiceObjectMapping queryHandlerMapping = new XMLChoiceObjectMapping();
    queryHandlerMapping.setAttributeName("queryHandler");
    queryHandlerMapping.addChoiceElement("jpql", JPQLQueryHandler.class);
    queryHandlerMapping.addChoiceElement("named-query", NamedQueryHandler.class);
    queryHandlerMapping.addChoiceElement("sql", SQLQueryHandler.class);
    queryHandlerMapping.addChoiceElement("stored-procedure", StoredProcedureQueryHandler.class);
    queryHandlerMapping.addChoiceElement("stored-function", StoredFunctionQueryHandler.class);
    descriptor.addMapping(queryHandlerMapping);
    return descriptor;
}
Also used : XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 3 with XMLChoiceObjectMapping

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

the class EmployeeProject method getEmployeeDescriptor.

private XMLDescriptor getEmployeeDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(Employee.class);
    descriptor.setDefaultRootElement("employee");
    XMLDirectMapping nameMapping = new XMLDirectMapping();
    nameMapping.setAttributeName("name");
    nameMapping.setXPath("name/text()");
    descriptor.addMapping(nameMapping);
    XMLChoiceObjectMapping choiceMapping = new XMLChoiceObjectMapping();
    choiceMapping.setAttributeName("choice");
    choiceMapping.addChoiceElement("street/text()", String.class);
    choiceMapping.addChoiceElement("address", Address.class);
    choiceMapping.addChoiceElement("integer/text()", Integer.class);
    choiceMapping.setConverter(new WrapperConverter());
    descriptor.addMapping(choiceMapping);
    XMLDirectMapping phoneMapping = new XMLDirectMapping();
    phoneMapping.setAttributeName("phone");
    phoneMapping.setXPath("phone/text()");
    descriptor.addMapping(phoneMapping);
    return descriptor;
}
Also used : XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping)

Example 4 with XMLChoiceObjectMapping

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

the class XMLChoiceWithReferenceProject method getEmployeeDescriptor.

public XMLDescriptor getEmployeeDescriptor() {
    XMLDescriptor desc = new XMLDescriptor();
    desc.setJavaClass(Employee.class);
    XMLDirectMapping name = new XMLDirectMapping();
    name.setAttributeName("name");
    name.setXPath("name/text()");
    desc.addMapping(name);
    XMLChoiceObjectMapping mapping = new XMLChoiceObjectMapping();
    mapping.setAttributeName("contact");
    List<XMLField> sourceFields = new ArrayList<XMLField>();
    List<XMLField> targetFields = new ArrayList<XMLField>();
    XMLField source = new XMLField("address/address-id/text()");
    sourceFields.add(source);
    source = new XMLField("address/zip/text()");
    sourceFields.add(source);
    XMLField target = new XMLField("@id");
    targetFields.add(target);
    target = new XMLField("zip/text()");
    targetFields.add(target);
    mapping.addChoiceElement(sourceFields, Address.class, targetFields);
    sourceFields = new ArrayList<XMLField>();
    source = new XMLField("phone/@id");
    sourceFields.add(source);
    targetFields = new ArrayList<XMLField>();
    target = new XMLField("@id");
    targetFields.add(target);
    mapping.addChoiceElement(sourceFields, PhoneNumber.class, targetFields);
    desc.addMapping(mapping);
    return desc;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) ArrayList(java.util.ArrayList)

Example 5 with XMLChoiceObjectMapping

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

the class XMLDescriptor method initialize.

/**
 * INTERNAL:
 * Initialize the mappings as a separate step.
 * This is done as a separate step to ensure that inheritance has been first resolved.
 */
@Override
public void initialize(AbstractSession session) throws DescriptorException {
    if (this.hasInheritance()) {
        ((org.eclipse.persistence.internal.oxm.QNameInheritancePolicy) this.getInheritancePolicy()).setNamespaceResolver(this.getNamespaceResolver());
    }
    if (null != this.defaultRootElementField) {
        defaultRootElementField.setNamespaceResolver(this.namespaceResolver);
        defaultRootElementField.initialize();
    }
    if (schemaReference != null && schemaReference.getSchemaContext() != null && (schemaReference.getType() == XMLSchemaReference.COMPLEX_TYPE || schemaReference.getType() == XMLSchemaReference.SIMPLE_TYPE) && getDefaultRootElementType() == null) {
        if (hasInheritance() && isChildDescriptor()) {
            XMLField parentField = ((XMLDescriptor) getInheritancePolicy().getParentDescriptor()).getDefaultRootElementField();
            // if this descriptor has a root element field different than it's parent set the leaf element type of the default root field
            if (parentField == null || (parentField != null && defaultRootElementField != null && !defaultRootElementField.getXPathFragment().equals(parentField.getXPathFragment()))) {
                setDefaultRootElementType(schemaReference.getSchemaContextAsQName(getNamespaceResolver()));
            }
        } else {
            setDefaultRootElementType(schemaReference.getSchemaContextAsQName(getNamespaceResolver()));
        }
    }
    if (null != primaryKeyFields) {
        for (int x = 0, primaryKeyFieldsSize = this.primaryKeyFields.size(); x < primaryKeyFieldsSize; x++) {
            XMLField pkField = (XMLField) this.primaryKeyFields.get(x);
            pkField.setNamespaceResolver(this.namespaceResolver);
            pkField.initialize();
        }
    }
    // If defined as read-only, add to it's project's default read-only classes collection.
    if (shouldBeReadOnly() && (!session.getDefaultReadOnlyClasses().contains(getJavaClass()))) {
        session.getDefaultReadOnlyClasses().add(getJavaClass());
    }
    // Avoid repetitive initialization (this does not solve loops)
    if (isInitialized(INITIALIZED) || isInvalid()) {
        return;
    }
    setInitializationStage(INITIALIZED);
    // make sure that parent mappings are initialized?
    if (isChildDescriptor()) {
        ClassDescriptor parentDescriptor = getInheritancePolicy().getParentDescriptor();
        parentDescriptor.initialize(session);
        if (parentDescriptor.hasEventManager()) {
            getEventManager();
        }
    }
    for (Enumeration<DatabaseMapping> mappingsEnum = getMappings().elements(); mappingsEnum.hasMoreElements(); ) {
        DatabaseMapping mapping = mappingsEnum.nextElement();
        validateMappingType(mapping);
        mapping.initialize(session);
        if (mapping.isObjectReferenceMapping()) {
            this.hasReferenceMappings = true;
        }
        if (mapping instanceof XMLChoiceObjectMapping) {
            XMLChoiceObjectMapping choiceMapping = ((XMLChoiceObjectMapping) mapping);
            for (XMLMapping next : choiceMapping.getChoiceElementMappings().values()) {
                if (((DatabaseMapping) next).isObjectReferenceMapping()) {
                    this.hasReferenceMappings = true;
                }
            }
        }
        if (mapping instanceof XMLChoiceCollectionMapping) {
            XMLChoiceCollectionMapping choiceMapping = ((XMLChoiceCollectionMapping) mapping);
            for (XMLMapping next : choiceMapping.getChoiceElementMappings().values()) {
                if (((DatabaseMapping) next).isObjectReferenceMapping()) {
                    this.hasReferenceMappings = true;
                }
            }
        }
        // Add all the fields in the mapping to myself.
        Helper.addAllUniqueToVector(getFields(), mapping.getFields());
    }
    // If this has inheritance then it needs to be initialized before all fields is set.
    if (hasInheritance()) {
        getInheritancePolicy().initialize(session);
    }
    // Initialize the allFields to its fields, this can be done now because the fields have been computed.
    setAllFields((Vector) getFields().clone());
    getObjectBuilder().initialize(session);
    if (hasInterfacePolicy()) {
        interfaceInitialization(session);
    }
    if (hasReturningPolicy()) {
        getReturningPolicy().initialize(session);
    }
    if (eventManager != null) {
        eventManager.initialize(session);
    }
    if (copyPolicy != null) {
        copyPolicy.initialize(session);
    }
    getInstantiationPolicy().initialize(session);
    if (getSchemaReference() != null) {
        getSchemaReference().initialize(session);
    }
    // If a Location Accessor is set on a superclass, inherit it
    if (getInheritancePolicyOrNull() != null && getInheritancePolicy().getParentDescriptor() != null) {
        XMLDescriptor d = (XMLDescriptor) getInheritancePolicy().getParentDescriptor();
        locationAccessor = d.getLocationAccessor();
    }
    if (locationAccessor != null) {
        locationAccessor.initializeAttributes(getJavaClass());
    }
}
Also used : XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLMapping(org.eclipse.persistence.oxm.mappings.XMLMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping)

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