1 package org.simantics.document.linking.report.templates.custom;
\r
3 import java.util.List;
\r
4 import java.util.Map;
\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
18 * SCL-script based customization
\r
20 * NOTE: this does not work, since at the moment the CommandSession is not able to attach itself to given read transaction.
\r
22 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
25 public class SCLCustomizableContent implements CustomizableContent {
\r
27 private CommandSession session;
\r
28 private String sclCode[];
\r
30 private String label;
\r
32 public SCLCustomizableContent(String label) {
\r
33 session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, null);
\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
43 public String getCustomizationDescription() {
\r
48 public String getContent(ReadGraph graph, Resource resource, Map<Object, Object> context)
\r
49 throws DatabaseException {
\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
58 public void print(String text) {
\r
59 responseBuilder.append(text).append('\n');
\r
63 public void printError(String error) {
\r
64 errorBuilder.append(error).append('\n');
\r
68 public void printCommand(String command) {
\r
71 if(errorBuilder.length() > 0)
\r
72 throw new DatabaseException("Error executing SCL \"" + code + "\" " + errorBuilder.toString().trim());
\r
74 return responseBuilder.toString().trim();
\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
84 public String[] getSclCode() {
\r
88 public void setSclCode(String[] sclCode) {
\r
89 this.sclCode = sclCode;
\r