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

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

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

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import io.sphere.client.CommandRequest;
import io.sphere.client.SphereResult;
import io.sphere.client.exceptions.SphereBackendException;
import io.sphere.client.exceptions.SphereException;
import io.sphere.client.model.products.BackendProduct;
import io.sphere.client.shop.ApiMode;
import io.sphere.client.shop.CategoryTree;
import io.sphere.client.shop.model.Product;
import io.sphere.internal.ProductConversion;
import io.sphere.internal.util.Util;
import com.google.common.base.Function;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

public class ProductCommandRequest implements CommandRequest {
    private CommandRequest underlyingRequest;
    private final CategoryTree categoryTree;
    private final ApiMode apiMode;
    final @Nullable Function transformError;


    public ProductCommandRequest(
            @Nonnull CategoryTree categoryTree, 
            @Nonnull CommandRequest underlyingRequest, 
            @Nonnull ApiMode apiMode) {
       this(categoryTree, underlyingRequest, apiMode, null);
    }

    public ProductCommandRequest(
            @Nonnull CategoryTree categoryTree,
            @Nonnull CommandRequest underlyingRequest,
            @Nonnull ApiMode apiMode, 
            Function transformError) {
        if (underlyingRequest == null) throw new NullPointerException("underlyingRequest");
        this.underlyingRequest = underlyingRequest;
        this.categoryTree = categoryTree;
        this.apiMode = apiMode;
        this.transformError = transformError;
    }

    @Override
    public Product execute() { return Util.syncResult(executeAsync()); }

    @Override
    public ListenableFuture> executeAsync() {
        CommandRequest request;
        if (transformError != null) request = underlyingRequest.withErrorHandling(transformError);
        else request = underlyingRequest;
        
        return Futures.transform(request.executeAsync(), new Function, SphereResult>() {
            @Override
            public SphereResult apply(SphereResult backendProductResult) {
                assert backendProductResult != null;
                return backendProductResult.transform(new Function() {
                    @Override
                    public Product apply(BackendProduct backendProduct) {
                        assert backendProduct != null;
                        return ProductConversion.fromBackendProduct(backendProduct, categoryTree, apiMode);
                    }
                });
            }
        });   
    }

    @Override
    public CommandRequest withErrorHandling(@Nonnull Function transformError) {
        return new ProductCommandRequest(categoryTree, underlyingRequest, apiMode, transformError);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy