package org.hibernate.criterion;
import java.io.Serializable;
import java.sql.Types;
import java.util.Locale;
import org.hibernate.Criteria;
import org.hibernate.NullPrecedence;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.type.Type;
public class Order implements Serializable {
private boolean ascending;
private boolean ignoreCase;
private String propertyName;
private NullPrecedence nullPrecedence;
public static Order asc(String propertyName) {
return new Order( propertyName, true );
}
public static Order desc(String propertyName) {
return new Order( propertyName, false );
}
protected Order(String propertyName, boolean ascending) {
this.propertyName = propertyName;
this.ascending = ascending;
}
public Order ignoreCase() {
ignoreCase = true;
return this;
}
public Order nulls(NullPrecedence nullPrecedence) {
this.nullPrecedence = nullPrecedence;
return this;
}
public String getPropertyName() {
return propertyName;
}
@SuppressWarnings("UnusedDeclaration")
public boolean isAscending() {
return ascending;
}
@SuppressWarnings("UnusedDeclaration")
public boolean isIgnoreCase() {
return ignoreCase;
}
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) {
final String[] columns = criteriaQuery.getColumnsUsingProjection( criteria, propertyName );
final Type type = criteriaQuery.getTypeUsingProjection( criteria, propertyName );
final SessionFactoryImplementor factory = criteriaQuery.getFactory();
final int[] sqlTypes = type.sqlTypes( factory );
final StringBuilder fragment = new StringBuilder();
for ( int i=0; i<columns.length; i++ ) {
final StringBuilder expression = new StringBuilder();
boolean lower = false;
if ( ignoreCase ) {
final int sqlType = sqlTypes[i];
lower = sqlType == Types.VARCHAR
|| sqlType == Types.CHAR
|| sqlType == Types.LONGVARCHAR;
}
if ( lower ) {
expression.append( factory.getDialect().getLowercaseFunction() )
.append( '(' );
}
expression.append( columns[i] );
if ( lower ) {
expression.append( ')' );
}
fragment.append(
factory.getDialect().renderOrderByElement(
expression.toString(),
null,
ascending ? "asc" : "desc",
nullPrecedence != null ? nullPrecedence : factory.getSettings().getDefaultNullPrecedence()
)
);
if ( i < columns.length-1 ) {
fragment.append( ", " );
}
}
return fragment.toString();
}
@Override
public String toString() {
return propertyName + ' '
+ ( ascending ? "asc" : "desc" )
+ ( nullPrecedence != null ? ' ' + nullPrecedence.name().toLowerCase(Locale.ROOT) : "" );
}
}