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

io.sphere.internal.request.ProductFetchRequest 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.base.Optional;
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.client.FetchRequest;
import io.sphere.client.shop.CategoryTree;
import io.sphere.client.shop.model.Product;
import io.sphere.internal.util.Util;

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

/** Transforms results of type {@link io.sphere.client.model.products.BackendProductProjection} to {@link Product}. */
public class ProductFetchRequest implements FetchRequest  {
    private FetchRequest underlyingRequest;
    private final CategoryTree categoryTree;

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

    @Override public Optional fetch() {
        return Util.sync(fetchAsync());
    }

    @Override public ListenableFuture> fetchAsync() {
        return Futures.transform(underlyingRequest.fetchAsync(), new Function, Optional>() {
            @Override public Optional apply(@Nullable Optional backendProduct) {
                assert backendProduct != null;
                if (!backendProduct.isPresent()) return Optional.absent();
                return Optional.of(ProductConversion.fromBackendProductProjection(backendProduct.get(), categoryTree));
            }
        });
    }

    @Override public ProductFetchRequest expand(String... paths) {
        underlyingRequest = underlyingRequest.expand(paths);
        return this;
    }

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy