x7.core.bean.CriteriaBuilder Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package x7.core.bean;
import x7.core.bean.Criteria.ResultMappedCriteria;
import x7.core.bean.Criteria.X;
import x7.core.util.BeanMapUtil;
import x7.core.util.BeanUtilX;
import x7.core.util.NumberUtil;
import x7.core.util.StringUtil;
import x7.core.web.Direction;
import x7.core.web.Fetched;
import x7.core.web.MapResult;
import x7.core.web.Paged;
import java.util.*;
import java.util.Map.Entry;
/**
* Standard Query Builder
*
* @author Sim
*/
public class CriteriaBuilder {
private Criteria criteria;
private CriteriaBuilder instance;
public PageBuilder paged() {
return this.pageBuilder;
}
public void paged(Paged paged) {
criteria.paged(paged);
DataPermission.Chain.onBuild(criteria, paged);
}
public ConditionBuilder and() {
X x = new X();
x.setConjunction(Conjunction.AND);
x.setValue(Conjunction.AND);
X current = conditionBuilder.getX();
if (current != null) {
X parent = current.getParent();
if (parent != null) {
List subList = parent.getSubList();
if (subList != null) {
subList.add(x);
x.setParent(parent);
}
} else {
this.criteria.add(x);
}
} else {
this.criteria.add(x);
}
conditionBuilder.under(x);
return conditionBuilder;
}
public ConditionBuilder or() {
X x = new X();
x.setConjunction(Conjunction.OR);
x.setValue(Conjunction.OR);
X current = conditionBuilder.getX();
if (current != null) {
X parent = current.getParent();
if (parent != null) {
List subList = parent.getSubList();
if (subList != null) {
subList.add(x);
x.setParent(parent);
}
} else {
this.criteria.add(x);
}
} else {
this.criteria.add(x);
}
conditionBuilder.under(x);
return conditionBuilder;
}
public CriteriaBuilder endSub() {
X x = new X();
x.setPredicate(Predicate.SUB_END);
x.setValue(Predicate.SUB_END);
X current = conditionBuilder.getX();
X parent = current.getParent();
if (parent != null) {
List subList = parent.getSubList();
if (subList != null) {
subList.add(x);
}
this.conditionBuilder.under(parent);
}
return instance;
}
private PageBuilder pageBuilder = new PageBuilder() {
@Override
public PageBuilder scroll(boolean isScroll) {
criteria.setScroll(isScroll);
return this;
}
@Override
public PageBuilder rows(int rows) {
criteria.setRows(rows);
return this;
}
@Override
public PageBuilder page(int page) {
criteria.setPage(page);
return this;
}
@Override
public PageBuilder orderIn(String porperty, List extends Object> inList) {
if (Objects.nonNull(inList) && inList.size() > 0) {
KV kv = new KV(porperty, inList);
criteria.getFixedSortList().add(kv);
}
return this;
}
@Override
public PageBuilder sort(String orderBy, Direction direction) {
if (StringUtil.isNullOrEmpty(orderBy))
return this;
List sortList = criteria.getSortList();
if (sortList == null){
sortList = new ArrayList<>();
criteria.setSortList(sortList);
}
Sort sort = new Sort(orderBy,direction);
sortList.add(sort);
return this;
}
};
private ConditionBuilder conditionBuilder = new ConditionBuilder() {
private X x = null;
@Override
public X getX() {
return x;
}
@Override
public void under(X x) {
this.x = x;
}
@Override
public CriteriaBuilder eq(String property, Object value) {
if (value == null)
return instance;
if (Objects.nonNull(criteria.getParsed())) {
if (isBaseType_0(property, value))
return instance;
}
if (isNullOrEmpty(value))
return instance;
x.setPredicate(Predicate.EQ);
x.setKey(property);
x.setValue(value);
return instance;
}
@Override
public CriteriaBuilder lt(String property, Object value) {
if (value == null)
return instance;
if (isBaseType_0(property, value))
return instance;
if (isNullOrEmpty(value))
return instance;
x.setPredicate(Predicate.LT);
x.setKey(property);
x.setValue(value);
return instance;
}
@Override
public CriteriaBuilder lte(String property, Object value) {
if (value == null)
return instance;
if (isBaseType_0(property, value))
return instance;
if (isNullOrEmpty(value))
return instance;
x.setPredicate(Predicate.LTE);
x.setKey(property);
x.setValue(value);
return instance;
}
@Override
public CriteriaBuilder gt(String property, Object value) {
if (value == null)
return instance;
if (isBaseType_0(property, value))
return instance;
if (isNullOrEmpty(value))
return instance;
x.setPredicate(Predicate.GT);
x.setKey(property);
x.setValue(value);
return instance;
}
@Override
public CriteriaBuilder gte(String property, Object value) {
if (value == null)
return instance;
if (isBaseType_0(property, value))
return instance;
if (isNullOrEmpty(value))
return instance;
x.setPredicate(Predicate.GTE);
x.setKey(property);
x.setValue(value);
return instance;
}
@Override
public CriteriaBuilder ne(String property, Object value) {
if (value == null)
return instance;
if (isBaseType_0(property, value))
return instance;
if (isNullOrEmpty(value))
return instance;
x.setPredicate(Predicate.NE);
x.setKey(property);
x.setValue(value);
return instance;
}
@Override
public CriteriaBuilder like(String property, String value) {
if (StringUtil.isNullOrEmpty(value))
return instance;
x.setPredicate(Predicate.LIKE);
x.setKey(property);
x.setValue(SqlScript.LIKE_HOLDER + value + SqlScript.LIKE_HOLDER);
return instance;
}
@Override
public CriteriaBuilder likeRight(String property, String value) {
if (StringUtil.isNullOrEmpty(value))
return instance;
x.setPredicate(Predicate.LIKE);
x.setKey(property);
x.setValue(value + SqlScript.LIKE_HOLDER);
return instance;
}
@Override
public CriteriaBuilder notLike(String property, String value) {
if (StringUtil.isNullOrEmpty(value))
return instance;
x.setPredicate(Predicate.NOT_LIKE);
x.setKey(property);
x.setValue(SqlScript.LIKE_HOLDER + value + SqlScript.LIKE_HOLDER);
return instance;
}
@Override
public CriteriaBuilder between(String property, Object min, Object max) {
if (min == null || max == null)
return instance;
if (isBaseType_0(property, max))
return instance;
if (isNullOrEmpty(max))
return instance;
if (isNullOrEmpty(min))
return instance;
MinMax minMax = new MinMax();
minMax.setMin(min);
minMax.setMax(max);
x.setPredicate(Predicate.BETWEEN);
x.setKey(property);
x.setValue(minMax);
return instance;
}
@Override
public CriteriaBuilder in(String property, List extends Object> list) {
if (list == null || list.isEmpty())
return instance;
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy