Search in sources :

Example 1 with CollectionContainerPolicy

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

the class MappingAccessor method setIndirectionPolicy.

/**
 * INTERNAL:
 * Set the correct indirection policy on a collection mapping. Method
 * assume that the reference class has been set on the mapping before
 * calling this method.
 */
protected void setIndirectionPolicy(ContainerMapping mapping, String mapKey, boolean usesIndirection) {
    MetadataClass rawClass = getRawClass();
    boolean containerPolicySet = false;
    if (usesIndirection && (mapping instanceof ForeignReferenceMapping)) {
        containerPolicySet = true;
        CollectionMapping collectionMapping = (CollectionMapping) mapping;
        if (rawClass.isClass(Map.class)) {
            if (collectionMapping.isDirectMapMapping()) {
                ((DirectMapMapping) mapping).useTransparentMap();
            } else {
                collectionMapping.useTransparentMap(mapKey);
            }
        } else if (rawClass.isClass(List.class)) {
            collectionMapping.useTransparentList();
        } else if (rawClass.isClass(Collection.class)) {
            collectionMapping.useTransparentCollection();
        } else if (rawClass.isClass(Set.class)) {
            collectionMapping.useTransparentSet();
        } else {
            getLogger().logWarningMessage(MetadataLogger.WARNING_INVALID_COLLECTION_USED_ON_LAZY_RELATION, getJavaClass(), getAnnotatedElement(), rawClass);
            processIndirection((ForeignReferenceMapping) mapping);
            containerPolicySet = false;
        }
    } else {
        if (mapping instanceof CollectionMapping) {
            ((CollectionMapping) mapping).dontUseIndirection();
        }
    }
    if (!containerPolicySet) {
        if (rawClass.isClass(Map.class)) {
            if (mapping instanceof DirectMapMapping) {
                ((DirectMapMapping) mapping).useMapClass(java.util.Hashtable.class);
            } else {
                mapping.useMapClass(java.util.Hashtable.class, mapKey);
            }
        } else if (rawClass.isClass(Set.class)) {
            // This will cause it to use a CollectionContainerPolicy type
            mapping.useCollectionClass(java.util.HashSet.class);
        } else if (rawClass.isClass(List.class)) {
            // This will cause a ListContainerPolicy type to be used or
            // OrderedListContainerPolicy if ordering is specified.
            mapping.useCollectionClass(java.util.Vector.class);
        } else if (rawClass.isClass(Collection.class)) {
            // Force CollectionContainerPolicy type to be used with a
            // collection implementation.
            mapping.setContainerPolicy(new CollectionContainerPolicy(java.util.Vector.class));
        } else {
            // Use the supplied collection class type if its not an interface
            if (mapKey == null || mapKey.equals("")) {
                if (rawClass.isList()) {
                    mapping.useListClassName(rawClass.getName());
                } else {
                    mapping.useCollectionClassName(rawClass.getName());
                }
            } else {
                mapping.useMapClassName(rawClass.getName(), mapKey);
            }
        }
    }
}
Also used : ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) Set(java.util.Set) MetadataClass(org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataClass) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) CollectionMapping(org.eclipse.persistence.mappings.CollectionMapping) DirectMapMapping(org.eclipse.persistence.mappings.DirectMapMapping)

Example 2 with CollectionContainerPolicy

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

the class PreLoginMappingAdapter method preLogin.

/* (non-Javadoc)
     * @see org.eclipse.persistence.internal.jaxb.SessionEventListener#preLogin(org.eclipse.persistence.sessions.SessionEvent)
     */
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void preLogin(SessionEvent event) {
    Project project = event.getSession().getProject();
    ClassLoader cl = jpaSession.getDatasourcePlatform().getConversionManager().getLoader();
    DefaultXMLNameTransformer xmlNameTransformer = new DefaultXMLNameTransformer();
    for (Object descriptorAlias : project.getAliasDescriptors().keySet()) {
        ClassDescriptor descriptor = project.getAliasDescriptors().get(descriptorAlias);
        if (!PersistenceWeavedRest.class.isAssignableFrom(descriptor.getJavaClass())) {
            continue;
        }
        if (descriptor.isXMLDescriptor()) {
            XMLDescriptor xmlDescriptor = (XMLDescriptor) project.getAliasDescriptors().get(descriptorAlias);
            if (null != xmlDescriptor) {
                if (null == xmlDescriptor.getDefaultRootElement()) {
                    // set root element
                    xmlDescriptor.setDefaultRootElement(xmlNameTransformer.transformRootElementName(xmlDescriptor.getJavaClass().getName()));
                    // set resultAlwaysXMLRoot to false, so that the elements are not wrapped in JAXBElements when unmarshalling.
                    xmlDescriptor.setResultAlwaysXMLRoot(false);
                }
            }
        }
        XMLCompositeCollectionMapping relationshipMapping = new XMLCompositeCollectionMapping();
        relationshipMapping.setAttributeName("_persistence_relationshipInfo");
        relationshipMapping.setGetMethodName("_persistence_getRelationships");
        relationshipMapping.setSetMethodName("_persistence_setRelationships");
        relationshipMapping.setDescriptor(descriptor);
        CollectionContainerPolicy containerPolicy = new CollectionContainerPolicy(ArrayList.class);
        relationshipMapping.setContainerPolicy(containerPolicy);
        relationshipMapping.setField(new XMLField("_relationships"));
        relationshipMapping.setReferenceClass(Link.class);
        XMLJavaTypeConverter converter = new XMLJavaTypeConverter(RelationshipLinkAdapter.class);
        converter.initialize(relationshipMapping, event.getSession());
        relationshipMapping.setConverter(converter);
        descriptor.addMapping(relationshipMapping);
        XMLCompositeObjectMapping hrefMapping = new XMLCompositeObjectMapping();
        hrefMapping.setAttributeName("_persistence_href");
        hrefMapping.setGetMethodName("_persistence_getHref");
        hrefMapping.setSetMethodName("_persistence_setHref");
        hrefMapping.setDescriptor(descriptor);
        hrefMapping.setField(new XMLField("_link"));
        hrefMapping.setReferenceClass(Link.class);
        hrefMapping.setXPath(".");
        descriptor.addMapping(hrefMapping);
        XMLCompositeObjectMapping itemLinksMapping = new XMLCompositeObjectMapping();
        itemLinksMapping.setAttributeName("_persistence_links");
        itemLinksMapping.setGetMethodName("_persistence_getLinks");
        itemLinksMapping.setSetMethodName("_persistence_setLinks");
        itemLinksMapping.setDescriptor(descriptor);
        itemLinksMapping.setReferenceClass(ItemLinks.class);
        itemLinksMapping.setXPath(".");
        descriptor.addMapping(itemLinksMapping);
        ClassDescriptor jpaDescriptor = jpaSession.getDescriptorForAlias(descriptor.getAlias());
        Vector<DatabaseMapping> descriptorMappings = (Vector<DatabaseMapping>) descriptor.getMappings().clone();
        for (DatabaseMapping mapping : descriptorMappings) {
            if (mapping.isXMLMapping()) {
                if (mapping.isAbstractCompositeObjectMapping() || mapping.isAbstractCompositeCollectionMapping()) {
                    if (mapping.isAbstractCompositeCollectionMapping()) {
                        XMLInverseReferenceMapping inverseMapping = ((XMLCompositeCollectionMapping) mapping).getInverseReferenceMapping();
                        if (inverseMapping != null) {
                            break;
                        }
                    } else if (mapping.isAbstractCompositeObjectMapping()) {
                        XMLInverseReferenceMapping inverseMapping = ((XMLCompositeObjectMapping) mapping).getInverseReferenceMapping();
                        if (inverseMapping != null) {
                            break;
                        }
                    }
                    if (jpaDescriptor != null) {
                        DatabaseMapping dbMapping = jpaDescriptor.getMappingForAttributeName(mapping.getAttributeName());
                        if ((dbMapping != null) && (dbMapping instanceof ForeignReferenceMapping)) {
                            ForeignReferenceMapping jpaMapping = (ForeignReferenceMapping) dbMapping;
                            if (jpaMapping.getMappedBy() != null) {
                                ClassDescriptor inverseDescriptor = project.getDescriptorForAlias(jpaMapping.getReferenceDescriptor().getAlias());
                                if (inverseDescriptor != null) {
                                    DatabaseMapping inverseMapping = inverseDescriptor.getMappingForAttributeName(jpaMapping.getMappedBy());
                                    if (inverseMapping != null) {
                                        convertMappingToXMLInverseReferenceMapping(inverseDescriptor, inverseMapping, jpaMapping);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        InheritancePolicy inheritancePolicy = descriptor.getInheritancePolicyOrNull();
        if ((inheritancePolicy != null) && (inheritancePolicy.isRootParentDescriptor())) {
            boolean isAbstract = Modifier.isAbstract(descriptor.getJavaClass().getModifiers());
            if (isAbstract) {
                Class subClassToInstantiate = null;
                Map<?, ?> classIndicatorMapping = inheritancePolicy.getClassIndicatorMapping();
                // get one of subclasses that extends this abstract class
                for (Map.Entry<?, ?> entry : classIndicatorMapping.entrySet()) {
                    Object value = entry.getValue();
                    if (value instanceof Class) {
                        subClassToInstantiate = (Class) value;
                        isAbstract = Modifier.isAbstract(subClassToInstantiate.getModifiers());
                        if (!isAbstract) {
                            InstantiationPolicy instantiationPolicy = new InstantiationPolicy();
                            instantiationPolicy.useFactoryInstantiationPolicy(new ConcreteSubclassFactory(subClassToInstantiate), "createConcreteSubclass");
                            descriptor.setInstantiationPolicy(instantiationPolicy);
                            break;
                        }
                    }
                }
            }
        }
    }
    for (Object descriptorAlias : project.getAliasDescriptors().keySet()) {
        ClassDescriptor descriptor = project.getAliasDescriptors().get(descriptorAlias);
        ClassDescriptor jpaDescriptor = jpaSession.getDescriptorForAlias(descriptor.getAlias());
        Vector<DatabaseMapping> descriptorMappings = (Vector<DatabaseMapping>) descriptor.getMappings().clone();
        for (DatabaseMapping mapping : descriptorMappings) {
            if (mapping.isXMLMapping()) {
                if (mapping.isAbstractCompositeObjectMapping() || mapping.isAbstractCompositeCollectionMapping()) {
                    if (jpaDescriptor != null) {
                        DatabaseMapping dbMapping = jpaDescriptor.getMappingForAttributeName(mapping.getAttributeName());
                        if ((dbMapping instanceof ForeignReferenceMapping)) {
                            ForeignReferenceMapping jpaMapping = (ForeignReferenceMapping) dbMapping;
                            ClassDescriptor jaxbDescriptor = project.getDescriptorForAlias(jpaMapping.getDescriptor().getAlias());
                            convertMappingToXMLChoiceMapping(jaxbDescriptor, jpaMapping, cl, jpaSession);
                        }
                    } else if (mapping instanceof XMLCompositeObjectMapping) {
                        // Fix for Bug 403113 - JPA-RS Isn't Serializing an Embeddable defined in an ElementCollection to JSON Correctly
                        // add choice mapping for one-to-one relationships within embeddables
                        // Based on (http://wiki.eclipse.org/EclipseLink/Examples/JPA/NoSQL#Step_2_:_Map_the_data),
                        // the mappedBy option on relationships is not supported for NoSQL data, so no need to add inverse mapping
                        XMLCompositeObjectMapping jpaMapping = (XMLCompositeObjectMapping) mapping;
                        ClassDescriptor jaxbDescriptor = project.getDescriptorForAlias(jpaMapping.getDescriptor().getAlias());
                        if (jaxbDescriptor != null) {
                            Class clazz = jpaMapping.getReferenceClass();
                            if (clazz != null) {
                                if ((jpaSession.getDescriptor(clazz) != null) && (jpaSession.getDescriptor(clazz).isEISDescriptor()))
                                    convertMappingToXMLChoiceMapping(jaxbDescriptor, jpaMapping, cl, jpaSession);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) InstantiationPolicy(org.eclipse.persistence.internal.descriptors.InstantiationPolicy) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) Vector(java.util.Vector) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter) XMLField(org.eclipse.persistence.oxm.XMLField) PersistenceWeavedRest(org.eclipse.persistence.internal.jpa.rs.weaving.PersistenceWeavedRest) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Project(org.eclipse.persistence.sessions.Project) InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) DefaultXMLNameTransformer(org.eclipse.persistence.jaxb.DefaultXMLNameTransformer) Map(java.util.Map) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 3 with CollectionContainerPolicy

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

the class ObjectPersistenceRuntimeXMLProject method buildFetchGroupDescriptor.

protected ClassDescriptor buildFetchGroupDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(FetchGroup.class);
    descriptor.setDefaultRootElement("fetch-group");
    XMLDirectMapping fetchGroupNameMapping = new XMLDirectMapping();
    fetchGroupNameMapping.setAttributeName("name");
    fetchGroupNameMapping.setXPath(getPrimaryNamespaceXPath() + "name");
    descriptor.addMapping(fetchGroupNameMapping);
    XMLCompositeDirectCollectionMapping fetchGroupAttributeMapping = new XMLCompositeDirectCollectionMapping();
    CollectionContainerPolicy containerPolicy = new CollectionContainerPolicy(TreeSet.class);
    fetchGroupAttributeMapping.setContainerPolicy(containerPolicy);
    fetchGroupAttributeMapping.setAttributeName("attributes");
    fetchGroupAttributeMapping.setGetMethodName("getAttributeNames");
    fetchGroupAttributeMapping.setSetMethodName("setAttributeNames");
    fetchGroupAttributeMapping.setXPath(getPrimaryNamespaceXPath() + "fetch-group-attributes/" + getPrimaryNamespaceXPath() + "fetch-group-attribute/text()");
    descriptor.addMapping(fetchGroupAttributeMapping);
    return descriptor;
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLCompositeDirectCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping)

Example 4 with CollectionContainerPolicy

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

the class ContainerCloningTest method test.

@Override
public void test() {
    ContainerPolicy cp = new CollectionContainerPolicy();
    cp.setContainerClass(ClassConstants.ArrayList_class);
    Collection<Employee> originalC = java.util.Arrays.asList(new Employee());
    Collection cloneC = (Collection) cp.cloneFor(originalC);
    cp = new MapContainerPolicy();
    cp.setContainerClass(java.util.WeakHashMap.class);
    Map originalM = new java.util.WeakHashMap();
    originalM.put(1, 2);
    Map cloneM = (Map) cp.cloneFor(originalM);
    if ((originalC == cloneC) || (originalC.size() != cloneC.size())) {
        throw new TestErrorException("Cloned Collections are not copies.");
    }
    if ((originalM == cloneM) || (originalM.size() != cloneM.size())) {
        throw new TestErrorException("Cloned Maps are not copies.");
    }
    if (!originalC.iterator().next().equals(cloneC.iterator().next())) {
        throw new TestErrorException("Cloned Collections are not the same.");
    }
    if (!originalM.get(1).equals(cloneM.get(1))) {
        throw new TestErrorException("Cloned Maps are not the same.");
    }
}
Also used : CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) MapContainerPolicy(org.eclipse.persistence.internal.queries.MapContainerPolicy) CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) MapContainerPolicy(org.eclipse.persistence.internal.queries.MapContainerPolicy) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) Collection(java.util.Collection) Map(java.util.Map)

Example 5 with CollectionContainerPolicy

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

the class RemoteSessionController method cursorSelectObjects.

/**
 * Returns a remote cursor stub in a transporter
 */
public Transporter cursorSelectObjects(Transporter remoteTransporter) {
    Transporter transporter = new Transporter();
    try {
        CursorPolicy policy = (CursorPolicy) remoteTransporter.getObject();
        // Clear the unit of work, as the client unit of work may have been cleared.
        this.unitOfWork = null;
        AbstractSession executionSession = getExecutionSession();
        if (policy.isCursoredStreamPolicy()) {
            // wrap the cursored stream into a RemoteCursoredStream object and send the object to the client
            CursoredStream stream = (CursoredStream) executionSession.executeQuery(policy.getQuery());
            RemoteCursoredStream remoteStream = new RemoteCursoredStream(stream);
            // For bug 3452418 prevents reading the initial objects twice.
            remoteStream.setObjectCollection(stream.nextElements(stream.getObjectCollection().size()));
            getRemoteCursors().put(remoteStream.getID(), stream);
            if (stream.getQuery().isReadAllQuery() && (!stream.getQuery().isReportQuery())) {
                transporter.setObjectDescriptors(replaceValueHoldersInAll(remoteStream.getObjectCollection(), new CollectionContainerPolicy(ClassConstants.Vector_class)));
            }
            transporter.setObject(remoteStream);
        } else if (policy.isScrollableCursorPolicy()) {
            // wrap the scrollable cursor into a RemoteScrollableCursor object and send the object to the client
            ScrollableCursor stream = (ScrollableCursor) executionSession.executeQuery(policy.getQuery());
            RemoteScrollableCursor remoteStream = new RemoteScrollableCursor(stream);
            getRemoteCursors().put(remoteStream.getID(), stream);
            transporter.setObject(remoteStream);
        }
    } catch (RuntimeException exception) {
        transporter.setException(exception);
    }
    return transporter;
}
Also used : CursoredStream(org.eclipse.persistence.queries.CursoredStream) CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) ScrollableCursor(org.eclipse.persistence.queries.ScrollableCursor) CursorPolicy(org.eclipse.persistence.queries.CursorPolicy) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

CollectionContainerPolicy (org.eclipse.persistence.internal.queries.CollectionContainerPolicy)6 Collection (java.util.Collection)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Set (java.util.Set)2 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)2 ForeignReferenceMapping (org.eclipse.persistence.mappings.ForeignReferenceMapping)2 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)2 WebApplicationException (jakarta.ws.rs.WebApplicationException)1 ResponseBuilder (jakarta.ws.rs.core.Response.ResponseBuilder)1 MessageBodyReader (jakarta.ws.rs.ext.MessageBodyReader)1 JAXBContext (jakarta.xml.bind.JAXBContext)1 JAXBElement (jakarta.xml.bind.JAXBElement)1 JAXBException (jakarta.xml.bind.JAXBException)1 UnmarshalException (jakarta.xml.bind.UnmarshalException)1 Unmarshaller (jakarta.xml.bind.Unmarshaller)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Deque (java.util.Deque)1 HashSet (java.util.HashSet)1