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

io.sphere.sdk.search.MetaModelSearchDslImpl Maven / Gradle / Ivy

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

import com.fasterxml.jackson.core.type.TypeReference;
import io.sphere.sdk.client.HttpRequestIntent;
import io.sphere.sdk.client.SphereRequestBase;
import io.sphere.sdk.expansion.ExpansionPath;
import io.sphere.sdk.http.HttpMethod;
import io.sphere.sdk.http.HttpResponse;
import io.sphere.sdk.http.HttpQueryParameter;
import io.sphere.sdk.http.UrlQueryBuilder;
import io.sphere.sdk.models.LocalizedStringEntry;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Function;

import static io.sphere.sdk.search.SearchParameterKeys.*;
import static io.sphere.sdk.utils.ListUtils.listOf;
import static java.lang.String.format;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;

/**
 *
 * @param  type of the search result
 * @param  type of the class implementing this class
 * @param  type of the search model
 * @param  type of the expansion model
 */
public abstract class MetaModelSearchDslImpl, S, E> extends SphereRequestBase implements MetaModelSearchDsl {

    @Nullable
    final LocalizedStringEntry text;
    final List> facets;
    final List> resultFilters;
    final List> queryFilters;
    final List> facetFilters;
    final List> sort;
    @Nullable
    final Long limit;
    @Nullable
    final Long offset;
    final List> expansionPaths;
    final List additionalQueryParameters;
    final String endpoint;
    final S searchModel;
    final E expansionModel;
    final Function> resultMapper;
    final Function, C> searchDslBuilderFunction;

    public MetaModelSearchDslImpl(@Nullable final LocalizedStringEntry text, final List> facets, final List> resultFilters,
                                  final List> queryFilters, final List> facetFilters,
                                  final List> sort, @Nullable final Long limit, @Nullable final Long offset,
                                  final String endpoint, final Function> resultMapper,
                                  final List> expansionPaths, final List additionalQueryParameters,
                                  final S searchModel, final E expansionModel, final Function, C> searchDslBuilderFunction) {
        Optional.ofNullable(offset).ifPresent(presentOffset -> {
            if (presentOffset < MIN_OFFSET || presentOffset > MAX_OFFSET) {
                throw new IllegalArgumentException(format("The offset parameter must be in the range of [%d..%d], but was %d.", MIN_OFFSET, MAX_OFFSET, presentOffset));
            }
        });
        this.searchDslBuilderFunction = requireNonNull(searchDslBuilderFunction);
        this.text = text;
        this.facets = requireNonNull(facets);
        this.resultFilters = requireNonNull(resultFilters);
        this.queryFilters = requireNonNull(queryFilters);
        this.facetFilters = requireNonNull(facetFilters);
        this.sort = requireNonNull(sort);
        this.limit = limit;
        this.offset = offset;
        this.endpoint = requireNonNull(endpoint);
        this.resultMapper = requireNonNull(resultMapper);
        this.expansionPaths = requireNonNull(expansionPaths);
        this.additionalQueryParameters = requireNonNull(additionalQueryParameters);
        this.expansionModel = requireNonNull(expansionModel);
        this.searchModel = requireNonNull(searchModel);
    }

    public MetaModelSearchDslImpl(final String endpoint, final TypeReference> pagedSearchResultTypeReference,
                                  final S searchModel, final E expansionModel, final Function, C> searchDslBuilderFunction,
                                  final List additionalQueryParameters) {
        this(null, emptyList(), emptyList(), emptyList(), emptyList(), emptyList(), null, null, endpoint, httpResponse -> deserialize(httpResponse, pagedSearchResultTypeReference),
                emptyList(), additionalQueryParameters, searchModel, expansionModel, searchDslBuilderFunction);
    }

    public MetaModelSearchDslImpl(final String endpoint, final TypeReference> pagedSearchResultTypeReference,
                                  final S searchModel, final E expansionModel, final Function, C> searchDslBuilderFunction) {
        this(endpoint, pagedSearchResultTypeReference, searchModel, expansionModel, searchDslBuilderFunction, emptyList());
    }

    public MetaModelSearchDslImpl(final MetaModelSearchDslBuilder builder) {
        this(builder.text, builder.facets, builder.resultFilters, builder.queryFilters, builder.facetFilters, builder.sort,
                builder.limit, builder.offset, builder.endpoint, builder.resultMapper, builder.expansionPaths, builder.additionalQueryParameters,
                builder.searchModel, builder.expansionModel, builder.searchDslBuilderFunction);
    }

    @Override
    public C withText(@Nullable final LocalizedStringEntry text) {
        return copyBuilder().text(text).build();
    }

    @Override
    public C withText(final Locale locale, final String text) {
        final LocalizedStringEntry locStringEntry = LocalizedStringEntry.of(requireNonNull(locale), requireNonNull(text));
        return withText(locStringEntry);
    }

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

    @Override
    public C withFacets(final FacetExpression facet) {
        return withFacets(singletonList(requireNonNull(facet)));
    }

    @Override
    public C withFacets(final Function> m) {
        return withFacets(m.apply(searchModel));
    }

    @Override
    public C plusFacets(final List> facets) {
        return withFacets(listOf(facets(), facets));
    }

    @Override
    public C plusFacets(final FacetExpression facet) {
        return plusFacets(singletonList(requireNonNull(facet)));
    }

    @Override
    public C plusFacets(final Function> m) {
        return plusFacets(m.apply(searchModel));
    }

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

    @Override
    public C withResultFilters(final FilterExpression resultFilter) {
        return withResultFilters(singletonList(requireNonNull(resultFilter)));
    }

    @Override
    public C withResultFilters(final Function> m) {
        return withResultFilters(m.apply(searchModel));
    }

    @Override
    public C plusResultFilters(final List> resultFilters) {
        return withResultFilters(listOf(resultFilters(), resultFilters));
    }

    @Override
    public C plusResultFilters(final FilterExpression resultFilter) {
        return plusResultFilters(singletonList(requireNonNull(resultFilter)));
    }

    @Override
    public C plusResultFilters(final Function> m) {
        return plusResultFilters(m.apply(searchModel));
    }

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

    @Override
    public C withQueryFilters(final FilterExpression queryFilter) {
        return withQueryFilters(singletonList(requireNonNull(queryFilter)));
    }

    @Override
    public C withQueryFilters(final Function> m) {
        return withQueryFilters(m.apply(searchModel));
    }

    @Override
    public C plusQueryFilters(final List> queryFilters) {
        return withQueryFilters(listOf(queryFilters(), queryFilters));
    }

    @Override
    public C plusQueryFilters(final FilterExpression queryFilter) {
        return plusQueryFilters(singletonList(requireNonNull(queryFilter)));
    }

    @Override
    public C plusQueryFilters(final Function> m) {
        return plusQueryFilters(m.apply(searchModel));
    }

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

    @Override
    public C withFacetFilters(final FilterExpression facetFilter) {
        return withFacetFilters(singletonList(requireNonNull(facetFilter)));
    }

    @Override
    public C withFacetFilters(final Function> m) {
        return withFacetFilters(m.apply(searchModel));
    }

    @Override
    public C plusFacetFilters(final List> facetFilters) {
        return withFacetFilters(listOf(facetFilters(), facetFilters));
    }

    @Override
    public C plusFacetFilters(final FilterExpression facetFilter) {
        return plusFacetFilters(singletonList(requireNonNull(facetFilter)));
    }

    @Override
    public C plusFacetFilters(final Function> m) {
        return plusFacetFilters(m.apply(searchModel));
    }

    private C withSort(final List> sort) {
        return copyBuilder().sort(sort).build();
    }

    @Override
    public C withSort(final Function> m) {
        return withSort(m.apply(searchModel));
    }

    @Override
    public C withSort(final SearchSort sort) {
        return withSort(singletonList(sort));
    }

    @Override
    public C withLimit(final long limit) {
        return copyBuilder().limit(limit).build();
    }

    @Override
    public C withOffset(final long offset) {
        return copyBuilder().offset(offset).build();
    }

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

    @Override
    public C withExpansionPaths(final ExpansionPath expansionPath) {
        return withExpansionPaths(singletonList(requireNonNull(expansionPath)));
    }

    @Override
    public C withExpansionPaths(final Function> m) {
        return withExpansionPaths(singletonList(m.apply(expansionModel)));
    }

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

    @Override
    public C plusExpansionPaths(final ExpansionPath expansionPath) {
        return plusExpansionPaths(singletonList(requireNonNull(expansionPath)));
    }

    @Override
    public C plusExpansionPaths(final Function> m) {
        return plusExpansionPaths(m.apply(expansionModel));
    }

    @Override
    @Nullable
    public LocalizedStringEntry text() {
        return text;
    }

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

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

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

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

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

    @Override
    @Nullable
    public Long limit() {
        return limit;
    }

    @Override
    @Nullable
    public Long offset() {
        return offset;
    }

    @Override
    public String endpoint() {
        return endpoint;
    }

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

    protected List additionalQueryParameters() {
        return additionalQueryParameters;
    }

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

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

    @Override
    public PagedSearchResult deserialize(final HttpResponse httpResponse) {
        return resultMapper.apply(httpResponse);
    }

    String queryParametersToString(final boolean urlEncoded) {
        final UrlQueryBuilder builder = UrlQueryBuilder.of();
        Optional.ofNullable(text()).ifPresent(t -> builder.add(TEXT + "." + t.getLocale().getLanguage(), t.getValue(), urlEncoded));
        facets().forEach(f -> builder.add(FACET, f.toSearchExpression(), urlEncoded));
        resultFilters().forEach(f -> builder.add(FILTER, f.toSearchExpression(), urlEncoded));
        queryFilters().forEach(f -> builder.add(FILTER_QUERY, f.toSearchExpression(), urlEncoded));
        facetFilters().forEach(f -> builder.add(FILTER_FACETS, f.toSearchExpression(), urlEncoded));
        sort().forEach(s -> builder.add(SORT, s.toSphereSort(), urlEncoded));
        Optional.ofNullable(limit()).ifPresent(l -> builder.add(LIMIT, l.toString(), urlEncoded));
        Optional.ofNullable(offset()).ifPresent(o -> builder.add(OFFSET, o.toString(), urlEncoded));
        expansionPaths().forEach(path -> builder.add(EXPAND, path.toSphereExpand(), urlEncoded));
        additionalQueryParameters().forEach(p -> builder.add(p.getKey(), p.getValue(), urlEncoded));
        return builder.toStringWithOptionalQuestionMark();
    }

    @Override
    public final boolean equals(Object o) {
        return o != null && (o instanceof Search) && ((Search)o).httpRequestIntent().getPath().equals(httpRequestIntent().getPath());
    }

    @Override
    public final int hashCode() {
        return httpRequestIntent().getPath().hashCode();
    }

    @Override
    public String toString() {
        final String readablePath = endpoint + queryParametersToString(false);

        return "SearchDslImpl{" +
                ", text=" + text +
                ", facets=" + facets +
                ", resultFilters=" + resultFilters +
                ", queryFilters=" + queryFilters +
                ", facetFilters=" + facetFilters +
                ", sort=" + sort +
                ", additionalQueryParameters=" + additionalQueryParameters +
                ", limit=" + limit +
                ", offset=" + offset +
                ", endpoint='" + endpoint + '\'' +
                ", resultMapper=" + resultMapper +
                ", readablePath=" + readablePath +
                ", request=" + httpRequestIntent() +
                '}';
    }

    S getSearchModel() {
        return searchModel;
    }

    E getExpansionModel() {
        return expansionModel;
    }

    Function> getResultMapper() {
        return resultMapper;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy