]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelDependenciesBean.java
Added resourceId and GUID to diagram DnD content
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / ModelDependenciesBean.java
1 package org.simantics.db.layer0.util;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.LinkedList;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Set;
10
11 import org.simantics.databoard.Bindings;
12 import org.simantics.databoard.adapter.AdaptException;
13 import org.simantics.databoard.binding.Binding;
14 import org.simantics.databoard.binding.mutable.Variant;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.common.request.ObjectsWithType;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.layer0.adapter.CopyHandler;
20 import org.simantics.db.layer0.migration.MigrationState;
21 import org.simantics.db.layer0.migration.MigrationStateKeys;
22 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
23 import org.simantics.graph.representation.TransferableGraph1;
24 import org.simantics.layer0.Layer0;
25
26 public class ModelDependenciesBean {
27
28         public static final String EXTENSION_KEY = ModelDependenciesBean.class.getSimpleName();
29
30         public static final Binding BINDING = Bindings.getBindingUnchecked(ModelDependenciesBean.class);
31
32         public ModelDependency[] dependencies;
33
34         public ModelDependenciesBean() {
35                 dependencies = new ModelDependency[0];
36         }
37
38         public ModelDependenciesBean(ModelDependency[] dependencies) {
39                 this.dependencies = dependencies;
40         }
41
42         /*
43          * Returns the linked shared ontologies in topological order. 
44          */
45         public static List<Resource> collectDependencies(ReadGraph graph, Resource resource) throws DatabaseException {
46                 LinkedList<Resource> order = new LinkedList<>();
47                 collectDependencies(graph, resource, order, new HashSet<>());
48                 return order;
49         }
50
51         private static void collectDependencies(ReadGraph graph, Resource resource, LinkedList<Resource> order, Set<Resource> visited) throws DatabaseException {
52                 Layer0 L0 = Layer0.getInstance(graph);
53                 for(Resource library : graph.syncRequest(new ObjectsWithType(resource, L0.IsLinkedTo, L0.SharedOntology))) {
54                         if (order.contains(library)) continue;
55                         if (visited.contains(library)) throw new DatabaseException("Cyclic dependency detected.");
56                         visited.add(library);
57                         collectDependencies(graph, library, order, visited);
58                         order.addFirst(library);
59                 }
60         }
61
62         private static List<ModelDependency> collectModelDependencies(ReadGraph graph, Resource resource) throws DatabaseException {
63                 List<Resource> order = collectDependencies(graph, resource);
64                 Collections.reverse(order);
65
66                 List<ModelDependency> modelDependencies = new ArrayList<>(order.size());
67
68                 for (Resource library : order) {
69                         String uri = graph.getPossibleURI(library);
70                         if(uri == null) continue;
71                         CopyHandler ch = graph.adapt(library, CopyHandler.class);
72                         SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
73                         ch.copyToClipboard(graph, clipboard);
74                         for (Set<Representation> object : clipboard.getContents()) {
75                                 TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
76                                 if(tg != null) {
77                                         modelDependencies.add(new ModelDependency(uri, tg));
78                                 }
79                         }
80                 }
81                 return modelDependencies;
82         }
83
84         public static ModelDependenciesBean create(ReadGraph graph, Resource resource) throws DatabaseException {
85                 List<ModelDependency> dependencies = collectModelDependencies(graph, resource);
86                 return new ModelDependenciesBean(dependencies.toArray(new ModelDependency[dependencies.size()]));
87         }
88
89         public static ModelDependenciesBean fromMigrationState(MigrationState state) throws DatabaseException, AdaptException {
90                 Map<String,Variant> extensions = state.getProperty(MigrationStateKeys.TG_EXTENSIONS);
91                 final Variant variant = extensions.get(ModelDependenciesBean.EXTENSION_KEY);
92                 return variant != null ? (ModelDependenciesBean) variant.getValue(ModelDependenciesBean.BINDING) : null;
93         }
94
95 }