KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > criterion > Order


1 //$Id: Order.java,v 1.10 2005/07/15 16:52:10 oneovthafew Exp $
2
package org.hibernate.criterion;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.Types JavaDoc;
6
7 import org.hibernate.Criteria;
8 import org.hibernate.HibernateException;
9 import org.hibernate.engine.SessionFactoryImplementor;
10 import org.hibernate.type.Type;
11
12 /**
13  * Represents an order imposed upon a <tt>Criteria</tt> result set
14  * @author Gavin King
15  */

16 public class Order implements Serializable JavaDoc {
17
18     private boolean ascending;
19     private boolean ignoreCase;
20     private String JavaDoc propertyName;
21     
22     public String JavaDoc toString() {
23         return propertyName + ' ' + (ascending?"asc":"desc");
24     }
25     
26     public Order ignoreCase() {
27         ignoreCase = true;
28         return this;
29     }
30
31     /**
32      * Constructor for Order.
33      */

34     protected Order(String JavaDoc propertyName, boolean ascending) {
35         this.propertyName = propertyName;
36         this.ascending = ascending;
37     }
38
39     /**
40      * Render the SQL fragment
41      *
42      */

43     public String JavaDoc toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
44     throws HibernateException {
45         String JavaDoc[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
46         Type type = criteriaQuery.getTypeUsingProjection(criteria, propertyName);
47         StringBuffer JavaDoc fragment = new StringBuffer JavaDoc();
48         for ( int i=0; i<columns.length; i++ ) {
49             SessionFactoryImplementor factory = criteriaQuery.getFactory();
50             boolean lower = ignoreCase && type.sqlTypes( factory )[i]==Types.VARCHAR;
51             if (lower) {
52                 fragment.append( factory.getDialect().getLowercaseFunction() )
53                     .append('(');
54             }
55             fragment.append( columns[i] );
56             if (lower) fragment.append(')');
57             fragment.append( ascending ? " asc" : " desc" );
58             if ( i<columns.length-1 ) fragment.append(", ");
59         }
60         return fragment.toString();
61     }
62
63     /**
64      * Ascending order
65      *
66      * @param propertyName
67      * @return Order
68      */

69     public static Order asc(String JavaDoc propertyName) {
70         return new Order(propertyName, true);
71     }
72
73     /**
74      * Descending order
75      *
76      * @param propertyName
77      * @return Order
78      */

79     public static Order desc(String JavaDoc propertyName) {
80         return new Order(propertyName, false);
81     }
82
83 }
84
Popular Tags