Search in sources :

Example 46 with TypeEntityDefinition

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;
}
Also used : TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition)

Example 47 with TypeEntityDefinition

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);
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) SimpleLog(eu.esdihumboldt.hale.common.core.report.SimpleLog) Cell(eu.esdihumboldt.hale.common.align.model.Cell) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) ListMultimap(com.google.common.collect.ListMultimap) Set(java.util.Set) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) AlignmentMigration(eu.esdihumboldt.hale.common.align.migrate.AlignmentMigration) Collectors(java.util.stream.Collectors) DefaultCellMigrator(eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator) List(java.util.List) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) JoinFunction(eu.esdihumboldt.hale.common.align.model.functions.JoinFunction) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) MigrationOptions(eu.esdihumboldt.hale.common.align.migrate.MigrationOptions) CellLog(eu.esdihumboldt.hale.common.align.model.annotations.messages.CellLog) Value(eu.esdihumboldt.hale.common.core.io.Value) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition)

Example 48 with TypeEntityDefinition

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Arrays(java.util.Arrays) Cell(eu.esdihumboldt.hale.common.align.model.Cell) Text(eu.esdihumboldt.hale.common.core.io.Text) ListMultimap(com.google.common.collect.ListMultimap) GroovyConstants(eu.esdihumboldt.cst.functions.groovy.GroovyConstants) AlignmentMigration(eu.esdihumboldt.hale.common.align.migrate.AlignmentMigration) AlignmentUtil(eu.esdihumboldt.hale.common.align.model.AlignmentUtil) MergeUtil(eu.esdihumboldt.hale.common.align.merge.MergeUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) JoinFunction(eu.esdihumboldt.hale.common.align.model.functions.JoinFunction) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) Pair(eu.esdihumboldt.util.Pair) LinkedHashSet(java.util.LinkedHashSet) Value(eu.esdihumboldt.hale.common.core.io.Value) SimpleLog(eu.esdihumboldt.hale.common.core.report.SimpleLog) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) Set(java.util.Set) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Collectors(java.util.stream.Collectors) CellUtil(eu.esdihumboldt.hale.common.align.model.CellUtil) GroovyJoin(eu.esdihumboldt.cst.functions.groovy.GroovyJoin) List(java.util.List) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) Collections(java.util.Collections) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Text(eu.esdihumboldt.hale.common.core.io.Text) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) Cell(eu.esdihumboldt.hale.common.align.model.Cell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) Pair(eu.esdihumboldt.util.Pair) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 49 with TypeEntityDefinition

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);
    }
}
Also used : TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter)

Example 50 with TypeEntityDefinition

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);
    }
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)

Aggregations

TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)64 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)23 ArrayList (java.util.ArrayList)19 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)16 Type (eu.esdihumboldt.hale.common.align.model.Type)16 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)16 DefaultType (eu.esdihumboldt.hale.common.align.model.impl.DefaultType)15 Cell (eu.esdihumboldt.hale.common.align.model.Cell)14 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)12 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)11 JoinParameter (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter)10 QName (javax.xml.namespace.QName)9 MutableAlignment (eu.esdihumboldt.hale.common.align.model.MutableAlignment)8 DefaultAlignment (eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment)8 JoinCondition (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition)6 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)6 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)6 LinkedList (java.util.LinkedList)6 Entity (eu.esdihumboldt.hale.common.align.model.Entity)5 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)5