java常用组件--Hibernate QueryParameters
/**
*
*/
package cn.ccb.jstsccf.common.dao.utils;import java.util.ArrayList;
importjava.util.Iterator;
importjava.util.List;
import java.util.Set;/**
*@authorghl
*
*/
publicclassQueryParameters{
private List list;public QueryParameters() {
list=newArrayList(3);
}public QueryParameters add(String propName, Object propValue) {
list.add(newParamter(propName,propValue,null));
returnthis;
}public QueryParameters addOrder(String propName) {
returnaddOrder(propName,true);
}public QueryParameters addOrder(String propName, boolean ascending) {
list.add(newOrder(propName,ascending,null));
returnthis;
}public void removeOrderByName(String propName) {
if(propName==null)
return;
for(inti=0;i<list.size();i++){
Objectobj=list.get(i);
if(objinstanceofOrder){
Orderorder=(Order)obj;
if(propName.equals(order.propName))
list.remove(obj);
}
}}
public void removeAllOrder() {
for(inti=0;i<list.size();i++){
Objectobj=list.get(i);
if(objinstanceofOrder)
list.remove(obj);
}}
public QueryParameters groupby(String propNames[]) {
list.add(newGroupBy(propNames,null));
returnthis;
}public QueryParameters addProperties(String properties[]) {
list.add(newProperties(properties,null));
returnthis;
}public QueryParameters sum(String propName) {
list.add(newSum(propName,null));
returnthis;
}public QueryParameters max(String propName) {
list.add(newMax(propName,null));
returnthis;
}public QueryParameters min(String propName) {
list.add(newMin(propName,null));
returnthis;
}public QueryParameters isNull(String propName) {
list.add(newNull(propName));
returnthis;
}
publicQueryParametersisNotNull(StringpropName){
list.add(newNotNull(propName));
returnthis;
}public QueryParameters notEquals(String propName, Object propValue) {
list.add(newNotEquals(propName,propValue));
returnthis;
}
private static class Order {private String propName;
private boolean ascending;
private Order(String propName, boolean ascending) {
this.propName=propName;
this.ascending=ascending;
}Order(String s, boolean flag, Order order) {
this(s,flag);
}
}private static class GroupBy {
private String propNames[];
private GroupBy(String propNames[]) {
this.propNames=propNames;
}GroupBy(String as[], GroupBy groupby1) {
this(as);
}
}private static class Properties {
private String properties[];
private Properties(String propList[]) {
properties=propList;
}Properties(String as[], Properties properties1) {
this(as);
}
}private static class Sum {
private String propName;
private Sum(String propName) {
this.propName=propName;
}Sum(String s, Sum sum1) {
this(s);
}
}private static class Max {
private String propName;
private Max(String propName) {
this.propName=propName;
}Max(String s, Max max1) {
this(s);
}
}private static class Min {
private String propName;
private Min(String propName) {
this.propName=propName;
}Min(String s, Min min1) {
this(s);
}
}public static class Paramter {
private String propName;
private Object propValue;
publicStringgetPropName(){
returnpropName;
}public Object getPropValue() {
returnpropValue;
}private Paramter(String propName, Object propValue) {
this.propName=propName;
this.propValue=propValue;
}Paramter(String s, Object obj, Paramter paramter) {
this(s,obj);
}
}
public static final class Pattern {public String getPattern() {
returnpattern;
}private String pattern;
Pattern(String text) {
pattern=text;
}
}public static class Null {
public String getPropName() {
returnpropName;
}public void setPropName(String propName) {
this.propName=propName;
}private String propName;
Null(String propName) {
this.propName=propName;
}
}
public static class NotNull {public String getPropName() {
returnpropName;
}public void setPropName(String propName) {
this.propName=propName;
}private String propName;
NotNull(String propName) {
this.propName=propName;
}
}
publicstaticclassNotEquals{
private String propName;private Object propValue;
publicStringgetPropName(){
returnpropName;
}public Object getPropValue() {
returnpropValue;
}
publicNotEquals(StringpropName,ObjectpropValue){
this.propName=propName;
this.propValue=propValue;
}
}public static interface Compiler {
public abstract Object compile(QueryParameters queryparameters); }
public static abstract class AbstractCompiler implements Compiler {
public Object compile(QueryParameters params) {
for(Iteratoriterator=params.list.iterator();iterator.hasNext();){
Objecte=iterator.next();
if(einstanceofParamter){
Paramterp=(Paramter)e;
ObjectpropValue=p.getPropValue();
if(propValueinstanceofRange)
range(p.getPropName(),(Range)propValue);
elseif(propValueinstanceofPattern)
like(p.getPropName(),((Pattern)propValue)
.getPattern());
elseif(propValueinstanceofSet)
in(p.getPropName(),(Set)propValue);
else
eq(p.getPropName(),propValue);
}elseif(einstanceofProperties){
Propertiesp=(Properties)e;
properties(p.properties);
}elseif(einstanceofGroupBy){
GroupByg=(GroupBy)e;
groupBy(g.propNames);
}elseif(einstanceofOrder){
Ordero=(Order)e;
order(o.propName,o.ascending);
}elseif(einstanceofSum){
Sums=(Sum)e;
sum(s.propName);
}elseif(einstanceofMax){
Maxm=(Max)e;
max(m.propName);
}elseif(einstanceofMin){
Minm=(Min)e;
min(m.propName);
}elseif(einstanceofNull){
Nullm=(Null)e;
isNull(m.propName);
}elseif(einstanceofNotNull){
NotNullm=(NotNull)e;
isNotNull(m.propName);
}elseif(einstanceofNotEquals){
NotEqualsne=(NotEquals)e;
ne(ne.getPropName(),ne.getPropValue());
}
}return getAssembly(); }
protected abstract Object getAssembly();
protected void range(String propName, Range range) {
if(range.hasHighLimit()&&range.hasLowLimit()
&&range.inRange(range.getLow())
&&range.inRange(range.getHigh())){
between(propName,range.getLow(),range.getHigh());
return;
}
if(range.hasLowLimit())
if(range.inRange(range.getLow()))
ge(propName,range.getLow());
else
gt(propName,range.getLow());
if(range.hasHighLimit())
if(range.inRange(range.getHigh()))
le(propName,range.getHigh());
else
lt(propName,range.getHigh());
}protected abstract void eq(String s, Object obj);
protected abstract void ne(String s, Object obj);
protected abstract void lt(String s, Object obj);
protected abstract void le(String s, Object obj);
protected abstract void gt(String s, Object obj);
protected abstract void ge(String s, Object obj);
protected abstract void between(String s, Object obj, Object obj1);
protected abstract void in(String s, Set set);
protected abstract void like(String s, String s1);
protected abstract void order(String s, boolean flag);
protected abstract void groupBy(String as[]);
protected abstract void properties(String as[]);
protected abstract void sum(String s);
protected abstract void max(String s);
protected abstract void min(String s);
protected abstract void isNull(String s); protected abstract void isNotNull(String propName);
public AbstractCompiler() {
}
}
}