java常用組件--Hibernate QueryParameters

/**
*
*/
package cn.ccb.jstsccf.common.dao.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
* @author ghl
*
*/
public class QueryParameters {
private List list;

public QueryParameters() {
list = new ArrayList(3);
}

public QueryParameters add(String propName, Object propValue) {
list.add(new Paramter(propName, propValue, null));
return this;
}

public QueryParameters addOrder(String propName) {
return addOrder(propName, true);
}

public QueryParameters addOrder(String propName, boolean ascending) {
list.add(new Order(propName, ascending, null));
return this;
}

public void removeOrderByName(String propName) {
if (propName == null)
return;
for (int i = 0; i < list.size(); i++) {
Object obj = list.get(i);
if (obj instanceof Order) {
Order order = (Order) obj;
if (propName.equals(order.propName))
list.remove(obj);
}
}

}

public void removeAllOrder() {
for (int i = 0; i < list.size(); i++) {
Object obj = list.get(i);
if (obj instanceof Order)
list.remove(obj);
}

}

public QueryParameters groupby(String propNames[]) {
list.add(new GroupBy(propNames, null));
return this;
}

public QueryParameters addProperties(String properties[]) {
list.add(new Properties(properties, null));
return this;
}

public QueryParameters sum(String propName) {
list.add(new Sum(propName, null));
return this;
}

public QueryParameters max(String propName) {
list.add(new Max(propName, null));
return this;
}

public QueryParameters min(String propName) {
list.add(new Min(propName, null));
return this;
}

public QueryParameters isNull(String propName) {
list.add(new Null(propName));
return this;
}

public QueryParameters isNotNull(String propName) {
list.add(new NotNull(propName));
return this;
}

public QueryParameters notEquals(String propName, Object propValue) {
list.add(new NotEquals(propName, propValue));
return this;
}

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;

public String getPropName() {
return propName;
}

public Object getPropValue() {
return propValue;
}

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() {
return pattern;
}

private String pattern;

Pattern(String text) {
pattern = text;
}
}

public static class Null {

public String getPropName() {
return propName;
}

public void setPropName(String propName) {
this.propName = propName;
}

private String propName;

Null(String propName) {
this.propName = propName;
}
}

public static class NotNull {

public String getPropName() {
return propName;
}

public void setPropName(String propName) {
this.propName = propName;
}

private String propName;

NotNull(String propName) {
this.propName = propName;
}
}

public static class NotEquals{
private String propName;

private Object propValue;

public String getPropName() {
return propName;
}

public Object getPropValue() {
return propValue;
}

public NotEquals(String propName, Object propValue) {
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 (Iterator iterator = params.list.iterator(); iterator.hasNext();) {
Object e = iterator.next();
if (e instanceof Paramter) {
Paramter p = (Paramter) e;
Object propValue = p.getPropValue();
if (propValue instanceof Range)
range(p.getPropName(), (Range) propValue);
else if (propValue instanceof Pattern)
like(p.getPropName(), ((Pattern) propValue)
.getPattern());
else if (propValue instanceof Set)
in(p.getPropName(), (Set) propValue);
else
eq(p.getPropName(), propValue);
} else if (e instanceof Properties) {
Properties p = (Properties) e;
properties(p.properties);
} else if (e instanceof GroupBy) {
GroupBy g = (GroupBy) e;
groupBy(g.propNames);
} else if (e instanceof Order) {
Order o = (Order) e;
order(o.propName, o.ascending);
} else if (e instanceof Sum) {
Sum s = (Sum) e;
sum(s.propName);
} else if (e instanceof Max) {
Max m = (Max) e;
max(m.propName);
} else if (e instanceof Min) {
Min m = (Min) e;
min(m.propName);
} else if (e instanceof Null) {
Null m = (Null) e;
isNull(m.propName);
}else if (e instanceof NotNull) {
NotNull m = (NotNull) e;
isNotNull(m.propName);
}else if (e instanceof NotEquals) {
NotEquals ne = (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() {
}
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章