]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/ConnectionRelationRemover.java
Expose proxied subject in GroupProxySymbolItem
[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.utils.RemoverUtils;
28 import org.simantics.modeling.utils.RemoverUtils.ValidationResult;
29
30 /**
31  * @author Tuukka Lehtonen
32  * @see TerminalRemover
33  */
34 public class ConnectionRelationRemover extends AbstractRemover {
35
36     public ConnectionRelationRemover(Resource connectionRelation) {
37         super(connectionRelation);
38     }
39
40     @Override
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;
47         else
48             connectionRelation = resource;
49         ValidationResult result = RemoverUtils.validateConnectionRelationRemoval(graph, connectionRelation, diagramConnectionRelation);
50         if (result.inUse())
51             return RemoverUtils.formatError(graph, result);
52         return null;
53     }
54
55     @Override
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);
60
61         Collection<Resource> lifts = graph.getObjects(resource, DIA.Lifts);
62
63         // Not necessary, this is legacy support only.
64         for (Resource def : graph.getObjects(resource, L0.ConcernsRelation_Inverse))
65             graph.deny(def);
66
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);
74         }
75         removeTerminals(graph, DIA, resource);
76
77         Resource connectionRelationInverse = graph.getPossibleObject(resource, L0.InverseOf);
78         graph.deny(resource);
79         if (connectionRelationInverse != null)
80             graph.deny(connectionRelationInverse);
81
82         if (!lifts.isEmpty())
83             for (Resource lift : lifts)
84                 RemoverUtil.remove(graph, lift);
85     }
86     
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);
91             }
92             RemoverUtil.remove(graph, terminal);
93         }
94     }
95
96 }