use of eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition in project hale by halestudio.
the class JoinExplanation method getExplanation.
@Override
protected String getExplanation(Cell cell, boolean html, ServiceProvider services, Locale locale) {
JoinParameter join = CellUtil.getFirstParameter(cell, PARAMETER_JOIN).as(JoinParameter.class);
if (join != null && join.getTypes() != null && !join.getTypes().isEmpty()) {
// types
StringBuilder types = new StringBuilder();
boolean first = true;
for (TypeEntityDefinition type : join.getTypes()) {
if (first) {
first = false;
} else {
types.append(", ");
}
types.append(formatEntity(type, html, false, locale));
}
StringBuilder sb = new StringBuilder();
sb.append(MessageFormat.format(getMessage("main", locale), types.toString()));
sb.append("\n\n");
for (JoinCondition condition : join.getConditions()) {
sb.append(formatFullEntity(condition.baseProperty, html, locale));
sb.append(" = ");
sb.append(formatFullEntity(condition.joinProperty, html, locale));
sb.append('\n');
}
sb.append('\n');
// finalize
String explanation = sb.toString();
if (html)
explanation = explanation.replaceAll("\n", "<br />");
return explanation;
}
return null;
}
use of eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition in project hale by halestudio.
the class JoinMigrator method convertJoinParameter.
/**
* Migrate a join parameter based on an {@link AlignmentMigration}.
*
* @param joinParam the join parameter to migrate
* @param migration the alignment migration
* @param options the migration options
* @param log the migration log
* @return the migrated join parameter
*/
private JoinParameter convertJoinParameter(JoinParameter joinParam, AlignmentMigration migration, MigrationOptions options, SimpleLog log) {
List<TypeEntityDefinition> types = joinParam.getTypes().stream().map(type -> {
return (TypeEntityDefinition) migration.entityReplacement(type, log).orElse(type);
}).collect(Collectors.toList());
Set<JoinCondition> conditions = joinParam.getConditions().stream().map(condition -> {
PropertyEntityDefinition baseProperty = (PropertyEntityDefinition) migration.entityReplacement(condition.baseProperty, log).orElse(condition.baseProperty);
PropertyEntityDefinition joinProperty = (PropertyEntityDefinition) migration.entityReplacement(condition.joinProperty, log).orElse(condition.joinProperty);
JoinCondition result = new JoinCondition(baseProperty, joinProperty);
return result;
}).collect(Collectors.toSet());
return new JoinParameter(types, conditions);
}
use of eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition in project hale by halestudio.
the class JoinContext method apply.
/**
* Apply information collected in the context to the cell.
*
* @param newCell the merged cell
* @param log the cell log
* @param migration the alignment migration
*/
public void apply(MutableCell newCell, AlignmentMigration migration, SimpleLog log) {
ListMultimap<String, ParameterValue> params = ArrayListMultimap.create();
/*
* Order: Keep original order but replace entities w/ all matches
*/
Set<TypeEntityDefinition> types = new LinkedHashSet<>();
for (TypeEntityDefinition type : orgParameter.getTypes()) {
List<TypeEntityDefinition> repl = replacements.get(type);
if (repl.isEmpty()) {
log.error("Could not find replacement for type {0} in join order", type);
types.add(type);
} else {
types.addAll(repl);
}
}
/*
* Conditions: (1) add conditions from matches and (2) add conditions
* from original cell translated to new schema (via property mapping),
* if they are not duplicates
*/
Set<Pair<PropertyEntityDefinition, PropertyEntityDefinition>> cons = new LinkedHashSet<>();
// add conditions from matches
for (Cell match : joinMatches) {
JoinParameter matchParameter = CellUtil.getFirstParameter(match, JoinFunction.PARAMETER_JOIN).as(JoinParameter.class);
for (JoinCondition condition : matchParameter.getConditions()) {
cons.add(new Pair<>(condition.baseProperty, condition.joinProperty));
}
}
// migrate original conditions
Set<JoinCondition> migrated = orgParameter.getConditions().stream().map(condition -> {
PropertyEntityDefinition baseProperty = processOriginalConditionProperty(condition.baseProperty, migration, log);
PropertyEntityDefinition joinProperty = processOriginalConditionProperty(condition.joinProperty, migration, log);
JoinCondition result = new JoinCondition(baseProperty, joinProperty);
return result;
}).collect(Collectors.toSet());
for (JoinCondition condition : migrated) {
if (!condition.baseProperty.equals(condition.joinProperty)) {
// migrated condition may contain "loop" condition
cons.add(new Pair<>(condition.baseProperty, condition.joinProperty));
}
}
// add messages on dropped filter/conditions
for (EntityDefinition stripped : strippedSources) {
if (!AlignmentUtil.isDefaultEntity(stripped)) {
String msg = "Conditions/contexts for an original source could not be transfered and were dropped: " + MergeUtil.getContextInfoString(stripped);
log.warn(msg);
}
}
// all conditions
Set<JoinCondition> conditions = new HashSet<>();
for (Pair<PropertyEntityDefinition, PropertyEntityDefinition> condition : cons) {
conditions.add(new JoinCondition(condition.getFirst(), condition.getSecond()));
}
JoinParameter newParam = new JoinParameter(new ArrayList<>(types), conditions);
params.replaceValues(JoinFunction.PARAMETER_JOIN, Collections.singleton(new ParameterValue(Value.of(newParam))));
// Use Groovy Join if original or match uses a script
if (!scripts.isEmpty()) {
boolean originalScript = scripts.size() == 1 && GroovyJoin.ID.equals(newCell.getTransformationIdentifier());
Text script;
if (originalScript) {
// use original script
script = new Text(scripts.get(0).getSecond());
// create annotation
log.warn("The Groovy script from the original cell was reused, logic and references to sources are very likely not valid anymore.");
} else {
// dummy script with all original scripts
newCell.setTransformationIdentifier(GroovyJoin.ID);
script = buildScript(scripts);
// create annotation
log.warn("At least one source mapping used a Groovy script, the script could not be combined automatically and was replaced with a dummy script (old scripts are commented out). Please check how you can migrate the old functionality.");
}
params.replaceValues(GroovyConstants.PARAMETER_SCRIPT, Collections.singleton(new ParameterValue(Value.of(script))));
}
newCell.setTransformationParameters(params);
}
use of eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition in project hale by halestudio.
the class JoinMergeMigrator method mergeJoinSource.
@SuppressWarnings("unused")
private void mergeJoinSource(MutableCell cell, EntityDefinition source, Cell match, Cell originalCell, SimpleLog log, JoinContext context, boolean groovy) {
/*
* Sources: Add all from match (should be at least two)
*/
addSources(cell, source, match, log, false);
// add source that was replaced and filter/contexts were not retained
context.addStrippedSource(source);
/*
* Join order: Replace type with matched sources (use match join order)
*/
JoinParameter matchParameter = CellUtil.getFirstParameter(match, JoinFunction.PARAMETER_JOIN).as(JoinParameter.class);
if (matchParameter != null) {
context.addOrderReplacement((TypeEntityDefinition) source, matchParameter.getTypes().toArray(new TypeEntityDefinition[matchParameter.getTypes().size()]));
}
// add join match to context (for match conditions)
context.addJoinMatch(match);
if (groovy) {
addScript(match, context);
}
}
use of eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition in project hale by halestudio.
the class JoinMergeMigrator method mergeRetypeSource.
@SuppressWarnings("unused")
private void mergeRetypeSource(MutableCell cell, EntityDefinition source, Cell match, Cell originalCell, SimpleLog log, JoinContext context, boolean groovy) {
/*
* Sources: Add all from match (should be one)
*/
addSources(cell, source, match, log, true);
/*
* Join order: Replace type with matched source
*/
Entity matchEntity = CellUtil.getFirstEntity(match.getSource());
TypeEntityDefinition matchSource = null;
if (matchEntity != null) {
EntityDefinition def = matchEntity.getDefinition();
if (def instanceof TypeEntityDefinition) {
matchSource = (TypeEntityDefinition) def;
}
}
if (matchSource != null) {
context.addOrderReplacement((TypeEntityDefinition) source, matchSource);
} else {
log.error("Match for source {0} is invalid and does not have a type source", source.getDefinition());
return;
}
if (groovy) {
addScript(match, context);
}
}
Aggregations