Java Code Examples for org.springframework.data.jpa.domain.Specifications

The following examples show how to use org.springframework.data.jpa.domain.Specifications. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public Iterable<T> findAllActive(Iterable<ID> ids) {
	if (ids == null || !ids.iterator().hasNext())
		return Collections.emptyList();

	if (entityInformation.hasCompositeId()) {
		List<T> results = new ArrayList<T>();

		for (ID id : ids)
			results.add(findOneActive(id));

		return results;
	}

	ByIdsSpecification<T> specification = new ByIdsSpecification<T>(entityInformation);
	TypedQuery<T> query = getQuery(Specifications.where(specification).and(notDeleted()), (Sort) null);

	return query.setParameter(specification.parameter, ids).getResultList();
}
 
Example 2
/**
 * @name 搜索资源
 * @param resourceType 资源类型
 * @param resourceName 资源名称
 * @param page 分页的页码
 * @param size 每页的数量
 * @return
 * @throws Exception
 */
public Object search(String resourceType,
                     String resourceName,
                     int page,
                     int size) throws Exception {
    addResourcesAuto();
    Sort sort = new Sort(Sort.Direction.DESC, "id");
    PageRequest pageRequest = new PageRequest(page, size, sort);
    Specifications<Resource> conditions = null;
    if (StringUtils.isNotBlank(resourceName)) {
        if (conditions == null)
            conditions = Specifications.where(SpecificationFactory.containsLike("resource_name", resourceName));
        else
            conditions = conditions.and(SpecificationFactory.containsLike("resource_name", resourceName));
    }
    if (StringUtils.isNotBlank(resourceType)) {
        if (conditions == null)
            conditions = Specifications.where(SpecificationFactory.containsLike("resource_type", resourceType));
        else
            conditions = conditions.and(SpecificationFactory.containsLike("resource_type", resourceType));
    }
    Page<Resource> page1 = null;
    if (conditions == null)
        page1 = resourceRepository.findAll(pageRequest);
    else
        page1 = resourceRepository.findAll(conditions, pageRequest);
    return page1;
}
 
Example 3
Source Project: springboot-vue.js-bbs   Source File: BoardService.java    License: Apache License 2.0 5 votes vote down vote up
@Transactional(readOnly = true)
public Page<Board> findAll(Pageable pageable, Pagination pagination) {
    if (pagination.getKeyword() == null) {
        return boardRepository.findAll(pageable);
    }

    String keyword = pagination.getKeyword();
    return (pagination.filterMatcher(Pagination.FilterType.ALL)) ?
            boardRepository.findAll(Specifications.where(BoardSpecification.findByAll(keyword)), pageable) :
            boardRepository.findAll(Specifications.where(BoardSpecification.findByFilter(pagination)), pageable);
}
 
Example 4
Source Project: mojito   Source File: AssetService.java    License: Apache License 2.0 5 votes vote down vote up
public List<Asset> findAll(Long repositoryId, String path, Boolean deleted, Boolean virtual, Long branchId) {

        logger.debug("Find all assets for repositoryId: {}, path: {}, deleted: {}, virtual: {}, branchId: {}",
                repositoryId, path, deleted, virtual, branchId);

        Specifications<Asset> assetSpecifications = distinct(ifParamNotNull(repositoryIdEquals(repositoryId)))
                .and(ifParamNotNull(pathEquals(path)))
                .and(ifParamNotNull(deletedEquals(deleted)))
                .and(ifParamNotNull(virtualEquals(virtual)))
                .and(ifParamNotNull(branchId(branchId, deleted)));

        List<Asset> all = assetRepository.findAll(assetSpecifications);
        return all;
    }
 
Example 5
public Page<T> findAll(Pageable pageable, SearchForm searchForm, User user, AclClassName aclClassName, PermissionName permissionName) {
    Specification<T> securedSpecification = SecuredSpecifications.getSpecification(user, aclClassName, permissionName);
    Specification<T> searchFormSpecification = SearchFormSpecifications.getSpecification(searchForm);
    Specification<T> specification = AndSpecifications.getSpecification(searchFormSpecification, securedSpecification);

    return (Page) (null == pageable ? new PageImpl(this.findAll()) : this.findAll(Specifications.where(specification), pageable));
}
 
Example 6
public List<T> findAll(SearchForm searchForm, User user, AclClassName aclClassName, PermissionName permissionName) {
    Specification<T> securedSpecification = SecuredSpecifications.getSpecification(user, aclClassName, permissionName);
    Specification<T> searchFormSpecification = SearchFormSpecifications.getSpecification(searchForm);
    Specification<T> specification = AndSpecifications.getSpecification(searchFormSpecification, securedSpecification);

    return this.findAll(Specifications.where(specification));
}
 
Example 7
@Override
public ChartIndex getChartIndex(Index index, ChartType type,
		ChartHistoSize histoSize, ChartHistoMovingAverage histoAverage,
		ChartHistoTimeSpan histoPeriod, Integer intradayWidth,
		Integer intradayHeight) {
	
	log.debug("getChartIndex("+index+", "+type+", "+histoSize+", "+histoAverage+", "+histoPeriod+", "+intradayWidth+", "+intradayHeight+")");
	
	Specification<ChartIndex> spec = new ChartSpecifications<ChartIndex>().typeEquals(type);
	
	if(type.equals(ChartType.HISTO)){
		if(histoSize != null){
			spec = Specifications.where(spec).and(new ChartSpecifications<ChartIndex>().sizeEquals(histoSize));
		}
		if(histoAverage != null){
			spec = Specifications.where(spec).and(new ChartSpecifications<ChartIndex>().histoMovingAverageEquals(histoAverage));
		}
		if(histoPeriod != null){
			spec = Specifications.where(spec).and(new ChartSpecifications<ChartIndex>().histoTimeSpanEquals(histoPeriod));
		}
	}
	else{
		if(intradayWidth != null){
			spec = Specifications.where(spec).and(new ChartSpecifications<ChartIndex>().intradayWidthEquals(intradayWidth));
		}
		if(intradayHeight != null){
			spec = Specifications.where(spec).and(new ChartSpecifications<ChartIndex>().intradayHeightEquals(intradayHeight));
		}
	}
	
	spec = Specifications.where(spec).and(new ChartSpecifications<ChartIndex>().indexEquals(index));
	log.debug("DB CALL");

	return chartIndexRepository.findAll(spec,new Sort(Direction.DESC,"id")).stream().findFirst().orElse(null);
}
 
Example 8
@Override
public Page<StockProduct> get(String indexId, String exchangeId, MarketId marketId, String startWith, Specification<StockProduct> spec, Pageable pageable, boolean validResults) {
	if(!StringUtils.isEmpty(indexId)){
		Index index = indexRepository.findOne(indexId);
		if(index == null){
			throw new NoResultException("No result found for the index ID "+indexId+" !");
		}
		return stockProductRepository.findByIndices(index, pageable);
	}

	if(!StringUtils.isEmpty(startWith)){
		spec = Specifications.where(spec).and(new ProductSpecifications<StockProduct>().nameStartsWith(startWith));
	}
	
	if(marketId != null){
		Market market = marketRepository.findOne(marketId);
		if(market == null){
			throw new NoResultException("No result found for the market ID "+marketId+" !");
		}
		spec = Specifications.where(spec).and(new ProductSpecifications<StockProduct>().marketIdEquals(marketId));
	}
	
	if(!StringUtils.isEmpty(exchangeId)){
		spec = Specifications.where(spec).and(new ProductSpecifications<StockProduct>().exchangeIdEquals(exchangeId));
	}
	
	spec = Specifications.where(spec)
			.and(new ProductSpecifications<StockProduct>().nameNotNull())
			.and(new ProductSpecifications<StockProduct>().exchangeNotNull());

	if(validResults){
		spec = Specifications.where(spec)
				.and(new ProductSpecifications<StockProduct>().currencyNotNull())
				.and(new ProductSpecifications<StockProduct>().hasAPrice());
	}
	
	return stockProductRepository.findAll(spec, pageable);
}
 
Example 9
@Override
public ChartStock getChartStock(StockProduct index, ChartType type,
		ChartHistoSize histoSize, ChartHistoMovingAverage histoAverage,
		ChartHistoTimeSpan histoPeriod, Integer intradayWidth,
		Integer intradayHeight) {
	
	Specification<ChartStock> spec = new ChartSpecifications<ChartStock>().typeEquals(type);
	
	if(type.equals(ChartType.HISTO)){
		if(histoSize != null){
			spec = Specifications.where(spec).and(new ChartSpecifications<ChartStock>().sizeEquals(histoSize));
		}
		if(histoAverage != null){
			spec = Specifications.where(spec).and(new ChartSpecifications<ChartStock>().histoMovingAverageEquals(histoAverage));
		}
		if(histoPeriod != null){
			spec = Specifications.where(spec).and(new ChartSpecifications<ChartStock>().histoTimeSpanEquals(histoPeriod));
		}
	}
	else{
		if(intradayWidth != null){
			spec = Specifications.where(spec).and(new ChartSpecifications<ChartStock>().intradayWidthEquals(intradayWidth));
		}
		if(intradayHeight != null){
			spec = Specifications.where(spec).and(new ChartSpecifications<ChartStock>().intradayHeightEquals(intradayHeight));
		}
	}
	
	spec = Specifications.where(spec).and(new ChartSpecifications<ChartStock>().indexEquals(index));
	return chartStockRepository.findAll(spec,new Sort(Direction.DESC,"id")).stream().findFirst().orElse(null);
}
 
Example 10
@Override
public long countByExample(final T entity, final SearchParameters sp) {
    Validate.notNull(entity, "The entity cannot be null");
    if (sp.hasNamedQuery()) {
        return getNamedQueryUtil().numberByNamedQuery(sp).intValue();
    }
    Specifications<T> spec = Specifications.where(byExampleSpecification.byExampleOnEntity(entity, sp));
    spec = RangeSpecification.andRangeIfSet(spec, sp.getRanges());
    spec = PropertySelectorSpecification.andPropertySelectorIfSet(spec, sp);
    return super.count(spec);
}
 
Example 11
@Override
public Page<T> findByExample(final T example, final List<Range<?, ?>> ranges, final Pageable pageable) {
    SearchParameters searchParameter = new SearchParameters();
    Specifications<T> spec = Specifications.where(byExampleSpecification.byExampleOnEntity(example,
            searchParameter));
    spec = RangeSpecification.andRangeIfSet(spec, ranges);
    spec = PropertySelectorSpecification.andPropertySelectorIfSet(spec, searchParameter);
    return findAll(spec, pageable);
}
 
Example 12
@Override
public Page<T> findByExample(final T example, final Pageable pageable) {
    SearchParameters searchParameter = new SearchParameters();
    Specifications<T> spec = Specifications.where(byExampleSpecification.byExampleOnEntity(example,
            searchParameter));
    spec = RangeSpecification.andRangeIfSet(spec, searchParameter.getRanges());
    spec = PropertySelectorSpecification.andPropertySelectorIfSet(spec, searchParameter);
    return findAll(spec, pageable);
}
 
Example 13
@Override
public List<T> findByExample(final T entity, final SearchParameters searchParameter) {
    Assert.notNull(searchParameter, "Search parameters required");
    if (searchParameter.hasNamedQuery()) {
        return getNamedQueryUtil().findByNamedQuery(searchParameter);
    }
    Specifications<T> spec = Specifications.where(byExampleSpecification.byExampleOnEntity(entity,
            searchParameter));
    spec = RangeSpecification.andRangeIfSet(spec, searchParameter.getRanges());
    spec = PropertySelectorSpecification.andPropertySelectorIfSet(spec, searchParameter);
    return findAll(spec);
}
 
Example 14
@Override
public Optional<T> findOneByExample(final T entity, final SearchParameters searchParameter) {
    Assert.notNull(searchParameter, "Search parameters required");
    Specifications<T> spec = Specifications.where(byExampleSpecification.byExampleOnEntity(entity,
            searchParameter));
    spec = RangeSpecification.andRangeIfSet(spec, searchParameter.getRanges());
    spec = PropertySelectorSpecification.andPropertySelectorIfSet(spec, searchParameter);
    return Optional.ofNullable(super.findOne(spec));
}
 
Example 15
public static <E,D extends Comparable<? super D>> Specifications<E> andRangeIfSet(Specifications<E> specifications, final List<Range<?, ?>> ranges) {
    for (Range<?, ?> r : ranges) {
        if (r.isSet()) {
            Range<E, D> range = (Range<E, D>) r;
            specifications = specifications.and(toSpecification(range));
        }
    }
    return specifications;
}
 
Example 16
Source Project: docs-manage   Source File: BaseRepositoryImpl.java    License: MIT License 4 votes vote down vote up
@Override
public Page<E> findByExampleWithRange(ExampleNode<E> exampleNode, List<Range<E>> ranges, Pageable pageable) {
    Specification<E> exampleSpecification = new ExampleSpecification<>(exampleNode);
    Specification<E> rangesSpecification = new RangeSpecification<>(ranges);
    return findAll(Specifications.where(exampleSpecification).and(rangesSpecification), pageable);
}
 
Example 17
public JpaDynamicSpecificationBuilder() {
    this.specification = Specifications.where(null);
}
 
Example 18
public JpaDynamicSpecificationBuilder and(final Iterable<SearchFilter> searchFilters) {
    for (SearchFilter filter : searchFilters)
        this.specification = Specifications.where(this.specification).and(bySearchFilter(filter));
    return this;
}
 
Example 19
public JpaDynamicSpecificationBuilder and(final Specification customSpecification) {
    this.specification = Specifications.where(this.specification).and(customSpecification);
    return this;
}
 
Example 20
public JpaDynamicSpecificationBuilder and(final Collection<Specification> customSpecifications) {
    for (Specification specification : customSpecifications)
        this.specification = Specifications.where(this.specification).and(specification);
    return this;
}
 
Example 21
public JpaDynamicSpecificationBuilder or(final Iterable<SearchFilter> searchFilters) {
    for (SearchFilter filter : searchFilters)
        this.specification = Specifications.where(this.specification).or(bySearchFilter(filter));
    return this;
}
 
Example 22
public JpaDynamicSpecificationBuilder or(final Specification customSpecification) {
    this.specification = Specifications.where(this.specification).or(customSpecification);
    return this;
}
 
Example 23
public JpaDynamicSpecificationBuilder or(final Collection<Specification> customSpecifications) {
    for (Specification specification : customSpecifications)
        this.specification = Specifications.where(this.specification).or(specification);
    return this;
}
 
Example 24
@Override
@SuppressWarnings("unchecked")
protected <S extends T> TypedQuery<Long> getCountQuery(Specification<S> spec,
    Class<S> domainClass) {
  return super.getCountQuery(((Specifications<S>)aclJpaSpec()).and(spec), domainClass);
}
 
Example 25
@Override
@SuppressWarnings("unchecked")
protected <S extends T> TypedQuery<S> getQuery(Specification<S> spec, Class<S> domainClass,
      Sort sort) {
  return super.getQuery(((Specifications<S>) aclJpaSpec()).and(spec), domainClass, sort);
}
 
Example 26
private Specifications<T> aclJpaSpec() {
  Specification<T> spec = jpaSpecProvider.jpaSpecFor(getDomainClass());
  logger.debug("Using ACL JPA specification for objects '{}': {}",
      getDomainClass().getSimpleName(), spec);
  return where(spec);
}
 
Example 27
@Override
public T findOneActive(ID id) {
	return super.findOne(
			Specifications.where(new ByIdSpecification<T, ID>(entityInformation, id)).and(notDeleted()));
}
 
Example 28
private static final <T> Specification<T> notDeleted() {
	return Specifications.where(new DeletedIsNull<T>()).or(new DeletedTimeGreatherThanNow<T>());
}
 
Example 29
public Page<T> findAll(Pageable pageable, User user, AclClassName aclClassName, PermissionName permissionName) {
    Specification<T> securedSpecification = SecuredSpecifications.getSpecification(user, aclClassName, permissionName);

    return (Page) (null == pageable ? new PageImpl(this.findAll()) : this.findAll(Specifications.where(securedSpecification), pageable));
}
 
Example 30
public List<T> findAll(User user, AclClassName aclClassName, PermissionName permissionName) {
    Specification<T> securedSpecification = SecuredSpecifications.getSpecification(user, aclClassName, permissionName);

    return this.findAll(Specifications.where(securedSpecification));
}