Skip to content

Commit

Permalink
Merge pull request #542 from commercetools/search-builder
Browse files Browse the repository at this point in the history
Search builder
  • Loading branch information
jenschude authored Jan 23, 2024
2 parents 7c20076 + a5f0481 commit 154b530
Show file tree
Hide file tree
Showing 30 changed files with 2,238 additions and 2 deletions.
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ a8ec45c8ea4ba559247b654d01b0d35b21a68865
430a1a0a5dd4efe78e21526c37bec9dbce036401



3 changes: 3 additions & 0 deletions api-java-mixin.raml
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,9 @@ types:
(java-implements):
'ByProjectKeyProductProjectionsGetMixin'
/search:
get:
(java-implements):
'ByProjectKeyProductProjectionsSearchGetMixin'
post:
(java-implements): 'ByProjectKeyProductProjectionsSearchPostMixin'
/product-selections:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
@Generated(value = "io.vrap.rmf.codegen.rendering.CoreCodeGenerator", comments = "https://github.com/commercetools/rmf-codegen")
public class ByProjectKeyProductProjectionsSearchGet extends
TypeApiMethod<ByProjectKeyProductProjectionsSearchGet, com.commercetools.api.models.product.ProductProjectionPagedSearchResponse>
implements com.commercetools.api.client.SortableTrait<ByProjectKeyProductProjectionsSearchGet>,
implements ByProjectKeyProductProjectionsSearchGetMixin,
com.commercetools.api.client.SortableTrait<ByProjectKeyProductProjectionsSearchGet>,
com.commercetools.api.client.PagingTrait<ByProjectKeyProductProjectionsSearchGet>,
com.commercetools.api.client.ProjectionselectingTrait<ByProjectKeyProductProjectionsSearchGet>,
com.commercetools.api.client.PriceselectingTrait<ByProjectKeyProductProjectionsSearchGet>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

package com.commercetools.api.client;

import java.util.function.Function;

import com.commercetools.api.search.products.FacetExpression;
import com.commercetools.api.search.products.FilterExpression;
import com.commercetools.api.search.products.ProductFacetExpressionBuilder;
import com.commercetools.api.search.products.ProductFilterExpressionBuilder;

public interface ByProjectKeyProductProjectionsSearchGetMixin {
public <TValue> ByProjectKeyProductProjectionsSearchGet addFilter(final TValue filter);

public <TValue> ByProjectKeyProductProjectionsSearchGet addFilterFacets(final TValue filterFacets);

public <TValue> ByProjectKeyProductProjectionsSearchGet addFilterQuery(final TValue filterQuery);

public <TValue> ByProjectKeyProductProjectionsSearchGet addFacet(final TValue facet);

default ProductFilterExpressionBuilder filterDsl() {
return new ProductFilterExpressionBuilder();
}

default ProductFacetExpressionBuilder facetDsl() {
return new ProductFacetExpressionBuilder();
}

default ByProjectKeyProductProjectionsSearchGet filter(
Function<ProductFilterExpressionBuilder, FilterExpression> fn) {
return addFilter(fn.apply(filterDsl()).render());
}

default ByProjectKeyProductProjectionsSearchGet filterFacets(
Function<ProductFilterExpressionBuilder, FilterExpression> fn) {
return addFilterFacets(fn.apply(filterDsl()).render());
}

default ByProjectKeyProductProjectionsSearchGet filterQuery(
Function<ProductFilterExpressionBuilder, FilterExpression> fn) {
return addFilterQuery(fn.apply(filterDsl()).render());
}

default ByProjectKeyProductProjectionsSearchGet facet(
Function<ProductFacetExpressionBuilder, FacetExpression<?>> fn) {
return addFacet(fn.apply(facetDsl()).render());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;

import com.commercetools.api.search.products.FacetExpression;
import com.commercetools.api.search.products.FilterExpression;
import com.commercetools.api.search.products.ProductFacetExpressionBuilder;
import com.commercetools.api.search.products.ProductFilterExpressionBuilder;

import io.vrap.rmf.base.client.ApiMethod;

public interface ByProjectKeyProductProjectionsSearchPostMixin {
Expand Down Expand Up @@ -1654,4 +1659,32 @@ public default <TValue> ByProjectKeyProductProjectionsSearchPost addText(final S
.map(s -> new ApiMethod.ParamEntry<>(placeholderName, s.toString()))
.collect(Collectors.toList()));
}

default ProductFilterExpressionBuilder filterDsl() {
return new ProductFilterExpressionBuilder();
}

default ProductFacetExpressionBuilder facetDsl() {
return new ProductFacetExpressionBuilder();
}

default ByProjectKeyProductProjectionsSearchPost filter(
Function<ProductFilterExpressionBuilder, FilterExpression> fn) {
return addFilter(fn.apply(filterDsl()).render());
}

default ByProjectKeyProductProjectionsSearchPost filterFacets(
Function<ProductFilterExpressionBuilder, FilterExpression> fn) {
return addFilterFacets(fn.apply(filterDsl()).render());
}

default ByProjectKeyProductProjectionsSearchPost filterQuery(
Function<ProductFilterExpressionBuilder, FilterExpression> fn) {
return addFilterQuery(fn.apply(filterDsl()).render());
}

default ByProjectKeyProductProjectionsSearchPost facet(
Function<ProductFacetExpressionBuilder, FacetExpression<?>> fn) {
return addFacet(fn.apply(facetDsl()).render());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ public interface Identifiable<T> {
* @return ID
*/
String getId();

static <T> Identifiable<T> of(final String id) {
return new SimpleIdentifiable<>(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

package com.commercetools.api.models;

public class SimpleIdentifiable<T> implements Identifiable<T> {

private final String id;

public SimpleIdentifiable(String id) {
this.id = id;
}

@Override
public String getId() {
return id;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import org.javamoney.moneta.spi.JDKCurrencyProvider;

final class CurrencyUtils {
public final class CurrencyUtils {
static final JDKCurrencyProvider CURRENCY_PROVIDER = new JDKCurrencyProvider();

public static CurrencyUnit ofCode(final String currencyCode) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

package com.commercetools.api.search.products;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import com.commercetools.api.models.Identifiable;
import com.commercetools.api.models.category.Category;

public class CategoryTermFacetExpression extends TermFacetExpression<String> {

public CategoryTermFacetExpression(PathExpression expression) {
super(expression, TermFormatter::format);
}

public CategoryTermFacetExpression(PathExpression expression, List<FilterExpression> term) {
super(expression, term, TermFormatter::format);
}

public CategoryTermFacetExpression is(String value) {
List<FilterExpression> terms = Optional.ofNullable(terms()).map(term -> {
List<FilterExpression> expressions = new ArrayList<>(terms());
expressions.add(formatter().apply(value));
return expressions;
}).orElse(Collections.singletonList(formatter().apply(value)));
return new CategoryTermFacetExpression(expression(), terms);
}

public CategoryTermFacetExpression is(Identifiable<Category> value) {
return is(value.getId());
}

public CategoryTermFacetExpression subTree(String value) {
List<FilterExpression> terms = Optional.ofNullable(terms()).map(term -> {
List<FilterExpression> expressions = new ArrayList<>(terms());
expressions.add(
ContainerExpression.of().parent(ConstantExpression.of("subTree")).inner(formatter().apply(value)));
return expressions;
})
.orElse(Collections.singletonList(
ContainerExpression.of().parent(ConstantExpression.of("subTree")).inner(formatter().apply(value))));
return new CategoryTermFacetExpression(expression(), terms);
}

public CategoryTermFacetExpression subTree(Identifiable<Category> value) {
return subTree(value.getId());
}

public CategoryTermFacetExpression isIn(Iterable<String> values) {
List<FilterExpression> terms = Optional.ofNullable(terms()).map(term -> {
List<FilterExpression> expressions = new ArrayList<>(terms());
values.forEach(v -> expressions.add(formatter().apply(v)));
return expressions;
}).orElse(StreamSupport.stream(values.spliterator(), false).map(formatter()).collect(Collectors.toList()));

return new CategoryTermFacetExpression(expression(), terms);
}

public CategoryTermFacetExpression containsAny(Iterable<Identifiable<Category>> values) {
return isIn(
StreamSupport.stream(values.spliterator(), false).map(Identifiable::getId).collect(Collectors.toList()));
}

public static CategoryTermFacetExpression of(PathExpression expression) {
return new CategoryTermFacetExpression(expression);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

package com.commercetools.api.search.products;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import com.commercetools.api.models.Identifiable;
import com.commercetools.api.models.category.Category;

public class CategoryTermFilterExpression extends TermFilterExpression<String> {

public CategoryTermFilterExpression(PathExpression expression) {
super(expression, TermFormatter::format);
}

public CategoryTermFilterExpression(PathExpression expression, List<FilterExpression> term) {
super(expression, term, TermFormatter::format);
}

public CategoryTermFilterExpression is(String value) {
List<FilterExpression> terms = Optional.ofNullable(terms()).map(term -> {
List<FilterExpression> expressions = new ArrayList<>(terms());
expressions.add(formatter().apply(value));
return expressions;
}).orElse(Collections.singletonList(formatter().apply(value)));
return new CategoryTermFilterExpression(expression(), terms);
}

public CategoryTermFilterExpression is(Identifiable<Category> value) {
return is(value.getId());
}

public CategoryTermFilterExpression subTree(String value) {
List<FilterExpression> terms = Optional.ofNullable(terms()).map(term -> {
List<FilterExpression> expressions = new ArrayList<>(terms());
expressions.add(
ContainerExpression.of().parent(ConstantExpression.of("subTree")).inner(formatter().apply(value)));
return expressions;
})
.orElse(Collections.singletonList(
ContainerExpression.of().parent(ConstantExpression.of("subTree")).inner(formatter().apply(value))));
return new CategoryTermFilterExpression(expression(), terms);
}

public CategoryTermFilterExpression subTree(Identifiable<Category> value) {
return subTree(value.getId());
}

public CategoryTermFilterExpression isIn(Iterable<String> values) {
List<FilterExpression> terms = Optional.ofNullable(terms()).map(term -> {
List<FilterExpression> expressions = new ArrayList<>(terms());
values.forEach(v -> expressions.add(formatter().apply(v)));
return expressions;
}).orElse(StreamSupport.stream(values.spliterator(), false).map(formatter()).collect(Collectors.toList()));

return new CategoryTermFilterExpression(expression(), terms);
}

public CategoryTermFilterExpression containsAny(Iterable<Identifiable<Category>> values) {
return isIn(
StreamSupport.stream(values.spliterator(), false).map(Identifiable::getId).collect(Collectors.toList()));
}

public static CategoryTermFilterExpression of(PathExpression expression) {
return new CategoryTermFilterExpression(expression);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

package com.commercetools.api.search.products;

public class ConstantExpression implements FilterExpression {
private final String value;

public ConstantExpression(String value) {
this.value = value;
}

public String value() {
return value;
}

@Override
public String render() {
return value;
}

public static ConstantExpression of(String value) {
return new ConstantExpression(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

package com.commercetools.api.search.products;

import java.util.Objects;

public class ContainerExpression implements FilterExpression {
private final FilterExpression parent;

private final FilterExpression inner;

private final boolean renderInnerWithoutParentheses;

public ContainerExpression() {
parent = null;
inner = null;
renderInnerWithoutParentheses = false;
}

public ContainerExpression(final FilterExpression parent, final FilterExpression inner) {
this.parent = parent;
this.inner = inner;
this.renderInnerWithoutParentheses = false;
}

public ContainerExpression(final FilterExpression parent, final FilterExpression inner,
final boolean renderInnerWithoutParentheses) {
this.parent = parent;
this.inner = inner;
this.renderInnerWithoutParentheses = renderInnerWithoutParentheses;
}

public FilterExpression parent() {
return parent;
}

public FilterExpression inner() {
return inner;
}

public boolean renderInnerWithOutParentheses() {
return renderInnerWithoutParentheses;
}

public ContainerExpression parent(final FilterExpression parent) {
return new ContainerExpression(parent, inner, renderInnerWithoutParentheses);
}

public ContainerExpression inner(final FilterExpression element) {
return new ContainerExpression(parent, element, renderInnerWithoutParentheses);
}

public ContainerExpression renderInnerWithOutParentheses(final boolean renderInnerWithoutParentheses) {
return new ContainerExpression(parent, inner, renderInnerWithoutParentheses);
}

@Override
public String render() {
if (renderInnerWithoutParentheses) {
return parent().render() + " " + Objects.requireNonNull(inner).render();
}
return parent().render() + "(" + Objects.requireNonNull(inner).render() + ")";
}

public static ContainerExpression of() {
return new ContainerExpression();
}

public static ContainerExpression of(final FilterExpression parent, final FilterExpression inner) {
return new ContainerExpression(parent, inner);
}

public static ContainerExpression of(final FilterExpression parent, final FilterExpression inner,
final boolean renderInnerWithoutParentheses) {
return new ContainerExpression(parent, inner, renderInnerWithoutParentheses);
}
}
Loading

0 comments on commit 154b530

Please sign in to comment.