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