]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/function/SearchFunction.java
0f33b28082dd8c5a42d3a49d08ed2e8929e1539c
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / function / SearchFunction.java
1 package org.simantics.document.ui.function;\r
2 \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
7 \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
23 \r
24 public class SearchFunction extends FunctionImpl5<IProgressMonitor, ReadGraph, Resource, SearchQuery, Integer, SearchResult> {\r
25 \r
26         @Override\r
27         public SearchResult apply(IProgressMonitor monitor, ReadGraph graph, Resource model, SearchQuery query, Integer maxResults) {\r
28                  try {\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
34                 }\r
35                 return null;\r
36         }\r
37         \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
42 \r
43         SearchResult result = new SearchResult(NameRow.columns);\r
44         Set<Resource> processed = new HashSet<Resource>();\r
45 \r
46         for (Map<String, Object> r : results) {\r
47             Resource resource = (Resource) r.get(Dependencies.FIELD_RESOURCE);\r
48 \r
49             // Ignore non-documents\r
50             if (!graph.isInstanceOf(resource, doc.Document))\r
51                 continue;\r
52 \r
53            \r
54             // Prevent index corruption from producing duplicate results.\r
55             if (!processed.add(resource))\r
56                 continue;\r
57 \r
58           \r
59 \r
60 \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
66                 if (p != null)\r
67                         parent = p;\r
68             }\r
69 \r
70             NameRow rst = new NameRow();\r
71             rst.resource = NamedResource.of(graph, resource, name);\r
72             rst.parent = NamedResource.of(graph, parent);\r
73 \r
74             result.addRow(rst);\r
75         }\r
76         return result;\r
77     }\r
78         \r
79 }\r
80 \r