]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/function/SearchFunction.java
Index tokenized lowercase versions of name and types for UI searches
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / function / SearchFunction.java
1 package org.simantics.document.ui.function;
2
3 import java.util.Collection;
4 import java.util.HashSet;
5 import java.util.Map;
6 import java.util.Set;
7
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;
23
24 public class SearchFunction extends FunctionImpl5<IProgressMonitor, ReadGraph, Resource, SearchQuery, Integer, SearchResult> {
25
26         @Override
27         public SearchResult apply(IProgressMonitor monitor, ReadGraph graph, Resource model, SearchQuery query, Integer maxResults) {
28                  try {
29                          Collection<Map<String, Object>> results = Searching.performSearch(graph, Layer0X.getInstance(graph).Dependencies, model,
30                             query.escapedWithForcedCase(false, false).getQuery(Dependencies.FIELD_NAME_SEARCH, Dependencies.FIELD_TYPES_SEARCH), maxResults);
31                         return generateSearchResults(graph, results);
32                 } catch (DatabaseException e) {
33                     Logger.defaultLogError(e);
34                 }
35                 return null;
36         }
37         
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);
42
43         SearchResult result = new SearchResult(NameRow.columns);
44         Set<Resource> processed = new HashSet<Resource>();
45
46         for (Map<String, Object> r : results) {
47             Resource resource = (Resource) r.get(Dependencies.FIELD_RESOURCE);
48
49             // Ignore non-documents
50             if (!graph.isInstanceOf(resource, doc.Document))
51                 continue;
52
53            
54             // Prevent index corruption from producing duplicate results.
55             if (!processed.add(resource))
56                 continue;
57
58           
59
60
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);
66                 if (p != null)
67                         parent = p;
68             }
69
70             NameRow rst = new NameRow();
71             rst.resource = NamedResource.of(graph, resource, name);
72             rst.parent = NamedResource.of(graph, parent);
73
74             result.addRow(rst);
75         }
76         return result;
77     }
78         
79 }
80