Search in sources :

Example 61 with ChildContext

use of eu.esdihumboldt.hale.common.align.model.ChildContext in project hale by halestudio.

the class EntityDefinitionServiceImpl method replace.

/**
 * Creates a new ListMultimap with all occurrences of originalDef replaced
 * by newDef. newDef must be a sibling of originalDef.
 *
 * @param entities the original list
 * @param originalDef the entity definition to be replaced
 * @param newDef the entity definition to use
 * @return a new list
 */
private ListMultimap<String, ? extends Entity> replace(ListMultimap<String, ? extends Entity> entities, EntityDefinition originalDef, EntityDefinition newDef) {
    ListMultimap<String, Entity> newList = ArrayListMultimap.create();
    for (Entry<String, ? extends Entity> entry : entities.entries()) {
        EntityDefinition entryDef = entry.getValue().getDefinition();
        Entity newEntry;
        if (AlignmentUtil.isParent(originalDef, entryDef)) {
            if (entry.getValue() instanceof Type) {
                // entry is a Type, so the changed Definition must be a
                // Type, too.
                newEntry = new DefaultType((TypeEntityDefinition) newDef);
            } else if (entry.getValue() instanceof Property) {
                // entry is a Property, check changed Definition.
                if (originalDef.getPropertyPath().isEmpty()) {
                    // Type changed.
                    newEntry = new DefaultProperty(new PropertyEntityDefinition(newDef.getType(), entryDef.getPropertyPath(), entryDef.getSchemaSpace(), newDef.getFilter()));
                } else {
                    // Some element of the property path changed.
                    List<ChildContext> newPath = new ArrayList<ChildContext>(entryDef.getPropertyPath());
                    int lastIndexOfChangedDef = newDef.getPropertyPath().size() - 1;
                    newPath.set(lastIndexOfChangedDef, newDef.getPropertyPath().get(lastIndexOfChangedDef));
                    newEntry = new DefaultProperty(new PropertyEntityDefinition(entryDef.getType(), newPath, entryDef.getSchemaSpace(), entryDef.getFilter()));
                }
            } else {
                throw new IllegalStateException("Entity is neither a Type nor a Property.");
            }
        } else {
            newEntry = entry.getValue();
        }
        newList.put(entry.getKey(), newEntry);
    }
    return newList;
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) DefaultProperty(eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty) 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) Type(eu.esdihumboldt.hale.common.align.model.Type) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Property(eu.esdihumboldt.hale.common.align.model.Property) DefaultProperty(eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty)

Example 62 with ChildContext

use of eu.esdihumboldt.hale.common.align.model.ChildContext in project hale by halestudio.

the class EntityDefinitionServiceImpl method editConditionContext.

/**
 * @see eu.esdihumboldt.hale.ui.service.entity.EntityDefinitionService#editConditionContext(eu.esdihumboldt.hale.common.align.model.EntityDefinition,
 *      eu.esdihumboldt.hale.common.instance.model.Filter)
 */
@Override
public EntityDefinition editConditionContext(final EntityDefinition sibling, Filter filter) {
    List<ChildContext> path = sibling.getPropertyPath();
    if (sibling.getSchemaSpace() == SchemaSpaceID.TARGET && path.isEmpty()) {
        // XXX throw exception instead?
        return null;
    }
    // Check whether there actually is a change. If not, we are done.
    Condition oldCondition = AlignmentUtil.getContextCondition(sibling);
    if (Objects.equal(filter, oldCondition == null ? null : oldCondition.getFilter()))
        return sibling;
    // Create the new entity. Do not add context yet, since the user could
    // still abort the process (see below).
    EntityDefinition newDef = AlignmentUtil.getDefaultEntity(sibling);
    if (filter != null)
        newDef = createWithCondition(sibling, new Condition(filter));
    AlignmentService as = PlatformUI.getWorkbench().getService(AlignmentService.class);
    Alignment alignment = as.getAlignment();
    // Collect cells to replace.
    // All cells of the EntityDefinition's type can be affected.
    Collection<? extends Cell> potentiallyAffected = alignment.getCells(sibling.getType(), sibling.getSchemaSpace());
    Predicate<Cell> associatedCellPredicate = new Predicate<Cell>() {

        @Override
        public boolean apply(Cell input) {
            return input != null && AlignmentUtil.associatedWith(sibling, input, false, true);
        }
    };
    Collection<? extends Cell> affected = new HashSet<Cell>(Collections2.filter(potentiallyAffected, associatedCellPredicate));
    // Check whether base alignment cells are affected.
    boolean baseCellsAffected = false;
    Predicate<Cell> baseCellPredicate = new Predicate<Cell>() {

        @Override
        public boolean apply(Cell input) {
            return input != null && input.isBaseCell();
        }
    };
    if (Iterables.find(affected, baseCellPredicate, null) != null) {
        // Check whether the user wants to continue.
        final Display display = PlatformUI.getWorkbench().getDisplay();
        final AtomicBoolean abort = new AtomicBoolean();
        display.syncExec(new Runnable() {

            @Override
            public void run() {
                MessageBox mb = new MessageBox(display.getActiveShell(), SWT.YES | SWT.NO | SWT.ICON_QUESTION);
                mb.setMessage("Some base alignment cells reference the entity definition you wish to change.\n" + "The change will only affect cells which aren't from any base alignment.\n\n" + "Do you still wish to continue?");
                mb.setText("Continue?");
                abort.set(mb.open() != SWT.YES);
            }
        });
        if (abort.get())
            return null;
        // Filter base alignment cells out.
        baseCellsAffected = true;
        affected = Collections2.filter(affected, Predicates.not(baseCellPredicate));
    }
    // Add condition context if necessary
    if (filter != null)
        addConditionContext(sibling, filter);
    // Replace affected (filtered) cells.
    Map<Cell, MutableCell> replaceMap = new HashMap<Cell, MutableCell>();
    for (Cell cell : affected) {
        DefaultCell newCell = new DefaultCell(cell);
        if (newDef.getSchemaSpace() == SchemaSpaceID.SOURCE)
            newCell.setSource(replace(newCell.getSource(), sibling, newDef));
        else
            newCell.setTarget(replace(newCell.getTarget(), sibling, newDef));
        replaceMap.put(cell, newCell);
    }
    as.replaceCells(replaceMap);
    // nor do any base alignment cells still use it.
    if (oldCondition != null && !baseCellsAffected)
        removeContext(sibling);
    return newDef;
}
Also used : Condition(eu.esdihumboldt.hale.common.align.model.Condition) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) HashMap(java.util.HashMap) Predicate(com.google.common.base.Predicate) MessageBox(org.eclipse.swt.widgets.MessageBox) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) AlignmentService(eu.esdihumboldt.hale.ui.service.align.AlignmentService) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) Cell(eu.esdihumboldt.hale.common.align.model.Cell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) HashSet(java.util.HashSet) Display(org.eclipse.swt.widgets.Display)

Example 63 with ChildContext

use of eu.esdihumboldt.hale.common.align.model.ChildContext in project hale by halestudio.

the class EntityDefinitionServiceImpl method removeContext.

/**
 * @see EntityDefinitionService#removeContext(EntityDefinition)
 */
@Override
public void removeContext(EntityDefinition entity) {
    EntityDefinition def = AlignmentUtil.getDefaultEntity(entity);
    // XXX any checks? Alignment must still be valid! see also
    // InstanceContextTester
    List<ChildContext> path = entity.getPropertyPath();
    if (path.isEmpty()) {
        // type entity definition
        Filter filter = entity.getFilter();
        if (filter != null) {
            synchronized (conditionContexts) {
                conditionContexts.remove(def, new Condition(filter));
            }
        // XXX what about the children of this context?
        }
        notifyContextRemoved(entity);
        return;
    }
    boolean removed = false;
    ChildContext lastContext = path.get(path.size() - 1);
    if (lastContext.getContextName() != null) {
        synchronized (namedContexts) {
            namedContexts.remove(def, lastContext.getContextName());
        }
        removed = true;
    }
    if (lastContext.getIndex() != null) {
        synchronized (indexContexts) {
            indexContexts.remove(def, lastContext.getIndex());
        }
        removed = true;
    }
    if (lastContext.getCondition() != null) {
        synchronized (conditionContexts) {
            conditionContexts.remove(def, lastContext.getCondition());
        }
        removed = true;
    }
    if (removed) {
        notifyContextRemoved(entity);
    }
}
Also used : Condition(eu.esdihumboldt.hale.common.align.model.Condition) 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) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext)

Aggregations

ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)63 ArrayList (java.util.ArrayList)32 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)29 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)24 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)16 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)15 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)12 QName (javax.xml.namespace.QName)11 Property (eu.esdihumboldt.hale.common.align.model.Property)7 DefaultProperty (eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty)7 List (java.util.List)7 Condition (eu.esdihumboldt.hale.common.align.model.Condition)6 Entity (eu.esdihumboldt.hale.common.align.model.Entity)6 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)5 ChildDefinition (eu.esdihumboldt.hale.common.schema.model.ChildDefinition)5 Cell (eu.esdihumboldt.hale.common.align.model.Cell)4 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)4 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)3 Type (eu.esdihumboldt.hale.common.align.model.Type)3 DefaultType (eu.esdihumboldt.hale.common.align.model.impl.DefaultType)3