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

io.sphere.internal.OrderServiceImpl Maven / Gradle / Ivy

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

import com.google.common.base.Function;
import io.sphere.client.*;
import io.sphere.client.exceptions.OutOfStockException;
import io.sphere.client.exceptions.PriceChangedException;
import io.sphere.client.exceptions.SphereBackendException;
import io.sphere.client.exceptions.SphereException;
import io.sphere.client.model.QueryResult;
import io.sphere.client.model.VersionedId;
import io.sphere.client.shop.ApiMode;
import io.sphere.client.shop.CreateOrderBuilder;
import io.sphere.client.shop.OrderService;
import io.sphere.client.shop.model.Order;
import io.sphere.client.shop.model.PaymentState;
import io.sphere.client.shop.model.ShipmentState;
import io.sphere.client.shop.model.*;
import io.sphere.internal.command.*;
import io.sphere.internal.request.RequestFactory;
import com.google.common.base.Optional;
import org.codehaus.jackson.type.TypeReference;

import static io.sphere.internal.util.Util.*;

import javax.annotation.Nullable;

public class OrderServiceImpl extends ProjectScopedAPI implements OrderService {
    public OrderServiceImpl(RequestFactory requestFactory, ProjectEndpoints endpoints) {
        super(requestFactory, endpoints, new TypeReference() {}, new TypeReference>() { });
    }

    @Override public FetchRequest byId(String id) {
        return requestFactory.createFetchRequest(
                endpoints.orders.byId(id),
                Optional.absent(),
                new TypeReference() {});
    }

    @Deprecated
    @Override public QueryRequest all() {
        return query();
    }

    @Override public QueryRequest query() {
        return queryImpl(endpoints.orders.root());
    }

    @Override public QueryRequest forCustomer(String customerId) {
        return requestFactory.createQueryRequest(
                endpoints.orders.queryByCustomerId(customerId),
                Optional.absent(),
                new TypeReference>() {});
    }

    @Deprecated
    @Override public CommandRequest updatePaymentState(VersionedId orderId, PaymentState paymentState) {
        return updateOrder(orderId, new OrderUpdate().setPaymentState(paymentState));
    }

    @Deprecated
    @Override public CommandRequest updateShipmentState(VersionedId orderId, ShipmentState shipmentState) {
        return updateOrder(orderId, new OrderUpdate().setShipmentState(shipmentState));
    }

    @Override
    public CommandRequest updateOrder(VersionedId orderId, OrderUpdate orderUpdate) {
        return update(orderId, orderUpdate);
    }

    private CommandRequest update(VersionedId orderId, OrderUpdate orderUpdate) {
        return createCommandRequest(
                endpoints.orders.byId(orderId.getId()),
                new UpdateCommand(orderId.getVersion(), orderUpdate));
    }

    @Override public CommandRequest createOrder(VersionedId cartId, PaymentState paymentState) {
        return createOrder(new CreateOrderBuilder(cartId, paymentState));
    }

    @Override public CommandRequest createOrder(VersionedId cartId) {
        return createOrder(cartId, null);
    }

    @Override public CommandRequest createOrder(CreateOrderBuilder builder) {
        return createOrder(builder.build());
    }

    private CommandRequest createOrder(CartCommands.OrderCart createCustomerCommand) {
        return requestFactory.createCommandRequest(
                endpoints.orders.root(), createCustomerCommand, new TypeReference() {}).
                withErrorHandling(new Function() {
                    public SphereException apply(@Nullable SphereBackendException e) {
                        SphereError.OutOfStock outOfStockError = getError(e, SphereError.OutOfStock.class);
                        if (outOfStockError != null)
                            return new OutOfStockException(outOfStockError.getLineItemIds());
                        SphereError.PriceChanged priceChangedError = getError(e, SphereError.PriceChanged.class);
                        if (priceChangedError != null)
                            return new PriceChangedException(priceChangedError.getLineItemIds());
                        return null;
                    }
                });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy