1 package org.simantics.db.layer0.util;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.request.IsParent;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
14 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec;
15 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec.SeedSpecType;
17 public class TGConfigurer2 {
19 final Collection<SeedSpec> seeds = new ArrayList<SeedSpec>();
20 final Map<Resource, ExtentStatus> preStatus = new HashMap<Resource, ExtentStatus>();
21 final boolean ignoreVirtualResources;
22 final boolean validate;
24 public TGConfigurer2(boolean ignoreVirtualResources, boolean validate) {
25 this.ignoreVirtualResources = ignoreVirtualResources;
26 this.validate = validate;
29 public TGConfigurer2 addInternalSeed(ReadGraph graph, Resource r) throws DatabaseException {
31 // If parent is already an internal => skip this
32 for(SeedSpec spec : seeds) {
33 if(SeedSpecType.INTERNAL.equals(spec.specType)) {
34 if(graph.syncRequest(new IsParent(spec.resource, r))) return this;
37 // If some existing internal is a child of this one => remove them
38 List<Resource> removals = new ArrayList<>();
39 for(SeedSpec spec : seeds) {
40 if(SeedSpecType.INTERNAL.equals(spec.specType)) {
41 if(graph.syncRequest(new IsParent(r, spec.resource))) removals.add(spec.resource);
44 for(Resource removal : removals) {
45 SeedSpec spec = getSeed(removal);
51 seeds.add(new SeedSpec(r, "", SeedSpecType.INTERNAL));
57 private SeedSpec getSeed(Resource r) {
58 for(SeedSpec spec : seeds) {
59 if(spec.resource.equals(r)) return spec;
64 public TGConfigurer2 addExternal(Resource r) throws DatabaseException {
65 preStatus.put(r, ExtentStatus.EXTERNAL);
69 public TGConfigurer2 addExcluded(Resource r) throws DatabaseException {
70 preStatus.put(r, ExtentStatus.EXCLUDED);
74 public TransferableGraphConfiguration2 create() throws DatabaseException {
75 return new TransferableGraphConfiguration2(null, seeds, preStatus, ignoreVirtualResources, validate);