/
SearchService.java
381 lines (345 loc) · 13 KB
/
SearchService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* OpenKM, Open Document Management System (http://www.openkm.com)
* Copyright (c) 2006-2017 Paco Avila & Josep Llort
* <p>
* No bytes were intentionally harmed during the development of this application.
* <p>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.openkm.rest.endpoint;
import com.openkm.bean.ResultSet;
import com.openkm.core.MimeTypeConfig;
import com.openkm.dao.bean.QueryParams;
import com.openkm.module.ModuleManager;
import com.openkm.module.SearchModule;
import com.openkm.rest.GenericException;
import com.openkm.rest.util.*;
import com.openkm.util.ISO8601;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.*;
import java.util.Map.Entry;
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Api(description = "search-service", value = "search-service")
@Path("/search")
public class SearchService {
private static Logger log = LoggerFactory.getLogger(SearchService.class);
@GET
@Path("/findByContent")
public QueryResultList findByContent(@QueryParam("content") String content) throws GenericException {
try {
log.debug("findByContent({})", content);
SearchModule sm = ModuleManager.getSearchModule();
QueryResultList qrl = new QueryResultList();
qrl.getList().addAll(sm.findByContent(null, content));
log.debug("findByContent: {}", qrl);
return qrl;
} catch (Exception e) {
throw new GenericException(e);
}
}
@GET
@Path("/findByName")
public QueryResultList findByName(@QueryParam("name") String name) throws GenericException {
try {
log.debug("findByName({})", name);
SearchModule sm = ModuleManager.getSearchModule();
QueryResultList qrl = new QueryResultList();
qrl.getList().addAll(sm.findByName(null, name));
log.debug("findByName: {}", qrl);
return qrl;
} catch (Exception e) {
throw new GenericException(e);
}
}
@GET
@Path("/findByKeywords")
public QueryResultList findByKeywords(@QueryParam("keyword") List<String> keywords) throws GenericException {
try {
log.debug("findByKeywords({})", keywords);
SearchModule sm = ModuleManager.getSearchModule();
Set<String> set = new HashSet<>(keywords);
QueryResultList qrl = new QueryResultList();
qrl.getList().addAll(sm.findByKeywords(null, set));
log.debug("findByKeywords: {}", qrl);
return qrl;
} catch (Exception e) {
throw new GenericException(e);
}
}
@GET
@Path("/find")
// Default "domain" is "1" for documents.
public QueryResultList find(@QueryParam("content") String content, @QueryParam("name") String name,
@DefaultValue("1") @QueryParam("domain") int domain, @QueryParam("keyword") List<String> keywords,
@QueryParam("category") List<String> categories, @QueryParam("property") List<String> properties,
@QueryParam("author") String author, @QueryParam("mimeType") String mimeType,
@QueryParam("lastModifiedFrom") String lastModifiedFrom, @QueryParam("lastModifiedTo") String lastModifiedTo,
@QueryParam("mailSubject") String mailSubject, @QueryParam("mailFrom") String mailFrom, @QueryParam("mailTo") String mailTo,
@QueryParam("path") String path) throws GenericException {
try {
QueryParams params = copyToQueryParams(content, name, domain, keywords, categories, properties, author, mimeType,
lastModifiedFrom, lastModifiedTo, mailSubject, mailFrom, mailTo, path);
log.debug("find({})", params);
SearchModule sm = ModuleManager.getSearchModule();
QueryResultList qrl = new QueryResultList();
qrl.getList().addAll(sm.find(null, params));
log.debug("find: {}", qrl);
return qrl;
} catch (Exception e) {
throw new GenericException(e);
}
}
@GET
@Path("/findPaginated")
// Default "domain" is "1" for documents.
public ResultSet findPaginated(@DefaultValue("0") @QueryParam("offset") int offset, @DefaultValue("10") @QueryParam("limit") int limit,
@QueryParam("content") String content, @QueryParam("name") String name, @DefaultValue("1") @QueryParam("domain") int domain,
@QueryParam("keyword") List<String> keywords, @QueryParam("category") List<String> categories,
@QueryParam("property") List<String> properties, @QueryParam("author") String author, @QueryParam("mimeType") String mimeType,
@QueryParam("lastModifiedFrom") String lastModifiedFrom, @QueryParam("lastModifiedTo") String lastModifiedTo,
@QueryParam("mailSubject") String mailSubject, @QueryParam("mailFrom") String mailFrom, @QueryParam("mailTo") String mailTo,
@QueryParam("path") String path) throws GenericException {
try {
QueryParams params = copyToQueryParams(content, name, domain, keywords, categories, properties, author, mimeType,
lastModifiedFrom, lastModifiedTo, mailSubject, mailFrom, mailTo, path);
log.debug("findPaginated({})", params);
SearchModule sm = ModuleManager.getSearchModule();
ResultSet rs = sm.findPaginated(null, params, offset, limit);
log.debug("findPaginated: {}", rs);
return rs;
} catch (Exception e) {
throw new GenericException(e);
}
}
@GET
@Path("/findByQuery")
public QueryResultList findByQuery(@QueryParam("query") String query) throws GenericException {
try {
log.debug("findByQuery({})", query);
SearchModule sm = ModuleManager.getSearchModule();
QueryResultList qrl = new QueryResultList();
qrl.getList().addAll(sm.findByQuery(null, query));
log.debug("findByQuery: {}", qrl);
return qrl;
} catch (Exception e) {
throw new GenericException(e);
}
}
@GET
@Path("/findByQueryPaginated")
public ResultSet findByQueryPaginated(@DefaultValue("0") @QueryParam("offset") int offset,
@DefaultValue("10") @QueryParam("limit") int limit, @QueryParam("query") String query) throws GenericException {
try {
log.debug("findByQueryPaginated({})", query);
SearchModule sm = ModuleManager.getSearchModule();
ResultSet rs = sm.findByQueryPaginated(null, query, offset, limit);
log.debug("findByQueryPaginated: {}", rs);
return rs;
} catch (Exception e) {
throw new GenericException(e);
}
}
@GET
@Path("/findSimpleQueryPaginated")
// Default "domain" is "1" for documents.
public ResultSet findSimpleQueryPaginated(@DefaultValue("0") @QueryParam("offset") int offset,
@DefaultValue("10") @QueryParam("limit") int limit, @QueryParam("statement") String statement) throws GenericException {
try {
log.debug("findSimpleQueryPaginated({},{},{})", offset, limit, statement);
SearchModule sm = ModuleManager.getSearchModule();
ResultSet rs = sm.findSimpleQueryPaginated(null, statement, offset, limit);
log.debug("findSimpleQueryPaginated: {}", rs);
return rs;
} catch (Exception e) {
throw new GenericException(e);
}
}
@GET
@Path("/findSimpleQueryPaginated/{uuid}/{max}")
// Default "domain" is "1" for documents.
public ResultSet findMoreLikeThis(@PathParam("uuid") String uuid, @PathParam("max") int max) throws GenericException {
try {
log.debug("findMoreLikeThis({}, {})", uuid, max);
SearchModule sm = ModuleManager.getSearchModule();
ResultSet rs = sm.findMoreLikeThis(null, uuid, max);
log.debug("findSimpleQueryPaginated: {}", rs);
return rs;
} catch (Exception e) {
throw new GenericException(e);
}
}
@GET
@Path("/getKeywordMap")
public KeywordMapList getKeywordMap(@QueryParam("filter") String[] filter) throws GenericException {
try {
log.debug("getKeywordMap({})", (Object[]) filter);
SearchModule sm = ModuleManager.getSearchModule();
List<String> alFilter = Arrays.asList(filter);
Map<String, Integer> map = sm.getKeywordMap(null, alFilter);
KeywordMapList kml = new KeywordMapList();
// Marshall HashMap
for (Entry<String, Integer> entry : map.entrySet()) {
KeywordMap km = new KeywordMap();
km.setKeyword(entry.getKey());
km.setOccurs(entry.getValue());
kml.getList().add(km);
}
log.debug("getKeywordMap: {}", kml);
return kml;
} catch (Exception e) {
throw new GenericException(e);
}
}
@GET
@Path("/getCategorizedDocuments/{categoryId}")
public DocumentList getCategorizedDocuments(@PathParam("categoryId") String categoryId) throws GenericException {
try {
log.debug("getCategorizedDocuments({})", categoryId);
SearchModule sm = ModuleManager.getSearchModule();
DocumentList dl = new DocumentList();
dl.getList().addAll(sm.getCategorizedDocuments(null, categoryId));
log.debug("getCategorizedDocuments: {}", dl);
return dl;
} catch (Exception e) {
throw new GenericException(e);
}
}
@POST
@Path("/saveSearch")
@Produces(MimeTypeConfig.MIME_TEXT)
// The "params" parameter comes in the POST request body (encoded as XML or JSON).
public Long saveSearch(QueryParams params) throws GenericException {
try {
log.debug("saveSearch({})", params);
SearchModule sm = ModuleManager.getSearchModule();
long id = sm.saveSearch(null, params);
log.debug("saveSearch: {}", id);
return id;
} catch (Exception e) {
throw new GenericException(e);
}
}
@PUT
@Path("/updateSearch")
// The "params" parameter comes in the PUT request body (encoded as XML or JSON).
public void updateSearch(QueryParams params) throws GenericException {
try {
log.debug("updateSearch({})", params);
SearchModule sm = ModuleManager.getSearchModule();
sm.updateSearch(null, params);
log.debug("updateSearch: void");
} catch (Exception e) {
throw new GenericException(e);
}
}
@GET
@Path("/getSearch/{qpId}")
public QueryParams getSearch(@PathParam("qpId") int qpId) throws GenericException {
try {
log.debug("getSearch({})", qpId);
SearchModule sm = ModuleManager.getSearchModule();
QueryParams qp = sm.getSearch(null, qpId);
log.debug("getSearch: {}", qp);
return qp;
} catch (Exception e) {
throw new GenericException(e);
}
}
@GET
@Path("/getAllSearchs")
public QueryParamsList getAllSearchs() throws GenericException {
try {
log.debug("getAllSearchs()");
SearchModule sm = ModuleManager.getSearchModule();
QueryParamsList qpl = new QueryParamsList();
qpl.getList().addAll(sm.getAllSearchs(null));
log.debug("getAllSearchs: {}", qpl);
return qpl;
} catch (Exception e) {
throw new GenericException(e);
}
}
@DELETE
@Path("/deleteSearch/{qpId}")
public void deleteSearch(@PathParam("qpId") int qpId) throws GenericException {
try {
log.debug("deleteSearch({})", qpId);
SearchModule sm = ModuleManager.getSearchModule();
sm.deleteSearch(null, qpId);
log.debug("deleteSearch: void");
} catch (Exception e) {
throw new GenericException(e);
}
}
/**
* copyToQueryParams
*/
private QueryParams copyToQueryParams(String content, String name, int domain, List<String> keywords, List<String> categories,
List<String> properties, String author, String mimeType, String lastModifiedFrom, String lastModifiedTo, String mailSubject,
String mailFrom, String mailTo, String path) {
QueryParams params = new QueryParams();
Map<String, String> propMap = new HashMap<>();
for (String propVal : properties) {
String[] keyVal = propVal.split("=");
propMap.put(keyVal[0], keyVal[1]);
}
if (content != null && !content.isEmpty()) {
params.setContent(content);
}
if (name != null && !name.isEmpty()) {
params.setName(name);
}
params.setDomain(domain);
params.setKeywords(new HashSet<>(keywords));
params.setCategories(new HashSet<>(categories));
params.setProperties(propMap);
if (author != null && !author.isEmpty()) {
params.setAuthor(author);
}
if (mimeType != null && !mimeType.isEmpty()) {
params.setMimeType(mimeType);
}
if (path != null && !path.isEmpty()) {
params.setPath(path);
}
if (lastModifiedFrom != null && !lastModifiedFrom.isEmpty()) {
if (lastModifiedFrom.length() == 6) {
lastModifiedFrom += "000000";
}
params.setLastModifiedFrom(ISO8601.parseBasic(lastModifiedFrom));
}
if (lastModifiedTo != null && !lastModifiedTo.isEmpty()) {
if (lastModifiedTo.length() == 6) {
lastModifiedTo += "000000";
}
params.setLastModifiedTo(ISO8601.parseBasic(lastModifiedTo));
}
if (mailSubject != null && !mailSubject.isEmpty()) {
params.setMailSubject(mailSubject);
}
if (mailFrom != null && !mailFrom.isEmpty()) {
params.setMailFrom(mailFrom);
}
if (mailTo != null && !mailTo.isEmpty()) {
params.setMailTo(mailTo);
}
return params;
}
}