]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/request/URIDocumentRequest.java
Do not override jersey in p2.site pom.xml as release/1.43.0 is jdk8
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / URIDocumentRequest.java
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                 IConsole console = null;
38                 IConsoleSupport cs = Simantics.getSession().peekService(IConsoleSupport.class);
39                 if (cs != null) {
40                         Variable root = ProxyVariables.proxyVariableRoot(graph, var);
41                         if (root == null)
42                                 return Collections.emptyList();
43                         Variable session = root.getParent(graph);
44                         String guid = session.getName(graph);
45                         console = cs.getConsole(guid);
46                 }
47
48                 SCLContext sclContext = SCLContext.getCurrent();
49                 SCLReportingHandler printer = (console != null) ? new ConsoleSCLReportingHandler(console)
50                                 : (SCLReportingHandler) sclContext.get(SCLReportingHandler.REPORTING_HANDLER);
51                 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
52
53                 try {
54                         List<JSONObject> result = new ArrayList<>(graph.syncRequest(new DocumentRequest(var)));
55                         Collections.sort(result, COMPARATOR);
56                         return result;
57                 } finally {
58                         sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
59                 }
60
61         }
62
63         private static final Comparator<JSONObject> COMPARATOR = (o1, o2) -> o1.id.compareTo(o2.id);
64
65 }