com.app.common.condition.Condition Maven / Gradle / Ivy
The newest version!
package com.app.common.condition;
import java.util.ArrayList;
import java.util.List;
public class Condition {
private String tableName;
private List conditionInfos = new ArrayList();
private List orderInfos = new ArrayList();
private boolean page = false;
private int pageIndex = 1;
private int pageSize = 50;
public boolean isPage() {
return page;
}
public void setPage(boolean page) {
this.page = page;
}
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public String getTableName() {
return tableName;
}
/**
* 如果是Ibaits方式,该名字为Ibatis转换的Po名字
*
* @param tableName
*/
public void setTableName(String tableName) {
this.tableName = tableName;
}
public List getOrderInfos() {
return orderInfos;
}
public void setOrderInfos(List orderInfos) {
this.orderInfos = orderInfos;
}
public List getConditionInfos() {
return processconditionInfo(conditionInfos);
}
public void setConditionInfos(List conditionInfos) {
this.conditionInfos = conditionInfos;
}
public Condition AndCondition(ConditionInfo conditionInfo) {
if (conditionInfo != null) {
conditionInfo.setConditionLogic(ConditionLogic.And);
conditionInfos.add(conditionInfo);
}
return this;
}
public Condition AndCondition(String fieldName, Object fieldValue,
ConditionOperator queryOperator) {
AndCondition(fieldName, fieldValue, queryOperator, false);
return this;
}
public Condition AndCondition(String fieldName, Object fieldValue,
ConditionOperator queryOperator, boolean excludeIfEmpty) {
ConditionInfo conditionInfo = new ConditionInfo(fieldName, fieldValue,
queryOperator, excludeIfEmpty);
AndCondition(conditionInfo);
return this;
}
public Condition OrCondition(ConditionInfo conditionInfo) {
if (conditionInfo != null) {
conditionInfo.setConditionLogic(ConditionLogic.Or);
conditionInfos.add(conditionInfo);
}
return this;
}
public Condition OrCondition(String fieldName, Object fieldValue,
ConditionOperator queryOperator, boolean excludeIfEmpty) {
ConditionInfo conditionInfo = new ConditionInfo(fieldName, fieldValue,
queryOperator, excludeIfEmpty);
AndCondition(conditionInfo);
return this;
}
public Condition OrCondition(String fieldName, Object fieldValue,
ConditionOperator queryOperator) {
OrCondition(fieldName, fieldValue, queryOperator, false);
return this;
}
public Object BuildCondition() throws Exception {
Object o = BuildCondition(BuildConditionType.Ibatis);
return o;
}
public Object BuildCondition(BuildConditionType buildConditionType)
throws Exception {
Object o = null;
IBuildCondition buildCondition = BuildConditionFactory
.CreateBuildCondition(buildConditionType);
o = buildCondition.Build(this);
return o;
}
private List processconditionInfo(
List conditionInfos) {
List lt = new ArrayList();
for (ConditionInfo conditionInfo : conditionInfos) {
if (!conditionInfo.isExcludeIfEmpty()) {
if (conditionInfo.getFieldValue() != null) {
Class> c = conditionInfo.getFieldValue().getClass();
if (c.isAssignableFrom(ArrayList.class)) {
@SuppressWarnings("rawtypes")
List ltt = (List) conditionInfo.getFieldValue();
if (ltt.size() > 0) {
lt.add(conditionInfo);
}
} else {
if ((conditionInfo.getFieldValue() + "").length() > 0) {
lt.add(conditionInfo);
}
}
}
} else {
lt.add(conditionInfo);
}
}
return lt;
}
}