Skip to content

Instantly share code, notes, and snippets.

@danielfoehrKn
Last active July 27, 2018 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielfoehrKn/548865474b4af77f153f6df6cfbb0c0c to your computer and use it in GitHub Desktop.
Save danielfoehrKn/548865474b4af77f153f6df6cfbb0c0c to your computer and use it in GitHub Desktop.
BusinessPartner Read filter Workaround
@Query(serviceName = "CrudService", entity = "BusinessPartner")
public QueryResponse queryCustomers(QueryRequest qryRequest) {
List<BusinessPartner> businessPartners = new BusinessPartnerReadCommand(new ErpConfigContext(),
qryRequest.getTopOptionValue(),
qryRequest.getSkipOptionValue(),
qryRequest.getSelectProperties(),
qryRequest.getOrderByProperties(),
qryRequest.getCustomQueryOptions())
.execute();
QueryResponse queryResponse = QueryResponse.setSuccess().setData(businessPartners).response();
return queryResponse;
}
import com.netflix.hystrix.exception.HystrixBadRequestException;
import com.sap.cloud.sdk.odatav2.connectivity.ODataException;
import com.sap.cloud.sdk.s4hana.connectivity.ErpCommand;
import com.sap.cloud.sdk.s4hana.connectivity.ErpConfigContext;
import com.sap.cloud.sdk.s4hana.datamodel.odata.helper.EntityField;
import com.sap.cloud.sdk.s4hana.datamodel.odata.helper.EntitySelectable;
import com.sap.cloud.sdk.s4hana.datamodel.odata.helper.ExpressionFluentHelper;
import com.sap.cloud.sdk.s4hana.datamodel.odata.helper.Order;
import com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.businesspartner.BusinessPartner;
import com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.businesspartner.BusinessPartnerField;
import com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.businesspartner.BusinessPartnerFluentHelper;
import com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.businesspartner.BusinessPartnerSelectable;
import com.sap.cloud.sdk.s4hana.datamodel.odata.services.BusinessPartnerService;
import com.sap.cloud.sdk.s4hana.datamodel.odata.services.DefaultBusinessPartnerService;
import com.sap.cloud.sdk.service.prov.api.request.OrderByExpression;
import java.util.*;
import java.util.stream.Collectors;
public class GetBusinessPartnerCommand extends ErpCommand<List<BusinessPartner>> {
private static final String EQ = "eq";
private static final String GREATER = ">";
private static final String SMALLER = "<";
private final int top;
private final int skip;
private final BusinessPartnerField[] selectedProperties;
private final List<OrderByExpression> orderByProperties;
private List<ExpressionFluentHelper<BusinessPartner>> filters = new ArrayList<>();
private final String[] FILTER_OPERATORS = {EQ, GREATER, SMALLER};
public GetBusinessPartnerCommand(ErpConfigContext erpConfigContext, int top, int skip, List<String> properties, List<OrderByExpression> orderByProperties, Map<String, String> customQueryOptions) {
super(GetBusinessPartnerCommand.class, erpConfigContext);
this.top = top;
this.skip = skip;
selectedProperties = properties.stream().
map(property -> new BusinessPartnerField(property))
.toArray(BusinessPartnerField[]::new);
this.orderByProperties = orderByProperties;
this.filters = customQueryOptions.entrySet().stream().filter(option -> !option.getValue().isEmpty()).map(options -> new BusinessPartnerField<>(options.getKey().replaceAll(" ", "")).eq(options.getValue().replaceAll(" ", ""))).collect(Collectors.toList());
this.filters.addAll(customQueryOptions.entrySet().stream().filter(option -> option.getValue().isEmpty()).map(option -> option.getKey())
.flatMap(key -> {
Map<String, String[]> operators = new HashMap<>();
Optional<String> match = Arrays.stream(FILTER_OPERATORS)
.filter(op -> key.contains(op))
.findFirst();
match.ifPresent(op -> operators.put(op, key.split(op)));
return operators.entrySet().stream();
}).filter(entry -> entry.getValue().length == 2)
.map(entry -> {
String filterName = entry.getValue()[0].replaceAll(" ", "");
String filterValue = entry.getValue()[1].replaceAll(" ", "");
String operator = entry.getKey().replaceAll(" ", "");
switch (operator) {
case EQ:
return new BusinessPartnerField<>(filterName).eq(filterValue);
case GREATER:
return new BusinessPartnerField<>(filterName).gt(filterValue);
case SMALLER:
return new BusinessPartnerField<>(filterName).lt(filterValue);
default:
throw new UnsupportedOperationException(String.format("Filter with value % is not supported", operator));
}
}).collect(Collectors.toList()));
}
@Override
protected List<BusinessPartner> run() {
BusinessPartnerFluentHelper service = new DefaultBusinessPartnerService()
.getAllBusinessPartner();
orderByProperties.stream().forEach(expression -> service.orderBy(new BusinessPartnerField<>(expression.getOrderByProperty()), expression.isDescending() ? Order.DESC : Order.ASC));
service.select(selectedProperties);
filters.forEach(filter -> service.filter(filter));
if (skip > 0)
service.skip(skip);
if (top > 0)
service.top(top);
try {
return service.execute();
} catch (final ODataException e) {
throw new HystrixBadRequestException(e.getMessage(), e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment