io.github.fsixteen.data.jpa.base.generator.plugins.ComparableBuilderPlugin Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of data-jpa-base-generator-interpreter Show documentation
Show all versions of data-jpa-base-generator-interpreter Show documentation
基于类快速生成Jpa Specification的框架工具中的注解解释器内容.
The newest version!
package io.github.fsixteen.data.jpa.base.generator.plugins;
import java.lang.annotation.Annotation;
import java.util.Objects;
import java.util.Optional;
import javax.persistence.criteria.AbstractQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.github.fsixteen.data.jpa.base.generator.plugins.constant.ComparableType;
import io.github.fsixteen.data.jpa.base.generator.plugins.descriptors.AnnotationDescriptor;
import io.github.fsixteen.data.jpa.base.generator.plugins.descriptors.ComputerDescriptor;
/**
* 有关{@link java.lang.Comparable}类型计算内容的注解解释器.
*
* @author FSixteen
* @since 1.0.0
*/
public class ComparableBuilderPlugin extends AbstractComputerBuilderPlugin {
private static final Logger log = LoggerFactory.getLogger(ComparableBuilderPlugin.class);
private ComparableType type = ComparableType.GT;
public ComparableBuilderPlugin(ComparableType type) {
this.type = type;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public ComputerDescriptor toPredicate(AnnotationDescriptor ad, Object obj, Root root, AbstractQuery query,
CriteriaBuilder cb) {
try {
Object fieldValue = this.trimIfPresent(ad, ad.getValueFieldPd().getReadMethod().invoke(obj));
if (ad.isRequired() && Objects.isNull(fieldValue)) {
return this.toNullValuePredicate(ad, root, cb);
}
if (ad.isIgnoreNull() && Objects.isNull(fieldValue)) {
return null;
}
if (ad.isIgnoreEmpty() && String.class.isInstance(fieldValue) && String.class.cast(fieldValue).isEmpty()) {
return null;
}
if (ad.isIgnoreBlank() && String.class.isInstance(fieldValue) && String.class.cast(fieldValue).trim().isEmpty()) {
return null;
}
Optional optional = Optional.ofNullable(fieldValue)
// 值类型判断
.filter(it -> {
switch (ad.getValueType()) {
case VALUE:
return Comparable.class.isInstance(it)
&& root.getModel().getAttribute(ad.getComputerFieldName()).getJavaType() == ad.getValueField().getType();
case COLUMN:
return String.class.isInstance(it);
case FUNCTION:
return this.checkValueProcessor(ad);
default:
return false;
}
})
// 值转换
.map(it -> {
switch (ad.getValueType()) {
case VALUE:
return cb.>literal(Comparable.class.cast(it));
case COLUMN:
return root.>get(String.class.cast(it));
case FUNCTION:
try {
return this.>applyValueProcessor(ad, obj, it, root, query, cb);
} catch (ReflectiveOperationException e) {
log.error(e.getMessage(), e);
return null;
}
default:
return null;
}
})
// Predicate创建
.map(it -> {
switch (this.type) {
case GT:
return cb.greaterThan(root.get(ad.getComputerFieldName()), it);
case GTE:
return cb.greaterThanOrEqualTo(root.get(ad.getComputerFieldName()), it);
case LT:
return cb.lessThan(root.get(ad.getComputerFieldName()), it);
case LTE:
return cb.lessThanOrEqualTo(root.get(ad.getComputerFieldName()), it);
case EQ:
default:
return cb.equal(root.get(ad.getComputerFieldName()), it);
}
});
if (optional.isPresent()) {
return ComputerDescriptor.of(ad, this.logicReverse(ad, optional.get(), cb));
} else {
this.printWarn(ad, root);
}
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
log.error(e.getMessage(), e);
}
return null;
}
}