io.katharsis.jpa.internal.query.AbstractJpaQueryImpl Maven / Gradle / Ivy
package io.katharsis.jpa.internal.query;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.criteria.JoinType;
import io.katharsis.jpa.internal.meta.MetaAttribute;
import io.katharsis.jpa.internal.meta.MetaAttributePath;
import io.katharsis.jpa.internal.meta.MetaDataObject;
import io.katharsis.jpa.internal.meta.MetaLookup;
import io.katharsis.jpa.internal.query.backend.JpaQueryBackend;
import io.katharsis.jpa.query.JpaQuery;
import io.katharsis.queryspec.Direction;
import io.katharsis.queryspec.FilterOperator;
import io.katharsis.queryspec.FilterSpec;
import io.katharsis.queryspec.IncludeFieldSpec;
import io.katharsis.queryspec.SortSpec;
public abstract class AbstractJpaQueryImpl> implements JpaQuery {
protected final EntityManager em;
protected final MetaDataObject meta;
protected final Class clazz;
protected JoinType defaultJoinType = JoinType.INNER;
protected final Map joinTypes = new HashMap<>();
protected ArrayList filterSpecs = new ArrayList<>();
protected ArrayList sortSpecs = new ArrayList<>();
protected ArrayList includedFields = new ArrayList<>();
protected boolean autoDistinct = true;
protected boolean autoGroupBy = false;
protected boolean distinct = false;
protected boolean ensureTotalOrder = true;
protected Class> parentEntityClass;
protected List> parentIds;
protected MetaAttribute parentAttr;
protected boolean parentIdSelection;
private ComputedAttributeRegistryImpl computedAttrs;
protected AbstractJpaQueryImpl(MetaLookup metaLookup, EntityManager em, Class clazz,
ComputedAttributeRegistryImpl computedAttrs) {
this.em = em;
this.clazz = clazz;
this.meta = metaLookup.getMeta(clazz).asDataObject();
this.computedAttrs = computedAttrs;
}
@SuppressWarnings("unchecked")
public AbstractJpaQueryImpl(MetaLookup metaLookup, EntityManager em, Class> entityClass,
ComputedAttributeRegistryImpl virtualAttrs, String attrName, List> entityIds) {
this.em = em;
this.computedAttrs = virtualAttrs;
MetaDataObject parentMeta = metaLookup.getMeta(entityClass).asDataObject();
MetaAttribute attrMeta = parentMeta.getAttribute(attrName);
if (attrMeta.getType().isCollection()) {
this.meta = attrMeta.getType().asCollection().getElementType().asEntity();
}
else {
this.meta = attrMeta.getType().asEntity();
}
this.clazz = (Class) meta.getImplementationClass();
this.parentEntityClass = entityClass;
this.parentAttr = attrMeta;
this.parentIds = entityIds;
}
@Override
public void addParentIdSelection() {
this.parentIdSelection = true;
}
@Override
public void addSelection(List path) {
includedFields.add(new IncludeFieldSpec(path));
}
@Override
public JpaQuery setEnsureTotalOrder(boolean ensureTotalOrder) {
this.ensureTotalOrder = ensureTotalOrder;
return this;
}
@Override
public JpaQuery addFilter(FilterSpec filters) {
this.filterSpecs.add(filters);
return this;
}
@Override
public JpaQuery addSortBy(List attributePath, Direction dir) {
this.sortSpecs.add(new SortSpec(attributePath, dir));
return this;
}
@Override
public JpaQuery addSortBy(SortSpec order) {
this.sortSpecs.add(order);
return this;
}
@Override
public JpaQuery setDefaultJoinType(JoinType joinType) {
this.defaultJoinType = joinType;
return this;
}
@Override
public JpaQuery setJoinType(List path, JoinType joinType) {
joinTypes.put(meta.resolvePath(path), joinType);
return this;
}
@Override
public JpaQuery setAutoGroupBy(boolean autoGroupBy) {
this.autoGroupBy = autoGroupBy;
return this;
}
@Override
public JpaQuery setDistinct(boolean distinct) {
this.autoDistinct = false;
this.distinct = distinct;
return this;
}
@Override
public JpaQuery addFilter(String attrPath, FilterOperator filterOperator, Object value) {
return addFilter(Arrays.asList(attrPath.split("\\.")), filterOperator, value);
}
@Override
public JpaQuery addFilter(List attrPath, FilterOperator filterOperator, Object value) {
addFilter(new FilterSpec(attrPath, filterOperator, value));
return this;
}
public List getSortSpecs() {
return sortSpecs;
}
public boolean getEnsureTotalOrder() {
return ensureTotalOrder;
}
public List getIncludedFields() {
return includedFields;
}
public JoinType getJoinType(MetaAttributePath path) {
JoinType joinType = joinTypes.get(path);
if (joinType == null)
joinType = defaultJoinType;
return joinType;
}
public ComputedAttributeRegistryImpl getComputedAttrs() {
return computedAttrs;
}
public MetaDataObject getMeta() {
return meta;
}
@Override
public Class getEntityClass() {
return clazz;
}
@Override
public AbstractQueryExecutorImpl buildExecutor() {
B backend = newBackend();
@SuppressWarnings({ "rawtypes", "unchecked" })
QueryBuilder executorFactory = new QueryBuilder(this, backend);
Map selectionBindings = executorFactory.applySelectionSpec();
executorFactory.applyFilterSpec();
executorFactory.applySortSpec();
int numAutoSelections = executorFactory.applyDistinct();
return newExecutor(backend, numAutoSelections, selectionBindings);
}
protected abstract AbstractQueryExecutorImpl newExecutor(B ctx, int numAutoSelections,
Map selectionBindings);
protected abstract B newBackend();
@SuppressWarnings({ "unchecked", "hiding" })
public List getParentIds() {
return (List) parentIds;
}
public List getFilterSpecs() {
return filterSpecs;
}
public MetaAttribute getParentAttr() {
return parentAttr;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy