+package org.simantics.document.linking.report.templates.custom;\r
+\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.document.linking.report.DocumentLine;\r
+import org.simantics.scl.compiler.commands.CommandSession;\r
+import org.simantics.scl.compiler.types.Types;\r
+import org.simantics.scl.osgi.SCLOsgi;\r
+import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler;\r
+\r
+\r
+\r
+/**\r
+ * SCL-script based customization\r
+ * \r
+ * NOTE: this does not work, since at the moment the CommandSession is not able to attach itself to given read transaction.\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public class SCLCustomizableContent implements CustomizableContent {\r
+ \r
+ private CommandSession session;\r
+ private String sclCode[];\r
+ \r
+ private String label;\r
+ \r
+ public SCLCustomizableContent(String label) {\r
+ session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, null);\r
+ \r
+ sclCode = new String[]{"import \"Simantics/Ontologies\"",\r
+ "nameOfResource r = match possibleRelatedValue r L0.HasName with\n"+\r
+ " Just name -> name\n"+\r
+ " Nothing -> \"no name\"",\r
+ "nameOfResource res"}; \r
+ }\r
+ \r
+ @Override\r
+ public String getCustomizationDescription() {\r
+ return label;\r
+ }\r
+ \r
+ @Override\r
+ public String getContent(ReadGraph graph, Resource resource, Map<Object, Object> context)\r
+ throws DatabaseException {\r
+ \r
+ session.setVariable("res", Types.con("Simantics/DB", "Resource"), resource);\r
+ final StringBuilder responseBuilder = new StringBuilder();\r
+ final StringBuilder errorBuilder = new StringBuilder();\r
+ for (String code : sclCode) {\r
+ session.execute(code, new AbstractSCLReportingHandler() {\r
+ \r
+ @Override\r
+ public void print(String text) {\r
+ responseBuilder.append(text).append('\n');\r
+ }\r
+ \r
+ @Override\r
+ public void printError(String error) {\r
+ errorBuilder.append(error).append('\n');\r
+ }\r
+ \r
+ @Override\r
+ public void printCommand(String command) {\r
+ }\r
+ });\r
+ if(errorBuilder.length() > 0)\r
+ throw new DatabaseException("Error executing SCL \"" + code + "\" " + errorBuilder.toString().trim());\r
+ }\r
+ return responseBuilder.toString().trim();\r
+ }\r
+ \r
+ @Override\r
+ public List<DocumentLine> getLines(ReadGraph graph, Resource resource, Map<Object, Object> context)\r
+ throws DatabaseException {\r
+ // TODO Auto-generated method stub\r
+ return null;\r
+ }\r
+ \r
+ public String[] getSclCode() {\r
+ return sclCode;\r
+ }\r
+ \r
+ public void setSclCode(String[] sclCode) {\r
+ this.sclCode = sclCode;\r
+ }\r
+ \r
+\r
+}\r