]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/ConnectionRelationRemover.java
WIP: Some old-ish adapter registry initialization improvements
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / ConnectionRelationRemover.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.adapters;
13
14 import java.util.Collection;
15 import java.util.Map;
16
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;
28
29 /**
30  * @author Tuukka Lehtonen
31  * @see TerminalRemover
32  */
33 public class ConnectionRelationRemover extends AbstractRemover {
34
35     public ConnectionRelationRemover(Resource connectionRelation) {
36         super(connectionRelation);
37     }
38
39     @Override
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;
46         else
47             connectionRelation = resource;
48         ValidationResult result = Removers.validateConnectionRelationRemoval(graph, connectionRelation, diagramConnectionRelation);
49         if (result.inUse())
50             return Removers.formatError(graph, result);
51         return null;
52     }
53
54     @Override
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);
59
60         Collection<Resource> lifts = graph.getObjects(resource, DIA.Lifts);
61
62         // Not necessary, this is legacy support only.
63         for (Resource def : graph.getObjects(resource, L0.ConcernsRelation_Inverse))
64             graph.deny(def);
65
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);
73         }
74         removeTerminals(graph, DIA, resource);
75
76         Resource connectionRelationInverse = graph.getPossibleObject(resource, L0.InverseOf);
77         graph.deny(resource);
78         if (connectionRelationInverse != null)
79             graph.deny(connectionRelationInverse);
80
81         if (!lifts.isEmpty())
82             for (Resource lift : lifts)
83                 RemoverUtil.remove(graph, lift);
84     }
85     
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);
90             }
91             RemoverUtil.remove(graph, terminal);
92         }
93     }
94
95 }