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.adapters.Removers.ValidationResult;
30 * @author Tuukka Lehtonen
31 * @see TerminalRemover
33 public class ConnectionRelationRemover extends AbstractRemover {
35 public ConnectionRelationRemover(Resource connectionRelation) {
36 super(connectionRelation);
40 public String canRemove(ReadGraph graph, Map<Object, Object> aux) throws DatabaseException {
41 ModelingResources MOD = ModelingResources.getInstance(graph);
42 Resource diagramConnectionRelation = null;
43 Resource connectionRelation = graph.getPossibleObject(resource, MOD.DiagramConnectionRelationToConnectionRelation);
44 if (connectionRelation != null)
45 diagramConnectionRelation = resource;
47 connectionRelation = resource;
48 ValidationResult result = Removers.validateConnectionRelationRemoval(graph, connectionRelation, diagramConnectionRelation);
50 return Removers.formatError(graph, result);
55 public void remove(WriteGraph graph) throws DatabaseException {
56 Layer0 L0 = Layer0.getInstance(graph);
57 DiagramResource DIA = DiagramResource.getInstance(graph);
58 ModelingResources MOD = ModelingResources.getInstance(graph);
60 Collection<Resource> lifts = graph.getObjects(resource, DIA.Lifts);
62 // Not necessary, this is legacy support only.
63 for (Resource def : graph.getObjects(resource, L0.ConcernsRelation_Inverse))
66 // Remove possible symbol terminal(s).
67 for (Resource diagramConnectionRelation : graph.getObjects(resource, MOD.ConnectionRelationToDiagramConnectionRelation)) {
68 removeTerminals(graph, DIA, diagramConnectionRelation);
69 Resource diagramConnectionRelationInverse = graph.getPossibleObject(diagramConnectionRelation, L0.InverseOf);
70 graph.deny(diagramConnectionRelation);
71 if (diagramConnectionRelationInverse != null)
72 graph.deny(diagramConnectionRelationInverse);
74 removeTerminals(graph, DIA, resource);
76 Resource connectionRelationInverse = graph.getPossibleObject(resource, L0.InverseOf);
78 if (connectionRelationInverse != null)
79 graph.deny(connectionRelationInverse);
82 for (Resource lift : lifts)
83 RemoverUtil.remove(graph, lift);
86 private void removeTerminals(WriteGraph graph, DiagramResource DIA, Resource diagramConnectionRelation) throws DatabaseException {
87 for (Resource terminal : graph.getObjects(diagramConnectionRelation, DIA.HasConnectionPoint_Inverse)) {
88 for (Resource ownerList : OrderedSetUtils.getOwnerLists(graph, terminal, DIA.Composite)) {
89 OrderedSetUtils.remove(graph, ownerList, terminal);
91 RemoverUtil.remove(graph, terminal);