1 package org.simantics.document.linking.report.templates.custom;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.document.linking.report.DocumentLine;
10 import org.simantics.scl.compiler.commands.CommandSession;
11 import org.simantics.scl.compiler.types.Types;
12 import org.simantics.scl.osgi.SCLOsgi;
13 import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler;
18 * SCL-script based customization
20 * NOTE: this does not work, since at the moment the CommandSession is not able to attach itself to given read transaction.
22 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
25 public class SCLCustomizableContent implements CustomizableContent {
27 private CommandSession session;
28 private String sclCode[];
32 public SCLCustomizableContent(String label) {
33 session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, null);
35 sclCode = new String[]{"import \"Simantics/Ontologies\"",
36 "nameOfResource r = match possibleRelatedValue r L0.HasName with\n"+
37 " Just name -> name\n"+
38 " Nothing -> \"no name\"",
39 "nameOfResource res"};
43 public String getCustomizationDescription() {
48 public String getContent(ReadGraph graph, Resource resource, Map<Object, Object> context)
49 throws DatabaseException {
51 session.setVariable("res", Types.con("Simantics/DB", "Resource"), resource);
52 final StringBuilder responseBuilder = new StringBuilder();
53 final StringBuilder errorBuilder = new StringBuilder();
54 for (String code : sclCode) {
55 session.execute(code, new AbstractSCLReportingHandler() {
58 public void print(String text) {
59 responseBuilder.append(text).append('\n');
63 public void printError(String error) {
64 errorBuilder.append(error).append('\n');
68 public void printCommand(String command) {
71 if(errorBuilder.length() > 0)
72 throw new DatabaseException("Error executing SCL \"" + code + "\" " + errorBuilder.toString().trim());
74 return responseBuilder.toString().trim();
78 public List<DocumentLine> getLines(ReadGraph graph, Resource resource, Map<Object, Object> context)
79 throws DatabaseException {
80 // TODO Auto-generated method stub
84 public String[] getSclCode() {
88 public void setSclCode(String[] sclCode) {
89 this.sclCode = sclCode;