]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TGConfigurer2.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 / TGConfigurer2.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.List;
7 import java.util.Map;
8
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;
16
17 public class TGConfigurer2 {
18
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;
23
24         public TGConfigurer2(boolean ignoreVirtualResources, boolean validate) {
25                 this.ignoreVirtualResources = ignoreVirtualResources;
26                 this.validate = validate;
27         }
28
29         public TGConfigurer2 addInternalSeed(ReadGraph graph, Resource r) throws DatabaseException {
30
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;
35                         }
36                 }
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);
42                         }
43                 }
44                 for(Resource removal : removals) {
45                         SeedSpec spec = getSeed(removal);
46                         if(spec != null) {
47                                 seeds.remove(spec);
48                         }
49                 }
50
51                 seeds.add(new SeedSpec(r, "", SeedSpecType.INTERNAL));
52
53                 return this;
54
55         }
56
57         private SeedSpec getSeed(Resource r) {
58                 for(SeedSpec spec : seeds) {
59                         if(spec.resource.equals(r)) return spec;
60                 }
61                 return null;
62         }
63
64         public TGConfigurer2 addExternal(Resource r) throws DatabaseException {
65                 preStatus.put(r, ExtentStatus.EXTERNAL);
66                 return this;
67         }
68
69         public TGConfigurer2 addExcluded(Resource r) throws DatabaseException {
70                 preStatus.put(r, ExtentStatus.EXCLUDED);
71                 return this;
72         }
73
74         public TransferableGraphConfiguration2 create() throws DatabaseException {
75                 return new TransferableGraphConfiguration2(null, seeds, preStatus, ignoreVirtualResources, validate);
76         }
77
78 }