com.sap.cds.adapter.odata.v2.utils.AggregateTransformation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cds-adapter-odata-v2 Show documentation
Show all versions of cds-adapter-odata-v2 Show documentation
OData V2 adapter for CDS Services Java
/**************************************************************************
* (C) 2019-2021 SAP SE or an SAP affiliate company. All rights reserved. *
**************************************************************************/
package com.sap.cds.adapter.odata.v2.utils;
import static com.sap.cds.impl.builder.model.ElementRefImpl.element;
import static com.sap.cds.ql.CQL.sort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.olingo.odata2.api.edm.EdmException;
import org.apache.olingo.odata2.api.uri.SelectItem;
import org.apache.olingo.odata2.api.uri.UriInfo;
import com.sap.cds.ql.CQL;
import com.sap.cds.ql.ElementRef;
import com.sap.cds.ql.Select;
import com.sap.cds.ql.Value;
import com.sap.cds.ql.cqn.CqnElementRef;
import com.sap.cds.ql.cqn.CqnSelectListValue;
import com.sap.cds.ql.cqn.CqnSortSpecification;
import com.sap.cds.ql.cqn.CqnValue;
import com.sap.cds.reflect.CdsAnnotatable;
import com.sap.cds.reflect.CdsAnnotation;
import com.sap.cds.reflect.CdsBaseType;
import com.sap.cds.reflect.CdsElement;
import com.sap.cds.reflect.CdsEntity;
import com.sap.cds.reflect.CdsSimpleType;
import com.sap.cds.reflect.CdsType;
import com.sap.cds.services.ServiceException;
import com.sap.cds.services.utils.CdsErrorStatuses;
import com.sap.cds.services.utils.ErrorStatusException;
import com.sap.cds.util.CdsModelUtils;
public class AggregateTransformation {
private static final String AGGREGATION_DEFAULT = "Aggregation.default";
private static final String ANALYTICS_MEASURE = "Analytics.Measure";
private static final String SUPPORTED_RESTRICTIONS = "Aggregation.ApplySupported.PropertyRestrictions";
private static final String SAP_AGGREGATION_ROLE = "sap.aggregation-role";
public static final String AGGREGATE_ID = "ID__";
private final CdsEntity target;
private final Select> select;
private final UriInfo uriInfo;
private List dimensions;
private List selectListItems;
public AggregateTransformation(CdsEntity target, Select> select, UriInfo uriInfo) {
this.target = target;
this.select = select;
this.uriInfo = uriInfo;
}
public boolean applyAggregation() {
boolean applied = false;
if (isAggregateEntity()) {
dimensions = new ArrayList<>();
selectListItems = new ArrayList<>();
uriInfo.getSelect().forEach(this::collectListItems);
select.columns(selectListItems);
select.groupBy(dimensions);
select.orderBy(getOrderBy());
applied = true;
}
// $count
if (uriInfo.isCount()) {
select.columns(CQL.count().as("count"));
}
return applied;
}
private void collectListItems(SelectItem i) {
try {
String itemName = i.getProperty().getName();
// exclude technical ID__ from select list as it is not in CDS Model
if (isAggregateID(itemName, target)) {
return;
}
target.getQualifier();
CqnElementRef ref = CQL.get(itemName);
CdsElement element = target.getElement(itemName);
if (isMeasure(element)) {
selectListItems.add(toFunctionCall(element).as(itemName));
} else {
// if element is not an aggregate, add it to group by
selectListItems.add(ref);
dimensions.add(ref);
}
} catch (EdmException e) {
throw new ServiceException(e);
}
}
private Value> toFunctionCall(CdsElement element) {
ElementRef
© 2015 - 2025 Weber Informatics LLC | Privacy Policy