Search in sources :

Example 46 with ContainerPolicy

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

the class XMLBinaryDataCollectionMapping method valueFromRow.

@Override
public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery query, CacheKey cacheKey, AbstractSession executionSession, boolean isTargetProtected, Boolean[] wasCacheUsed) {
    ContainerPolicy cp = this.getContainerPolicy();
    Object fieldValue = row.getValues(this.getField());
    if (fieldValue == null) {
        if (reuseContainer) {
            Object currentObject = ((XMLRecord) row).getCurrentObject();
            Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
            return container != null ? container : cp.containerInstance();
        } else {
            return cp.containerInstance();
        }
    }
    Vector fieldValues = this.getDescriptor().buildDirectValuesFromFieldValue(fieldValue);
    if (fieldValues == null) {
        if (reuseContainer) {
            Object currentObject = ((XMLRecord) row).getCurrentObject();
            Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
            return container != null ? container : cp.containerInstance();
        } else {
            return cp.containerInstance();
        }
    }
    Object result = null;
    if (reuseContainer) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        result = container != null ? container : cp.containerInstance();
    } else {
        result = cp.containerInstance(fieldValues.size());
    }
    for (Enumeration stream = fieldValues.elements(); stream.hasMoreElements(); ) {
        Object element = stream.nextElement();
        // PERF: Direct variable access.
        // Object value = row.get(field);
        // Object fieldValue = null;
        XMLUnmarshaller unmarshaller = ((XMLRecord) row).getUnmarshaller();
        if (element instanceof String) {
            if (this.isSwaRef() && (unmarshaller.getAttachmentUnmarshaller() != null)) {
                fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler((String) element);
            } else if (!this.isSwaRef()) {
                // should be base64
                byte[] bytes = ((XMLConversionManager) executionSession.getDatasourcePlatform().getConversionManager()).convertSchemaBase64ToByteArray(element);
                fieldValue = bytes;
            }
        } else {
            // this was an element, so do the XOP/SWAREF/Inline binary cases for an element
            XMLRecord record = (XMLRecord) element;
            if (getNullPolicy().valueIsNull((Element) record.getDOM())) {
                fieldValue = null;
            } else {
                record.setSession(executionSession);
                if ((unmarshaller.getAttachmentUnmarshaller() != null) && unmarshaller.getAttachmentUnmarshaller().isXOPPackage() && !this.isSwaRef() && !this.shouldInlineBinaryData()) {
                    // look for the include element:
                    String xpath = XMLConstants.EMPTY_STRING;
                    // need a prefix for XOP
                    String prefix = null;
                    NamespaceResolver descriptorResolver = ((XMLDescriptor) getDescriptor()).getNamespaceResolver();
                    if (descriptorResolver != null) {
                        prefix = descriptorResolver.resolveNamespaceURI(XMLConstants.XOP_URL);
                    }
                    if (prefix == null) {
                        prefix = XMLConstants.XOP_PREFIX;
                    }
                    NamespaceResolver tempResolver = new NamespaceResolver();
                    tempResolver.put(prefix, XMLConstants.XOP_URL);
                    xpath = prefix + XMLConstants.COLON + INCLUDE + "/@href";
                    XMLField field = new XMLField(xpath);
                    field.setNamespaceResolver(tempResolver);
                    String includeValue = (String) record.get(field);
                    if (element != null && includeValue != null) {
                        if ((getAttributeElementClass() == ClassConstants.ABYTE) || (getAttributeElementClass() == ClassConstants.APBYTE)) {
                            fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsByteArray(includeValue);
                        } else {
                            fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler(includeValue);
                        }
                    } else {
                        // If we didn't find the Include element, check for inline
                        fieldValue = record.get(XMLConstants.TEXT);
                        // should be a base64 string
                        fieldValue = ((XMLConversionManager) executionSession.getDatasourcePlatform().getConversionManager()).convertSchemaBase64ToByteArray(fieldValue);
                    }
                } else if ((unmarshaller.getAttachmentUnmarshaller() != null) && isSwaRef()) {
                    String refValue = (String) record.get(XMLConstants.TEXT);
                    if (refValue != null) {
                        fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler(refValue);
                    }
                } else {
                    fieldValue = record.get(XMLConstants.TEXT);
                    // should be a base64 string
                    fieldValue = ((XMLConversionManager) executionSession.getDatasourcePlatform().getConversionManager()).convertSchemaBase64ToByteArray(fieldValue);
                }
            }
        }
        Object attributeValue = convertDataValueToObjectValue(fieldValue, executionSession, unmarshaller);
        cp.addInto(attributeValue, result, query.getSession());
    }
    return result;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Enumeration(java.util.Enumeration) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) Vector(java.util.Vector) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 47 with ContainerPolicy

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

the class XMLCompositeDirectCollectionMapping method valueFromRow.

/**
 * INTERNAL:
 * Build the nested collection from the database row.
 */
@Override
public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, CacheKey cacheKey, AbstractSession executionSession, boolean isTargetProtected, Boolean[] wasCacheUsed) throws DatabaseException {
    ContainerPolicy cp = this.getContainerPolicy();
    DOMRecord domRecord = (DOMRecord) row;
    Object fieldValue = domRecord.getValues(this.getField(), this.getNullPolicy());
    if (fieldValue == null) {
        if (reuseContainer) {
            Object currentObject = ((XMLRecord) row).getCurrentObject();
            Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
            return container != null ? container : cp.containerInstance();
        } else {
            return cp.containerInstance();
        }
    }
    Vector fieldValues = this.getDescriptor().buildDirectValuesFromFieldValue(fieldValue);
    if (fieldValues == null) {
        if (reuseContainer) {
            Object currentObject = ((XMLRecord) row).getCurrentObject();
            Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
            return container != null ? container : cp.containerInstance();
        } else {
            return cp.containerInstance();
        }
    }
    Object result = null;
    if (reuseContainer) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        result = container != null ? container : cp.containerInstance();
    } else {
        result = cp.containerInstance(fieldValues.size());
    }
    for (Enumeration stream = fieldValues.elements(); stream.hasMoreElements(); ) {
        Object element = stream.nextElement();
        element = convertDataValueToObjectValue(element, executionSession, ((XMLRecord) row).getUnmarshaller());
        if (element != null && element.getClass() == ClassConstants.STRING) {
            if (isCollapsingStringValues) {
                element = XMLConversionManager.getDefaultXMLManager().collapseStringValue((String) element);
            } else if (isNormalizingStringValues) {
                element = XMLConversionManager.getDefaultXMLManager().normalizeStringValue((String) element);
            }
        }
        cp.addInto(element, result, sourceQuery.getSession());
    }
    return result;
}
Also used : CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) Enumeration(java.util.Enumeration) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) Vector(java.util.Vector)

Example 48 with ContainerPolicy

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

the class XMLCompositeDirectCollectionMapping method writeFromObjectIntoRow.

/**
 * INTERNAL:
 */
@Override
public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session, WriteType writeType) {
    if (this.isReadOnly()) {
        return;
    }
    Object attributeValue = this.getAttributeValueFromObject(object);
    if (attributeValue == null) {
        row.put(this.getField(), null);
        return;
    }
    ContainerPolicy cp = this.getContainerPolicy();
    Vector elements = new Vector(cp.sizeFor(attributeValue));
    Object iter = cp.iteratorFor(attributeValue);
    if (null != iter) {
        while (cp.hasNext(iter)) {
            Object element = cp.next(iter, session);
            element = convertObjectValueToDataValue(element, session, ((XMLRecord) row).getMarshaller());
            if (element != null) {
                elements.addElement(element);
            } else if (!usesSingleNode()) {
                AbstractNullPolicy nullPolicy = getNullPolicy();
                if (nullPolicy == null) {
                    elements.addElement(null);
                } else {
                    if (nullPolicy.getMarshalNullRepresentation() == XMLNullRepresentationType.XSI_NIL) {
                        elements.addElement(XMLRecord.NIL);
                    } else if (nullPolicy.getMarshalNullRepresentation() == XMLNullRepresentationType.ABSENT_NODE) {
                    // Do nothing
                    } else {
                        elements.addElement(XMLConstants.EMPTY_STRING);
                    }
                }
            }
        }
    }
    Object fieldValue = null;
    if (!elements.isEmpty()) {
        fieldValue = this.getDescriptor().buildFieldValueFromDirectValues(elements, elementDataTypeName, session);
    }
    row.put(this.getField(), fieldValue);
}
Also used : AbstractNullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy) CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) Vector(java.util.Vector) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord)

Example 49 with ContainerPolicy

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

the class XMLCompositeDirectCollectionMapping method initialize.

/**
 * INTERNAL:
 * Initialize the mapping.
 */
@Override
public void initialize(AbstractSession session) throws DescriptorException {
    super.initialize(session);
    if (this.getField() instanceof XMLField) {
        if (valueConverter instanceof TypeConversionConverter) {
            TypeConversionConverter converter = (TypeConversionConverter) valueConverter;
            this.getField().setType(converter.getObjectClass());
        }
        String xpathString = ((XMLField) getField()).getXPath();
        if (this.isAbstractCompositeDirectCollectionMapping() && (xpathString.indexOf(XMLConstants.ATTRIBUTE) == -1) && (!xpathString.endsWith(XMLConstants.TEXT))) {
            throw DescriptorException.invalidXpathForXMLDirectMapping(this);
        }
    }
    ContainerPolicy cp = getContainerPolicy();
    if (cp != null) {
        if (cp.getContainerClass() == null) {
            Class<Object> cls = session.getDatasourcePlatform().getConversionManager().convertClassNameToClass(cp.getContainerClassName());
            cp.setContainerClass(cls);
        }
    }
    ((XMLField) this.getField()).setIsCDATA(this.isCDATA());
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) TypeConversionConverter(org.eclipse.persistence.mappings.converters.TypeConversionConverter) CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy)

Example 50 with ContainerPolicy

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

the class XMLFragmentCollectionMapping method writeFromObjectIntoRow.

/**
 * INTERNAL:
 */
@Override
public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session, WriteType writeType) {
    if (this.isReadOnly()) {
        return;
    }
    Object attributeValue = this.getAttributeValueFromObject(object);
    if (attributeValue == null) {
        row.put(this.getField(), null);
        return;
    }
    ContainerPolicy cp = this.getContainerPolicy();
    Vector elements = new Vector(cp.sizeFor(attributeValue));
    for (Object iter = cp.iteratorFor(attributeValue); cp.hasNext(iter); ) {
        Object element = cp.next(iter, session);
        if (element != null) {
            elements.addElement(element);
        }
    }
    Object fieldValue = null;
    if (!elements.isEmpty()) {
        fieldValue = this.getDescriptor().buildFieldValueFromDirectValues(elements, elementDataTypeName, session);
    }
    row.put(this.getField(), fieldValue);
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) Vector(java.util.Vector)

Aggregations

ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)119 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)28 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)17 XMLRecord (org.eclipse.persistence.oxm.record.XMLRecord)17 List (java.util.List)14 Vector (java.util.Vector)14 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)14 XMLField (org.eclipse.persistence.oxm.XMLField)14 ArrayList (java.util.ArrayList)13 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)13 Collection (java.util.Collection)12 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)10 CollectionContainerPolicy (org.eclipse.persistence.internal.queries.CollectionContainerPolicy)9 InstanceVariableAttributeAccessor (org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor)8 MethodAttributeAccessor (org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)8 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)8 HashMap (java.util.HashMap)7 VirtualAttributeAccessor (org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor)7 CustomAccessorAttributeAccessor (org.eclipse.persistence.internal.jaxb.CustomAccessorAttributeAccessor)7 JAXBSetMethodAttributeAccessor (org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor)7