]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TransferableGraphConfiguration2.java
Clean up and support internal seed resources in tg export
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / TransferableGraphConfiguration2.java
1 package org.simantics.db.layer0.util;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.Map;
7 import java.util.TreeMap;
8
9 import org.simantics.databoard.binding.mutable.Variant;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.RequestProcessor;
12 import org.simantics.db.Resource;
13 import org.simantics.db.Statement;
14 import org.simantics.db.common.NamedResource;
15 import org.simantics.db.common.request.UniqueRead;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
18 import org.simantics.db.layer0.util.DomainProcessor3.ExclusionDecision;
19 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec.SeedSpecType;
20 import org.simantics.scl.runtime.function.Function1;
21
22 public class TransferableGraphConfiguration2 {
23
24         public static class SeedSpec {
25
26                 public static enum SeedSpecType {
27                         INTERNAL, ROOT, SPECIAL_ROOT
28                 }
29
30                 public final Resource resource;
31                 public final String name;
32                 // Special roots are not normal seeds - e.g. %model
33                 public final SeedSpecType specType;
34                 /**
35                  * Optional, may be null.
36                  */
37                 public final String type;
38
39                 public SeedSpec(Resource resource, String name, SeedSpecType specType) {
40                         this(resource, name, specType, null);
41                 }
42
43                 public SeedSpec(Resource resource, String name, SeedSpecType specType, String type) {
44                         if (resource == null)
45                                 throw new NullPointerException("null resource");
46                         if (name == null)
47                                 throw new NullPointerException("null name");
48                         this.resource = resource;
49                         this.name = name;
50                         this.specType = specType;
51                         this.type = type;
52                 }
53
54                 public static SeedSpec internal(Resource resource) {
55                         return new SeedSpec(resource, null, SeedSpecType.ROOT);
56                 }
57
58                 @Override
59                 public String toString() {
60                         return "SeedSpec[" + name + ", " + resource + ", " + specType + ", " + type + "]";
61                 }
62
63                 @Override
64                 public int hashCode() {
65                         final int prime = 31;
66                         int result = 1;
67                         result = prime * result + specType.hashCode();
68                         result = prime * result + resource.hashCode();
69                         result = prime * result + name.hashCode();
70                         result = prime * result + (type != null ? type.hashCode() : 0);
71                         return result;
72                 }
73
74                 @Override
75                 public boolean equals(Object obj) {
76                         if (this == obj)
77                                 return true;
78                         if (obj == null)
79                                 return false;
80                         if (getClass() != obj.getClass())
81                                 return false;
82                         SeedSpec other = (SeedSpec) obj;
83                         return specType.equals(other.specType) && resource.equals(other.resource) && name.equals(other.name)
84                                         && objectEquals(type, other.type);
85                 }
86
87                 private boolean objectEquals(Object o1, Object o2) {
88                         if (o1 == o2) return true;
89                         if (o1 == null && o2 == null) return true;
90                         if (o1 == null || o2 == null) return false;
91                         return o1.equals(o2);
92                 }
93         }
94
95         final public TreeMap<String, Variant> baseExtensions = new TreeMap<>();
96         final public Resource indexRoot;
97         final public Collection<SeedSpec> seeds;
98         final public Map<Resource, ExtentStatus> preStatus;
99         final public boolean ignoreVirtualResources;
100         final public boolean validate;
101
102         /**
103          * <code>true</code> to export values, <code>false</code> to skip the actual
104          * values and only write the resource id.
105          */
106         public boolean values = true;
107
108         public Collection<TGValueModifier> valueModifiers;
109         public Function1<Statement,ExclusionDecision> exclusionFunction;
110
111         public TransferableGraphConfiguration2(Resource indexRoot, Collection<SeedSpec> seeds, Map<Resource, ExtentStatus> preStatus, boolean ignoreVirtualResources, boolean validate) {
112                 this.indexRoot = indexRoot;
113                 this.seeds = seeds;
114                 this.preStatus = preStatus;
115                 this.ignoreVirtualResources = ignoreVirtualResources;
116                 this.validate = validate;
117                 this.valueModifiers = null;
118         }
119
120         public TransferableGraphConfiguration2(Resource indexRoot, Collection<SeedSpec> roots, Map<Resource, ExtentStatus> preStatus, boolean ignoreVirtualResources) {
121                 this(indexRoot, roots, preStatus, ignoreVirtualResources, true);
122         }
123
124         public TransferableGraphConfiguration2(Resource indexRoot, Collection<SeedSpec> roots, Map<Resource, ExtentStatus> preStatus) {
125                 this(indexRoot, roots, preStatus, true);
126         }
127
128         public TransferableGraphConfiguration2(TransferableGraphConfiguration2 conf) throws DatabaseException {
129                 this(conf.indexRoot, conf.seeds, conf.preStatus, conf.ignoreVirtualResources, conf.validate);
130         }
131
132         public TransferableGraphConfiguration2(ReadGraph graph, Resource model, boolean ignoreVirtualResources, boolean validate) throws DatabaseException {
133                 this(new TGConfigurer(graph, ignoreVirtualResources, validate).roots2(Collections.singletonList(model)).create());
134         }
135
136         public TransferableGraphConfiguration2(ReadGraph graph, Collection<Resource> roots, boolean ignoreVirtualResources, boolean validate) throws DatabaseException {
137             this(new TGConfigurer(graph, ignoreVirtualResources, validate).roots2(roots).create());
138         }
139
140         public TransferableGraphConfiguration2(ReadGraph graph, Resource model, boolean ignoreVirtualResources) throws DatabaseException {
141                 this(graph, model, ignoreVirtualResources, true);
142         }
143
144         public TransferableGraphConfiguration2(ReadGraph graph, Resource model) throws DatabaseException {
145                 this(graph, model, true);
146         }
147
148         public TransferableGraphConfiguration2(ReadGraph graph, Collection<SeedSpec> roots, Collection<Resource> resourceRoots, Collection<Resource> exclusions) throws DatabaseException {
149                 this(new TGConfigurer(graph, true).roots(roots).roots2(resourceRoots).exclusions(exclusions).create());
150         }
151
152         public TransferableGraphConfiguration2(ReadGraph graph, Collection<SeedSpec> roots, Collection<Resource> resourceRoots, Collection<Resource> exclusions, boolean ignoreVirtualResource, boolean validate) throws DatabaseException {
153                 this(new TGConfigurer(graph, ignoreVirtualResource, validate).roots(roots).roots2(resourceRoots).exclusions(exclusions).create());
154         }
155
156         public static TransferableGraphConfiguration2 createWithNames(RequestProcessor processor, final Collection<NamedResource> roots, final Collection<Resource> exclusions, final boolean ignoreVirtualResource, final boolean validate) throws DatabaseException {
157                 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
158
159                         @Override
160                         public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
161                                 return new TransferableGraphConfiguration2(graph, translate(roots), Collections.<Resource>emptyList(), exclusions, ignoreVirtualResource, validate);
162                         }
163
164                 });
165         }
166
167         public static TransferableGraphConfiguration2 createWithNames2(RequestProcessor processor, final Collection<SeedSpec> roots, final Collection<Resource> exclusions, final boolean ignoreVirtualResource, final boolean validate) throws DatabaseException {
168                 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
169
170                         @Override
171                         public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
172                                 return new TransferableGraphConfiguration2(graph, roots, Collections.<Resource>emptyList(), exclusions, ignoreVirtualResource, validate);
173                         }
174
175                 });
176         }
177
178         public static TransferableGraphConfiguration2 createWithNames(RequestProcessor processor, final Collection<NamedResource> roots, final Collection<Resource> exclusions) throws DatabaseException {
179                 return createWithNames(processor, roots, exclusions, true, true);
180         }
181
182         public static TransferableGraphConfiguration2 createWithResources(RequestProcessor processor, final Collection<Resource> roots, final Collection<Resource> exclusions) throws DatabaseException {
183                 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
184
185                         @Override
186                         public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
187                                 return new TransferableGraphConfiguration2(graph, Collections.<SeedSpec>emptyList(), roots, exclusions);
188                         }
189
190                 });
191         }
192
193         public static TransferableGraphConfiguration2 createForModel(RequestProcessor processor, final Resource model) throws DatabaseException {
194                 return createWithResources(processor, Collections.singletonList(model), Collections.<Resource>emptyList());
195         }
196
197         private static Collection<SeedSpec> translate(Collection<NamedResource> roots) {
198                 ArrayList<SeedSpec> result = new ArrayList<>();
199                 for(NamedResource nr : roots) result.add(new SeedSpec(nr.getResource(), nr.getName(), SeedSpecType.ROOT));
200                 return result;
201         }
202
203 }