Search in sources :

Example 26 with XMLRecord

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

the class XMLChoiceCollectionMapping method writeFromObjectIntoRow.

@Override
public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session, WriteType writeType) throws DescriptorException {
    if (this.isReadOnly()) {
        return;
    }
    Object attributeValue = getAttributeValueFromObject(object);
    List<XMLEntry> nestedRows = new ArrayList<>();
    XMLRecord record = (XMLRecord) row;
    // First determine which Field is associated with each value:
    if (null != attributeValue) {
        ContainerPolicy cp = getContainerPolicy();
        Object iterator = cp.iteratorFor(attributeValue);
        if (null != iterator) {
            while (cp.hasNext(iterator)) {
                Object value = cp.next(iterator, session);
                value = convertObjectValueToDataValue(value, session, record.getMarshaller());
                NodeValue associatedNodeValue = null;
                XMLField associatedField = null;
                Object fieldValue = value;
                if (value instanceof XMLRoot) {
                    XMLRoot rootValue = (XMLRoot) value;
                    String localName = rootValue.getLocalName();
                    String namespaceUri = rootValue.getNamespaceURI();
                    fieldValue = rootValue.getObject();
                    associatedField = getFieldForName(localName, namespaceUri);
                    if (associatedField == null) {
                        associatedField = getClassToFieldMappings().get(fieldValue.getClass());
                    }
                } else {
                    associatedField = getClassToFieldMappings().get(value.getClass());
                }
                if (associatedField == null) {
                    // this may be a reference mapping
                    List<XMLField> sourceFields = classToSourceFieldsMappings.get(value.getClass());
                    if (sourceFields != null && sourceFields.size() > 0) {
                        DatabaseMapping xmlMapping = (DatabaseMapping) this.choiceElementMappings.get(sourceFields.get(0));
                        for (XMLField next : sourceFields) {
                            fieldValue = ((XMLCollectionReferenceMapping) xmlMapping).buildFieldValue(value, next, session);
                            XMLEntry entry = new XMLEntry();
                            entry.setValue(fieldValue);
                            entry.setXMLField(next);
                            nestedRows.add(entry);
                        }
                    }
                } else {
                    DatabaseMapping xmlMapping = (DatabaseMapping) this.choiceElementMappings.get(associatedField);
                    if (xmlMapping.isAbstractCompositeCollectionMapping()) {
                        fieldValue = ((XMLCompositeCollectionMapping) xmlMapping).buildCompositeRow(fieldValue, session, row, writeType);
                    }
                    XMLEntry entry = new XMLEntry();
                    entry.setValue(fieldValue);
                    entry.setXMLField(associatedField);
                    nestedRows.add(entry);
                }
            }
        }
    }
    row.put(getFields(), nestedRows);
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) NodeValue(org.eclipse.persistence.internal.oxm.NodeValue) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) ArrayList(java.util.ArrayList) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLEntry(org.eclipse.persistence.oxm.record.XMLEntry) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord)

Example 27 with XMLRecord

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

the class XMLChoiceCollectionMapping method valueFromRow.

@Override
public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, CacheKey cacheKey, AbstractSession executionSession, boolean isTargetProtected, Boolean[] wasCacheUsed) throws DatabaseException {
    List<XMLEntry> values = ((DOMRecord) row).getValuesIndicatingNoEntry(this.getFields());
    Object container = getContainerPolicy().containerInstance(values.size());
    for (XMLEntry next : values) {
        Field valueField = next.getXMLField();
        DatabaseMapping nextMapping = (DatabaseMapping) this.choiceElementMappings.get(valueField);
        if (nextMapping.isAbstractCompositeCollectionMapping()) {
            XMLCompositeCollectionMapping xmlMapping = (XMLCompositeCollectionMapping) nextMapping;
            Object value = xmlMapping.buildObjectFromNestedRow((AbstractRecord) next.getValue(), joinManager, sourceQuery, executionSession, isTargetProtected);
            value = convertDataValueToObjectValue(value, executionSession, ((XMLRecord) row).getUnmarshaller());
            getContainerPolicy().addInto(value, container, executionSession);
        } else if (nextMapping instanceof XMLCompositeDirectCollectionMapping) {
            XMLCompositeDirectCollectionMapping xmlMapping = (XMLCompositeDirectCollectionMapping) nextMapping;
            Object value = next.getValue();
            value = convertDataValueToObjectValue(value, executionSession, ((XMLRecord) row).getUnmarshaller());
            getContainerPolicy().addInto(value, container, executionSession);
        }
    }
    ArrayList<XMLMapping> processedMappings = new ArrayList<>();
    for (XMLMapping mapping : choiceElementMappings.values()) {
        if (((DatabaseMapping) mapping).isObjectReferenceMapping() && ((DatabaseMapping) mapping).isCollectionMapping() && !(processedMappings.contains(mapping))) {
            ((XMLCollectionReferenceMapping) mapping).readFromRowIntoObject(row, joinManager, ((XMLRecord) row).getCurrentObject(), cacheKey, sourceQuery, executionSession, isTargetProtected, container);
            processedMappings.add(mapping);
        }
    }
    return container;
}
Also used : DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) ArrayList(java.util.ArrayList) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLEntry(org.eclipse.persistence.oxm.record.XMLEntry) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) Field(org.eclipse.persistence.internal.oxm.mappings.Field) XMLField(org.eclipse.persistence.oxm.XMLField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField)

Example 28 with XMLRecord

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

the class XMLVariableXPathCollectionMapping method writeFromObjectIntoRow.

@Override
public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session, WriteType writeType) throws DescriptorException {
    if (this.isReadOnly()) {
        return;
    }
    Object attributeValue = this.getAttributeValueFromObject(object);
    ContainerPolicy cp = this.getContainerPolicy();
    Object iter = cp.iteratorFor(attributeValue);
    if (null != iter) {
        while (cp.hasNext(iter)) {
            Object element = cp.next(iter, session);
            // convert the value - if necessary
            element = convertObjectValueToDataValue(element, session, ((XMLRecord) row).getMarshaller());
            if (element != null) {
                XMLField variableField = new XMLField();
                XMLRecord xmlRow = (XMLRecord) row;
                // variableField.setXPathFragment(getXPathFragmentForValue(element,(XMLRecord)row));
                variableField.setXPathFragment(getXPathFragmentForValue(element, xmlRow.getNamespaceResolver(), xmlRow.isNamespaceAware(), xmlRow.getNamespaceSeparator()));
                row.put(variableField, buildCompositeRow(variableField, element, session, row, writeType));
            }
        }
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) MapContainerPolicy(org.eclipse.persistence.internal.queries.MapContainerPolicy) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord)

Example 29 with XMLRecord

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

the class EISOneToManyMapping method valueFromRow.

/**
 * INTERNAL:
 * Return the value of the reference attribute or a value holder.
 * Check whether the mapping's attribute should be optimized through batch and joining.
 */
@Override
public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, CacheKey cacheKey, AbstractSession executionSession, boolean isTargetProtected, Boolean[] wasCacheUsed) throws DatabaseException {
    if (this.descriptor.getCachePolicy().isProtectedIsolation()) {
        if (this.isCacheable && isTargetProtected && cacheKey != null) {
            // cachekey will be null when isolating to uow
            // used cached collection
            Object cached = cacheKey.getObject();
            if (cached != null) {
                if (wasCacheUsed != null) {
                    wasCacheUsed[0] = Boolean.TRUE;
                }
                // the indirection object is responsible for cloning the value.
                return getAttributeValueFromObject(cached);
            }
        } else if (!this.isCacheable && !isTargetProtected && cacheKey != null) {
            return this.indirectionPolicy.buildIndirectObject(new ValueHolder<>(null));
        }
    }
    if (((EISDescriptor) this.getDescriptor()).isXMLFormat()) {
        ((XMLRecord) row).setSession(executionSession);
    }
    ReadQuery targetQuery = getSelectionQuery();
    if (!this.isForeignKeyRelationship) {
        // if the source query is cascading then the target query must use the same settings
        if (targetQuery.isObjectLevelReadQuery() && (sourceQuery.shouldCascadeAllParts() || (sourceQuery.shouldCascadePrivateParts() && isPrivateOwned()) || (sourceQuery.shouldCascadeByMapping() && this.cascadeRefresh))) {
            targetQuery = (ObjectLevelReadQuery) targetQuery.clone();
            ((ObjectLevelReadQuery) targetQuery).setShouldRefreshIdentityMapResult(sourceQuery.shouldRefreshIdentityMapResult());
            targetQuery.setCascadePolicy(sourceQuery.getCascadePolicy());
            // CR #4365
            targetQuery.setQueryId(sourceQuery.getQueryId());
            // For queries that have turned caching off, such as aggregate collection, leave it off.
            if (targetQuery.shouldMaintainCache()) {
                targetQuery.setShouldMaintainCache(sourceQuery.shouldMaintainCache());
            }
        }
        return getIndirectionPolicy().valueFromQuery(targetQuery, row, sourceQuery.getSession());
    } else {
        if (getIndirectionPolicy().usesIndirection()) {
            EISOneToManyQueryBasedValueHolder valueholder = new EISOneToManyQueryBasedValueHolder(this, targetQuery, row, sourceQuery.getSession());
            return getIndirectionPolicy().buildIndirectObject(valueholder);
        } else {
            Vector<AbstractRecord> subRows = getForeignKeyRows(row, executionSession);
            if (subRows == null) {
                return null;
            }
            ContainerPolicy cp = this.getContainerPolicy();
            Object results = cp.containerInstance(subRows.size());
            for (int i = 0; i < subRows.size(); i++) {
                XMLRecord subRow = (XMLRecord) subRows.elementAt(i);
                subRow.setSession(executionSession);
                Object object = getIndirectionPolicy().valueFromQuery(targetQuery, subRow, sourceQuery.getSession());
                if (object instanceof Collection) {
                    java.util.Iterator<?> iter = ((Collection<?>) object).iterator();
                    while (iter.hasNext()) {
                        cp.addInto(iter.next(), results, executionSession);
                    }
                } else if (object instanceof java.util.Map) {
                    java.util.Iterator<?> iter = ((java.util.Map<?, ?>) object).values().iterator();
                    while (iter.hasNext()) {
                        cp.addInto(iter.next(), results, executionSession);
                    }
                } else {
                    cp.addInto(object, results, executionSession);
                }
            }
            if (cp.sizeFor(results) == 0) {
                return null;
            }
            return results;
        }
    }
}
Also used : AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) EISOneToManyQueryBasedValueHolder(org.eclipse.persistence.internal.indirection.EISOneToManyQueryBasedValueHolder) EISDescriptor(org.eclipse.persistence.eis.EISDescriptor) EISOneToManyQueryBasedValueHolder(org.eclipse.persistence.internal.indirection.EISOneToManyQueryBasedValueHolder) ValueHolder(org.eclipse.persistence.indirection.ValueHolder) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) Map(java.util.Map) Iterator(java.util.Iterator) Collection(java.util.Collection) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) ReadQuery(org.eclipse.persistence.queries.ReadQuery) HashMap(java.util.HashMap) Map(java.util.Map)

Example 30 with XMLRecord

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

the class XMLInteraction method createInputDOM.

/**
 * Create a DOM for this interaction.
 * Convert the database row or arguments into an XML DOM tree.
 */
public Element createInputDOM(EISAccessor accessor) {
    Element dom = null;
    // or the modify row.
    if ((getInputRow() != null) && (!hasArguments())) {
        if (getInputResultPath().length() == 0) {
            if (getInputRow() instanceof XMLRecord) {
                dom = (Element) ((XMLRecord) getInputRow()).getDOM();
                // Rename the root element if specified to be different.
                if (!dom.getTagName().equals(getInputRootElementName())) {
                    XMLRecord parameterRow = createXMLRecord(getInputRootElementName());
                    parameterRow.put("/" + getInputRootElementName(), getInputRow());
                    dom = (Element) parameterRow.getDOM();
                }
            } else {
                XMLRecord parameterRow = createXMLRecord(getInputRootElementName());
                for (int index = 0; index < getInputRow().size(); index++) {
                    parameterRow.put(getInputRow().getFields().elementAt(index), getInputRow().getValues().elementAt(index));
                }
                dom = (Element) parameterRow.getDOM();
            }
        } else {
            XMLRecord parameterRow = createXMLRecord(getInputRootElementName());
            parameterRow.put(getInputResultPath(), getInputRow());
            dom = (Element) parameterRow.getDOM();
        }
    } else {
        XMLRecord parameterRow = createXMLRecord(getInputRootElementName());
        for (int index = 0; index < getArgumentNames().size(); index++) {
            String parameterName = getArgumentNames().get(index);
            Object parameter = getParameters().get(index);
            // If no arguments were passed to the call execution find the paramter from the row.
            if ((parameter == null) && (getInputRow() != null)) {
                parameter = getInputRow().get(parameterName);
            }
            parameterRow.put(parameterName, parameter);
        }
        dom = (Element) parameterRow.getDOM();
    }
    return dom;
}
Also used : Element(org.w3c.dom.Element) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord)

Aggregations

XMLRecord (org.eclipse.persistence.oxm.record.XMLRecord)50 XMLField (org.eclipse.persistence.oxm.XMLField)19 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)16 DOMRecord (org.eclipse.persistence.oxm.record.DOMRecord)14 Element (org.w3c.dom.Element)11 Vector (java.util.Vector)10 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)10 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)8 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)7 XMLObjectBuilder (org.eclipse.persistence.internal.oxm.XMLObjectBuilder)7 InstanceVariableAttributeAccessor (org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor)6 MethodAttributeAccessor (org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)6 VirtualAttributeAccessor (org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor)6 CustomAccessorAttributeAccessor (org.eclipse.persistence.internal.jaxb.CustomAccessorAttributeAccessor)6 JAXBSetMethodAttributeAccessor (org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor)6 JAXBArrayAttributeAccessor (org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor)6 MapValueAttributeAccessor (org.eclipse.persistence.internal.jaxb.many.MapValueAttributeAccessor)6 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)6 XMLUnmarshaller (org.eclipse.persistence.oxm.XMLUnmarshaller)6 Field (org.eclipse.persistence.internal.oxm.mappings.Field)5