All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
foundation.jpa.querydsl.spring.impl.SearchEngineImpl Maven / Gradle / Ivy
package foundation.jpa.querydsl.spring.impl;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.EntityPath;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import foundation.jpa.querydsl.QueryContext;
import foundation.jpa.querydsl.QueryVariables;
import foundation.jpa.querydsl.spring.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import javax.persistence.EntityManager;
public class SearchEngineImpl implements SearchEngine {
private final JPAQueryFactory queryFactory;
private final QueryContext queryContext;
public SearchEngineImpl(JPAQueryFactory queryFactory, QueryContext queryContext) {
this.queryFactory = queryFactory;
this.queryContext = queryContext;
}
public SearchEngineImpl(EntityManager entityManager) {
this(new JPAQueryFactory(entityManager), new JpaQueryContext(entityManager));
}
@Override
public SearchResult search(SearchCriteria extends EntityPath> criteria, QueryVariables variables) {
return search(criteria, variables, queryFactory.selectFrom(criteria.getEntityPath()));
}
@Override
public SearchResult search(Predicate implicitPredicate, SearchCriteria extends EntityPath> criteria, QueryVariables variables) {
return search(criteria, variables, queryFactory.selectFrom(criteria.getEntityPath()).where(implicitPredicate));
}
@Override
public SearchResult search(EntityPath entityPath, String query, String sort, Pageable pageable, QueryVariables variables) {
return search(new SearchCriteriaImpl<>("query", query, sort, pageable, entityPath), variables);
}
@Override
public SearchResult aggregate(AggregateCriteria extends EntityPath> criteria, QueryVariables variables) {
try {
JPAQuery query = queryFactory.selectFrom(criteria.getEntityPath())
.where(queryContext.parsePredicate(criteria.getEntityPath(), criteria.getQuery(), variables))
.orderBy(queryContext.parseOrderSpecifier(criteria.getEntityPath(), criteria.getSort()))
.groupBy(queryContext.parseSelect(criteria.getEntityPath(), criteria.groupBy()))
.select(queryContext.parseSelect(criteria.getEntityPath(), criteria.select()))
.offset(criteria.getPageable().getOffset())
.limit(criteria.getPageable().getPageSize());
return new AggregationResultImpl(new PageImpl<>(query.fetch(), criteria.getPageable(), query.fetchCount()), null);
} catch (Throwable e) {
return new AggregationResultImpl(Page.empty(), e);
}
}
private SearchResult search(SearchCriteria extends EntityPath> criteria, QueryVariables variables, JPAQuery jpaQuery) {
try {
JPAQuery query = jpaQuery.where(queryContext.parsePredicate(criteria.getEntityPath(), criteria.getQuery(), variables))
.orderBy(queryContext.parseOrderSpecifier(criteria.getEntityPath(), criteria.getSort()))
.offset(criteria.getPageable().getOffset())
.limit(criteria.getPageable().getPageSize());
return new SearchResultImpl<>(criteria, new PageImpl<>(query.fetch(), criteria.getPageable(), query.fetchCount()), null);
} catch (Throwable e) {
return new SearchResultImpl<>(criteria, Page.empty(), e);
}
}
}