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.genericrelation.IndexQueries;
16 import org.simantics.db.layer0.util.Layer0Utils;
17 import org.simantics.layer0.Layer0;
18 import org.simantics.scl.runtime.function.Function1;
19 import org.simantics.utils.datastructures.Triple;
21 public final class QueryIndexUtils {
23 public static List<Resource> searchByTypeShallow(ReadGraph graph, Resource model, Resource type) throws DatabaseException {
24 return graph.syncRequest(new QueryIndex(model, type, ""), TransientCacheListener.<List<Resource>>instance());
27 public static List<Resource> searchByType(ReadGraph graph, Resource model, Resource type) throws DatabaseException {
28 Instances query = graph.adapt(type, Instances.class);
29 return Layer0Utils.sortByCluster(graph, query.find(graph, model));
32 public static List<Resource> searchByGUID(ReadGraph graph, Resource indexRoot, GUID guid) throws DatabaseException {
33 return searchByGUID(graph, indexRoot, guid.indexString());
36 public static List<Resource> searchByGUID(ReadGraph graph, Resource indexRoot, String indexString) throws DatabaseException {
37 return searchByQueryShallow(graph, indexRoot, "GUID:" + IndexQueries.quoteTerm(indexString));
40 public static List<Resource> searchByQueryShallow(ReadGraph graph, Resource model, String query) throws DatabaseException {
41 return graph.syncRequest(new QueryIndex(model, Layer0.getInstance(graph).Entity, query), TransientCacheListener.<List<Resource>>instance());
44 public static List<Resource> searchByQuery(ReadGraph graph, Resource model, String query) throws DatabaseException {
45 Instances instances = graph.adapt(Layer0.getInstance(graph).Entity, Instances.class);
46 Collection<Resource> queryResult = instances.find(graph, model, query);
47 return Layer0Utils.sortByCluster(graph, queryResult);
50 public static List<Resource> searchByTypeAndFilter(ReadGraph graph, Resource model, Resource type, Function1<Resource,Boolean> filter) throws DatabaseException {
51 Instances query = graph.adapt(type, Instances.class);
52 ArrayList<Resource> result = new ArrayList<Resource>();
53 for(Resource r : query.find(graph, model)) {
60 public static List<Triple<Resource, Resource, String>> getIndexEntries(ReadGraph graph, Resource model, String filter) throws DatabaseException {
61 Layer0 L0 = Layer0.getInstance(graph);
62 List<Resource> entries = searchByQuery(graph, model, filter);
63 List<Triple<Resource, Resource, String>> listOfTriples = new ArrayList<Triple<Resource,Resource,String>>();
64 for (Resource entry : entries) {
65 Resource type = graph.getPossibleObject(entry, L0.InstanceOf);
66 String name = NameUtils.getSafeName(graph, entry);
67 listOfTriples.add(new Triple<Resource, Resource, String>(entry, type, name));
72 public static String listIndexEntries(ReadGraph graph, Resource model, String filter) throws DatabaseException {
73 List<Triple<Resource, Resource, String>> listOfTriples = getIndexEntries(graph, model, filter);
74 StringBuilder sb = new StringBuilder();
75 sb.append("== LISTING INDEX ENTRIES OF INDEX: " + NameUtils.getSafeName(graph, model) + ". AMOUNT OF ENTRIES: " + listOfTriples.size() + " ==\n");
76 for (Triple<Resource, Resource, String> entry : listOfTriples) {
77 String instanceOf = NameUtils.getSafeName(graph, entry.second);
78 sb.append("Name: " + entry.third + " instanceOf: " + instanceOf + " Resource: " + entry.first.toString() + "\n");
83 public static List<Resource> searchByTypeAndName(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
84 Instances query = graph.adapt(type, Instances.class);
85 ArrayList<Resource> result = new ArrayList<Resource>();
86 for(Resource r : query.findByName(graph, model, name)) {
87 if(graph.isInstanceOf(r, type))
93 public static List<Resource> searchByTypeAndNameShallow(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
94 return graph.syncRequest(new QueryIndex(model, type, IndexQueries.quoteTerm(name)), TransientCacheListener.<List<Resource>>instance());