]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/QueryIndexUtils.java
Introduce QueryIndexUtils in favor of bloated ModelingUtils
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / QueryIndexUtils.java
1 package org.simantics.db.layer0;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6
7 import org.simantics.datatypes.literal.GUID;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
11 import org.simantics.db.common.utils.NameUtils;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.adapter.Instances;
14 import org.simantics.db.layer0.adapter.impl.EntityInstances.QueryIndex;
15 import org.simantics.db.layer0.util.Layer0Utils;
16 import org.simantics.layer0.Layer0;
17 import org.simantics.scl.runtime.function.Function1;
18 import org.simantics.utils.datastructures.Triple;
19
20 public final class QueryIndexUtils {
21
22     public static List<Resource> searchByTypeShallow(ReadGraph graph, Resource model, Resource type) throws DatabaseException {
23         return graph.syncRequest(new QueryIndex(model, type, ""), TransientCacheListener.<List<Resource>>instance());
24     }
25
26     public static List<Resource> searchByType(ReadGraph graph, Resource model, Resource type) throws DatabaseException {
27         Instances query = graph.adapt(type, Instances.class);
28         return Layer0Utils.sortByCluster(graph, query.find(graph, model));
29     }
30
31     public static List<Resource> searchByGUID(ReadGraph graph, Resource indexRoot, GUID guid) throws DatabaseException {
32         return searchByGUID(graph, indexRoot, guid.indexString());
33     }
34     
35     public static List<Resource> searchByGUID(ReadGraph graph, Resource indexRoot, String indexString) throws DatabaseException {
36         return searchByQueryShallow(graph, indexRoot, "GUID:" + indexString);
37     }
38
39     public static List<Resource> searchByQueryShallow(ReadGraph graph, Resource model, String query) throws DatabaseException {
40         return graph.syncRequest(new QueryIndex(model, Layer0.getInstance(graph).Entity, query), TransientCacheListener.<List<Resource>>instance());
41     }
42
43     public static List<Resource> searchByQuery(ReadGraph graph, Resource model, String query) throws DatabaseException {
44         Instances instances = graph.adapt(Layer0.getInstance(graph).Entity, Instances.class);
45         Collection<Resource> queryResult = instances.find(graph, model, query);
46         return Layer0Utils.sortByCluster(graph, queryResult);
47     }
48
49     public static List<Resource> searchByTypeAndFilter(ReadGraph graph, Resource model, Resource type, Function1<Resource,Boolean> filter) throws DatabaseException {
50         Instances query = graph.adapt(type, Instances.class);
51         ArrayList<Resource> result =  new ArrayList<Resource>();
52         for(Resource r : query.find(graph, model)) {
53             if(filter.apply(r))
54                 result.add(r);
55         }
56         return result;
57     }
58
59     public static List<Triple<Resource, Resource, String>> getIndexEntries(ReadGraph graph, Resource model, String filter) throws DatabaseException {
60         Layer0 L0 = Layer0.getInstance(graph);
61         List<Resource> entries = searchByQuery(graph, model, filter);
62         List<Triple<Resource, Resource, String>> listOfTriples = new ArrayList<Triple<Resource,Resource,String>>();
63         for (Resource entry : entries) {
64             Resource type = graph.getPossibleObject(entry, L0.InstanceOf);
65             String name = NameUtils.getSafeName(graph, entry);
66             listOfTriples.add(new Triple<Resource, Resource, String>(entry, type, name));
67         }
68         return listOfTriples;
69     }
70
71     public static String listIndexEntries(ReadGraph graph, Resource model, String filter) throws DatabaseException {
72         List<Triple<Resource, Resource, String>> listOfTriples = getIndexEntries(graph, model, filter);
73         StringBuilder sb = new StringBuilder();
74         sb.append("== LISTING INDEX ENTRIES OF INDEX: " + NameUtils.getSafeName(graph, model) + ". AMOUNT OF ENTRIES: " + listOfTriples.size() + " ==\n");
75         for (Triple<Resource, Resource, String> entry : listOfTriples) {
76             String instanceOf = NameUtils.getSafeName(graph, entry.second);
77             sb.append("Name: " + entry.third + " instanceOf: " + instanceOf + " Resource: " + entry.first.toString() + "\n");
78         }
79         return sb.toString();
80     }
81
82     public static List<Resource> searchByTypeAndName(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
83         Instances query = graph.adapt(type, Instances.class);
84         ArrayList<Resource> result =  new ArrayList<Resource>();
85         for(Resource r : query.findByName(graph, model, name)) {
86             if(graph.isInstanceOf(r, type))
87                 result.add(r);
88         }
89         return result;
90     }
91
92     public static List<Resource> searchByTypeAndNameShallow(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
93         return graph.syncRequest(new QueryIndex(model, type, name), TransientCacheListener.<List<Resource>>instance());
94     }
95 }