com.sap.cds.jdbc.generic.LocaleNameTableNameResolver Maven / Gradle / Ivy
/************************************************************************
* © 2019-2023 SAP SE or an SAP affiliate company. All rights reserved. *
************************************************************************/
package com.sap.cds.jdbc.generic;
import static com.sap.cds.DataStoreConfiguration.SUPPORTED_LOCALES;
import static com.sap.cds.impl.localized.LocaleUtils.isLocalized;
import static com.sap.cds.impl.localized.LocaleUtils.localeSpecificViewName;
import java.util.Arrays;
import java.util.Locale;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sap.cds.DataStoreConfiguration;
import com.sap.cds.impl.localized.LocaleUtils;
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-specific views
*/
public class LocaleNameTableNameResolver implements TableNameResolver {
private static final Logger logger = LoggerFactory.getLogger(LocaleNameTableNameResolver.class);
private static final String DEFAULT_SUPPORTED_LOCALES = "de,fr";
private final Function mapping;
protected final DataStoreConfiguration dataStoreConfiguration;
protected final Locale locale;
public LocaleNameTableNameResolver(Function mapping,
DataStoreConfiguration dataStoreConfiguration, Locale locale) {
this.mapping = mapping;
this.dataStoreConfiguration = dataStoreConfiguration;
this.locale = locale;
}
@Override
public String tableName(CdsEntity entity) {
SqlMapping sqlMapping = mapping.apply(entity);
String entityName = entity.getQualifiedName();
if (locale == null || !isLocalized(entity)) {
logger.debug("Resolve {} to plain table.", entityName);
return sqlMapping.tableName();
}
if (hasLocaleSpecificView(locale)) {
String localeSpecifcViewName = localeSpecificViewName(entityName, locale);
logger.debug("Resolve {} using locale-specific view.", entityName);
return sqlMapping.plainTableName(localeSpecifcViewName);
}
logger.debug("Resolve {} using generic localized view.", entityName);
return sqlMapping.localizedViewName();
}
private boolean hasLocaleSpecificView(Locale locale) {
if (locale == null) {
return false;
}
String supportedLocales = dataStoreConfiguration.getProperty(SUPPORTED_LOCALES, DEFAULT_SUPPORTED_LOCALES);
return Arrays.asList(supportedLocales.split(",")).contains(LocaleUtils.getLocaleString(locale));
}
}