1 package org.simantics.db.layer0.util;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
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;
20 public class TGConfigurer {
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;
28 public TGConfigurer(ReadGraph graph, boolean ignoreVirtualResources, boolean validate) {
30 this.ignoreVirtualResources = ignoreVirtualResources;
31 this.validate = validate;
34 public TGConfigurer(ReadGraph graph, boolean ignoreVirtualResources) {
35 this(graph, ignoreVirtualResources, true);
38 public TGConfigurer exclusions(Collection<Resource> exclusions) {
39 for(Resource r : exclusions) {
40 preStatus.put(r, ExtentStatus.EXCLUDED);
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);
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);
66 roots.add(new SeedSpec(root, name, SeedSpecType.ROOT));
67 preStatus.put(root, ExtentStatus.INTERNAL);
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);
83 public TransferableGraphConfiguration2 create() throws DatabaseException {
87 // -Exclude root library if not explicitly included
88 if (!roots.contains(graph.getRootLibrary()))
89 preStatus.put(graph.getRootLibrary(), ExtentStatus.EXCLUDED);
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);
103 preStatus.put(root.resource, ExtentStatus.EXTERNAL);
107 Resource owner = CommonDBUtils.getNearestOwner(graph, rootList);
109 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(owner));
110 return new TransferableGraphConfiguration2(indexRoot, roots, preStatus, ignoreVirtualResources, validate);
112 return new TransferableGraphConfiguration2(null, roots, preStatus, ignoreVirtualResources, validate);