]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/ComponentAndElementCopyHandler.java
Layer0Utils.addL0Identifier to prevent possible differentiation of code
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / mapping / ComponentAndElementCopyHandler.java
1 package org.simantics.modeling.mapping;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.NamedResource;
10 import org.simantics.db.common.utils.OrderedSetUtils;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.adapter.impl.DefaultCopyHandler;
13 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
14 import org.simantics.diagram.stubs.DiagramResource;
15 import org.simantics.layer0.Layer0;
16 import org.simantics.modeling.ModelingResources;
17 import org.simantics.structural.stubs.StructuralResource2;
18
19
20 /**
21  * Copy handler for Component and it's Element.
22  * 
23  * FIXME: Requires patched ModelTransferableGraphSourceRequest
24  * 
25  *  in classifyPredicates(), excluded resources must be filtered. Otherwise the code fails with :Diagram.hasInverse having no URI.
26  *  
27  *  Resource predicate = stm.getPredicate();
28  *      Resource object = stm.getObject();              
29  *      if (exclusions.contains(object) || exclusions.contains(predicate))
30  *              continue; 
31  *
32  * 
33  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
34  *
35  */
36 public class ComponentAndElementCopyHandler extends DefaultCopyHandler{
37
38         protected boolean includeComponent = true;
39         protected boolean includeElement = true;
40         
41         public ComponentAndElementCopyHandler(Resource resource) {
42                 super(resource);
43         }
44         
45         /**
46          * 
47          * @param resource Component, if component is included. Otherwise the Element.
48          * @param includeComponent
49          * @param includeElement
50          */
51         public ComponentAndElementCopyHandler(Resource resource, boolean includeComponent, boolean includeElement) {
52                 super(resource);
53                 if (!includeComponent && !includeElement)
54                         throw new IllegalArgumentException();
55                 this.includeComponent = includeComponent;
56                 this.includeElement = includeElement;
57         }
58         
59         
60
61         
62         @Override
63         protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException {
64         ModelingResources MR = ModelingResources.getInstance(graph);
65
66         ArrayList<Resource> exclusions = new ArrayList<Resource>();
67
68         ArrayList<NamedResource> roots = new ArrayList<NamedResource>();
69         
70         Resource component = null;
71         Resource element = null;
72         
73         if (includeComponent) {
74                 component = getResource();
75                 element = graph.getPossibleObject(component, MR.ComponentToElement);
76         } else {
77                 element = getResource();
78                 component = graph.getPossibleObject(element, MR.ElementToComponent);
79         }
80                 // Include component as root
81         if (component != null) {
82                 processComponent(graph, component, includeComponent, roots, exclusions);
83         }
84                 
85             if (element != null) {
86                 processElement(graph, element, includeElement, roots, exclusions);
87             }
88             
89         return TransferableGraphConfiguration2.createWithNames(graph, roots, exclusions, true, false);
90         }
91         
92         protected void processComponent(ReadGraph graph, Resource component, boolean includeComponent, ArrayList<NamedResource> roots, ArrayList<Resource> exclusions) throws DatabaseException {
93                 Layer0 L0 = Layer0.getInstance(graph);
94         StructuralResource2 SR = StructuralResource2.getInstance(graph);
95         Resource resource = getResource();
96                 if (includeComponent) {
97                         String rootName = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);
98                         roots.add(new NamedResource(rootName, resource));
99                 } else {
100                         exclusions.add(component);
101                 }
102
103                 // filter connections
104                 for (Resource connection : graph.getObjects(resource, SR.IsConnectedTo))
105                         exclusions.add(connection);
106         }
107         
108         protected void processElement(ReadGraph graph, Resource element, boolean includeElement, ArrayList<NamedResource> roots, ArrayList<Resource> exclusions) throws DatabaseException {
109                 Layer0 L0 = Layer0.getInstance(graph);
110                 DiagramResource DIA = DiagramResource.getInstance(graph);
111         StructuralResource2 SR = StructuralResource2.getInstance(graph);
112                 
113                 if (includeElement) {
114                         // Add element as a root.
115                         String name = graph.getRelatedValue(element, L0.HasName, Bindings.STRING);
116                         roots.add(new NamedResource(name, element));
117                 } else {
118                         exclusions.add(element);
119                 }
120                 
121                 // exclude diagram from copied data.
122                 Collection<Resource> diagram_ = OrderedSetUtils.getOwnerLists(graph, element, DIA.Diagram);
123                 for (Resource diagram : diagram_) {
124                         
125                         Resource inv = graph.getInverse(diagram);
126                         exclusions.add(diagram);
127                         exclusions.add(inv);
128                         
129                         // Exclude next and previous elements in the diagram
130                         Resource next = OrderedSetUtils.next(graph, diagram, element);
131                         Resource previous = OrderedSetUtils.prev(graph, diagram, element);
132                         if (next != null && !next.equals(element))
133                                 exclusions.add(next);
134                         if (previous != null && !previous.equals(element) && !previous.equals(next))
135                                 exclusions.add(previous);
136                 }
137                 // filter connections
138         for (Resource connection : graph.getObjects(element, SR.IsConnectedTo))
139                 exclusions.add(connection);
140         }
141                 
142 }