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