]> gerrit.simantics Code Review - simantics/platform.git/blob
94c6084f294417efaa8b6232c2625e78c014e951
[simantics/platform.git] /
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.scl.runtime.function.Function1;
20
21 public class TransferableGraphConfiguration2 {
22         
23         public static class RootSpec {
24                 public final Resource resource;
25                 public final String name;
26                 public final boolean internal;
27
28                 public RootSpec(Resource resource, String name, boolean internal) {
29                         if (resource == null)
30                                 throw new NullPointerException("null resource");
31                         if (name == null)
32                                 throw new NullPointerException("null name");
33                         this.resource = resource;
34                         this.name = name;
35                         this.internal = internal;
36                 }
37
38                 @Override
39                 public String toString() {
40                         return "RootSpec[" + name + ", " + resource + ", " + internal + "]"; 
41                 }
42
43                 @Override
44                 public int hashCode() {
45                         final int prime = 31;
46                         int result = 1;
47                         result = prime * result + (internal ? 1231 : 1237);
48                         result = prime * result + resource.hashCode();
49                         result = prime * result + name.hashCode();
50                         return result;
51                 }
52
53                 @Override
54                 public boolean equals(Object obj) {
55                         if (this == obj)
56                                 return true;
57                         if (obj == null)
58                                 return false;
59                         if (getClass() != obj.getClass())
60                                 return false;
61                         RootSpec other = (RootSpec) obj;
62                         return internal == other.internal && resource.equals(other.resource) && name.equals(other.name);
63                 }
64         }
65         
66         final public TreeMap<String, Variant> baseExtensions = new TreeMap<String,Variant>();
67         final public Resource indexRoot;
68         final public Collection<RootSpec> roots;
69         final public Map<Resource, ExtentStatus> preStatus;
70         final public boolean ignoreVirtualResources;
71         final public boolean validate;
72         
73         /**
74          * <code>true</code> to export values, <code>false</code> to skip the actual
75          * values and only write the resource id.
76          */
77         public boolean values = true;
78
79         public Collection<TGValueModifier> valueModifiers;
80         public Function1<Statement,ExclusionDecision> exclusionFunction;
81
82         public TransferableGraphConfiguration2(Resource indexRoot, Collection<RootSpec> roots, Map<Resource, ExtentStatus> preStatus, boolean ignoreVirtualResources, boolean validate) {
83                 this.indexRoot = indexRoot;
84                 this.roots = roots;
85                 this.preStatus = preStatus;
86                 this.ignoreVirtualResources = ignoreVirtualResources;
87                 this.validate = validate;
88                 this.valueModifiers = null;
89         }
90
91         public TransferableGraphConfiguration2(Resource indexRoot, Collection<RootSpec> roots, Map<Resource, ExtentStatus> preStatus, boolean ignoreVirtualResources) {
92                 this(indexRoot, roots, preStatus, ignoreVirtualResources, true);
93         }
94
95         public TransferableGraphConfiguration2(Resource indexRoot, Collection<RootSpec> roots, Map<Resource, ExtentStatus> preStatus) {
96                 this(indexRoot, roots, preStatus, true);
97         }
98
99         public TransferableGraphConfiguration2(TransferableGraphConfiguration2 conf) throws DatabaseException {
100                 this(conf.indexRoot, conf.roots, conf.preStatus, conf.ignoreVirtualResources, conf.validate);
101         }
102
103         public TransferableGraphConfiguration2(ReadGraph graph, Resource model, boolean ignoreVirtualResources, boolean validate) throws DatabaseException {
104                 this(new TGConfigurer(graph, ignoreVirtualResources, validate).roots2(Collections.singletonList(model)).create());
105         }
106
107         public TransferableGraphConfiguration2(ReadGraph graph, Collection<Resource> roots, boolean ignoreVirtualResources, boolean validate) throws DatabaseException {
108             this(new TGConfigurer(graph, ignoreVirtualResources, validate).roots2(roots).create());
109         }
110
111         public TransferableGraphConfiguration2(ReadGraph graph, Resource model, boolean ignoreVirtualResources) throws DatabaseException {
112                 this(graph, model, ignoreVirtualResources, true);
113         }
114
115         public TransferableGraphConfiguration2(ReadGraph graph, Resource model) throws DatabaseException {
116                 this(graph, model, true);
117         }
118         
119         public TransferableGraphConfiguration2(ReadGraph graph, Collection<RootSpec> roots, Collection<Resource> resourceRoots, Collection<Resource> exclusions) throws DatabaseException {
120                 this(new TGConfigurer(graph, true).roots(roots).roots2(resourceRoots).exclusions(exclusions).create());
121         }
122
123         public TransferableGraphConfiguration2(ReadGraph graph, Collection<RootSpec> roots, Collection<Resource> resourceRoots, Collection<Resource> exclusions, boolean ignoreVirtualResource, boolean validate) throws DatabaseException {
124                 this(new TGConfigurer(graph, ignoreVirtualResource, validate).roots(roots).roots2(resourceRoots).exclusions(exclusions).create());
125         }
126
127         public static TransferableGraphConfiguration2 createWithNames(RequestProcessor processor, final Collection<NamedResource> roots, final Collection<Resource> exclusions, final boolean ignoreVirtualResource, final boolean validate) throws DatabaseException {
128                 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
129
130                         @Override
131                         public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
132                                 return new TransferableGraphConfiguration2(graph, translate(roots), Collections.<Resource>emptyList(), exclusions, ignoreVirtualResource, validate);
133                         }
134                         
135                 });
136         }
137
138         public static TransferableGraphConfiguration2 createWithNames2(RequestProcessor processor, final Collection<RootSpec> roots, final Collection<Resource> exclusions, final boolean ignoreVirtualResource, final boolean validate) throws DatabaseException {
139                 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
140
141                         @Override
142                         public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
143                                 return new TransferableGraphConfiguration2(graph, roots, Collections.<Resource>emptyList(), exclusions, ignoreVirtualResource, validate);
144                         }
145                         
146                 });
147         }
148
149         public static TransferableGraphConfiguration2 createWithNames(RequestProcessor processor, final Collection<NamedResource> roots, final Collection<Resource> exclusions) throws DatabaseException {
150                 return createWithNames(processor, roots, exclusions, true, true);
151         }
152
153         public static TransferableGraphConfiguration2 createWithResources(RequestProcessor processor, final Collection<Resource> roots, final Collection<Resource> exclusions) throws DatabaseException {
154                 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
155
156                         @Override
157                         public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
158                                 return new TransferableGraphConfiguration2(graph, Collections.<RootSpec>emptyList(), roots, exclusions);
159                         }
160                         
161                 });
162         }
163
164         public static TransferableGraphConfiguration2 createForModel(RequestProcessor processor, final Resource model) throws DatabaseException {
165                 return createWithResources(processor, Collections.singletonList(model), Collections.<Resource>emptyList());
166         }
167
168         private static Collection<RootSpec> translate(Collection<NamedResource> roots) {
169                 ArrayList<RootSpec> result = new ArrayList<RootSpec>();
170                 for(NamedResource nr : roots) result.add(new RootSpec(nr.getResource(), nr.getName(), true));
171                 return result;
172         }
173         
174 }