use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.
the class AppSchemaMappingTest method testFormattedStringHandler.
@Test
public void testFormattedStringHandler() {
final String PATTERN = "Class 2007: {ucs2007}; Class 2013: {ucs2013}";
final String OCQL = "strConcat(strConcat(strConcat('Class 2007: ', ucs2007), '; Class 2013: '), ucs2013)";
DefaultCell cell = new DefaultCell();
cell.setTransformationIdentifier(FormattedStringFunction.ID);
ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
parameters.put(FormattedStringFunction.PARAMETER_PATTERN, new ParameterValue(PATTERN));
ListMultimap<String, Property> source = ArrayListMultimap.create();
source.putAll(FormattedStringFunction.ENTITY_VARIABLE, getUcs2007SourceProperty().values());
source.putAll(FormattedStringFunction.ENTITY_VARIABLE, getUcs2013SourceProperty().values());
cell.setSource(source);
cell.setTarget(getDescriptionTargetProperty());
cell.setTransformationParameters(parameters);
FormattedStringHandler handler = new FormattedStringHandler();
AttributeMappingType attrMapping = handler.handlePropertyTransformation(getDefaultTypeCell(unitDenormType, landCoverUnitType), cell, new AppSchemaMappingContext(mappingWrapper));
assertEquals(OCQL, attrMapping.getSourceExpression().getOCQL());
assertEquals(TARGET_DESCRIPTION, attrMapping.getTargetAttribute());
}
use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.
the class AppSchemaMappingTest method testEncodeIfEmptyTrue.
@Test
public void testEncodeIfEmptyTrue() {
final String XLINK_HREF_CONSTANT = "#lcu.123";
final String OCQL_LITERAL = "'" + XLINK_HREF_CONSTANT + "'";
DefaultCell assign = new DefaultCell();
assign.setTransformationIdentifier(AssignFunction.ID);
ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
parameters.put(AssignFunction.PARAMETER_VALUE, new ParameterValue(XLINK_HREF_CONSTANT));
assign.setTransformationParameters(parameters);
assign.setTarget(getNestedUnitHrefTargetProperty());
AssignHandler assignHandler = new AssignHandler();
AttributeMappingType attrMapping = assignHandler.handlePropertyTransformation(getDefaultTypeCell(datasetType, landCoverDatasetType), assign, new AppSchemaMappingContext(mappingWrapper));
assertNull(attrMapping.getSourceExpression());
assertEquals("lcv:member", attrMapping.getTargetAttribute());
assertNotNull(attrMapping.getClientProperty());
assertEquals(1, attrMapping.getClientProperty().size());
assertEquals("xlink:href", attrMapping.getClientProperty().get(0).getName());
assertEquals(OCQL_LITERAL, attrMapping.getClientProperty().get(0).getValue());
// expression is constant, so encodeIfEmpty=true
assertNotNull(attrMapping.isEncodeIfEmpty());
assertTrue(attrMapping.isEncodeIfEmpty());
}
use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.
the class ClassificationHandler method getSourceExpressionAsCQL.
/**
* @see eu.esdihumboldt.hale.io.appschema.writer.internal.AbstractPropertyTransformationHandler#getSourceExpressionAsCQL()
*/
@Override
protected String getSourceExpressionAsCQL() {
Property source = AppSchemaMappingUtils.getSourceProperty(propertyCell);
PropertyDefinition sourceDef = source.getDefinition().getDefinition();
Property target = AppSchemaMappingUtils.getTargetProperty(propertyCell);
PropertyDefinition targetDef = target.getDefinition().getDefinition();
String sourceName = source.getDefinition().getDefinition().getName().getLocalPart();
ListMultimap<String, ParameterValue> parameters = propertyCell.getTransformationParameters();
LookupTable lookup = ClassificationMappingUtil.getClassificationLookup(parameters, new ServiceManager(ServiceManager.SCOPE_PROJECT));
if (lookup == null) {
log.warn("No classification specified");
return "''";
} else {
String cqlTemplate = "if_then_else(in(%s), Recode(%s,%s), %s)";
// build args to Recode function
StringBuilder recodeArgsBuilder = new StringBuilder();
Map<Value, Value> valueMap = lookup.asMap();
int counter = 0;
for (Value sourceValue : valueMap.keySet()) {
Value targetValue = valueMap.get(sourceValue);
String sourceLiteral = asCqlLiteral(sourceDef, sourceValue.as(String.class));
String targetLiteral = asCqlLiteral(targetDef, targetValue.as(String.class));
recodeArgsBuilder.append(sourceLiteral).append(",").append(targetLiteral);
if (counter < valueMap.size() - 1) {
recodeArgsBuilder.append(",");
}
counter++;
}
String recodeArgs = recodeArgsBuilder.toString();
// build args for in function
List<String> values = new ArrayList<String>();
for (Value v : valueMap.keySet()) {
String valueLiteral = asCqlLiteral(sourceDef, v.as(String.class));
values.add(valueLiteral);
}
values.add(0, sourceName);
String inArgs = Joiner.on(",").join(values);
// determine what to put in the "else" branch, based on
// transformation parameters
String elsePart = null;
List<ParameterValue> notClassifiedParam = parameters.get(PARAMETER_NOT_CLASSIFIED_ACTION);
String notClassifiedAction = null;
if (notClassifiedParam != null && notClassifiedParam.size() > 0) {
notClassifiedAction = notClassifiedParam.get(0).as(String.class);
} else {
notClassifiedAction = USE_NULL_ACTION;
}
if (USE_SOURCE_ACTION.equals(notClassifiedAction))
elsePart = sourceName;
else if (notClassifiedAction.startsWith(USE_FIXED_VALUE_ACTION_PREFIX))
elsePart = asCqlLiteral(targetDef, notClassifiedAction.substring(notClassifiedAction.indexOf(':') + 1));
else if (USE_NULL_ACTION.equals(notClassifiedAction))
elsePart = "Expression.NIL";
return String.format(cqlTemplate, inArgs, sourceName, recodeArgs, elsePart);
}
}
use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.
the class FormattedStringHandler method getSourceExpressionAsCQL.
/**
* @see eu.esdihumboldt.hale.io.appschema.writer.internal.AbstractPropertyTransformationHandler#getSourceExpressionAsCQL()
*/
@Override
protected String getSourceExpressionAsCQL() {
ListMultimap<String, ParameterValue> parameters = propertyCell.getTransformationParameters();
String pattern = parameters.get(PARAMETER_PATTERN).get(0).as(String.class);
List<int[]> startEndList = new ArrayList<int[]>();
List<String> varList = new ArrayList<String>();
Matcher m = VARIABLE_PATTERN.matcher(pattern);
while (m.find()) {
int[] startEnd = new int[2];
// index of '{' character
startEnd[0] = m.start();
// index of '}' character
startEnd[1] = m.end();
startEndList.add(startEnd);
// the variable name, without curly braces
varList.add(m.group(1));
}
// list of string to be concatenated, either string constants or
// variable names
String[] partsToConcat = new String[varList.size() * 2 + 1];
int lastPos = 0;
for (int i = 0; i < varList.size(); i++) {
int[] startEnd = startEndList.get(i);
String var = varList.get(i);
String textBeforeVar = pattern.substring(lastPos, startEnd[0]);
if (textBeforeVar != null && textBeforeVar.length() == 0) {
textBeforeVar = null;
}
partsToConcat[i * 2] = (textBeforeVar != null) ? "'" + textBeforeVar + "'" : null;
partsToConcat[i * 2 + 1] = var;
lastPos = startEnd[1];
}
// add text after last variable
String textAfterLastVar = pattern.substring(lastPos, pattern.length());
if (textAfterLastVar != null && textAfterLastVar.length() == 0) {
textAfterLastVar = null;
}
partsToConcat[partsToConcat.length - 1] = (textAfterLastVar != null) ? "'" + textAfterLastVar + "'" : null;
String strConcatExpr = "";
int lastPartIdx = 0;
while (lastPartIdx < partsToConcat.length) {
if (lastPartIdx == 0) {
// initialize strConcatExpr
for (int notNullIdx = lastPartIdx; notNullIdx < partsToConcat.length; notNullIdx++) {
if (partsToConcat[notNullIdx] != null) {
strConcatExpr = partsToConcat[notNullIdx];
lastPartIdx = notNullIdx;
break;
}
}
}
String secondArgument = null;
for (int notNullIdx = lastPartIdx + 1; notNullIdx < partsToConcat.length; notNullIdx++) {
if (partsToConcat[notNullIdx] != null) {
secondArgument = partsToConcat[notNullIdx];
lastPartIdx = notNullIdx;
break;
}
}
if (secondArgument != null) {
strConcatExpr = "strConcat(" + strConcatExpr + ", " + secondArgument + ")";
} else {
// no second argument could be found: should stop here
break;
}
}
// expression should be evaluated only if all conditions are met
if (propertyCell.getSource() != null) {
List<? extends Entity> sourceEntities = propertyCell.getSource().get(FormattedStringFunction.ENTITY_VARIABLE);
if (sourceEntities != null) {
for (Entity source : sourceEntities) {
PropertyEntityDefinition propEntityDef = (PropertyEntityDefinition) source.getDefinition();
strConcatExpr = getConditionalExpression(propEntityDef, strConcatExpr);
}
}
}
return strConcatExpr;
}
use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.
the class AdVMeasurementMigrator method mergeSource.
@Override
protected void mergeSource(MutableCell cell, String sourceName, EntityDefinition source, Cell match, Cell originalCell, SimpleLog log, Void context, AlignmentMigration migration, MergeIndex mergeIndex) {
if (AssignFunction.ID_BOUND.equals(match.getTransformationIdentifier())) {
// get value used in bound assign
String value = CellUtil.getFirstParameter(match, AssignFunction.PARAMETER_VALUE).as(String.class);
// convert value according to custom function
value = convertCode(value, log);
// configure cell
cell.setTransformationIdentifier(AssignFunction.ID_BOUND);
cell.setSource(ArrayListMultimap.create(match.getSource()));
ListMultimap<String, ParameterValue> params = ArrayListMultimap.create();
params.put(AssignFunction.PARAMETER_VALUE, new ParameterValue(value));
cell.setTransformationParameters(params);
return;
}
super.mergeSource(cell, sourceName, source, match, originalCell, log, context, migration, mergeIndex);
}
Aggregations