io.sphere.sdk.commands.MetaModelUpdateCommandDslImpl Maven / Gradle / Ivy
package io.sphere.sdk.commands;
import com.fasterxml.jackson.core.type.TypeReference;
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.http.HttpMethod;
import io.sphere.sdk.http.UrlQueryBuilder;
import io.sphere.sdk.models.ResourceView;
import io.sphere.sdk.models.Versioned;
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 that change one entity 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 expansion model */
public class MetaModelUpdateCommandDslImpl, C extends UpdateCommandDsl, E> extends CommandImpl implements UpdateCommandDsl, MetaModelExpansionDslExpansionModelRead {
final Versioned versioned;
final List extends UpdateAction> updateActions;
final TypeReference typeReference;
final String baseEndpointWithoutId;
final Function, C> creationFunction;
final E expansionModel;
final List> expansionPaths;
private MetaModelUpdateCommandDslImpl(final Versioned versioned,
final List extends UpdateAction> updateActions,
final TypeReference typeReference,
final String baseEndpointWithoutId,
final Function, C> creationFunction,
final E expansionModel,
final List> expansionPaths) {
this.expansionModel = requireNonNull(expansionModel);
this.expansionPaths = requireNonNull(expansionPaths);
this.creationFunction = requireNonNull(creationFunction);
this.versioned = requireNonNull(versioned);
this.updateActions = requireNonNull(updateActions);
this.typeReference = requireNonNull(typeReference);
this.baseEndpointWithoutId = requireNonNull(baseEndpointWithoutId);
}
protected MetaModelUpdateCommandDslImpl(final Versioned versioned,
final List extends UpdateAction> updateActions,
final JsonEndpoint endpoint,
final Function, C> creationFunction,
final E expansionModel) {
this(versioned, updateActions, endpoint.typeReference(), endpoint.endpoint(), creationFunction, expansionModel, Collections.>emptyList());
}
protected MetaModelUpdateCommandDslImpl(final MetaModelUpdateCommandDslBuilder builder) {
this(builder.getVersioned(), builder.getUpdateActions(), builder.getTypeReference(), builder.getBaseEndpointWithoutId(), builder.getCreationFunction(), builder.expansionModel, builder.expansionPaths);
}
@Override
protected TypeReference typeReference() {
return typeReference;
}
@Override
public HttpRequestIntent httpRequestIntent() {
if (!baseEndpointWithoutId.startsWith("/")) {
throw new RuntimeException("By convention the paths start with a slash, see baseEndpointWithoutId()");
}
final String additions = queryParametersToString(true);
final String path = baseEndpointWithoutId + "/" + getVersioned().getId() + (additions.length() > 1 ? additions : "");
return HttpRequestIntent.of(HttpMethod.POST, path, toJsonString(new UpdateCommandBody<>(getVersioned().getVersion(), getUpdateActions())));
}
private String queryParametersToString(final boolean urlEncoded) {
final UrlQueryBuilder builder = UrlQueryBuilder.of();
expansionPaths().forEach(path -> builder.add("expand", path.toSphereExpand(), urlEncoded));
return builder.toStringWithOptionalQuestionMark();
}
@Override
public C withVersion(final Versioned newVersioned) {
return copyBuilder().versioned(newVersioned).build();
}
public Versioned getVersioned() {
return versioned;
}
public List extends UpdateAction> getUpdateActions() {
return updateActions;
}
protected MetaModelUpdateCommandDslBuilder copyBuilder() {
return new MetaModelUpdateCommandDslBuilder<>(this);
}
String getBaseEndpointWithoutId() {
return baseEndpointWithoutId;
}
Function, C> getCreationFunction() {
return creationFunction;
}
TypeReference getTypeReference() {
return 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;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy