use of eu.esdihumboldt.hale.common.align.migrate.impl.MigrationOptionsImpl in project hale by halestudio.
the class AbstractMergeCellMigrator method mergeSources.
/**
* Update the cell sources.
*
* @param sources the old sources
* @param mergeIndex the merge index
* @param originalCell the original cell
* @param migration the alignment migration (may be useful for cases where
* only entity replacement needs to be done)
* @param getCellMigrator functions that yields a cell migrator for a
* function (may be useful for cases where only entity
* replacement needs to be done)
* @param log the migration process log
* @return the merged cell or cells
*/
protected Iterable<MutableCell> mergeSources(ListMultimap<String, ? extends Entity> sources, MergeIndex mergeIndex, Cell originalCell, AlignmentMigration migration, Function<String, CellMigrator> getCellMigrator, SimpleLog log) {
// XXX relevant here at all?
boolean transferBase = true;
if (sources.size() == 1) {
EntityDefinition source = sources.values().iterator().next().getDefinition();
List<Cell> matches = mergeIndex.getCellsForTarget(source);
List<String> storedMessages = new ArrayList<>();
boolean inaccurateMatch = false;
if (matches.isEmpty()) {
// try to find match via parent (in specific cases)
matches = findParentMatch(source, mergeIndex);
if (!matches.isEmpty()) {
inaccurateMatch = true;
// message may not be added in every case, because it may be
// a duplicate
// storedMessages.add(MessageFormat
// .format("Inaccurate match of {0} via parent entity", source));
}
}
if (!matches.isEmpty()) {
List<MutableCell> cells = new ArrayList<>();
for (Cell match : matches) {
// the original cell
if (isDirectMatch(match)) {
MigrationOptions replaceSource = new MigrationOptionsImpl(true, false, transferBase);
cells.add(getCellMigrator.apply(originalCell.getTransformationIdentifier()).updateCell(originalCell, migration, replaceSource, log));
} else // matching cell
if (isDirectMatch(originalCell)) {
MigrationOptions replaceTarget = new MigrationOptionsImpl(false, true, transferBase);
AlignmentMigration cellMigration = new AbstractMigration() {
@Override
protected Optional<EntityDefinition> findMatch(EntityDefinition entity) {
Entity target = CellUtil.getFirstEntity(originalCell.getTarget());
if (target != null) {
return Optional.ofNullable(target.getDefinition());
}
return Optional.empty();
}
};
MutableCell newCell = getCellMigrator.apply(match.getTransformationIdentifier()).updateCell(match, cellMigration, replaceTarget, log);
SimpleLog cellLog = SimpleLog.all(log, new CellLog(newCell, CELL_LOG_CATEGORY));
// source of original cell may have
// filters/conditions/contexts that are not applied by
// changing the target
// try to apply source contexts
Entity originalSource = CellUtil.getFirstEntity(originalCell.getSource());
applySourceContexts(newCell, originalSource, cellLog);
cells.add(newCell);
} else {
// otherwise, use custom logic to try to combine cells
MutableCell newCell = new DefaultCell(originalCell);
SimpleLog cellLog = SimpleLog.all(log, new CellLog(newCell, CELL_LOG_CATEGORY));
C context = newContext(originalCell);
// reset source
newCell.setSource(ArrayListMultimap.create());
if (inaccurateMatch) {
cellLog.warn(MessageFormat.format("Inaccurate match of {0} via parent entity", source));
}
mergeSource(newCell, sources.keys().iterator().next(), source, match, originalCell, cellLog, context, migration, mergeIndex);
finalize(newCell, migration, context, cellLog);
cells.add(newCell);
}
}
if (!cells.isEmpty() && !storedMessages.isEmpty()) {
// add stored messages
cells.forEach(cell -> {
CellLog cLog = new CellLog(cell, CELL_LOG_CATEGORY);
storedMessages.forEach(msg -> cLog.warn(msg));
});
}
return cells;
} else {
// no match -> remove?
// rather add original + documentation
MutableCell newCell = new DefaultCell(originalCell);
SimpleLog cellLog = SimpleLog.all(log, new CellLog(newCell, CELL_LOG_CATEGORY));
cellLog.warn("No match for source {0} found, unable to associate to new source schema", source);
return Collections.singleton(newCell);
}
} else {
// handle each source
// collects messages in case all matches are direct matches
List<String> directMessages = new ArrayList<>();
// determine if all matches are direct
boolean allDirect = sources.entries().stream().allMatch(source -> {
List<Cell> matches = mergeIndex.getCellsForTarget(source.getValue().getDefinition());
if (matches.isEmpty()) {
directMessages.add(MessageFormat.format("No match was found for source {0}, please check how this can be compensated.", source.getValue().getDefinition()));
// if there is no match, treat it as direct match
return true;
} else {
if (matches.size() > 1) {
directMessages.add(MessageFormat.format("Multiple matches for source {0}, only one was taken into account", source.getValue().getDefinition()));
}
return isDirectMatch(matches.get(0));
}
});
MutableCell newCell;
if (allDirect) {
// if the matching are all Retype/Rename, replace sources of
// the original cell
MigrationOptions replaceSource = new MigrationOptionsImpl(true, false, transferBase);
newCell = getCellMigrator.apply(originalCell.getTransformationIdentifier()).updateCell(originalCell, migration, replaceSource, log);
// add messages from match check
SimpleLog cellLog = SimpleLog.all(log, new CellLog(newCell, CELL_LOG_CATEGORY));
directMessages.forEach(msg -> cellLog.warn(msg));
} else {
// handle each source separately
newCell = new DefaultCell(originalCell);
SimpleLog cellLog = SimpleLog.all(log, new CellLog(newCell, CELL_LOG_CATEGORY));
C context = newContext(originalCell);
// reset source
newCell.setSource(ArrayListMultimap.create());
for (Entry<String, ? extends Entity> source : sources.entries()) {
List<Cell> matches = mergeIndex.getCellsForTarget(source.getValue().getDefinition());
if (!matches.isEmpty()) {
Cell match = matches.get(0);
mergeSource(newCell, source.getKey(), source.getValue().getDefinition(), match, originalCell, cellLog, context, migration, mergeIndex);
if (matches.size() > 1) {
// FIXME how can we deal w/ multiple matches?
cellLog.warn("Multiple matches for source {0}, only one was handled", source.getValue().getDefinition());
}
} else {
// no match, just not add source?
cellLog.warn("No match was found for source {0}, please check how this can be compensated.", source.getValue().getDefinition());
}
}
finalize(newCell, migration, context, cellLog);
}
return Collections.singleton(newCell);
}
}
use of eu.esdihumboldt.hale.common.align.migrate.impl.MigrationOptionsImpl 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