use of eu.esdihumboldt.hale.common.align.migrate.AlignmentMigration 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.migrate.AlignmentMigration in project hale by halestudio.
the class ReplaceEntitiesHandler method execute.
/**
* @see IHandler#execute(ExecutionEvent)
*/
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
// collect cells from selection
ISelection selection = HandlerUtil.getCurrentSelection(event);
if (selection instanceof IStructuredSelection) {
List<?> list = ((IStructuredSelection) selection).toList();
// create dummy alignment
DefaultAlignment dummy = new DefaultAlignment();
for (Object object : list) {
if (object instanceof MutableCell) {
dummy.addCell((MutableCell) object);
// FIXME what about others?
}
}
/*
* Replace entities on cells
*/
SimpleReporter reporter = new SimpleReporter("Replace entities for cells", null, false);
try {
// create migrator
AlignmentMigrator migrator = new DefaultAlignmentMigrator(HaleUI.getServiceProvider());
AlignmentMigration migration = new UserMigration(schemaSpace);
MigrationOptions options = new MigrationOptionsImpl(schemaSpace.equals(SchemaSpaceID.SOURCE), schemaSpace.equals(SchemaSpaceID.TARGET), false);
Alignment updated = migrator.updateAligmment(dummy, migration, options, reporter);
AlignmentService as = HaleUI.getServiceProvider().getService(AlignmentService.class);
Map<Cell, MutableCell> replacements = new HashMap<>();
for (Cell newCell : updated.getCells()) {
Cell oldCell = dummy.getCell(newCell.getId());
if (oldCell == null) {
reporter.error("No original cell with ID {0} found", newCell.getId());
} else {
// TODO detect where there has been no change?
replacements.put(oldCell, (MutableCell) newCell);
}
}
as.replaceCells(replacements);
reporter.setSuccess(true);
} catch (Throwable e) {
reporter.error("Fatal error when trying to replace entities", e);
reporter.setSuccess(false);
} finally {
HaleUI.getServiceProvider().getService(ReportService.class).addReport(reporter);
}
}
return null;
}
Aggregations