Search in sources :

Example 1 with CoreContainerPolicy

use of org.eclipse.persistence.internal.core.queries.CoreContainerPolicy in project eclipselink by eclipse-ee4j.

the class JAXBValueStore method copy.

@Override
public ValueStore copy() {
    AbstractSession session = ((JAXBContext) jaxbHelperContext.getJAXBContext()).getXMLContext().getSession(entity);
    Object originalEntity = entity;
    entity = descriptor.getInstantiationPolicy().buildNewInstance();
    for (SDOProperty sdoProperty : (List<SDOProperty>) dataObject.getType().getProperties()) {
        if (!sdoProperty.getType().isChangeSummaryType()) {
            Mapping mapping = getJAXBMappingForProperty(sdoProperty);
            CoreAttributeAccessor attributeAccessor = mapping.getAttributeAccessor();
            Object attributeValue = attributeAccessor.getAttributeValueFromObject(originalEntity);
            if (mapping.isCollectionMapping()) {
                Object containerCopy = null;
                SDOChangeSummary sdoChangeSummary = dataObject.getChangeSummary();
                if (null != sdoChangeSummary) {
                    List list = listWrappers.get(sdoProperty);
                    containerCopy = sdoChangeSummary.getOriginalElements().get(list);
                }
                if (null == containerCopy) {
                    CoreContainerPolicy containerPolicy = mapping.getContainerPolicy();
                    containerCopy = containerPolicy.containerInstance();
                    if (null != attributeValue) {
                        Object attributeValueIterator = containerPolicy.iteratorFor(attributeValue);
                        while (containerPolicy.hasNext(attributeValueIterator)) {
                            containerPolicy.addInto(containerPolicy.nextEntry(attributeValueIterator), containerCopy, session);
                        }
                    }
                }
                attributeAccessor.setAttributeValueInObject(entity, containerCopy);
            } else {
                attributeAccessor.setAttributeValueInObject(entity, attributeValue);
            }
        }
    }
    return new JAXBValueStore(jaxbHelperContext, originalEntity, descriptor, dataObject, listWrappers);
}
Also used : CoreAttributeAccessor(org.eclipse.persistence.core.mappings.CoreAttributeAccessor) SDOChangeSummary(org.eclipse.persistence.sdo.SDOChangeSummary) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) DataObject(commonj.sdo.DataObject) List(java.util.List) 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) SDOProperty(org.eclipse.persistence.sdo.SDOProperty) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) CoreContainerPolicy(org.eclipse.persistence.internal.core.queries.CoreContainerPolicy)

Example 2 with CoreContainerPolicy

use of org.eclipse.persistence.internal.core.queries.CoreContainerPolicy in project eclipselink by eclipse-ee4j.

the class XMLVariableXPathCollectionMappingNodeValue method marshal.

@Override
public boolean marshal(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, CoreAbstractSession session, NamespaceResolver namespaceResolver) {
    if (mapping.isReadOnly()) {
        return false;
    }
    CoreContainerPolicy cp = mapping.getContainerPolicy();
    Object collection = mapping.getAttributeAccessor().getAttributeValueFromObject(object);
    if (null == collection) {
        AbstractNullPolicy wrapperNP = mapping.getWrapperNullPolicy();
        if (wrapperNP != null && wrapperNP.getMarshalNullRepresentation().equals(XMLNullRepresentationType.XSI_NIL)) {
            marshalRecord.nilSimple(namespaceResolver);
            return true;
        } else {
            return false;
        }
    }
    Object iterator = cp.iteratorFor(collection);
    if (null != iterator && cp.hasNext(iterator)) {
        XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
        marshalRecord.closeStartGroupingElements(groupingFragment);
    } else {
        return marshalRecord.emptyCollection(xPathFragment, namespaceResolver, mapping.getWrapperNullPolicy() != null);
    }
    if (marshalRecord.getMarshaller().isApplicationJSON()) {
        List<XPathFragment> frags = new ArrayList();
        List<List> values = new ArrayList<>();
        // sort the elements. Results will be a list of xpathfragments and a corresponding list of
        // collections associated with those xpathfragments
        XPathFragment xmlRootFragment;
        while (cp.hasNext(iterator)) {
            Object nextValue = cp.next(iterator, session);
            nextValue = mapping.convertObjectValueToDataValue(nextValue, session, marshalRecord.getMarshaller());
            XPathFragment frag = mapping.getXPathFragmentForValue(nextValue, marshalRecord.getNamespaceResolver(), marshalRecord.isNamespaceAware(), marshalRecord.getNamespaceSeparator());
            if (frag != null) {
                int index = frags.indexOf(frag);
                if (index > -1) {
                    values.get(index).add(nextValue);
                } else {
                    frags.add(frag);
                    List valuesList = new ArrayList();
                    valuesList.add(nextValue);
                    values.add(valuesList);
                }
            }
        }
        for (int i = 0; i < frags.size(); i++) {
            XPathFragment nextFragment = frags.get(i);
            List listValue = values.get(i);
            if (nextFragment != null) {
                int valueSize = listValue.size();
                if (valueSize > 1) {
                    marshalRecord.startCollection();
                }
                for (int j = 0; j < valueSize; j++) {
                    marshalSingleValue(nextFragment, marshalRecord, object, listValue.get(j), session, namespaceResolver, ObjectMarshalContext.getInstance());
                }
                if (valueSize > 1) {
                    marshalRecord.endCollection();
                }
            }
        }
    } else {
        Object objectValue;
        while (cp.hasNext(iterator)) {
            objectValue = cp.next(iterator, session);
            objectValue = mapping.convertObjectValueToDataValue(objectValue, session, marshalRecord.getMarshaller());
            marshalSingleValue(xPathFragment, marshalRecord, object, objectValue, session, namespaceResolver, ObjectMarshalContext.getInstance());
        }
    }
    return true;
}
Also used : AbstractNullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) CoreContainerPolicy(org.eclipse.persistence.internal.core.queries.CoreContainerPolicy)

Example 3 with CoreContainerPolicy

use of org.eclipse.persistence.internal.core.queries.CoreContainerPolicy in project eclipselink by eclipse-ee4j.

the class XMLCompositeDirectCollectionMappingNodeValue method marshal.

/**
 * Override the method in XPathNode such that the marshaller can be set on the
 * marshalRecord - this is required for XMLConverter usage.
 */
@Override
public boolean marshal(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, CoreAbstractSession session, NamespaceResolver namespaceResolver) {
    if (xmlCompositeDirectCollectionMapping.isReadOnly()) {
        return false;
    }
    CoreContainerPolicy cp = getContainerPolicy();
    Object collection = xmlCompositeDirectCollectionMapping.getAttributeAccessor().getAttributeValueFromObject(object);
    if (null == collection) {
        AbstractNullPolicy wrapperNP = xmlCompositeDirectCollectionMapping.getWrapperNullPolicy();
        if (wrapperNP != null && wrapperNP.getMarshalNullRepresentation() == XMLNullRepresentationType.XSI_NIL) {
            marshalRecord.nilSimple(namespaceResolver);
            return true;
        } else {
            return false;
        }
    }
    Object iterator = cp.iteratorFor(collection);
    Field xmlField = (Field) xmlCompositeDirectCollectionMapping.getField();
    if (null != iterator && cp.hasNext(iterator)) {
        XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
        marshalRecord.closeStartGroupingElements(groupingFragment);
    } else {
        if (xmlField.usesSingleNode() && !xmlCompositeDirectCollectionMapping.isDefaultEmptyContainer()) {
            XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
            marshalRecord.closeStartGroupingElements(groupingFragment);
        } else {
            return marshalRecord.emptyCollection(xPathFragment, namespaceResolver, xmlCompositeDirectCollectionMapping.getWrapperNullPolicy() != null);
        }
    }
    Object objectValue;
    if (xmlField.usesSingleNode()) {
        StringBuilder stringValueStringBuilder = new StringBuilder();
        String newValue;
        QName schemaType = null;
        if (xPathFragment != null && !xPathFragment.isAttribute() && !xPathFragment.nameIsText) {
            marshalRecord.openStartElement(xPathFragment, namespaceResolver);
        }
        while (cp.hasNext(iterator)) {
            objectValue = cp.next(iterator, session);
            objectValue = xmlCompositeDirectCollectionMapping.convertObjectValueToDataValue(objectValue, session, marshalRecord.getMarshaller());
            schemaType = xmlField.getSchemaTypeForValue(objectValue, session);
            newValue = marshalRecord.getValueToWrite(schemaType, objectValue, (ConversionManager) session.getDatasourcePlatform().getConversionManager());
            if (null != newValue) {
                stringValueStringBuilder.append(newValue);
                if (cp.hasNext(iterator)) {
                    stringValueStringBuilder.append(SPACE);
                }
            }
        }
        XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
        if (xPathFragment != null && xPathFragment.isAttribute()) {
            marshalRecord.attribute(xPathFragment, namespaceResolver, stringValueStringBuilder.toString());
            marshalRecord.closeStartGroupingElements(groupingFragment);
        } else {
            marshalRecord.closeStartGroupingElements(groupingFragment);
            if (xmlCompositeDirectCollectionMapping.isCDATA()) {
                marshalRecord.cdata(stringValueStringBuilder.toString());
            } else {
                marshalRecord.characters(stringValueStringBuilder.toString());
                if (xPathFragment != null && !xPathFragment.isAttribute() && !xPathFragment.nameIsText) {
                    marshalRecord.endElement(xPathFragment, namespaceResolver);
                }
            }
        }
    } else {
        int valueSize = cp.sizeFor(collection);
        if (marshalRecord.getMarshaller().isApplicationJSON() && (valueSize > 1 || !marshalRecord.getMarshaller().isReduceAnyArrays())) {
            marshalRecord.startCollection();
        }
        while (cp.hasNext(iterator)) {
            objectValue = cp.next(iterator, session);
            marshalSingleValue(xPathFragment, marshalRecord, object, objectValue, session, namespaceResolver, ObjectMarshalContext.getInstance());
        }
        if (marshalRecord.getMarshaller().isApplicationJSON() && (valueSize > 1 || !marshalRecord.getMarshaller().isReduceAnyArrays())) {
            marshalRecord.endCollection();
        }
    }
    return true;
}
Also used : AbstractNullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy) Field(org.eclipse.persistence.internal.oxm.mappings.Field) QName(javax.xml.namespace.QName) CoreContainerPolicy(org.eclipse.persistence.internal.core.queries.CoreContainerPolicy)

Example 4 with CoreContainerPolicy

use of org.eclipse.persistence.internal.core.queries.CoreContainerPolicy in project eclipselink by eclipse-ee4j.

the class XMLAnyAttributeMappingNodeValue method marshal.

@Override
public boolean marshal(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, CoreAbstractSession session, NamespaceResolver namespaceResolver) {
    if (xmlAnyAttributeMapping.isReadOnly()) {
        return false;
    }
    Object collection = xmlAnyAttributeMapping.getAttributeValueFromObject(object);
    if (collection == null) {
        return false;
    }
    CoreContainerPolicy cp = getContainerPolicy();
    Object iter = cp.iteratorFor(collection);
    if (!cp.hasNext(iter)) {
        return false;
    }
    XPathFragment groupingElements = marshalRecord.openStartGroupingElements(namespaceResolver);
    List extraNamespaces = new ArrayList();
    NamespaceResolver nr = marshalRecord.getNamespaceResolver();
    while (cp.hasNext(iter)) {
        Map.Entry entry = (Map.Entry) cp.nextEntry(iter, session);
        Object key = entry.getKey();
        if (key instanceof QName) {
            QName name = (QName) key;
            String value = entry.getValue().toString();
            String qualifiedName = name.getLocalPart();
            if (nr != null) {
                String prefix = nr.resolveNamespaceURI(name.getNamespaceURI());
                if ((prefix != null) && prefix.length() > 0) {
                    qualifiedName = prefix + Constants.COLON + qualifiedName;
                } else if (name.getNamespaceURI() != null && name.getNamespaceURI().length() > 0) {
                    String generatedPrefix = nr.generatePrefix();
                    if (marshalRecord.hasCustomNamespaceMapper()) {
                        String customPrefix = marshalRecord.getMarshaller().getNamespacePrefixMapper().getPreferredPrefix(name.getNamespaceURI(), generatedPrefix, true);
                        if (customPrefix != null && customPrefix.length() > 0) {
                            generatedPrefix = customPrefix;
                        }
                    }
                    qualifiedName = generatedPrefix + Constants.COLON + qualifiedName;
                    nr.put(generatedPrefix, name.getNamespaceURI());
                    extraNamespaces.add(generatedPrefix);
                    marshalRecord.namespaceDeclaration(generatedPrefix, name.getNamespaceURI());
                }
            }
            if (javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(name.getNamespaceURI())) {
                marshalRecord.namespaceDeclaration(name.getLocalPart(), name.getNamespaceURI());
            } else {
                marshalRecord.attribute(name.getNamespaceURI(), name.getLocalPart(), qualifiedName, value);
            }
        }
    }
    for (int i = 0; i < extraNamespaces.size(); i++) {
        marshalRecord.getNamespaceResolver().removeNamespace((String) extraNamespaces.get(i));
    }
    marshalRecord.closeStartGroupingElements(groupingElements);
    return true;
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) CoreContainerPolicy(org.eclipse.persistence.internal.core.queries.CoreContainerPolicy)

Example 5 with CoreContainerPolicy

use of org.eclipse.persistence.internal.core.queries.CoreContainerPolicy in project eclipselink by eclipse-ee4j.

the class XMLAnyCollectionMappingNodeValue method marshal.

@Override
public boolean marshal(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, CoreAbstractSession session, NamespaceResolver namespaceResolver) {
    if (xmlAnyCollectionMapping.isReadOnly()) {
        return false;
    }
    CoreContainerPolicy cp = xmlAnyCollectionMapping.getContainerPolicy();
    Object collection = xmlAnyCollectionMapping.getAttributeAccessor().getAttributeValueFromObject(object);
    if (null == collection) {
        AbstractNullPolicy wrapperNP = xmlAnyCollectionMapping.getWrapperNullPolicy();
        if (wrapperNP != null && wrapperNP.getMarshalNullRepresentation().equals(XMLNullRepresentationType.XSI_NIL)) {
            marshalRecord.nilSimple(namespaceResolver);
            return true;
        } else {
            return false;
        }
    }
    Object iterator = cp.iteratorFor(collection);
    if (null != iterator && cp.hasNext(iterator)) {
        XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
        marshalRecord.closeStartGroupingElements(groupingFragment);
    } else {
        return marshalRecord.emptyCollection(xPathFragment, namespaceResolver, xmlAnyCollectionMapping.getWrapperNullPolicy() != null);
    }
    if (marshalRecord.getMarshaller().isApplicationJSON()) {
        List<XPathFragment> frags = new ArrayList();
        List<List> values = new ArrayList<>();
        List mixedValues = new ArrayList();
        // sort the elements. Results will be a list of xpathfragments and a corresponding list of
        // collections associated with those xpathfragments
        XPathFragment xmlRootFragment;
        while (cp.hasNext(iterator)) {
            Object nextValue = cp.next(iterator, session);
            nextValue = xmlAnyCollectionMapping.convertObjectValueToDataValue(nextValue, session, marshalRecord.getMarshaller());
            XPathFragment frag = getXPathFragmentForValue(nextValue, marshalRecord, marshalRecord.getMarshaller());
            if (frag != null) {
                if (frag == SIMPLE_FRAGMENT) {
                    mixedValues.add(nextValue);
                } else {
                    int index = frags.indexOf(frag);
                    if (index > -1) {
                        values.get(index).add(nextValue);
                    } else {
                        frags.add(frag);
                        List valuesList = new ArrayList();
                        valuesList.add(nextValue);
                        values.add(valuesList);
                    }
                }
            }
        }
        if (mixedValues.size() > 0) {
            frags.add(SIMPLE_FRAGMENT);
            values.add(mixedValues);
        }
        for (int i = 0; i < frags.size(); i++) {
            XPathFragment nextFragment = frags.get(i);
            List listValue = values.get(i);
            if (nextFragment != null) {
                int valueSize = listValue.size();
                if (valueSize > 1 || !marshalRecord.getMarshaller().isReduceAnyArrays()) {
                    marshalRecord.startCollection();
                }
                for (int j = 0; j < valueSize; j++) {
                    marshalSingleValue(nextFragment, marshalRecord, object, listValue.get(j), session, namespaceResolver, ObjectMarshalContext.getInstance());
                }
                if (valueSize > 1 || !marshalRecord.getMarshaller().isReduceAnyArrays()) {
                    marshalRecord.endCollection();
                }
            }
        }
        return true;
    } else {
        Object objectValue;
        marshalRecord.startCollection();
        while (cp.hasNext(iterator)) {
            objectValue = cp.next(iterator, session);
            objectValue = xmlAnyCollectionMapping.convertObjectValueToDataValue(objectValue, session, marshalRecord.getMarshaller());
            marshalSingleValue(xPathFragment, marshalRecord, object, objectValue, session, namespaceResolver, ObjectMarshalContext.getInstance());
        }
        marshalRecord.endCollection();
        return true;
    }
}
Also used : AbstractNullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CoreContainerPolicy(org.eclipse.persistence.internal.core.queries.CoreContainerPolicy)

Aggregations

CoreContainerPolicy (org.eclipse.persistence.internal.core.queries.CoreContainerPolicy)16 AbstractNullPolicy (org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy)7 List (java.util.List)5 ArrayList (java.util.ArrayList)4 QName (javax.xml.namespace.QName)4 Field (org.eclipse.persistence.internal.oxm.mappings.Field)4 Map (java.util.Map)2 CoreAttributeAccessor (org.eclipse.persistence.core.mappings.CoreAttributeAccessor)2 DataObject (commonj.sdo.DataObject)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Callable (java.util.concurrent.Callable)1 ConversionException (org.eclipse.persistence.exceptions.ConversionException)1 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)1 CacheId (org.eclipse.persistence.internal.identitymaps.CacheId)1 CollectionReferenceMapping (org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping)1 InverseReferenceMapping (org.eclipse.persistence.internal.oxm.mappings.InverseReferenceMapping)1 Mapping (org.eclipse.persistence.internal.oxm.mappings.Mapping)1 ObjectReferenceMapping (org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping)1 XMLReader (org.eclipse.persistence.internal.oxm.record.XMLReader)1