]> gerrit.simantics Code Review - simantics/platform.git/blob
2203ecaeb6bd792635aa1648495f20c3b5f03956
[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                  * Optional, may be null.
29                  */
30                 public final String type;
31
32                 public RootSpec(Resource resource, String name, boolean internal) {
33                         this(resource, name, internal, null);
34                 }
35
36                 public RootSpec(Resource resource, String name, boolean internal, String type) {
37                         if (resource == null)
38                                 throw new NullPointerException("null resource");
39                         if (name == null)
40                                 throw new NullPointerException("null name");
41                         this.resource = resource;
42                         this.name = name;
43                         this.internal = internal;
44                         this.type = type;
45                 }
46
47                 @Override
48                 public String toString() {
49                         return "RootSpec[" + name + ", " + resource + ", " + internal + ", " + type + "]"; 
50                 }
51
52                 @Override
53                 public int hashCode() {
54                         final int prime = 31;
55                         int result = 1;
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);
60                         return result;
61                 }
62
63                 @Override
64                 public boolean equals(Object obj) {
65                         if (this == obj)
66                                 return true;
67                         if (obj == null)
68                                 return false;
69                         if (getClass() != obj.getClass())
70                                 return false;
71                         RootSpec other = (RootSpec) obj;
72                         return internal == other.internal && resource.equals(other.resource) && name.equals(other.name)
73                                         && objectEquals(type, other.type);
74                 }
75
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;
80                         return o1.equals(o2);
81                 }
82         }
83
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;
90         
91         /**
92          * <code>true</code> to export values, <code>false</code> to skip the actual
93          * values and only write the resource id.
94          */
95         public boolean values = true;
96
97         public Collection<TGValueModifier> valueModifiers;
98         public Function1<Statement,ExclusionDecision> exclusionFunction;
99
100         public TransferableGraphConfiguration2(Resource indexRoot, Collection<RootSpec> roots, Map<Resource, ExtentStatus> preStatus, boolean ignoreVirtualResources, boolean validate) {
101                 this.indexRoot = indexRoot;
102                 this.roots = roots;
103                 this.preStatus = preStatus;
104                 this.ignoreVirtualResources = ignoreVirtualResources;
105                 this.validate = validate;
106                 this.valueModifiers = null;
107         }
108
109         public TransferableGraphConfiguration2(Resource indexRoot, Collection<RootSpec> roots, Map<Resource, ExtentStatus> preStatus, boolean ignoreVirtualResources) {
110                 this(indexRoot, roots, preStatus, ignoreVirtualResources, true);
111         }
112
113         public TransferableGraphConfiguration2(Resource indexRoot, Collection<RootSpec> roots, Map<Resource, ExtentStatus> preStatus) {
114                 this(indexRoot, roots, preStatus, true);
115         }
116
117         public TransferableGraphConfiguration2(TransferableGraphConfiguration2 conf) throws DatabaseException {
118                 this(conf.indexRoot, conf.roots, conf.preStatus, conf.ignoreVirtualResources, conf.validate);
119         }
120
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());
123         }
124
125         public TransferableGraphConfiguration2(ReadGraph graph, Collection<Resource> roots, boolean ignoreVirtualResources, boolean validate) throws DatabaseException {
126             this(new TGConfigurer(graph, ignoreVirtualResources, validate).roots2(roots).create());
127         }
128
129         public TransferableGraphConfiguration2(ReadGraph graph, Resource model, boolean ignoreVirtualResources) throws DatabaseException {
130                 this(graph, model, ignoreVirtualResources, true);
131         }
132
133         public TransferableGraphConfiguration2(ReadGraph graph, Resource model) throws DatabaseException {
134                 this(graph, model, true);
135         }
136         
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());
139         }
140
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());
143         }
144
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>() {
147
148                         @Override
149                         public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
150                                 return new TransferableGraphConfiguration2(graph, translate(roots), Collections.<Resource>emptyList(), exclusions, ignoreVirtualResource, validate);
151                         }
152                         
153                 });
154         }
155
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>() {
158
159                         @Override
160                         public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
161                                 return new TransferableGraphConfiguration2(graph, roots, Collections.<Resource>emptyList(), exclusions, ignoreVirtualResource, validate);
162                         }
163                         
164                 });
165         }
166
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);
169         }
170
171         public static TransferableGraphConfiguration2 createWithResources(RequestProcessor processor, final Collection<Resource> roots, final Collection<Resource> exclusions) throws DatabaseException {
172                 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
173
174                         @Override
175                         public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
176                                 return new TransferableGraphConfiguration2(graph, Collections.<RootSpec>emptyList(), roots, exclusions);
177                         }
178                         
179                 });
180         }
181
182         public static TransferableGraphConfiguration2 createForModel(RequestProcessor processor, final Resource model) throws DatabaseException {
183                 return createWithResources(processor, Collections.singletonList(model), Collections.<Resource>emptyList());
184         }
185
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));
189                 return result;
190         }
191         
192 }