]> gerrit.simantics Code Review - simantics/platform.git/blob
c4d64b68608533d5cf6429662017147ffd8ea9e5
[simantics/platform.git] /
1 package org.simantics.document.server.request;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.List;
7
8 import org.simantics.Simantics;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.common.request.UnaryRead;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.variable.ProxyVariables;
13 import org.simantics.db.layer0.variable.Variable;
14 import org.simantics.db.layer0.variable.Variables;
15 import org.simantics.document.server.ConsoleSCLReportingHandler;
16 import org.simantics.document.server.JSONObject;
17 import org.simantics.document.server.io.IConsole;
18 import org.simantics.document.server.io.IConsoleSupport;
19 import org.simantics.scl.runtime.SCLContext;
20 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
21
22 public class URIDocumentRequest extends UnaryRead<String, List<JSONObject>> {
23
24     public URIDocumentRequest(String var) {
25         super(var);
26         }
27
28         @Override
29         public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
30                 
31                 Variable var = Variables.getPossibleVariable(graph, parameter);
32                 if(var == null) {
33                         Variables.getPossibleVariable(graph, parameter);
34                         return Collections.emptyList();
35                 }
36                 
37         IConsoleSupport cs = Simantics.getSession().getService(IConsoleSupport.class);
38         Variable root = ProxyVariables.proxyVariableRoot(graph, var);
39         if(root == null) return Collections.emptyList(); 
40         Variable session = root.getParent(graph);
41         String guid = session.getName(graph);
42         IConsole console = cs.getConsole(guid);
43                 SCLReportingHandler printer = (console != null) ? new ConsoleSCLReportingHandler(console)
44                                 : (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
45         SCLContext sclContext = SCLContext.getCurrent();
46                 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
47
48                 try {
49                         
50                         ArrayList<JSONObject> result = new ArrayList<JSONObject>(graph.syncRequest(new DocumentRequest(var)));
51                         Collections.sort(result, new Comparator<JSONObject>() {
52         
53                                 @Override
54                                 public int compare(JSONObject o1, JSONObject o2) {
55                                         return o1.id.compareTo(o2.id);
56                                 }
57                                 
58                         });
59
60                         return result;
61                         
62                 } finally {
63                         sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
64                 }
65                                 
66         }
67         
68 }