1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.adapters;
14 import java.util.Collection;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.utils.OrderedSetUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.adapter.impl.AbstractRemover;
23 import org.simantics.db.layer0.util.RemoverUtil;
24 import org.simantics.diagram.stubs.DiagramResource;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.modeling.ModelingResources;
27 import org.simantics.modeling.utils.RemoverUtils;
28 import org.simantics.modeling.utils.RemoverUtils.ValidationResult;
31 * @author Tuukka Lehtonen
32 * @see TerminalRemover
34 public class ConnectionRelationRemover extends AbstractRemover {
36 public ConnectionRelationRemover(Resource connectionRelation) {
37 super(connectionRelation);
41 public String canRemove(ReadGraph graph, Map<Object, Object> aux) throws DatabaseException {
42 ModelingResources MOD = ModelingResources.getInstance(graph);
43 Resource diagramConnectionRelation = null;
44 Resource connectionRelation = graph.getPossibleObject(resource, MOD.DiagramConnectionRelationToConnectionRelation);
45 if (connectionRelation != null)
46 diagramConnectionRelation = resource;
48 connectionRelation = resource;
49 ValidationResult result = RemoverUtils.validateConnectionRelationRemoval(graph, connectionRelation, diagramConnectionRelation);
51 return RemoverUtils.formatError(graph, result);
56 public void remove(WriteGraph graph) throws DatabaseException {
57 Layer0 L0 = Layer0.getInstance(graph);
58 DiagramResource DIA = DiagramResource.getInstance(graph);
59 ModelingResources MOD = ModelingResources.getInstance(graph);
61 Collection<Resource> lifts = graph.getObjects(resource, DIA.Lifts);
63 // Not necessary, this is legacy support only.
64 for (Resource def : graph.getObjects(resource, L0.ConcernsRelation_Inverse))
67 // Remove possible symbol terminal(s).
68 for (Resource diagramConnectionRelation : graph.getObjects(resource, MOD.ConnectionRelationToDiagramConnectionRelation)) {
69 removeTerminals(graph, DIA, diagramConnectionRelation);
70 Resource diagramConnectionRelationInverse = graph.getPossibleObject(diagramConnectionRelation, L0.InverseOf);
71 graph.deny(diagramConnectionRelation);
72 if (diagramConnectionRelationInverse != null)
73 graph.deny(diagramConnectionRelationInverse);
75 removeTerminals(graph, DIA, resource);
77 Resource connectionRelationInverse = graph.getPossibleObject(resource, L0.InverseOf);
79 if (connectionRelationInverse != null)
80 graph.deny(connectionRelationInverse);
83 for (Resource lift : lifts)
84 RemoverUtil.remove(graph, lift);
87 private void removeTerminals(WriteGraph graph, DiagramResource DIA, Resource diagramConnectionRelation) throws DatabaseException {
88 for (Resource terminal : graph.getObjects(diagramConnectionRelation, DIA.HasConnectionPoint_Inverse)) {
89 for (Resource ownerList : OrderedSetUtils.getOwnerLists(graph, terminal, DIA.Composite)) {
90 OrderedSetUtils.remove(graph, ownerList, terminal);
92 RemoverUtil.remove(graph, terminal);