1 package org.simantics.db.layer0;
3 import java.util.ArrayList;
4 import java.util.Collection;
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;
20 public final class QueryIndexUtils {
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());
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));
31 public static List<Resource> searchByGUID(ReadGraph graph, Resource indexRoot, GUID guid) throws DatabaseException {
32 return searchByGUID(graph, indexRoot, guid.indexString());
35 public static List<Resource> searchByGUID(ReadGraph graph, Resource indexRoot, String indexString) throws DatabaseException {
36 return searchByQueryShallow(graph, indexRoot, "GUID:" + indexString);
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());
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);
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)) {
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));
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");
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))
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());