io.crnk.jpa.internal.query.AbstractJpaQueryImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of crnk-jpa Show documentation
Show all versions of crnk-jpa Show documentation
JSON API framework for Java
package io.crnk.jpa.internal.query;
import io.crnk.core.queryspec.*;
import io.crnk.jpa.internal.query.backend.JpaQueryBackend;
import io.crnk.jpa.query.JpaQuery;
import io.crnk.meta.model.MetaAttribute;
import io.crnk.meta.model.MetaAttributePath;
import io.crnk.meta.model.MetaDataObject;
import io.crnk.meta.provider.MetaPartition;
import javax.persistence.EntityManager;
import javax.persistence.criteria.JoinType;
import java.util.*;
public abstract class AbstractJpaQueryImpl> implements JpaQuery {
protected final EntityManager em;
protected final MetaDataObject meta;
protected final Class clazz;
protected final Map joinTypes = new HashMap<>();
protected JoinType defaultJoinType = JoinType.INNER;
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 MetaDataObject parentMeta;
protected List> parentIds;
protected MetaAttribute parentAttr;
protected boolean parentIdSelection;
private ComputedAttributeRegistryImpl computedAttrs;
private Object privateData;
protected AbstractJpaQueryImpl(MetaPartition metaPartition, EntityManager em, Class clazz,
ComputedAttributeRegistryImpl computedAttrs) {
this.em = em;
this.clazz = clazz;
this.meta = (MetaDataObject) metaPartition.getMeta(clazz);
this.computedAttrs = computedAttrs;
}
@SuppressWarnings("unchecked")
public AbstractJpaQueryImpl(MetaPartition metaPartition, EntityManager em, Class> entityClass,
ComputedAttributeRegistryImpl virtualAttrs, String attrName, List> entityIds) {
this.em = em;
this.computedAttrs = virtualAttrs;
this.parentMeta = (MetaDataObject) metaPartition.getMeta(entityClass);
MetaAttribute attrMeta = parentMeta.getAttribute(attrName);
if (attrMeta.getType().isCollection()) {
this.meta = (MetaDataObject) attrMeta.getType().asCollection().getElementType();
} else {
this.meta = (MetaDataObject) attrMeta.getType();
}
this.clazz = (Class) meta.getImplementationClass();
this.parentAttr = attrMeta;
this.parentIds = entityIds;
}
public Object getPrivateData() {
return privateData;
}
public void setPrivateData(Object privateData) {
this.privateData = privateData;
}
@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;
}
public MetaDataObject getParentMeta() {
return parentMeta;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy