]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/templates/custom/SCLCustomizableContent.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / report / templates / custom / SCLCustomizableContent.java
1 package org.simantics.document.linking.report.templates.custom;\r
2 \r
3 import java.util.List;\r
4 import java.util.Map;\r
5 \r
6 import org.simantics.db.ReadGraph;\r
7 import org.simantics.db.Resource;\r
8 import org.simantics.db.exception.DatabaseException;\r
9 import org.simantics.document.linking.report.DocumentLine;\r
10 import org.simantics.scl.compiler.commands.CommandSession;\r
11 import org.simantics.scl.compiler.types.Types;\r
12 import org.simantics.scl.osgi.SCLOsgi;\r
13 import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler;\r
14 \r
15 \r
16 \r
17 /**\r
18  * SCL-script based customization\r
19  * \r
20  * NOTE: this does not work, since at the moment the CommandSession is not able to attach itself to given read transaction.\r
21  * \r
22  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
23  *\r
24  */\r
25 public class SCLCustomizableContent implements CustomizableContent {\r
26         \r
27         private CommandSession session;\r
28         private String sclCode[];\r
29         \r
30         private String label;\r
31         \r
32         public SCLCustomizableContent(String label) {\r
33                 session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, null);\r
34                 \r
35                 sclCode = new String[]{"import \"Simantics/Ontologies\"",\r
36                                        "nameOfResource r = match possibleRelatedValue r L0.HasName with\n"+\r
37                                "    Just name -> name\n"+\r
38                                "    Nothing -> \"no name\"",\r
39                                "nameOfResource res"};                          \r
40         }\r
41         \r
42         @Override\r
43         public String getCustomizationDescription() {\r
44                 return label;\r
45         }\r
46         \r
47         @Override\r
48         public String getContent(ReadGraph graph, Resource resource, Map<Object, Object> context)\r
49                         throws DatabaseException {\r
50                 \r
51                 session.setVariable("res", Types.con("Simantics/DB", "Resource"), resource);\r
52                 final StringBuilder responseBuilder = new StringBuilder();\r
53                 final StringBuilder errorBuilder = new StringBuilder();\r
54                 for (String code : sclCode) {\r
55                         session.execute(code, new AbstractSCLReportingHandler() {\r
56                 \r
57                 @Override\r
58                 public void print(String text) {\r
59                     responseBuilder.append(text).append('\n');\r
60                 }\r
61                 \r
62                 @Override\r
63                 public void printError(String error) {\r
64                     errorBuilder.append(error).append('\n');\r
65                 }\r
66                 \r
67                 @Override\r
68                 public void printCommand(String command) {\r
69                 }\r
70             });\r
71                         if(errorBuilder.length() > 0)\r
72                             throw new DatabaseException("Error executing SCL \"" + code + "\" " + errorBuilder.toString().trim());\r
73                 }\r
74                 return responseBuilder.toString().trim();\r
75         }\r
76         \r
77         @Override\r
78         public List<DocumentLine> getLines(ReadGraph graph, Resource resource, Map<Object, Object> context)\r
79                         throws DatabaseException {\r
80                 // TODO Auto-generated method stub\r
81                 return null;\r
82         }\r
83         \r
84         public String[] getSclCode() {\r
85                 return sclCode;\r
86         }\r
87         \r
88         public void setSclCode(String[] sclCode) {\r
89                 this.sclCode = sclCode;\r
90         }\r
91         \r
92 \r
93 }\r