1 package org.simantics.document.ui.function;
3 import java.util.Collection;
4 import java.util.HashSet;
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.utils.Logger;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.genericrelation.Dependencies;
14 import org.simantics.document.DocumentResource;
15 import org.simantics.layer0.Layer0;
16 import org.simantics.operation.Layer0X;
17 import org.simantics.scl.runtime.function.FunctionImpl5;
18 import org.simantics.workbench.search.NameRow;
19 import org.simantics.workbench.search.NamedResource;
20 import org.simantics.workbench.search.SearchQuery;
21 import org.simantics.workbench.search.SearchResult;
22 import org.simantics.workbench.search.Searching;
24 public class SearchFunction extends FunctionImpl5<IProgressMonitor, ReadGraph, Resource, SearchQuery, Integer, SearchResult> {
27 public SearchResult apply(IProgressMonitor monitor, ReadGraph graph, Resource model, SearchQuery query, Integer maxResults) {
29 Collection<Map<String, Object>> results = Searching.performSearch(graph, Layer0X.getInstance(graph).Dependencies, model,
30 query.escaped(false).getQuery("Name","Types"), maxResults);
31 return generateSearchResults(graph, results);
32 } catch (DatabaseException e) {
33 Logger.defaultLogError(e);
38 public static final SearchResult generateSearchResults(ReadGraph graph,
39 Collection<Map<String, Object>> results) throws DatabaseException {
40 Layer0 l0 = Layer0.getInstance(graph);
41 DocumentResource doc = DocumentResource.getInstance(graph);
43 SearchResult result = new SearchResult(NameRow.columns);
44 Set<Resource> processed = new HashSet<Resource>();
46 for (Map<String, Object> r : results) {
47 Resource resource = (Resource) r.get(Dependencies.FIELD_RESOURCE);
49 // Ignore non-documents
50 if (!graph.isInstanceOf(resource, doc.Document))
54 // Prevent index corruption from producing duplicate results.
55 if (!processed.add(resource))
61 Resource parent = (Resource) r.get(Dependencies.FIELD_PARENT);
62 String name = (String) r.get(Dependencies.FIELD_NAME);
63 // TODO : hackfix: Sometimes index reports resource as its own parent.
64 if (resource.equals(parent)) {
65 Resource p = graph.getPossibleObject(resource, l0.PartOf);
70 NameRow rst = new NameRow();
71 rst.resource = NamedResource.of(graph, resource, name);
72 rst.parent = NamedResource.of(graph, parent);