Search in sources :

Example 11 with MappingAccessor

use of org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor in project eclipselink by eclipse-ee4j.

the class OrderByMetadata method process.

/**
 * INTERNAL:
 * Process an order by value (if specified) for the given collection
 * mapping. Order by specifies the ordering of the elements of a collection
 * valued association at the point when the association is retrieved.
 *
 * The syntax of the value ordering element is an orderby_list, as follows:
 *
 * orderby_list ::= orderby_item [, orderby_item]*
 * orderby_item ::= property_or_field_name [ASC | DESC]
 *
 * When ASC or DESC is not specified, ASC is assumed.
 *
 * If the ordering element is not specified, ordering by the primary key
 * of the associated entity is assumed.
 *
 * The property or field name must correspond to that of a persistent
 * property or field of the associated class. The properties or fields
 * used in the ordering must correspond to columns for which comparison
 * operators are supported.
 */
public void process(CollectionMapping mapping, MetadataDescriptor referenceDescriptor, MetadataClass javaClass) {
    if (m_value != null && !m_value.equals("")) {
        StringTokenizer commaTokenizer = new StringTokenizer(m_value, ",");
        while (commaTokenizer.hasMoreTokens()) {
            StringTokenizer spaceTokenizer = new StringTokenizer(commaTokenizer.nextToken());
            String propertyOrFieldName = spaceTokenizer.nextToken();
            String ordering;
            // If we don't have a property name to look up we need to default one.
            if (propertyOrFieldName.equals(ASCENDING) || propertyOrFieldName.equals(DESCENDING)) {
                ordering = propertyOrFieldName;
                propertyOrFieldName = referenceDescriptor.getIdAttributeName();
            } else {
                ordering = (spaceTokenizer.hasMoreTokens()) ? spaceTokenizer.nextToken() : ASCENDING;
            }
            // A direct collection mapping does not need a query key name.
            if (mapping.isDirectCollectionMapping()) {
                if (ordering.equals(DESCENDING)) {
                    ((DirectCollectionMapping) mapping).addDescendingOrdering();
                } else {
                    ((DirectCollectionMapping) mapping).addAscendingOrdering();
                }
            } else {
                // Validate the order by reference.
                MappingAccessor referenceAccessor = referenceDescriptor.getMappingAccessor(propertyOrFieldName);
                if (referenceAccessor == null) {
                    throw ValidationException.invalidOrderByValue(propertyOrFieldName, referenceDescriptor.getJavaClass(), getAccessibleObjectName(), javaClass);
                }
                String attributeName = referenceAccessor.getAttributeName();
                if (referenceAccessor.isEmbedded()) {
                    for (String orderByAttributeName : referenceDescriptor.getOrderByAttributeNames()) {
                        mapping.addAggregateOrderBy(propertyOrFieldName, orderByAttributeName, ordering.equals(DESCENDING));
                    }
                } else if (referenceAccessor.getClassAccessor().isEmbeddableAccessor()) {
                    // We have a specific order by from an embeddable, we need to rip off
                    // the last bit of a dot notation if specified and pass in the chained
                    // string names of the nested embeddables only.
                    String embeddableChain = "";
                    if (propertyOrFieldName.contains(".")) {
                        embeddableChain = propertyOrFieldName.substring(0, propertyOrFieldName.lastIndexOf('.'));
                    }
                    mapping.addAggregateOrderBy(embeddableChain, attributeName, ordering.equals(DESCENDING));
                } else {
                    mapping.addOrderBy(attributeName, ordering.equals(DESCENDING));
                }
            }
        }
    } else {
        if (mapping.isDirectCollectionMapping()) {
            ((DirectCollectionMapping) mapping).addAscendingOrdering();
        } else {
            // Default to the primary key field name(s).
            List<String> orderByAttributes = referenceDescriptor.getIdOrderByAttributeNames();
            if (referenceDescriptor.hasEmbeddedId()) {
                String embeddedIdAttributeName = referenceDescriptor.getEmbeddedIdAttributeName();
                for (String orderByAttribute : orderByAttributes) {
                    mapping.addAggregateOrderBy(embeddedIdAttributeName, orderByAttribute, false);
                }
            } else {
                for (String orderByAttribute : orderByAttributes) {
                    mapping.addOrderBy(orderByAttribute, false);
                }
            }
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) MappingAccessor(org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor) DirectCollectionMapping(org.eclipse.persistence.mappings.DirectCollectionMapping)

Aggregations

MappingAccessor (org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor)11 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ClassAccessor (org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor)3 EmbeddableAccessor (org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EmbeddableAccessor)2 BasicAccessor (org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.BasicAccessor)2 IdAccessor (org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.IdAccessor)2 MappedKeyMapAccessor (org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappedKeyMapAccessor)2 MetadataAccessibleObject (org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataAccessibleObject)2 NamedNativeQueryMetadata (org.eclipse.persistence.internal.jpa.metadata.queries.NamedNativeQueryMetadata)2 Writer (java.io.Writer)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 JavaFileObject (javax.tools.JavaFileObject)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)1 EntityAccessor (org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor)1