]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/GraphTemplate.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / GraphTemplate.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/GraphTemplate.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/GraphTemplate.java
new file mode 100644 (file)
index 0000000..210c0ad
--- /dev/null
@@ -0,0 +1,78 @@
+/*******************************************************************************\r
+ * Copyright (c) 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.layer0.adapter.impl;\r
+\r
+import java.util.Map;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.WriteOnlyGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.Template;\r
+import org.simantics.graph.db.IImportAdvisor;\r
+import org.simantics.graph.db.TransferableGraphs;\r
+import org.simantics.graph.representation.Root;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class GraphTemplate implements Template {\r
+\r
+    String[] parameters;\r
+    TransferableGraph1 tg;\r
+    \r
+    public GraphTemplate(String[] parameters, TransferableGraph1 tg) {\r
+        this.parameters = parameters;\r
+        this.tg = tg;\r
+    }\r
+\r
+    public static GraphTemplate create(ReadGraph g, Resource r) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(g);\r
+        String[] parameters = g.getRelatedValue(r, l0.HasTemplateParameters, Bindings.STRING_ARRAY);\r
+        TransferableGraph1 tg = g.getRelatedValue(r, l0.HasTemplate, TransferableGraph1.BINDING);\r
+        return new GraphTemplate(parameters, tg);\r
+    }\r
+\r
+    @Override\r
+    public void apply(WriteGraph graph, final Map<String, Object> parameters)\r
+            throws DatabaseException {\r
+        TransferableGraphs.importGraph1(graph, tg, new IImportAdvisor() {\r
+            @Override\r
+            public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {\r
+                String name = root.name;\r
+                Object obj = parameters.get(name);\r
+                if(obj == null) {\r
+                    Resource r = graph.newResource();\r
+                    parameters.put(name, r);\r
+                    return r;\r
+                }\r
+                else {\r
+                    Resource r;\r
+                    if(obj instanceof Resource)\r
+                        r = (Resource)obj;\r
+                    else {\r
+                        r = graph.newResource();\r
+                        graph.claimValue(r, obj);\r
+                    }\r
+                    return r;\r
+                }\r
+            }\r
+\r
+            @Override\r
+            public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {\r
+               return null;\r
+            }\r
+        });\r
+    }\r
+    \r
+}\r