1 package org.simantics.db.layer0.util;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
7 import java.util.TreeMap;
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.scl.runtime.function.Function1;
21 public class TransferableGraphConfiguration2 {
23 public static class RootSpec {
24 public final Resource resource;
25 public final String name;
26 public final boolean internal;
28 * Optional, may be null.
30 public final String type;
32 public RootSpec(Resource resource, String name, boolean internal) {
33 this(resource, name, internal, null);
36 public RootSpec(Resource resource, String name, boolean internal, String type) {
38 throw new NullPointerException("null resource");
40 throw new NullPointerException("null name");
41 this.resource = resource;
43 this.internal = internal;
48 public String toString() {
49 return "RootSpec[" + name + ", " + resource + ", " + internal + ", " + type + "]";
53 public int hashCode() {
56 result = prime * result + (internal ? 1231 : 1237);
57 result = prime * result + resource.hashCode();
58 result = prime * result + name.hashCode();
59 result = prime * result + (type != null ? type.hashCode() : 0);
64 public boolean equals(Object obj) {
69 if (getClass() != obj.getClass())
71 RootSpec other = (RootSpec) obj;
72 return internal == other.internal && resource.equals(other.resource) && name.equals(other.name)
73 && objectEquals(type, other.type);
76 private boolean objectEquals(Object o1, Object o2) {
77 if (o1 == o2) return true;
78 if (o1 == null && o2 == null) return true;
79 if (o1 == null || o2 == null) return false;
84 final public TreeMap<String, Variant> baseExtensions = new TreeMap<String,Variant>();
85 final public Resource indexRoot;
86 final public Collection<RootSpec> roots;
87 final public Map<Resource, ExtentStatus> preStatus;
88 final public boolean ignoreVirtualResources;
89 final public boolean validate;
92 * <code>true</code> to export values, <code>false</code> to skip the actual
93 * values and only write the resource id.
95 public boolean values = true;
97 public Collection<TGValueModifier> valueModifiers;
98 public Function1<Statement,ExclusionDecision> exclusionFunction;
100 public TransferableGraphConfiguration2(Resource indexRoot, Collection<RootSpec> roots, Map<Resource, ExtentStatus> preStatus, boolean ignoreVirtualResources, boolean validate) {
101 this.indexRoot = indexRoot;
103 this.preStatus = preStatus;
104 this.ignoreVirtualResources = ignoreVirtualResources;
105 this.validate = validate;
106 this.valueModifiers = null;
109 public TransferableGraphConfiguration2(Resource indexRoot, Collection<RootSpec> roots, Map<Resource, ExtentStatus> preStatus, boolean ignoreVirtualResources) {
110 this(indexRoot, roots, preStatus, ignoreVirtualResources, true);
113 public TransferableGraphConfiguration2(Resource indexRoot, Collection<RootSpec> roots, Map<Resource, ExtentStatus> preStatus) {
114 this(indexRoot, roots, preStatus, true);
117 public TransferableGraphConfiguration2(TransferableGraphConfiguration2 conf) throws DatabaseException {
118 this(conf.indexRoot, conf.roots, conf.preStatus, conf.ignoreVirtualResources, conf.validate);
121 public TransferableGraphConfiguration2(ReadGraph graph, Resource model, boolean ignoreVirtualResources, boolean validate) throws DatabaseException {
122 this(new TGConfigurer(graph, ignoreVirtualResources, validate).roots2(Collections.singletonList(model)).create());
125 public TransferableGraphConfiguration2(ReadGraph graph, Collection<Resource> roots, boolean ignoreVirtualResources, boolean validate) throws DatabaseException {
126 this(new TGConfigurer(graph, ignoreVirtualResources, validate).roots2(roots).create());
129 public TransferableGraphConfiguration2(ReadGraph graph, Resource model, boolean ignoreVirtualResources) throws DatabaseException {
130 this(graph, model, ignoreVirtualResources, true);
133 public TransferableGraphConfiguration2(ReadGraph graph, Resource model) throws DatabaseException {
134 this(graph, model, true);
137 public TransferableGraphConfiguration2(ReadGraph graph, Collection<RootSpec> roots, Collection<Resource> resourceRoots, Collection<Resource> exclusions) throws DatabaseException {
138 this(new TGConfigurer(graph, true).roots(roots).roots2(resourceRoots).exclusions(exclusions).create());
141 public TransferableGraphConfiguration2(ReadGraph graph, Collection<RootSpec> roots, Collection<Resource> resourceRoots, Collection<Resource> exclusions, boolean ignoreVirtualResource, boolean validate) throws DatabaseException {
142 this(new TGConfigurer(graph, ignoreVirtualResource, validate).roots(roots).roots2(resourceRoots).exclusions(exclusions).create());
145 public static TransferableGraphConfiguration2 createWithNames(RequestProcessor processor, final Collection<NamedResource> roots, final Collection<Resource> exclusions, final boolean ignoreVirtualResource, final boolean validate) throws DatabaseException {
146 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
149 public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
150 return new TransferableGraphConfiguration2(graph, translate(roots), Collections.<Resource>emptyList(), exclusions, ignoreVirtualResource, validate);
156 public static TransferableGraphConfiguration2 createWithNames2(RequestProcessor processor, final Collection<RootSpec> roots, final Collection<Resource> exclusions, final boolean ignoreVirtualResource, final boolean validate) throws DatabaseException {
157 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
160 public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
161 return new TransferableGraphConfiguration2(graph, roots, Collections.<Resource>emptyList(), exclusions, ignoreVirtualResource, validate);
167 public static TransferableGraphConfiguration2 createWithNames(RequestProcessor processor, final Collection<NamedResource> roots, final Collection<Resource> exclusions) throws DatabaseException {
168 return createWithNames(processor, roots, exclusions, true, true);
171 public static TransferableGraphConfiguration2 createWithResources(RequestProcessor processor, final Collection<Resource> roots, final Collection<Resource> exclusions) throws DatabaseException {
172 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
175 public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
176 return new TransferableGraphConfiguration2(graph, Collections.<RootSpec>emptyList(), roots, exclusions);
182 public static TransferableGraphConfiguration2 createForModel(RequestProcessor processor, final Resource model) throws DatabaseException {
183 return createWithResources(processor, Collections.singletonList(model), Collections.<Resource>emptyList());
186 private static Collection<RootSpec> translate(Collection<NamedResource> roots) {
187 ArrayList<RootSpec> result = new ArrayList<RootSpec>();
188 for(NamedResource nr : roots) result.add(new RootSpec(nr.getResource(), nr.getName(), true));