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

io.sphere.sdk.commands.MetaModelCreateCommandImpl Maven / Gradle / Ivy

There is a newer version: 1.0.0-M26
Show newest version
package io.sphere.sdk.commands;

import com.fasterxml.jackson.databind.JavaType;
import io.sphere.sdk.client.HttpRequestIntent;
import io.sphere.sdk.client.JsonEndpoint;
import io.sphere.sdk.expansion.ExpansionDslUtil;
import io.sphere.sdk.expansion.ExpansionPath;
import io.sphere.sdk.expansion.MetaModelExpansionDslExpansionModelRead;
import io.sphere.sdk.expansion.ExpansionPathContainer;
import io.sphere.sdk.http.HttpMethod;
import io.sphere.sdk.http.UrlQueryBuilder;
import io.sphere.sdk.json.SphereJsonUtils;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;

import static io.sphere.sdk.json.SphereJsonUtils.toJsonString;
import static io.sphere.sdk.utils.ListUtils.listOf;
import static java.util.Objects.requireNonNull;

/**
 * Internal base class to implement commands which create a resource in SPHERE.IO.
 *
 * @param  the type of the result of the command
 * @param  class which will serialized as JSON command body, most likely a template
 * @param  type of the draft object
 * @param  type of the expansion model
 */
public abstract class MetaModelCreateCommandImpl extends CommandImpl implements CreateCommand, MetaModelExpansionDslExpansionModelRead {

    final D body;
    final E expansionModel;
    final JsonEndpoint endpoint;
    final List> expansionPaths;
    final Function, C> creationFunction;

    protected MetaModelCreateCommandImpl(final D draft, final JsonEndpoint endpoint, final List> expansionPaths, final E expansionModel, final Function, C> creationFunction) {
        this.creationFunction = requireNonNull(creationFunction);
        this.expansionPaths = requireNonNull(expansionPaths);
        this.expansionModel = requireNonNull(expansionModel);
        this.body = requireNonNull(draft);
        this.endpoint = requireNonNull(endpoint);
    }

    protected MetaModelCreateCommandImpl(final MetaModelCreateCommandBuilder builder) {
        this(builder.body, builder.endpoint, builder.expansionPaths, builder.expansionModel, builder.creationFunction);
    }

    protected MetaModelCreateCommandImpl(final D draft, final JsonEndpoint endpoint, final E expansionModel, final Function, C> creationFunction) {
        this(draft, endpoint, Collections.emptyList(), expansionModel, creationFunction);
    }

    @Override
    public HttpRequestIntent httpRequestIntent() {
        final String additions = queryParametersToString(true);
        return HttpRequestIntent.of(httpMethod(), endpoint.endpoint() + (additions.length() > 1 ? additions : ""), httpBody());
    }

    private String queryParametersToString(final boolean urlEncoded) {
        final UrlQueryBuilder builder = UrlQueryBuilder.of();
        expansionPaths().forEach(path -> builder.add("expand", path.toSphereExpand(), urlEncoded));
        return builder.toStringWithOptionalQuestionMark();
    }

    protected HttpMethod httpMethod() {
        return HttpMethod.POST;
    }

    protected String httpBody() {
        return toJsonString(body);
    }

    @Override
    protected JavaType jacksonJavaType() {
        return SphereJsonUtils.convertToJavaType(endpoint.typeReference());
    }

    @Override
    public List> expansionPaths() {
        return expansionPaths;
    }

    @Override
    public final C withExpansionPaths(final List> expansionPaths) {
        return copyBuilder().expansionPaths(expansionPaths).build();
    }

    @Override
    public C withExpansionPaths(final ExpansionPath expansionPath) {
        return ExpansionDslUtil.withExpansionPaths(this, expansionPath);
    }

    @Override
    public C withExpansionPaths(final Function> m) {
        return ExpansionDslUtil.withExpansionPaths(this, m);
    }

    @Override
    public C plusExpansionPaths(final List> expansionPaths) {
        return withExpansionPaths(listOf(expansionPaths(), expansionPaths));
    }

    @Override
    public C plusExpansionPaths(final ExpansionPath expansionPath) {
        return ExpansionDslUtil.plusExpansionPaths(this, expansionPath);
    }

    @Override
    public C plusExpansionPaths(final Function> m) {
        return ExpansionDslUtil.plusExpansionPaths(this, m);
    }

    @Override
    public E expansionModel() {
        return expansionModel;
    }

    protected MetaModelCreateCommandBuilder copyBuilder() {
        return new MetaModelCreateCommandBuilder<>(this);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy