]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/resourceFile.ftl
(refs #7325) Fixed incorrect call of write in FilterCRWriter
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / resourceFiles / resourceFile.ftl
1 package ${packageName};
2
3 import org.simantics.db.RequestProcessor;
4 import org.simantics.db.Resource;
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.request.Read;
7 import org.simantics.db.Session;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.service.QueryControl;
10
11 public class ${className} {
12     
13     <#list resources as resource>
14     <#if resource.deprecated>@Deprecated </#if>public final Resource ${resource.javaName};
15     </#list>
16         
17     public static class URIs {
18         <#list resources as resource>
19         <#if resource.deprecated>@Deprecated </#if>public static final String ${resource.javaName} = "${resource.uri}";
20         </#list>
21     }
22     
23     public static Resource getResourceOrNull(ReadGraph graph, String uri) {
24         try {
25             return graph.getResource(uri);
26         } catch(DatabaseException e) {
27             System.err.println(e.getMessage());
28             return null;
29         }
30     }
31     
32     public ${className}(ReadGraph graph) {
33         <#list resources as resource>
34         ${resource.javaName} = getResourceOrNull(graph, URIs.${resource.javaName});
35         </#list>
36     }
37     
38     public static ${className} getInstance(ReadGraph graph) {
39         Session session = graph.getSession();
40         ${className} ret = session.peekService(${className}.class);
41         if(ret == null) {
42             QueryControl qc = graph.getService(QueryControl.class);
43             ret = new ${className}(qc.getIndependentGraph(graph));
44             session.registerService(${className}.class, ret);
45         }
46         return ret;
47     }
48     
49     public static ${className} getInstance(RequestProcessor session) throws DatabaseException {
50         ${className} ret = session.peekService(${className}.class);
51         if(ret == null) {
52             ret = session.syncRequest(new Read<${className}>() {
53                 public ${className} perform(ReadGraph graph) throws DatabaseException {
54                     QueryControl qc = graph.getService(QueryControl.class);
55                     return new ${className}(qc.getIndependentGraph(graph));
56                 }
57             });
58             session.registerService(${className}.class, ret);
59         }
60         return ret;
61     }
62     
63 }
64