Search in sources :

Example 31 with Cell

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

the class AlignmentServiceImpl method setCellProperty.

/**
 * @see eu.esdihumboldt.hale.ui.service.align.AlignmentService#setCellProperty(java.lang.String,
 *      java.lang.String, java.lang.Object)
 */
@Override
public void setCellProperty(String cellId, String propertyName, Object property) {
    if (propertyName == null || property == null) {
        throw new IllegalArgumentException("Mandatory parameter is null");
    }
    Cell cell = getAlignment().getCell(cellId);
    if (cell instanceof ModifiableCell && (Cell.PROPERTY_DISABLE_FOR.equals(propertyName) || Cell.PROPERTY_ENABLE_FOR.equals(propertyName))) {
        boolean disable = Cell.PROPERTY_DISABLE_FOR.equals(propertyName);
        if (property instanceof Cell) {
            Cell other = (Cell) property;
            if (!AlignmentUtil.isTypeCell(other))
                throw new IllegalArgumentException();
            // This call may fail, if the cell was disabled in a base
            // alignment and someone tries to enable it again.
            ((ModifiableCell) cell).setDisabledFor(other, disable);
        } else
            throw new IllegalArgumentException();
        notifyCellsPropertyChanged(Arrays.asList(cell), propertyName);
    } else if (Cell.PROPERTY_TRANSFORMATION_MODE.equals(propertyName)) {
        // set the transformation mode
        if (cell instanceof ModifiableCell && property instanceof TransformationMode) {
            ModifiableCell modCell = (ModifiableCell) cell;
            modCell.setTransformationMode((TransformationMode) property);
            notifyCellsPropertyChanged(Arrays.asList(cell), propertyName);
        }
    } else if (cell instanceof MutableCell) {
        MutableCell mutableCell = (MutableCell) cell;
        if (Cell.PROPERTY_PRIORITY.equals(propertyName)) {
            if (property instanceof Priority) {
                Priority priority = (Priority) property;
                mutableCell.setPriority(priority);
            }
            if (property instanceof String) {
                String priorityStr = (String) property;
                Priority priority = Priority.valueOf(priorityStr);
                if (priority != null) {
                    mutableCell.setPriority(priority);
                } else {
                    throw new IllegalArgumentException();
                }
            }
            notifyCellsPropertyChanged(Arrays.asList(cell), propertyName);
        }
    } else {
        throw new IllegalArgumentException("No mutable cell by the given id found: " + cellId);
    }
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) TransformationMode(eu.esdihumboldt.hale.common.align.model.TransformationMode) Priority(eu.esdihumboldt.hale.common.align.model.Priority) Cell(eu.esdihumboldt.hale.common.align.model.Cell) ModifiableCell(eu.esdihumboldt.hale.common.align.model.ModifiableCell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) BaseAlignmentCell(eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell) ModifiableCell(eu.esdihumboldt.hale.common.align.model.ModifiableCell)

Example 32 with Cell

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

the class AlignmentServiceImpl method addOrUpdateAlignment.

/**
 * @see AlignmentService#addOrUpdateAlignment(MutableAlignment)
 */
@Override
public void addOrUpdateAlignment(MutableAlignment alignment) {
    Collection<Cell> added = new ArrayList<Cell>();
    /*
		 * XXX Updates for disables not supported
		 * 
		 * One problem is, that base alignments shouldn't be added twice, so if
		 * the given alignment contains a base alignment that is also contained
		 * in the current alignment it won't be added.
		 * 
		 * Now what about cells in the given alignment, that reference those
		 * base alignments (-> disabled for)? They need to be changed to the
		 * base alignment cells of the current alignment.
		 * 
		 * Those cells can be obtained by replacing the prefix part of the cell
		 * and using getCell(String).
		 * 
		 * XXX Addition of base alignments not supported
		 * 
		 * Here the problem is, that it is not enough to gather the base
		 * alignment cells and change their prefix to a new one, since the base
		 * cell could be another base alignment cell, which would need to
		 * change, too. They would have to be added in order. An option would be
		 * to load them with the base alignment loading mechanism, which would
		 * result in a loss of additional disables.
		 * 
		 * XXX Changes of conflicting base alignment cells not handled
		 */
    // if (!alignment.getBaseAlignments().isEmpty()) {
    // _log.warn("Adding alignments currently does not support merging of base alignments. Import base alignments independently.");
    // }
    boolean addedBaseCells = false;
    // add cells
    synchronized (this) {
        for (Cell cell : alignment.getCells()) {
            if (cell instanceof MutableCell) {
                this.alignment.addCell((MutableCell) cell);
                added.add(cell);
            } else if (!(cell instanceof BaseAlignmentCell)) {
                throw new IllegalStateException("The given alignment contained a cell which is neither mutable nor from a base alignment.");
            } else {
                addedBaseCells = true;
            }
        }
    }
    synchronized (this) {
        // XXX this needs merging, see above
        boolean first = true;
        for (Entry<String, URI> baseAlignment : alignment.getBaseAlignments().entrySet()) {
            Collection<CustomPropertyFunction> baseFunctions;
            if (first) {
                // XXX hack - insert all base functions with the first base
                // alignment
                baseFunctions = alignment.getBasePropertyFunctions().values();
            } else {
                // base functions should only be added once
                baseFunctions = Collections.<CustomPropertyFunction>emptyList();
            }
            this.alignment.addBaseAlignment(baseAlignment.getKey(), baseAlignment.getValue(), // 
            alignment.getBaseAlignmentCells(baseAlignment.getValue()), baseFunctions);
        }
    }
    // add custom functions
    boolean addedFunctions = false;
    synchronized (this) {
        for (CustomPropertyFunction cf : alignment.getCustomPropertyFunctions().values()) {
            this.alignment.addCustomPropertyFunction(cf);
            addedFunctions = true;
        }
    }
    if (addedBaseCells || (!added.isEmpty() && addedFunctions)) {
        // only emit one combined event (not to trigger multiple
        // transformations)
        notifyAlignmentChanged();
    } else {
        if (!added.isEmpty()) {
            notifyCellsAdded(added);
        }
        if (addedFunctions) {
            notifyCustomFunctionsChanged();
        }
    }
}
Also used : BaseAlignmentCell(eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) ArrayList(java.util.ArrayList) Cell(eu.esdihumboldt.hale.common.align.model.Cell) ModifiableCell(eu.esdihumboldt.hale.common.align.model.ModifiableCell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) BaseAlignmentCell(eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell) URI(java.net.URI) CustomPropertyFunction(eu.esdihumboldt.hale.common.align.extension.function.custom.CustomPropertyFunction)

Example 33 with Cell

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

the class AlignmentServiceUndoSupport method setCellProperty.

/**
 * @see eu.esdihumboldt.hale.ui.service.align.AlignmentService#setCellProperty(java.lang.String,
 *      java.lang.String, java.lang.Object)
 */
@Override
public void setCellProperty(String cellId, String propertyName, Object property) {
    if (Cell.PROPERTY_DISABLE_FOR.equals(propertyName) || Cell.PROPERTY_ENABLE_FOR.equals(propertyName)) {
        IUndoableOperation operation = new DisableCellOperation(Cell.PROPERTY_DISABLE_FOR.equals(propertyName), cellId, (Cell) property);
        executeOperation(operation);
    } else if (Cell.PROPERTY_PRIORITY.equals(propertyName)) {
        if (property instanceof Priority) {
            Priority newPriority = (Priority) property;
            Cell cell = getAlignment().getCell(cellId);
            Priority oldPriority = cell.getPriority();
            IUndoableOperation operation = new SetCellPropertyOperation(cellId, propertyName, oldPriority, newPriority);
            executeOperation(operation);
        }
    } else if (Cell.PROPERTY_TRANSFORMATION_MODE.equals(propertyName)) {
        Cell cell = getAlignment().getCell(cellId);
        Object oldValue = cell.getTransformationMode();
        IUndoableOperation operation = new SetCellPropertyOperation(cellId, propertyName, oldValue, property);
        executeOperation(operation);
    } else {
        log.warn("An unknown cell property is set. No undo support.");
        alignmentService.setCellProperty(cellId, propertyName, property);
    }
}
Also used : IUndoableOperation(org.eclipse.core.commands.operations.IUndoableOperation) Priority(eu.esdihumboldt.hale.common.align.model.Priority) Cell(eu.esdihumboldt.hale.common.align.model.Cell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell)

Example 34 with Cell

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

the class RemoveCellHandler method execute.

/**
 * @see IHandler#execute(ExecutionEvent)
 */
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    // collect cells from selection
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (MessageDialog.openQuestion(HandlerUtil.getActiveShell(event), "Delete cells", "Do you really want to delete the selected cells?")) {
        AlignmentService as = PlatformUI.getWorkbench().getService(AlignmentService.class);
        if (selection instanceof IStructuredSelection) {
            List<?> list = ((IStructuredSelection) selection).toList();
            List<Cell> cells = new ArrayList<Cell>();
            for (Object object : list) {
                if (object instanceof Cell) {
                    // FIXME sanity checks for cell deletion? (e.g. don't
                    // allow remove type mapping if there are properties
                    // mapped?) where to do it?
                    // For now only done in activeWhen defined for handler
                    cells.add((Cell) object);
                }
            }
            as.removeCells(cells.toArray(new Cell[cells.size()]));
        }
    }
    return null;
}
Also used : AlignmentService(eu.esdihumboldt.hale.ui.service.align.AlignmentService) ISelection(org.eclipse.jface.viewers.ISelection) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Cell(eu.esdihumboldt.hale.common.align.model.Cell)

Example 35 with Cell

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

the class PreferencesGroovyService method getScriptHash.

/**
 * Calculates the current alignments script hash.
 *
 * @return the current alignments script hash
 */
private synchronized String getScriptHash() {
    if (scriptHash == null) {
        List<String> scripts = new ArrayList<>();
        // get all Groovy scripts
        for (Cell cell : alignmentService.getAlignment().getCells()) {
            ListMultimap<String, ParameterValue> parameters = cell.getTransformationParameters();
            if (parameters == null)
                continue;
            // Groovy transformations
            if (cell.getTransformationIdentifier().contains("groovy")) {
                List<ParameterValue> val = parameters.get(GroovyConstants.PARAMETER_SCRIPT);
                if (!val.isEmpty()) {
                    String script = getScriptString(val.get(0));
                    if (script != null) {
                        scripts.add(script);
                    }
                }
            }
            // GroovyScript function parameters
            for (ParameterValue value : parameters.values()) {
                if (GroovyScript.GROOVY_SCRIPT_ID.equals(value.getType())) {
                    String script = getScriptString(value);
                    if (script != null) {
                        scripts.add(script);
                    }
                }
            }
        }
        // Groovy scripts of custom property functions
        for (CustomPropertyFunction customFunction : alignmentService.getAlignment().getAllCustomPropertyFunctions().values()) {
            if (customFunction instanceof DefaultCustomPropertyFunction) {
                DefaultCustomPropertyFunction cf = (DefaultCustomPropertyFunction) customFunction;
                if (CustomPropertyFunctionType.GROOVY.equals(cf.getFunctionType())) {
                    Value functionDef = cf.getFunctionDefinition();
                    if (functionDef != null && !functionDef.isEmpty()) {
                        String script = getScriptString(functionDef);
                        if (script != null) {
                            scripts.add(script);
                        }
                    }
                }
            }
        }
        // order scripts (for consistent hash)
        Collections.sort(scripts);
        // modify the script in a undetectable way
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            for (String script : scripts) md.update(script.getBytes("UTF-8"));
            byte[] hash = md.digest();
            StringBuilder sb = new StringBuilder(2 * hash.length);
            for (byte b : hash) {
                sb.append(String.format("%02x", b & 0xff));
            }
            scriptHash = sb.toString();
        // Both exceptions cannot happen in a valid Java platform.
        // Anyways, if they happen, execution should stop here!
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("No MD5 MessageDigest!");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("No UTF-8 Charset!");
        }
    }
    return scriptHash;
}
Also used : ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DefaultCustomPropertyFunction(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction) Value(eu.esdihumboldt.hale.common.core.io.Value) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) MessageDigest(java.security.MessageDigest) Cell(eu.esdihumboldt.hale.common.align.model.Cell) DefaultCustomPropertyFunction(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction) CustomPropertyFunction(eu.esdihumboldt.hale.common.align.extension.function.custom.CustomPropertyFunction)

Aggregations

Cell (eu.esdihumboldt.hale.common.align.model.Cell)123 ArrayList (java.util.ArrayList)33 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)28 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)28 Test (org.junit.Test)27 Entity (eu.esdihumboldt.hale.common.align.model.Entity)24 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)24 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)18 BaseAlignmentCell (eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell)16 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)16 AlignmentService (eu.esdihumboldt.hale.ui.service.align.AlignmentService)16 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)15 Alignment (eu.esdihumboldt.hale.common.align.model.Alignment)13 HashSet (java.util.HashSet)13 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)12 Type (eu.esdihumboldt.hale.common.align.model.Type)11 List (java.util.List)11 ModifiableCell (eu.esdihumboldt.hale.common.align.model.ModifiableCell)9 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)9 Property (eu.esdihumboldt.hale.common.align.model.Property)8