Search in sources :

Example 51 with ContainerPolicy

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

the class XMLAnyAttributeMapping method initialize.

@Override
public void initialize(AbstractSession session) throws DescriptorException {
    if (getField() != null) {
        setField(getDescriptor().buildField(getField()));
    }
    ContainerPolicy cp = getContainerPolicy();
    if (cp != null && cp.getContainerClass() == null) {
        Class<Object> cls = session.getDatasourcePlatform().getConversionManager().convertClassNameToClass(cp.getContainerClassName());
        cp.setContainerClass(cls);
    }
}
Also used : MappedKeyMapContainerPolicy(org.eclipse.persistence.internal.queries.MappedKeyMapContainerPolicy) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy)

Example 52 with ContainerPolicy

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

the class XMLAnyCollectionMapping method buildObjectValuesFromDOMRecord.

private Object buildObjectValuesFromDOMRecord(DOMRecord record, AbstractSession session, ObjectBuildingQuery query, JoinedAttributeManager joinManager) {
    // This DOMRecord represents the root node of the AnyType instance
    // Grab ALL children to populate the collection.
    Node root = record.getDOM();
    NodeList children = root.getChildNodes();
    ContainerPolicy cp = getContainerPolicy();
    Object container = null;
    if (reuseContainer) {
        Object currentObject = record.getCurrentObject();
        Object value = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        container = value != null ? value : cp.containerInstance();
    } else {
        container = cp.containerInstance();
    }
    int length = children.getLength();
    Node next = null;
    if (length > 0) {
        next = record.getDOM().getFirstChild();
    }
    while (next != null) {
        Object objectValue = null;
        if (isUnmappedContent(next)) {
            if ((next.getNodeType() == Node.TEXT_NODE) && this.isMixedContent()) {
                if (next.getNodeValue().trim().length() > 0) {
                    objectValue = next.getNodeValue();
                    objectValue = convertDataValueToObjectValue(objectValue, session, record.getUnmarshaller());
                    cp.addInto(objectValue, container, session);
                }
            } else if (next.getNodeType() == Node.ELEMENT_NODE) {
                XMLDescriptor referenceDescriptor = null;
                // In this case it must be an element so we need to dig up the descriptor
                // make a nested record and build an object from it.
                DOMRecord nestedRecord = (DOMRecord) record.buildNestedRow((Element) next);
                if (!useXMLRoot) {
                    referenceDescriptor = getDescriptor(nestedRecord, session, null);
                    objectValue = buildObjectForNonXMLRoot(referenceDescriptor, getConverter(), query, record, nestedRecord, joinManager, session, next, container, cp);
                } else {
                    String schemaType = ((Element) next).getAttributeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, XMLConstants.SCHEMA_TYPE_ATTRIBUTE);
                    QName schemaTypeQName = null;
                    XPathFragment frag = new XPathFragment();
                    if ((null != schemaType) && (schemaType.length() > 0)) {
                        frag.setXPath(schemaType);
                        if (frag.hasNamespace()) {
                            String prefix = frag.getPrefix();
                            XMLPlatform xmlPlatform = XMLPlatformFactory.getInstance().getXMLPlatform();
                            String url = xmlPlatform.resolveNamespacePrefix(next, prefix);
                            frag.setNamespaceURI(url);
                            schemaTypeQName = new QName(url, frag.getLocalName());
                        }
                        XMLContext xmlContext = nestedRecord.getUnmarshaller().getXMLContext();
                        referenceDescriptor = xmlContext.getDescriptorByGlobalType(frag);
                    }
                    if (referenceDescriptor == null) {
                        try {
                            QName qname = new QName(nestedRecord.getNamespaceURI(), nestedRecord.getLocalName());
                            referenceDescriptor = getDescriptor(nestedRecord, session, qname);
                        } catch (XMLMarshalException e) {
                            referenceDescriptor = null;
                        }
                        // Check if descriptor is for a wrapper, if it is null it out and let continue
                        XMLDescriptor xmlReferenceDescriptor = referenceDescriptor;
                        if (referenceDescriptor != null && xmlReferenceDescriptor.isWrapper()) {
                            referenceDescriptor = null;
                        }
                    }
                    // is set, then we want to return either an Element or an XMLRoot wrapping an Element
                    if (getKeepAsElementPolicy() == UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT || (referenceDescriptor == null && getKeepAsElementPolicy() == UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT)) {
                        Object objVal = buildObjectNoReferenceDescriptor(record, getConverter(), session, next, null, null);
                        // if we know the descriptor use it to wrap the Element in an XMLRoot (if necessary)
                        if (referenceDescriptor != null) {
                            objVal = referenceDescriptor.wrapObjectInXMLRoot(objVal, next.getNamespaceURI(), next.getLocalName(), next.getPrefix(), false, record.isNamespaceAware(), record.getUnmarshaller());
                            cp.addInto(objVal, container, session);
                        } else {
                            // no descriptor, so manually build the XMLRoot
                            cp.addInto(buildXMLRoot(next, objVal), container, session);
                        }
                    } else // if we have a descriptor use it to build the object and wrap it in an XMLRoot
                    if (referenceDescriptor != null) {
                        buildObjectAndWrapInXMLRoot(referenceDescriptor, getConverter(), query, record, nestedRecord, joinManager, session, next, container, cp);
                    } else {
                        // no descriptor, but could be TEXT to wrap and return
                        XMLRoot rootValue;
                        if ((rootValue = buildXMLRootForText(next, schemaTypeQName, getConverter(), session, record)) != null) {
                            cp.addInto(rootValue, container, session);
                        }
                    }
                }
            }
        }
        next = next.getNextSibling();
    }
    return container;
}
Also used : XMLContext(org.eclipse.persistence.oxm.XMLContext) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) QName(javax.xml.namespace.QName) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) ListContainerPolicy(org.eclipse.persistence.internal.queries.ListContainerPolicy) CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLPlatform(org.eclipse.persistence.platform.xml.XMLPlatform) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException)

Example 53 with ContainerPolicy

use of org.eclipse.persistence.internal.queries.ContainerPolicy 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 54 with ContainerPolicy

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

the class ReadAllQuery method useMapClass.

/**
 * PUBLIC:
 * Configure the query to use an instance of the specified container class
 * to hold the result objects. The key used to index the value in the Map
 * is the value returned by a call to the specified zero-argument method.
 * The method must be implemented by the class (or a superclass) of the
 * value to be inserted into the Map.
 * <p>jdk1.2.x: The container class must implement (directly or indirectly) the Map interface.
 * <p>jdk1.1.x: The container class must be a subclass of Hashtable.
 * <p>The referenceClass must set before calling this method.
 */
public void useMapClass(Class<?> concreteClass, String methodName) {
    // the reference class has to be specified before coming here
    if (getReferenceClass() == null) {
        throw QueryException.referenceClassMissing(this);
    }
    ContainerPolicy policy = ContainerPolicy.buildPolicyFor(concreteClass);
    policy.setKeyName(methodName, getReferenceClass().getName());
    setContainerPolicy(policy);
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy)

Example 55 with ContainerPolicy

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

the class ReadAllQuery method registerResultSetInUnitOfWork.

/**
 * INTERNAL:
 * Version of the previous method for ResultSet optimization.
 *
 * @return the final (conformed, refreshed, wrapped) UnitOfWork query result
 */
public Object registerResultSetInUnitOfWork(ResultSet resultSet, Vector fields, DatabaseField[] fieldsArray, UnitOfWorkImpl unitOfWork, AbstractRecord arguments) throws SQLException {
    // TODO: add support for Conforming results in UOW - currently conforming in uow is not compatible with ResultSet optimization.
    ContainerPolicy cp = this.containerPolicy;
    Object clones = cp.containerInstance();
    ResultSetMetaData metaData = resultSet.getMetaData();
    boolean hasNext = resultSet.next();
    if (hasNext) {
        // TODO: possibly add support for SortedListContainerPolicy (cp.shouldAddAll() == true) - this policy currently is not compatible with ResultSet optimization
        boolean quickAdd = (clones instanceof Collection) && !this.descriptor.getObjectBuilder().hasWrapperPolicy();
        DatabaseAccessor dbAccessor = (DatabaseAccessor) getAccessor();
        boolean useSimple = this.descriptor.getObjectBuilder().isSimple();
        AbstractSession executionSession = getExecutionSession();
        DatabasePlatform platform = dbAccessor.getPlatform();
        boolean optimizeData = platform.shouldOptimizeDataConversion();
        if (useSimple) {
            // None of the fields are relational - the row could be reused, just clear all the values.
            SimpleResultSetRecord row = new SimpleResultSetRecord(fields, fieldsArray, resultSet, metaData, dbAccessor, executionSession, platform, optimizeData);
            if (this.descriptor.isDescriptorTypeAggregate()) {
                // Aggregate Collection may have an unmapped primary key referencing the owner, the corresponding field will not be used when the object is populated and therefore may not be cleared.
                row.setShouldKeepValues(true);
            }
            while (hasNext) {
                Object clone = buildObject(row);
                if (quickAdd) {
                    ((Collection) clones).add(clone);
                } else {
                    // TODO: investigate is it possible to support MappedKeyMapPolicy - this policy currently is not compatible with ResultSet optimization
                    cp.addInto(clone, clones, unitOfWork);
                }
                row.reset();
                hasNext = resultSet.next();
            }
        } else {
            boolean shouldKeepRow = this.descriptor.getObjectBuilder().shouldKeepRow();
            while (hasNext) {
                ResultSetRecord row = new ResultSetRecord(fields, fieldsArray, resultSet, metaData, dbAccessor, executionSession, platform, optimizeData);
                Object clone = buildObject(row);
                if (quickAdd) {
                    ((Collection) clones).add(clone);
                } else {
                    // TODO: investigate is it possible to support MappedKeyMapPolicy - this policy currently is not compatible with ResultSet optimization
                    cp.addInto(clone, clones, unitOfWork);
                }
                if (shouldKeepRow) {
                    if (row.hasResultSet()) {
                        // ResultSet has not been fully triggered - that means the cached object was used.
                        // Yet the row still may be cached in a value holder (see loadBatchReadAttributes and loadJoinedAttributes methods).
                        // Remove ResultSet to avoid attempt to trigger it (already closed) when pk or fk values (already extracted) accessed when the value holder is instantiated.
                        row.removeResultSet();
                    } else {
                        row.removeNonIndirectionValues();
                    }
                }
                hasNext = resultSet.next();
            }
        }
    }
    return clones;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) Collection(java.util.Collection) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) DatabaseAccessor(org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor) SimpleResultSetRecord(org.eclipse.persistence.internal.sessions.SimpleResultSetRecord) ResultSetRecord(org.eclipse.persistence.internal.sessions.ResultSetRecord) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) SimpleResultSetRecord(org.eclipse.persistence.internal.sessions.SimpleResultSetRecord) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

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