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

The following examples show how to use org.springframework.data.jpa.domain.Specifications. 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
Source File: SoftDeletesRepositoryImpl.java    From spring-boot-jpa-data-rest-soft-delete with MIT License 6 votes vote down vote up
@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
Source File: StockProductServiceImpl.java    From cloudstreetmarket.com with GNU General Public License v3.0 5 votes vote down vote up
@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 #3
Source File: DefaultPlatformJpaRepository.java    From abixen-platform with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 #4
Source File: DefaultPlatformJpaRepository.java    From abixen-platform with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 #5
Source File: AssetService.java    From mojito with 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 #6
Source File: IndexServiceImpl.java    From cloudstreetmarket.com with GNU General Public License v3.0 5 votes vote down vote up
@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 #7
Source File: StockProductServiceImpl.java    From cloudstreetmarket.com with GNU General Public License v3.0 5 votes vote down vote up
@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 #8
Source File: GenericRepositoryImpl.java    From gazpachoquest with GNU General Public License v3.0 5 votes vote down vote up
@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 #9
Source File: GenericRepositoryImpl.java    From gazpachoquest with GNU General Public License v3.0 5 votes vote down vote up
@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 #10
Source File: GenericRepositoryImpl.java    From gazpachoquest with GNU General Public License v3.0 5 votes vote down vote up
@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 #11
Source File: GenericRepositoryImpl.java    From gazpachoquest with GNU General Public License v3.0 5 votes vote down vote up
@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 #12
Source File: GenericRepositoryImpl.java    From gazpachoquest with GNU General Public License v3.0 5 votes vote down vote up
@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 #13
Source File: RangeSpecification.java    From gazpachoquest with GNU General Public License v3.0 5 votes vote down vote up
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 #14
Source File: ResourceService.java    From springboot-security-wechat with Apache License 2.0 5 votes vote down vote up
/**
 * @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 #15
Source File: BoardService.java    From springboot-vue.js-bbs with 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 #16
Source File: PropertySelectorSpecification.java    From gazpachoquest with GNU General Public License v3.0 4 votes vote down vote up
public static <E> Specifications<E> andPropertySelectorIfSet(Specifications<E> specifications,
        final SearchParameters sp) {
    specifications = specifications.and((Specification<E>) toSpecification(sp));
    return specifications;
}
 
Example #17
Source File: DefaultPlatformJpaRepository.java    From abixen-platform with GNU Lesser General Public License v2.1 4 votes vote down vote up
public List<T> findAll(SearchForm searchForm) {
    Specification<T> searchFormSpecification = SearchFormSpecifications.getSpecification(searchForm);

    return this.findAll(Specifications.where(searchFormSpecification));
}
 
Example #18
Source File: DefaultPlatformJpaRepository.java    From abixen-platform with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Page<T> findAll(Pageable pageable, SearchForm searchForm) {
    Specification<T> searchFormSpecification = SearchFormSpecifications.getSpecification(searchForm);

    return (Page) (null == pageable ? new PageImpl(this.findAll()) : this.findAll(Specifications.where(searchFormSpecification), pageable));
}
 
Example #19
Source File: DefaultPlatformJpaRepository.java    From abixen-platform with GNU Lesser General Public License v2.1 4 votes vote down vote up
public List<T> findAll(User user, AclClassName aclClassName, PermissionName permissionName) {
    Specification<T> securedSpecification = SecuredSpecifications.getSpecification(user, aclClassName, permissionName);

    return this.findAll(Specifications.where(securedSpecification));
}
 
Example #20
Source File: DefaultPlatformJpaRepository.java    From abixen-platform with GNU Lesser General Public License v2.1 4 votes vote down vote up
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 #21
Source File: JpaDynamicSpecificationBuilder.java    From spring-boot with Apache License 2.0 4 votes vote down vote up
public JpaDynamicSpecificationBuilder or(final Iterable<SearchFilter> searchFilters) {
    for (SearchFilter filter : searchFilters)
        this.specification = Specifications.where(this.specification).or(bySearchFilter(filter));
    return this;
}
 
Example #22
Source File: BaseRepositoryImpl.java    From docs-manage with 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 #23
Source File: JpaDynamicSpecificationBuilder.java    From spring-boot with Apache License 2.0 4 votes vote down vote up
public JpaDynamicSpecificationBuilder() {
    this.specification = Specifications.where(null);
}
 
Example #24
Source File: JpaDynamicSpecificationBuilder.java    From spring-boot with Apache License 2.0 4 votes vote down vote up
public JpaDynamicSpecificationBuilder and(final Iterable<SearchFilter> searchFilters) {
    for (SearchFilter filter : searchFilters)
        this.specification = Specifications.where(this.specification).and(bySearchFilter(filter));
    return this;
}
 
Example #25
Source File: JpaDynamicSpecificationBuilder.java    From spring-boot with Apache License 2.0 4 votes vote down vote up
public JpaDynamicSpecificationBuilder and(final Specification customSpecification) {
    this.specification = Specifications.where(this.specification).and(customSpecification);
    return this;
}
 
Example #26
Source File: JpaDynamicSpecificationBuilder.java    From spring-boot with Apache License 2.0 4 votes vote down vote up
public JpaDynamicSpecificationBuilder and(final Collection<Specification> customSpecifications) {
    for (Specification specification : customSpecifications)
        this.specification = Specifications.where(this.specification).and(specification);
    return this;
}
 
Example #27
Source File: SoftDeletesRepositoryImpl.java    From spring-boot-jpa-data-rest-soft-delete with MIT License 4 votes vote down vote up
private static final <T> Specification<T> notDeleted() {
	return Specifications.where(new DeletedIsNull<T>()).or(new DeletedTimeGreatherThanNow<T>());
}
 
Example #28
Source File: JpaDynamicSpecificationBuilder.java    From spring-boot with Apache License 2.0 4 votes vote down vote up
public JpaDynamicSpecificationBuilder or(final Specification customSpecification) {
    this.specification = Specifications.where(this.specification).or(customSpecification);
    return this;
}
 
Example #29
Source File: JpaDynamicSpecificationBuilder.java    From spring-boot with Apache License 2.0 4 votes vote down vote up
public JpaDynamicSpecificationBuilder or(final Collection<Specification> customSpecifications) {
    for (Specification specification : customSpecifications)
        this.specification = Specifications.where(this.specification).or(specification);
    return this;
}
 
Example #30
Source File: AclJpaRepository.java    From strategy-spring-security-acl with Apache License 2.0 4 votes vote down vote up
@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);
}