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);
}
}
}
}
}
Aggregations