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

com.sap.cds.jdbc.generic.LocaleVariableTableNameResolver Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
/*******************************************************************
 * © 2019 SAP SE or an SAP affiliate company. All rights reserved. *
 *******************************************************************/
package com.sap.cds.jdbc.generic;

import static com.sap.cds.impl.localized.LocaleUtils.isLocalized;

import java.util.Locale;
import java.util.function.Function;

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

import com.sap.cds.jdbc.spi.SqlMapping;
import com.sap.cds.jdbc.spi.TableNameResolver;
import com.sap.cds.reflect.CdsEntity;
import com.sap.cds.reflect.CdsStructuredType;

/**
 * A {@link TableNameResolver} that resolves localized strings using
 * locale-unspecific views using session variables
 */
public class LocaleVariableTableNameResolver implements TableNameResolver {
	private static final Logger logger = LoggerFactory.getLogger(LocaleVariableTableNameResolver.class);

	private final Function mapping;
	private final Locale locale;

	public LocaleVariableTableNameResolver(Function mapping, Locale locale) {
		this.mapping = mapping;
		this.locale = locale;
	}

	@Override
	public String tableName(CdsEntity entity) {
		SqlMapping sqlMapping = mapping.apply(entity);
		String entityName = entity.getQualifiedName();
		if (isLocalized(entity) && locale != null) {
			String localizedViewName = sqlMapping.localizedViewName();
			logger.debug("Locale is {}. Resolve {} using generic localized view {}.", locale.toLanguageTag(),
					entityName, localizedViewName);

			return localizedViewName;
		}
		String tableName = sqlMapping.tableName();

		logger.debug("No locale set. Resolve {} to plain table {}.", entityName, tableName);

		return tableName;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy