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

com.sap.cds.adapter.odata.v4.query.apply.ApplyConverter Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
/**************************************************************************
 * (C) 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. *
 **************************************************************************/
package com.sap.cds.adapter.odata.v4.query.apply;

import static com.sap.cds.ql.cqn.transformation.CqnTransformation.IDENTITY;

import java.util.ArrayList;
import java.util.List;

import org.apache.olingo.server.api.uri.queryoption.ApplyItem;
import org.apache.olingo.server.api.uri.queryoption.ApplyItem.Kind;
import org.apache.olingo.server.api.uri.queryoption.ApplyOption;
import org.apache.olingo.server.api.uri.queryoption.apply.Aggregate;
import org.apache.olingo.server.api.uri.queryoption.apply.Ancestors;
import org.apache.olingo.server.api.uri.queryoption.apply.Compute;
import org.apache.olingo.server.api.uri.queryoption.apply.Concat;
import org.apache.olingo.server.api.uri.queryoption.apply.CustomFunction;
import org.apache.olingo.server.api.uri.queryoption.apply.Descendants;
import org.apache.olingo.server.api.uri.queryoption.apply.Filter;
import org.apache.olingo.server.api.uri.queryoption.apply.GroupBy;
import org.apache.olingo.server.api.uri.queryoption.apply.OrderBy;
import org.apache.olingo.server.api.uri.queryoption.apply.Search;
import org.apache.olingo.server.api.uri.queryoption.apply.Skip;
import org.apache.olingo.server.api.uri.queryoption.apply.Top;

import com.sap.cds.adapter.odata.v4.query.ExpressionParser;
import com.sap.cds.ql.cqn.transformation.CqnTransformation;
import com.sap.cds.services.utils.CdsErrorStatuses;
import com.sap.cds.services.utils.ErrorStatusException;

public class ApplyConverter {

	private ExpressionParser expressionParser;

	public ApplyConverter(ExpressionParser expressionParser) {
		this.expressionParser = expressionParser;
	}
	
	public List> unfoldAndConvert(List items) {
		int n = items.size();
		ApplyItem last;
		if (n > 0 && (last = items.get(n - 1)).getKind() == Kind.CONCAT) {
			List stem = items.subList(0, n-1);
			return unfoldConcats(stem, (Concat) last);
		} else {
			return List.of(items.stream().map(this::transformation).toList());
		}
	}
	
	private List> unfoldConcats(List stem, Concat concat) {
			List concats = concat.getApplyOptions();
			int m = concats.size();
			List> all = new ArrayList<>(m);
			for (int i = 0; i < m; i++) {
				List unfolded = new ArrayList<>(stem);
				List concatItems = concats.get(i).getApplyItems();
				unfolded.addAll(concatItems);
				
				all.addAll(unfoldAndConvert(unfolded));
			}
			return all;
	}

	private CqnTransformation transformation(ApplyItem applyItem) {
		Kind kind = applyItem.getKind();
		switch (kind) {
			case IDENTITY:
				return IDENTITY;
			case AGGREGATE:
				return new AggregateTransformation((Aggregate) applyItem, expressionParser);
			case GROUP_BY:
				return new GroupByTransformation((GroupBy) applyItem, expressionParser);
			case ORDERBY:
				return new OrderByTransformation((OrderBy) applyItem, expressionParser);
			case TOP:
				return new TopTransformation((Top) applyItem);
			case SKIP:
				return new SkipTransformation((Skip) applyItem);
			case SEARCH:
				return new SearchTransformation((Search) applyItem);
			case FILTER:
				return new FilterTransformation((Filter) applyItem, expressionParser);
			case COMPUTE:
				return new ComputeTransformation((Compute) applyItem, expressionParser);
			case CUSTOM_FUNCTION:
				CustomFunction function = (CustomFunction) applyItem;
				if (TopLevelsTransformation.handles(function)) {
					return TopLevelsTransformation.topLevels(function, expressionParser);
				}
				return new CustomTransformation((CustomFunction) applyItem, expressionParser);
			case ANCESTORS:
				return  HierarchySubsetTransformation.ancestors((Ancestors) applyItem, expressionParser);
			case DESCENDANTS:
				return HierarchySubsetTransformation.descendants((Descendants) applyItem, expressionParser);
			case CONCAT:
				throw new ErrorStatusException(CdsErrorStatuses.UNSUPPORTED_CONCAT_LAST, kind.name());
			case EXPAND:
			default:
				throw new ErrorStatusException(CdsErrorStatuses.UNSUPPORTED_APPLY_OPTION, kind.name());
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy