Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public enum Level {
public static final String DIALECTS = "dialects";
public static final String DIALECT = "dialect";
public static final String DIALECT_TABLEAU = "TABLEAU";
public static final String DIALECT_ANSI_SQL = "ANSI_SQL";

// Relationship properties
public static final String CRITERIA = "criteria";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.apache.ossie.converter.ConverterConstants.Level;
import org.apache.ossie.converter.pipeline.PipelineStep;
import org.apache.ossie.exception.ConversionException;
import java.util.*;

import org.apache.ossie.util.MappingUtils;
Expand Down Expand Up @@ -78,9 +79,34 @@ private void mapOssieToSalesforce(

// Filter mappings to get only metric-related entries
Map<String, String> metricMappings = MappingUtils.filterMappingsByPrefix(mappings, METRICS);

Map<String, Object> mappedData = GenericMappingEngine.applyMappings(sourceData, metricMappings);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metric custom_extensions are never restored in this direction. DatasetMappingHandler and RelationshipMappingHandler both call restoreCustomExtensionAtLevel(...) after applyMappings, and CustomExtensionHandler already has a Level.METRICS branch, but nothing calls it. Salesforce -> Ossie stores label, aggregationType, level, dependencies, displayCategory, decimalPlace and the exact dataType in each metric's custom_extensions, so a round trip currently drop all of them.

Also, Ossie metrics have no label field. Unless custom_extensions restores one, the exported measurement has no label at all. The dataset and model handlers default label to apiName, and metrics should do the same.

Restore has to run before unwrapExpressions(), because restoreSalesforceCustomExtension only fills keys that are still absent. A test with a metric that carries SALESFORCE custom_extensions (for example label and dataType: Currency) would cover this.

Suggested change
Map<String, Object> mappedData = GenericMappingEngine.applyMappings(sourceData, metricMappings);
outputData.putAll(mappedData);
// Restore before unwrapExpressions(): restore only fills absent keys, so exact
// Salesforce values (e.g. dataType Currency) must land before derived ones.
customExtensionHandler.restoreCustomExtensionsAtLevel(outputData, sourceData, Level.METRICS);
List<Object> sfMetrics = getList(outputData, SEMANTIC_CALCULATED_MEASUREMENTS);
if (sfMetrics != null) {
unwrapExpressions(ossieMetrics, sfMetrics);
applyDefaults(sfMetrics);
}
}
/**
* Applies default values for required Salesforce calculated measurement properties.
* Used when converting Ossie → Salesforce.
*/
private void applyDefaults(List<Object> sfMetrics) {
for (Object sfMetricObj : sfMetrics) {
Map<String, Object> sfMetric = asMap(sfMetricObj);
if (!sfMetric.containsKey(LABEL) && sfMetric.containsKey(API_NAME)) {
sfMetric.put(LABEL, getString(sfMetric, API_NAME));
}
}
}

metricMappings.keySet().forEach(mappings::remove);

logger.debug("Metrics are not mapped in Ossie to Salesforce direction");
outputData.putAll(mappedData);

// Restore before unwrapExpressions(): restore only fills absent keys, so exact
// Salesforce values (e.g. dataType Currency) must land before derived ones.
customExtensionHandler.restoreCustomExtensionsAtLevel(outputData, sourceData, Level.METRICS);

List<Object> sfMetrics = getList(outputData, SEMANTIC_CALCULATED_MEASUREMENTS);
if (sfMetrics != null) {
unwrapExpressions(ossieMetrics, sfMetrics);
applyDefaults(sfMetrics);
}
}

/**
* Applies default values for required Salesforce calculated measurement properties.
* Used when converting Ossie → Salesforce.
*/
private void applyDefaults(List<Object> sfMetrics) {
for (Object sfMetricObj : sfMetrics) {
Map<String, Object> sfMetric = asMap(sfMetricObj);
if (!sfMetric.containsKey(LABEL) && sfMetric.containsKey(API_NAME)) {
sfMetric.put(LABEL, getString(sfMetric, API_NAME));
}
}
}

/**
Expand Down Expand Up @@ -118,6 +144,60 @@ private void mapSalesforceToOssie(
}


/**
* Unwraps expressions for Ossie→SF conversion, mirroring {@link #wrapExpressions}.
*
* <p>Picks the {@code TABLEAU}-dialect expression out of each Ossie metric's
* {@code expression.dialects[]} and flattens it into the Salesforce metric's
* {@code expression} string. {@code TABLEAU} is what Salesforce/Tableau CRM itself speaks;
* an {@code ANSI_SQL} expression is not translated to Tableau syntax, since that is the scope
* of #222's expression-language work, not this fix (see #403). A metric with no TABLEAU
* dialect fails the conversion rather than exporting an untranslated expression (#399).
*/
private void unwrapExpressions(List<Object> ossieMetrics, List<Object> sfMetrics) {
for (int i = 0; i < ossieMetrics.size() && i < sfMetrics.size(); i++) {
Map<String, Object> ossieMetric = asMap(ossieMetrics.get(i));
Map<String, Object> sfMetric = asMap(sfMetrics.get(i));

String expressionValue = extractExpression(ossieMetric, DIALECT_TABLEAU);
if (expressionValue == null) {
throw new ConversionException(
"Metric '" + getString(ossieMetric, NAME) + "' has no TABLEAU expression "
+ "to export to Salesforce; add one to expression.dialects[] or "
+ "remove the metric.");
}
sfMetric.put(EXPRESSION, expressionValue);

String datatype = SalesforceDataTypeMapper.toSalesforce(getString(ossieMetric, OSSIE_DATATYPE));
if (datatype != null) {
// Exact Salesforce value restored from custom_extensions wins over the
// mapped Ossie datatype (e.g. a restored "Currency" over a mapped "Decimal").
sfMetric.putIfAbsent(DATA_TYPE, datatype);
}
}
}

/**
* Finds the given dialect's expression string in an Ossie metric's
* {@code expression.dialects[]}, or {@code null} when the metric has no expression or no
* entry for that dialect.
*/
private String extractExpression(Map<String, Object> ossieMetric, String dialect) {
Map<String, Object> expression = getMap(ossieMetric, EXPRESSION);
if (expression == null) {
return null;
}
List<Object> dialects = getList(expression, DIALECTS);
if (dialects == null) {
return null;
}
return streamMaps(dialects)
.filter(d -> dialect.equals(getString(d, DIALECT)))
.map(d -> getString(d, EXPRESSION))
.findFirst()
.orElse(null);
}

/**
* Wraps expressions for SF→Ossie conversion.
*/
Expand Down
Loading
Loading