0f9ffa0a73730314f7ba6d56a655f507d28d9b0c
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / TGRepresentation.java
1 package org.simantics.db.layer0.util;
2
3 import java.util.Arrays;
4 import java.util.Map;
5
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.RequestProcessor;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.request.UniqueRead;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
12 import org.simantics.graph.db.TransferableGraphs;
13 import org.simantics.graph.representation.TransferableGraph1;
14 import org.simantics.utils.datastructures.hints.IHintContext.Key;
15
16 public class TGRepresentation implements Representation {
17
18         protected boolean ignoreVirtualResources = false;
19         protected Resource[] resources;
20         private TransferableGraph1 value = null;
21         private TransferableGraphConfiguration2 configuration;
22         
23         public TGRepresentation(Resource ... resources) {
24                 this.resources = resources;
25         }
26
27         public TGRepresentation(boolean ignoreVirtualResources, Resource ... resources) {
28                 this.ignoreVirtualResources = ignoreVirtualResources;
29                 this.resources = resources;
30         }
31
32         public TGRepresentation(TransferableGraphConfiguration2 configuration) {
33                 this.configuration = configuration;
34         }
35
36         public TGRepresentation(TransferableGraph1 value) {
37                 this.value = value;
38         }
39
40         @Override
41         public Key getKey() {
42                 return SimanticsKeys.KEY_TRANSFERABLE_GRAPH;
43         }
44         
45         public TransferableGraph1 compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {
46                 
47                 if(configuration == null) {
48                         configuration = new TransferableGraphConfiguration2(graph, Arrays.asList(resources), ignoreVirtualResources, false);
49                         configuration.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);
50                 }
51                 
52                 ModelTransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(configuration));
53                 return TransferableGraphs.create(graph, source);
54                 
55         }
56         
57         @SuppressWarnings("unchecked")
58         @Override
59         public <T> T getValue(RequestProcessor processor, Map<String,Object> hints) throws DatabaseException {
60                 
61                 if(value == null) {
62
63                         value = processor.syncRequest(new UniqueRead<TransferableGraph1>() {
64
65                                 @Override
66                                 public TransferableGraph1 perform(ReadGraph graph) throws DatabaseException {
67                                         return compute(graph, hints);
68                                 }
69
70                         });
71
72                 }
73                 
74                 return (T)value;
75                 
76         }
77         
78 }