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

com.sap.cds.adapter.odata.v2.query.CustomQueryLoader Maven / Gradle / Ivy

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

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sap.cds.adapter.odata.v2.search.SearchParser;
import com.sap.cds.impl.builder.model.PassThroughSearchPredicate;
import com.sap.cds.ql.Select;
import com.sap.cds.ql.cqn.CqnPredicate;
import com.sap.cds.services.environment.CdsProperties;
import com.sap.cds.services.utils.ErrorStatusException;
import com.sap.cds.services.utils.QueryParameters;
import com.sap.cds.services.utils.StringUtils;

public class CustomQueryLoader {

	private static final String SEARCH_ODATA_LENIENT = "odata-lenient";
	private static final String SEARCH_PASS_THROUGH = "pass-through";
	private static final Logger logger = LoggerFactory.getLogger(CustomQueryLoader.class);

	private CustomQueryLoader() {
	}

	/**
	 * Add custom query options such as 'search' to select statement.
	 *
	 * @param select      select statement
	 * @param queryParams custom query parameters
	 */
	public static void applySystemQueryOptions(Select select, Map queryParams, CdsProperties cdsProperties) {
		// search
		String searchString = queryParams.get(QueryParameters.SAP_SEARCH);
		if (!StringUtils.isEmpty(searchString)) {
			String searchMode = cdsProperties.getOdataV2().getSearchMode();
			if (SEARCH_PASS_THROUGH.equalsIgnoreCase(searchMode)) {
				// cds.odataV2.searchMode: pass-through
				select.search(new PassThroughSearchPredicate(searchString));
			} else {
				try {
					CqnPredicate searchPredicate = new SearchParser().parse(searchString);
					select.search(searchPredicate);
				} catch (ErrorStatusException e) {
					if (SEARCH_ODATA_LENIENT.equalsIgnoreCase(searchMode)) {
						// cds.odataV2.searchMode: odata-lenient
						logger.debug("Failed to parse search string, fall back to pass-through search: {}", e.getMessage());
						select.search(new PassThroughSearchPredicate(searchString));
					} else {
						throw e;
					}
				}
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy