KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > database > ObjectQuery


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.database;
6
7 import java.util.List JavaDoc ;
8 import java.util.ArrayList JavaDoc ;
9 import java.util.Date JavaDoc;
10 import java.text.SimpleDateFormat JavaDoc ;
11 /**
12  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
13  * @since Nov 25, 2004
14  * @version $Id$
15  */

16 public class ObjectQuery {
17   private static SimpleDateFormat JavaDoc ft_ = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss.SSS") ;
18   private Class JavaDoc type_ ;
19   private String JavaDoc orderBy_ ;
20   private List JavaDoc parameters_ ;
21   
22   public ObjectQuery(Class JavaDoc type) {
23     type_ = type ;
24     parameters_ = new ArrayList JavaDoc(3) ;
25   }
26   
27   public ObjectQuery addEQ(String JavaDoc field, Object JavaDoc value) {
28     if(value != null) {
29       parameters_.add(new Parameter(" = ", field, value)) ;
30     }
31     return this ;
32   }
33   
34   public ObjectQuery addGT(String JavaDoc field, Object JavaDoc value) {
35     if(value != null) {
36       parameters_.add(new Parameter(" > ", field, value)) ;
37     }
38     return this ;
39   }
40   
41   public ObjectQuery addLT(String JavaDoc field, Object JavaDoc value) {
42     if(value != null) {
43       parameters_.add(new Parameter(" < ", field, value)) ;
44     }
45     return this ;
46   }
47   
48   public ObjectQuery addLIKE(String JavaDoc field, String JavaDoc value) {
49     if(value != null && value.length() > 0) {
50       parameters_.add(new Parameter(" like ", field, value.replace('*', '%'))) ;
51     }
52     return this ;
53   }
54   
55   public ObjectQuery setAscOrderBy(String JavaDoc field) {
56     orderBy_ = " order by o." + field + " asc";
57     return this ;
58   }
59   
60   public ObjectQuery setDescOrderBy(String JavaDoc field) {
61     orderBy_ = " order by o." + field + " desc";
62     return this ;
63   }
64   
65   public String JavaDoc getHibernateQuery() {
66     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
67     b.append("from o in class ").append(type_.getName()) ;
68     if(parameters_.size() > 0) {
69       b.append(" where ") ;
70       for(int i = 0; i < parameters_.size(); i ++) {
71         if(i > 0) b.append(" and ") ;
72         Parameter p = (Parameter) parameters_.get(i) ;
73         if(p.value_ instanceof String JavaDoc) {
74           b.append(" o.").append(p.field_).append(p.op_).append("'").append(p.value_).append("'") ;
75         } else if(p.value_ instanceof Date JavaDoc) {
76           String JavaDoc value = ft_.format((Date JavaDoc) p.value_) ;
77           b.append(" o.").append(p.field_).append(p.op_).append("'").append(value).append("'") ;
78         } else {
79           b.append(" o.").append(p.field_).append(p.op_).append(p.value_);
80         }
81       }
82     }
83     if(orderBy_ != null ) b.append(orderBy_ );
84     return b.toString() ;
85   }
86   
87   public String JavaDoc getHibernateCountQuery() {
88     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
89     b.append("select count(o) from o in class ").append(type_.getName()) ;
90     if(parameters_.size() > 0) {
91       b.append(" where ") ;
92       for(int i = 0; i < parameters_.size(); i ++) {
93         if(i > 0) b.append(" and ") ;
94         Parameter p = (Parameter) parameters_.get(i) ;
95         if(p.value_ instanceof String JavaDoc) {
96           b.append(" o.").append(p.field_).append(p.op_).append("'").append(p.value_).append("'") ;
97         } else if(p.value_ instanceof Date JavaDoc) {
98           String JavaDoc value = ft_.format((Date JavaDoc) p.value_) ;
99           b.append(" o.").append(p.field_).append(p.op_).append("'").append(value).append("'") ;
100         } else {
101           b.append(" o.").append(p.field_).append(p.op_).append(p.value_);
102         }
103       }
104     }
105     return b.toString() ;
106   }
107   
108   static class Parameter {
109     String JavaDoc op_ ;
110     String JavaDoc field_ ;
111     Object JavaDoc value_ ;
112     
113     Parameter(String JavaDoc op, String JavaDoc field , Object JavaDoc value) {
114       op_ = op ;
115       field_ = field ;
116       value_ = value ;
117     }
118   }
119 }
120
Popular Tags