]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/resourceFile.ftl
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / resourceFiles / resourceFile.ftl
diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/resourceFile.ftl b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/resourceFile.ftl
new file mode 100644 (file)
index 0000000..432d14f
--- /dev/null
@@ -0,0 +1,64 @@
+package ${packageName};\r
+\r
+import org.simantics.db.RequestProcessor;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.service.QueryControl;\r
+\r
+public class ${className} {\r
+    \r
+    <#list resources as resource>\r
+    <#if resource.deprecated>@Deprecated </#if>public final Resource ${resource.javaName};\r
+    </#list>\r
+        \r
+    public static class URIs {\r
+        <#list resources as resource>\r
+        <#if resource.deprecated>@Deprecated </#if>public static final String ${resource.javaName} = "${resource.uri}";\r
+        </#list>\r
+    }\r
+    \r
+    public static Resource getResourceOrNull(ReadGraph graph, String uri) {\r
+        try {\r
+            return graph.getResource(uri);\r
+        } catch(DatabaseException e) {\r
+            System.err.println(e.getMessage());\r
+            return null;\r
+        }\r
+    }\r
+    \r
+    public ${className}(ReadGraph graph) {\r
+        <#list resources as resource>\r
+        ${resource.javaName} = getResourceOrNull(graph, URIs.${resource.javaName});\r
+        </#list>\r
+    }\r
+    \r
+    public static ${className} getInstance(ReadGraph graph) {\r
+        Session session = graph.getSession();\r
+        ${className} ret = session.peekService(${className}.class);\r
+        if(ret == null) {\r
+            QueryControl qc = graph.getService(QueryControl.class);\r
+            ret = new ${className}(qc.getIndependentGraph(graph));\r
+            session.registerService(${className}.class, ret);\r
+        }\r
+        return ret;\r
+    }\r
+    \r
+    public static ${className} getInstance(RequestProcessor session) throws DatabaseException {\r
+        ${className} ret = session.peekService(${className}.class);\r
+        if(ret == null) {\r
+            ret = session.syncRequest(new Read<${className}>() {\r
+                public ${className} perform(ReadGraph graph) throws DatabaseException {\r
+                    QueryControl qc = graph.getService(QueryControl.class);\r
+                    return new ${className}(qc.getIndependentGraph(graph));\r
+                }\r
+            });\r
+            session.registerService(${className}.class, ret);\r
+        }\r
+        return ret;\r
+    }\r
+    \r
+}\r
+\r