]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/function/SearchFunction.java
Index tokenized lowercase versions of name and types for UI searches
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / function / SearchFunction.java
1 package org.simantics.document.linking.function;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Set;
8
9 import org.eclipse.core.runtime.IProgressMonitor;
10 import org.simantics.databoard.Bindings;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.common.utils.Logger;
14 import org.simantics.db.common.utils.NameUtils;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.adapter.Instances;
17 import org.simantics.db.layer0.genericrelation.Dependencies;
18 import org.simantics.document.linking.ontology.DocumentLink;
19 import org.simantics.document.linking.utils.SourceLinkUtil;
20 import org.simantics.scl.runtime.function.FunctionImpl5;
21 import org.simantics.workbench.search.NamedResource;
22 import org.simantics.workbench.search.SearchQuery;
23 import org.simantics.workbench.search.SearchResult;
24 import org.simantics.workbench.search.SearchResultColumn;
25 import org.simantics.workbench.search.SearchResultRow;
26 import org.simantics.workbench.search.StringUtil;
27
28 public class SearchFunction extends FunctionImpl5<IProgressMonitor, ReadGraph, Resource, SearchQuery, Integer, SearchResult> {
29
30         public static List<SearchResultColumn> columns;
31         
32         static {
33                 columns = new ArrayList<SearchResultColumn>();
34                 columns.add(new SearchResultColumn("Name")); //$NON-NLS-1$
35                 columns.add(new SearchResultColumn("Comment")); //$NON-NLS-1$
36                 columns.add(new SearchResultColumn("Part Of")); //$NON-NLS-1$
37         }
38         
39         @Override
40         public SearchResult apply(IProgressMonitor monitor, ReadGraph graph, Resource model, SearchQuery query, Integer maxResults) {
41                  try {
42                                 DocumentLink sl = DocumentLink.getInstance(graph);
43                                 Instances instancesQuery = graph.adapt(sl.Source, Instances.class);
44                         Collection<Resource> found = instancesQuery.find(graph, model);
45                         return generateSearchResults(graph, found,query);
46                 } catch (DatabaseException e) {
47                     Logger.defaultLogError(e);
48                 }
49                 return null;
50         }
51         
52         
53         public static class NameComparator {
54                 private String name;
55                 int type = 0;
56                 
57                 public NameComparator(String query) {
58                 String parts[] = query.split(" OR "); //$NON-NLS-1$
59                 for (String s : parts) {
60                         if (s.startsWith("Name:")) { //$NON-NLS-1$
61                                  name = s.substring(5);
62                         }
63                 }
64                 if (name == null)
65                         return;
66                 name = name.trim();
67                 boolean freeStart = false;
68                 boolean freeEnd = false;
69                 if (name.endsWith("*")) { //$NON-NLS-1$
70                         name = name.substring(0,name.length()-1);
71                         freeEnd = true;
72                 }
73                 if (name.startsWith("*")) { //$NON-NLS-1$
74                         name = name.substring(1,name.length());
75                         freeStart = true;
76                 }
77                 if (freeStart && freeEnd)
78                         type = 1;
79                 else if (freeStart)
80                         type = 2;
81                 else if (freeEnd)
82                         type = 3;
83                 name = name.toLowerCase();
84                 }
85                 
86                 public boolean compare(String s) {
87                         switch (type) {
88                         case 0:
89                                 return s.toLowerCase().equals(name);
90                         case 1:
91                                 return s.toLowerCase().contains(name);
92                         case 2:
93                                 return s.toLowerCase().endsWith(name);
94                         case 3:
95                                 return s.toLowerCase().startsWith(name);
96                         }
97                         return false;
98                 }
99         }
100         
101     public static final SearchResult generateSearchResults(ReadGraph graph,
102                 Collection<Resource> results, SearchQuery query) throws DatabaseException {
103         DocumentLink sl = DocumentLink.getInstance(graph);
104
105         SearchResult result = new SearchResult(columns);
106         Set<Resource> processed = new HashSet<Resource>();
107
108         NameComparator c = new NameComparator(query.getQuery(Dependencies.FIELD_NAME_SEARCH));
109
110         for (Resource source : results) {
111                   // Prevent index corruption from producing duplicate results.
112             if (!processed.add(source))
113                 continue;
114             
115             Resource reference = SourceLinkUtil.getReferredDocument(graph, source);
116             if (!SourceLinkUtil.isValidReference(graph, reference)) {
117                 // TODO: searching removed references
118                 continue;
119             }
120
121             Resource parent = (Resource) graph.getSingleObject(source, sl.hasSource_Inverse);
122             String name = NameUtils.getSafeLabel(graph,reference);
123             if (name.length() == 0)
124                 name = NameUtils.getSafeName(graph, reference);
125        
126             if (!c.compare(name))
127                 continue;
128             
129             String parentName = NameUtils.getSafeLabel(graph, parent);
130             if (parentName.length() == 0)
131                 parentName = NameUtils.getSafeName(graph, parent);
132             
133             if (graph.isInstanceOf(source, sl.FunctionalSource)) {
134                 Resource relation = graph.getSingleObject(source, sl.consernsRelation);
135                 String relationName = NameUtils.getSafeLabel(graph, relation);
136                 if (relationName.length() == 0)
137                         relationName = NameUtils.getSafeName(graph, relation);
138                 parentName = parentName +"#"+relationName; //$NON-NLS-1$
139             }
140             
141             DocumentLinkRow rst = new DocumentLinkRow();
142             rst.resource = NamedResource.of(graph, reference, name);
143             rst.parent = NamedResource.of(graph, parent,parentName);
144             rst.comment = graph.getPossibleRelatedValue(source, sl.hasSourceComment,Bindings.STRING);
145
146 //          Collection<Resource> typeResources = graph.getTypes(reference);
147 //            Collection<Resource> principalTypeResources = graph.getPrincipalTypes(reference);
148 //            if (!typeResources.isEmpty()) {
149 //                rst.types = new ArrayList<NamedResource>(typeResources.size());
150 //                rst.principalTypes = new ArrayList<NamedResource>(principalTypeResources.size());
151 //                for (Resource t : typeResources) {
152 //                    String oname = NameUtils.getSafeLabel(graph, t);
153 //                    NamedResource nr = NamedResource.of(graph, t, oname);
154 //                    rst.types.add(nr);
155 //                    if (principalTypeResources.contains(t))
156 //                        rst.principalTypes.add(nr);
157 //                }
158 //            }
159
160             result.addRow(rst);
161         }
162         return result;
163     }
164     
165     private static class DocumentLinkRow implements SearchResultRow {
166         public NamedResource             resource;
167         public NamedResource             parent;
168         public String comment;
169         @Override
170         public String getContent(int column) {
171                 switch (column) {
172                 case 0:
173                         return "<a class=\"small\" href=\"resource:"+ resource.getResource() +"\"" + (resource.getUri() == null ? "" : " title=\""+resource.getUri()+"\">")+StringUtil.escape(resource.getName())+"</a>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
174                 case 1:
175                         if (comment != null)
176                                 return comment;
177                         return ""; //$NON-NLS-1$
178                 case 2:
179                         if (parent != null)
180                                 return "<a class=\"small\" href=\"resource:"+ parent.getResource() +"\"" + (parent.getUri() == null ? "" : " title=\""+parent.getUri()+"\">")+StringUtil.escape(parent.getName())+"</a>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
181                         return ""; //$NON-NLS-1$
182                 default:
183                         return ""; //$NON-NLS-1$
184                 }
185         }
186     }
187         
188 }
189