]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TGConfigurer.java
Merge "Added resourceId and GUID to diagram DnD content" into release/1.35.1
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / TGConfigurer.java
1 package org.simantics.db.layer0.util;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import org.simantics.databoard.Bindings;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.NamedResource;
12 import org.simantics.db.common.request.PossibleIndexRoot;
13 import org.simantics.db.common.utils.CommonDBUtils;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
16 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec;
17 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec.SeedSpecType;
18 import org.simantics.layer0.Layer0;
19
20 public class TGConfigurer {
21
22         final private ReadGraph graph;
23         final Collection<SeedSpec> roots = new ArrayList<>();
24         final Map<Resource, ExtentStatus> preStatus = new HashMap<>();
25         final boolean ignoreVirtualResources;
26         final boolean validate;
27         
28         public TGConfigurer(ReadGraph graph, boolean ignoreVirtualResources, boolean validate) {
29                 this.graph = graph;
30                 this.ignoreVirtualResources = ignoreVirtualResources;
31                 this.validate = validate;
32         }
33         
34         public TGConfigurer(ReadGraph graph, boolean ignoreVirtualResources) {
35                 this(graph, ignoreVirtualResources, true);
36         }
37
38         public TGConfigurer exclusions(Collection<Resource> exclusions) {
39                 for(Resource r : exclusions) {
40                         preStatus.put(r, ExtentStatus.EXCLUDED);
41                 }
42                 return this;
43         }
44
45         public TGConfigurer roots(Collection<SeedSpec> rs) throws DatabaseException {
46                 Layer0 L0 = Layer0.getInstance(graph);
47                 for(SeedSpec root : rs) {
48                         Resource nameResource = graph.getPossibleObject(root.resource, L0.HasName);
49                         if(nameResource != null) preStatus.put(nameResource, ExtentStatus.EXCLUDED);
50                         roots.add(new SeedSpec(root.resource, root.name, root.specType, root.type));
51                         preStatus.put(root.resource, ExtentStatus.INTERNAL);
52                 }
53                 return this;
54         }
55
56         public TGConfigurer roots2(Collection<Resource> rs) throws DatabaseException {
57                 Layer0 L0 = Layer0.getInstance(graph);
58                 for(Resource root : rs) {
59                         Resource nameResource = graph.getPossibleObject(root, L0.HasName);
60                         String name = "<no name>";
61                         if(nameResource != null) {
62                                 name = graph.getPossibleValue(nameResource, Bindings.STRING);
63                                 if(name == null) name = "<no name>";
64                                 preStatus.put(nameResource, ExtentStatus.EXCLUDED);
65                         }
66                         roots.add(new SeedSpec(root, name, SeedSpecType.ROOT));
67                         preStatus.put(root, ExtentStatus.INTERNAL);
68                 }
69                 return this;
70         }
71         
72         public TGConfigurer roots3(Collection<NamedResource> rs) throws DatabaseException {
73                 Layer0 L0 = Layer0.getInstance(graph);
74                 for(NamedResource root : rs) {
75                         Resource nameResource = graph.getPossibleObject(root.getResource(), L0.HasName);
76                         roots.add(new SeedSpec(root.getResource(), root.getName(), SeedSpecType.ROOT));
77                         preStatus.put(root.getResource(), ExtentStatus.INTERNAL);
78                         preStatus.put(nameResource, ExtentStatus.EXCLUDED);
79                 }
80                 return this;
81         }
82
83         public TransferableGraphConfiguration2 create() throws DatabaseException {
84
85                 // Final preparation
86
87                 // -Exclude root library if not explicitly included
88                 if (!roots.contains(graph.getRootLibrary()))
89                         preStatus.put(graph.getRootLibrary(), ExtentStatus.EXCLUDED);
90
91                 // -Exclude owners if not explicitly defined
92                 Layer0 L0 = Layer0.getInstance(graph);
93                 ArrayList<Resource> rootList = new ArrayList<>();
94                 for (SeedSpec root : roots) {
95                         if(!SeedSpecType.SPECIAL_ROOT.equals(root.specType)) {
96                                 rootList.add(root.resource);
97                                 for (Resource owner : graph.getObjects(root.resource, L0.IsOwnedBy)) {
98                                         ExtentStatus ownerStatus = preStatus.get(owner);
99                                         if(ownerStatus == null)
100                                                 preStatus.put(owner, ExtentStatus.EXCLUDED);
101                                 }
102                         } else {
103                                 preStatus.put(root.resource, ExtentStatus.EXTERNAL);
104                         }
105                 }
106                 
107                 Resource owner = CommonDBUtils.getNearestOwner(graph, rootList);
108                 if(owner != null) {
109                         Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(owner));
110                         return new TransferableGraphConfiguration2(indexRoot, roots, preStatus, ignoreVirtualResources, validate);
111                 } else {
112                         return new TransferableGraphConfiguration2(null, roots, preStatus, ignoreVirtualResources, validate);
113                 }
114                 
115         }
116         
117 }