-package org.simantics.db.layer0.util;\r
-\r
-import java.util.Arrays;\r
-import java.util.Map;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.RequestProcessor;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.util.SimanticsClipboard.Representation;\r
-import org.simantics.graph.db.TransferableGraphs;\r
-import org.simantics.graph.representation.TransferableGraph1;\r
-import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
-\r
-public class TGRepresentation implements Representation {\r
-\r
- protected boolean ignoreVirtualResources = false;\r
- protected Resource[] resources;\r
- private TransferableGraph1 value = null;\r
- private TransferableGraphConfiguration2 configuration;\r
- \r
- public TGRepresentation(Resource ... resources) {\r
- this.resources = resources;\r
- }\r
-\r
- public TGRepresentation(boolean ignoreVirtualResources, Resource ... resources) {\r
- this.ignoreVirtualResources = ignoreVirtualResources;\r
- this.resources = resources;\r
- }\r
-\r
- public TGRepresentation(TransferableGraphConfiguration2 configuration) {\r
- this.configuration = configuration;\r
- }\r
-\r
- public TGRepresentation(TransferableGraph1 value) {\r
- this.value = value;\r
- }\r
-\r
- @Override\r
- public Key getKey() {\r
- return SimanticsKeys.KEY_TRANSFERABLE_GRAPH;\r
- }\r
- \r
- public TransferableGraph1 compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {\r
- \r
- if(configuration == null) {\r
- configuration = new TransferableGraphConfiguration2(graph, Arrays.asList(resources), ignoreVirtualResources, false);\r
- configuration.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);\r
- }\r
- \r
- ModelTransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(configuration));\r
- return TransferableGraphs.create(graph, source);\r
- \r
- }\r
- \r
- @SuppressWarnings("unchecked")\r
- @Override\r
- public <T> T getValue(RequestProcessor processor, Map<String,Object> hints) throws DatabaseException {\r
- \r
- if(value == null) {\r
-\r
- value = processor.syncRequest(new UniqueRead<TransferableGraph1>() {\r
-\r
- @Override\r
- public TransferableGraph1 perform(ReadGraph graph) throws DatabaseException {\r
- return compute(graph, hints);\r
- }\r
-\r
- });\r
-\r
- }\r
- \r
- return (T)value;\r
- \r
- }\r
- \r
+package org.simantics.db.layer0.util;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Map;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
+import org.simantics.graph.db.TransferableGraphs;
+import org.simantics.graph.representation.TransferableGraph1;
+import org.simantics.utils.datastructures.hints.IHintContext.Key;
+
+public class TGRepresentation implements Representation {
+
+ protected boolean ignoreVirtualResources = false;
+ protected Resource[] resources;
+ private TransferableGraph1 value = null;
+ private TransferableGraphConfiguration2 configuration;
+
+ public TGRepresentation(Resource ... resources) {
+ this.resources = resources;
+ }
+
+ public TGRepresentation(boolean ignoreVirtualResources, Resource ... resources) {
+ this.ignoreVirtualResources = ignoreVirtualResources;
+ this.resources = resources;
+ }
+
+ public TGRepresentation(TransferableGraphConfiguration2 configuration) {
+ this.configuration = configuration;
+ }
+
+ public TGRepresentation(TransferableGraph1 value) {
+ this.value = value;
+ }
+
+ @Override
+ public Key getKey() {
+ return SimanticsKeys.KEY_TRANSFERABLE_GRAPH;
+ }
+
+ public TransferableGraph1 compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {
+
+ if(configuration == null) {
+ configuration = new TransferableGraphConfiguration2(graph, Arrays.asList(resources), ignoreVirtualResources, false);
+ configuration.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);
+ }
+
+ try (ModelTransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(configuration))) {
+ return TransferableGraphs.create(graph, source);
+ } catch (IOException e) {
+ throw new DatabaseException(e);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T> T getValue(RequestProcessor processor, Map<String,Object> hints) throws DatabaseException {
+
+ if(value == null) {
+
+ value = processor.syncRequest(new UniqueRead<TransferableGraph1>() {
+
+ @Override
+ public TransferableGraph1 perform(ReadGraph graph) throws DatabaseException {
+ return compute(graph, hints);
+ }
+
+ });
+
+ }
+
+ return (T)value;
+
+ }
+
}
\ No newline at end of file