1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.adapters;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.HashSet;
\r
17 import java.util.List;
\r
18 import java.util.Set;
\r
20 import org.simantics.db.ReadGraph;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.common.request.ObjectsWithType;
\r
23 import org.simantics.db.common.request.PossibleIndexRoot;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
\r
26 import org.simantics.db.layer0.adapter.impl.DefaultCopyHandler;
\r
27 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
\r
28 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.RootSpec;
\r
29 import org.simantics.diagram.stubs.DiagramResource;
\r
30 import org.simantics.layer0.Layer0;
\r
31 import org.simantics.modeling.ModelingResources;
\r
32 import org.simantics.modeling.ModelingUtils.CompositeInfo;
\r
33 import org.simantics.modeling.ModelingUtils.DiagramComponentInfo;
\r
34 import org.simantics.structural.stubs.StructuralResource2;
\r
37 * @author Tuukka Lehtonen
\r
39 public class CompositeCopyHandler extends DefaultCopyHandler {
\r
41 public CompositeCopyHandler(Resource composite) {
\r
45 public CompositeCopyHandler(Collection<Resource> composites) {
\r
50 protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException {
\r
52 Layer0 L0 = Layer0.getInstance(graph);
\r
53 StructuralResource2 SR = StructuralResource2.getInstance(graph);
\r
54 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
55 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
57 Set<Resource> resourceSet = (resources instanceof Set)
\r
58 ? (Set<Resource>) resources : new HashSet<>(resources);
\r
59 Set<Resource> exclusions = new HashSet<>();
\r
60 Set<Resource> externals = new HashSet<>();
\r
61 List<RootSpec> roots = new ArrayList<>();
\r
63 for(Resource resource : resources) {
\r
64 // Process all connection joins.
\r
65 // This is the only way to access all of them.
\r
66 for (Resource diagram : graph.getObjects(resource, MOD.CompositeToDiagram)) {
\r
67 for (Resource flag : graph.syncRequest(new ObjectsWithType(diagram, L0.ConsistsOf, DIA.Flag))) {
\r
68 for (Resource join : graph.getObjects(flag, DIA.FlagIsJoinedBy)) {
\r
69 // Joins with external references are omitted
\r
70 for (Resource comp : graph.getObjects(join, SR.JoinsComposite)) {
\r
71 if (!resourceSet.contains(comp))
\r
72 exclusions.add(join);
\r
74 // This code excludes joins with flags to external
\r
75 // diagrams that are not connected (have no
\r
76 // configuration for the flag)
\r
77 for (Resource flag2 : graph.getObjects(join, DIA.JoinsFlag)) {
\r
78 Resource diagram2 = graph.getPossibleObject(flag2, L0.PartOf);
\r
79 if (diagram2 != null) {
\r
80 Resource comp = graph.getPossibleObject(diagram2, MOD.DiagramToComposite);
\r
81 if (!resourceSet.contains(comp))
\r
82 exclusions.add(join);
\r
88 // Check all diagram monitor elements.
\r
89 // Any components referenced that are external to the exported diagrams must be excluded from the export.
\r
90 // This will leave the monitors without a monitored component but export and import will work anyway.
\r
91 for (Resource ref : graph.syncRequest(new ObjectsWithType(diagram, L0.ConsistsOf, DIA.Monitor))) {
\r
92 for (Resource monitoredComponent : graph.getObjects(ref, DIA.HasMonitorComponent)) {
\r
93 Resource monitoredComponentComposite = graph.getPossibleObject(monitoredComponent, L0.PartOf);
\r
94 if (monitoredComponentComposite != null && !resourceSet.contains(monitoredComponentComposite)) {
\r
95 exclusions.add(monitoredComponent);
\r
101 // Leave any diagram monitors out of the export that are contained
\r
102 // by diagrams that are not part of this export, when the monitored
\r
103 // components happen to be a part of the exported composites.
\r
104 for (Resource ref : graph.syncRequest(new ObjectsWithType(resource, L0.ConsistsOf, SR.Component))) {
\r
105 for (Resource monitor : graph.getObjects(ref, DIA.HasMonitorComponent_Inverse)) {
\r
106 Resource monitorDiagram = graph.getPossibleObject(monitor, L0.PartOf);
\r
107 if (monitorDiagram != null) {
\r
108 Resource monitorComposite = graph.getPossibleObject(monitorDiagram, MOD.DiagramToComposite);
\r
109 if (monitorComposite != null && !resourceSet.contains(monitorComposite))
\r
110 exclusions.add(monitor);
\r
115 // Include resource as root
\r
116 // GUID rootId = graph.getRelatedValue(resource, L0.identifier, GUID.BINDING);
\r
117 // String rootName = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);
\r
118 // String escapedRootName = URIStringUtils.escape(rootName);
\r
119 // String escapedPath = ModelingUtils.getDiagramCompositePath(graph, resource);
\r
120 CompositeInfo info = CompositeInfo.fromResource(graph, resource);
\r
121 roots.add(new RootSpec(resource, info.getTGName(), true));
\r
122 Resource id = graph.getPossibleObject(resource, L0.identifier);
\r
123 if(id != null) exclusions.add(id);
\r
124 // Include components as roots
\r
125 for(Resource child : graph.sync(new ObjectsWithType(resource, L0.ConsistsOf, SR.Component))) {
\r
126 DiagramComponentInfo cinfo = DiagramComponentInfo.fromResource(graph, info, child);
\r
127 // GUID childId = graph.getRelatedValue(resource, L0.identifier, GUID.BINDING);
\r
128 // String childName = graph.getRelatedValue(child, L0.HasName, Bindings.STRING);
\r
129 id = graph.getPossibleObject(child, L0.identifier);
\r
130 if(id != null) exclusions.add(id);
\r
131 roots.add(new RootSpec(child, cinfo.getTGName(info), true));
\r
135 Resource model = graph.syncRequest(new PossibleIndexRoot(resources.iterator().next()));
\r
136 if(model == null) throw new DatabaseException("Composite is not part of any index root");
\r
137 roots.add(new RootSpec(model, "%model", false));
\r
139 TransferableGraphConfiguration2 config = TransferableGraphConfiguration2.createWithNames2(graph, roots, exclusions, true, false);
\r
140 for (Resource external : externals)
\r
141 config.preStatus.put(external, ExtentStatus.EXTERNAL);
\r