]> gerrit.simantics Code Review - simantics/platform.git/blob
09fb371948deb5e592b2b1dc5ed5a47f4c08a3fc
[simantics/platform.git] /
1 package org.simantics.document.linking.report.templates.custom;
2
3 import java.util.List;
4 import java.util.Map;
5
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;
14
15
16
17 /**
18  * SCL-script based customization
19  * 
20  * NOTE: this does not work, since at the moment the CommandSession is not able to attach itself to given read transaction.
21  * 
22  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
23  *
24  */
25 public class SCLCustomizableContent implements CustomizableContent {
26         
27         private CommandSession session;
28         private String sclCode[];
29         
30         private String label;
31         
32         public SCLCustomizableContent(String label) {
33                 session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, null);
34                 
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"};                          
40         }
41         
42         @Override
43         public String getCustomizationDescription() {
44                 return label;
45         }
46         
47         @Override
48         public String getContent(ReadGraph graph, Resource resource, Map<Object, Object> context)
49                         throws DatabaseException {
50                 
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() {
56                 
57                 @Override
58                 public void print(String text) {
59                     responseBuilder.append(text).append('\n');
60                 }
61                 
62                 @Override
63                 public void printError(String error) {
64                     errorBuilder.append(error).append('\n');
65                 }
66                 
67                 @Override
68                 public void printCommand(String command) {
69                 }
70             });
71                         if(errorBuilder.length() > 0)
72                             throw new DatabaseException("Error executing SCL \"" + code + "\" " + errorBuilder.toString().trim());
73                 }
74                 return responseBuilder.toString().trim();
75         }
76         
77         @Override
78         public List<DocumentLine> getLines(ReadGraph graph, Resource resource, Map<Object, Object> context)
79                         throws DatabaseException {
80                 // TODO Auto-generated method stub
81                 return null;
82         }
83         
84         public String[] getSclCode() {
85                 return sclCode;
86         }
87         
88         public void setSclCode(String[] sclCode) {
89                 this.sclCode = sclCode;
90         }
91         
92
93 }