1 5 package org.exoplatform.services.database; 6 7 import java.util.List ; 8 import java.util.ArrayList ; 9 import java.util.Date ; 10 import java.text.SimpleDateFormat ; 11 16 public class ObjectQuery { 17 private static SimpleDateFormat ft_ = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS") ; 18 private Class type_ ; 19 private String orderBy_ ; 20 private List parameters_ ; 21 22 public ObjectQuery(Class type) { 23 type_ = type ; 24 parameters_ = new ArrayList (3) ; 25 } 26 27 public ObjectQuery addEQ(String field, Object value) { 28 if(value != null) { 29 parameters_.add(new Parameter(" = ", field, value)) ; 30 } 31 return this ; 32 } 33 34 public ObjectQuery addGT(String field, Object value) { 35 if(value != null) { 36 parameters_.add(new Parameter(" > ", field, value)) ; 37 } 38 return this ; 39 } 40 41 public ObjectQuery addLT(String field, Object value) { 42 if(value != null) { 43 parameters_.add(new Parameter(" < ", field, value)) ; 44 } 45 return this ; 46 } 47 48 public ObjectQuery addLIKE(String field, String 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 field) { 56 orderBy_ = " order by o." + field + " asc"; 57 return this ; 58 } 59 60 public ObjectQuery setDescOrderBy(String field) { 61 orderBy_ = " order by o." + field + " desc"; 62 return this ; 63 } 64 65 public String getHibernateQuery() { 66 StringBuffer b = new StringBuffer () ; 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 ) { 74 b.append(" o.").append(p.field_).append(p.op_).append("'").append(p.value_).append("'") ; 75 } else if(p.value_ instanceof Date ) { 76 String value = ft_.format((Date ) 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 getHibernateCountQuery() { 88 StringBuffer b = new StringBuffer () ; 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 ) { 96 b.append(" o.").append(p.field_).append(p.op_).append("'").append(p.value_).append("'") ; 97 } else if(p.value_ instanceof Date ) { 98 String value = ft_.format((Date ) 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 op_ ; 110 String field_ ; 111 Object value_ ; 112 113 Parameter(String op, String field , Object value) { 114 op_ = op ; 115 field_ = field ; 116 value_ = value ; 117 } 118 } 119 } 120 | Popular Tags |