All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sphere.internal.request.ProductSearchRequest Maven / Gradle / Ivy

There is a newer version: 0.72.1
Show newest version
package io.sphere.internal.request;

import com.google.common.base.Function;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import io.sphere.client.model.products.BackendProductProjection;
import io.sphere.internal.ProductConversion;
import io.sphere.internal.util.SearchResultUtil;
import io.sphere.client.ProductSort;
import io.sphere.client.SearchRequest;
import io.sphere.client.facets.expressions.FacetExpression;
import io.sphere.client.filters.expressions.FilterExpression;
import io.sphere.client.model.SearchResult;
import io.sphere.client.shop.CategoryTree;
import io.sphere.client.shop.model.Product;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/** Transforms results from {@link io.sphere.client.model.products.BackendProductProjection} to {@link Product}. */
public class ProductSearchRequest implements SearchRequest {
    private SearchRequest underlyingRequest;
    private final CategoryTree categoryTree;

    public ProductSearchRequest(@Nonnull SearchRequest underlyingRequest, CategoryTree categoryTree) {
        if (underlyingRequest == null) throw new NullPointerException("underlyingRequest");
        this.underlyingRequest = underlyingRequest;
        this.categoryTree = categoryTree;
    }

    @Override public SearchResult fetch() {
        return convertProducts(underlyingRequest.fetch(), categoryTree);
    }

    @Override public ListenableFuture> fetchAsync() {
        return Futures.transform(underlyingRequest.fetchAsync(), new Function, SearchResult>() {
            @Override public SearchResult apply(@Nullable SearchResult res) {
                return convertProducts(res, categoryTree);
            }
        });
    }

    private static SearchResult convertProducts(SearchResult res, CategoryTree categoryTree) {
        return SearchResultUtil.transform(
                res,
                ProductConversion.fromBackendProductProjections(res.getResults(), categoryTree));
    }

    @Override public SearchRequest page(int page) {
        underlyingRequest = underlyingRequest.page(page);
        return this;
    }

    @Override public SearchRequest pageSize(int pageSize) {
        underlyingRequest = underlyingRequest.pageSize(pageSize);
        return this;
    }

    @Override public SearchRequest filter(FilterExpression filter, FilterExpression... filters) {
        underlyingRequest = underlyingRequest.filter(filter, filters);
        return this;
    }

    @Override public SearchRequest filter(Iterable filters) {
        underlyingRequest = underlyingRequest.filter(filters);
        return this;
    }

    @Override public SearchRequest facet(FacetExpression facet, FacetExpression... facets) {
        underlyingRequest = underlyingRequest.facet(facet, facets);
        return this;
    }

    @Override public SearchRequest facet(Iterable facets) {
        underlyingRequest = underlyingRequest.facet(facets);
        return this;
    }

    @Override public SearchRequest sort(ProductSort sort) {
        underlyingRequest = underlyingRequest.sort(sort);
        return this;
    }

    @Override
    public SearchRequest sort(final String sort) {
        underlyingRequest = underlyingRequest.sort(sort);
        return this;
    }

    // testing purposes
    public SearchRequest getUnderlyingRequest() {
        return underlyingRequest;
    }

    // logging and debugging purposes
    @Override public String toString() {
        return underlyingRequest.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy