f9c425248c891675dc153deca746133ee5f386f5
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / DocumentRequest.java
1 package org.simantics.document.server.request;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collections;\r
5 import java.util.Comparator;\r
6 import java.util.HashSet;\r
7 import java.util.List;\r
8 import java.util.Set;\r
9 \r
10 import org.simantics.db.ReadGraph;\r
11 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
12 import org.simantics.db.exception.DatabaseException;\r
13 import org.simantics.db.layer0.request.VariableRead;\r
14 import org.simantics.db.layer0.variable.Variable;\r
15 import org.simantics.document.server.JSONObject;\r
16 \r
17 public class DocumentRequest extends VariableRead<List<JSONObject>> {\r
18         \r
19         public static boolean PROFILE = false;\r
20 \r
21     public DocumentRequest(Variable var) {\r
22         super(var);\r
23         }\r
24 \r
25         @Override\r
26         public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {\r
27                 \r
28                 long s = System.nanoTime();\r
29                 \r
30         Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());\r
31         HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result\r
32         if(nodes.isEmpty()) {\r
33             return Collections.emptyList();\r
34         }\r
35         \r
36         \r
37         /*TreeMap<String, Variable> nodeMap = new TreeMap<String, Variable>();\r
38         \r
39         for (Variable node : nodes) {\r
40                 nodeMap.put(node.getURI(graph), node);\r
41         }\r
42         System.out.println("*************************************************************************");\r
43         for (Variable node : nodeMap.values()) {\r
44                 System.out.println("               " + node.getURI(graph));\r
45         }*/\r
46         \r
47         for(Variable node : nodes) {\r
48             rs.add(graph.syncRequest(new NodeRequest(node), TransientCacheAsyncListener.<JSONObject>instance()));\r
49         }\r
50 \r
51                 ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);\r
52                 Collections.sort(result, new Comparator<JSONObject>() {\r
53 \r
54                         @Override\r
55                         public int compare(JSONObject o1, JSONObject o2) {\r
56                                 return o1.id.compareTo(o2.id);\r
57                         }\r
58                         \r
59                 });\r
60         \r
61         if(PROFILE) {\r
62                 long dura = System.nanoTime()-s;\r
63                 System.err.println("DocumentRequest " + System.identityHashCode(this) + " " + variable.getURI(graph) + " in " + 1e-6*dura + "ms.");\r
64         }\r
65 \r
66                 return result;\r
67 \r
68         }\r
69 }