1
|
package eu.dnetlib.data.search.web.api;
|
2
|
|
3
|
import eu.dnetlib.api.data.SearchServiceException;
|
4
|
import eu.dnetlib.data.search.app.SearchServiceImpl;
|
5
|
import eu.dnetlib.data.search.transform.Transformer;
|
6
|
import eu.dnetlib.data.search.transform.config.SearchRegistry;
|
7
|
import eu.dnetlib.data.search.utils.cql.CQLQueryBuilder;
|
8
|
import eu.dnetlib.data.search.web.utils.RequestResponseHandler;
|
9
|
import eu.dnetlib.domain.data.SearchResult;
|
10
|
import org.apache.log4j.Logger;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
import org.springframework.stereotype.Component;
|
13
|
|
14
|
import javax.servlet.http.HttpServletRequest;
|
15
|
import javax.servlet.http.HttpServletResponse;
|
16
|
import javax.ws.rs.*;
|
17
|
import javax.ws.rs.core.Context;
|
18
|
import javax.ws.rs.core.MediaType;
|
19
|
import javax.ws.rs.core.Response;
|
20
|
import javax.ws.rs.core.StreamingOutput;
|
21
|
import java.io.IOException;
|
22
|
import java.io.OutputStream;
|
23
|
import java.util.List;
|
24
|
import java.util.Locale;
|
25
|
|
26
|
/**
|
27
|
* Created by kiatrop on 2/6/2016.
|
28
|
*/
|
29
|
|
30
|
@Component
|
31
|
@Path("/v2")
|
32
|
public class SearchApiService {
|
33
|
|
34
|
private static final Logger logger = Logger.getLogger(SearchApiService.class);
|
35
|
|
36
|
@Autowired
|
37
|
private SearchServiceImpl searchService = null;
|
38
|
|
39
|
@Autowired
|
40
|
private SearchRegistry searchRegistry = null;
|
41
|
|
42
|
@GET
|
43
|
@Path("/api/resources")
|
44
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
45
|
public Response fetchResources(@QueryParam("query") String query,
|
46
|
@DefaultValue("0") @QueryParam("page") final int offset,
|
47
|
@DefaultValue("10") @QueryParam("size") final int limit,
|
48
|
@QueryParam("refine") @DefaultValue("false") final boolean refine,
|
49
|
@QueryParam("fields") final List<String> fields,
|
50
|
@QueryParam("fq") final List<String> fieldQueries,
|
51
|
@QueryParam("format") final String format,
|
52
|
@QueryParam("type") final String type,
|
53
|
@Context final HttpServletRequest request) {
|
54
|
|
55
|
return getResponseByEntity(query, extractEntity(type), offset, limit, format, request, refine, fields, fieldQueries);
|
56
|
}
|
57
|
|
58
|
@GET
|
59
|
@Path("/api/publications")
|
60
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
61
|
public Response fetchPublications(@QueryParam("q") final String keywords,
|
62
|
@QueryParam("doi") final String doi,
|
63
|
@QueryParam("sortBy") final String sortBy,
|
64
|
@DefaultValue("0") @QueryParam("page") final int offset,
|
65
|
@DefaultValue("10") @QueryParam("size") final int limit,
|
66
|
@QueryParam("refine") @DefaultValue("false") final boolean refine,
|
67
|
@QueryParam("fields") final List<String> fields,
|
68
|
@QueryParam("fq") final List<String> fieldQueries,
|
69
|
@QueryParam("format") final String format,
|
70
|
@Context final HttpServletRequest request) {
|
71
|
|
72
|
logger.debug("Publications request received");
|
73
|
//TODO do not allow q and keywords both - SWITCH back if needed
|
74
|
//String simpleQuery = buildSimpleSearchQuery(RequestResponseHandler.Entity.PUBLICATION, keywords) ;
|
75
|
String simpleQuery = buildSearchRequest(RequestResponseHandler.Entity.PUBLICATION, keywords, doi,sortBy, fieldQueries);
|
76
|
return getResponseByEntity(simpleQuery, RequestResponseHandler.Entity.PUBLICATION, offset, limit, format, request, refine, fields, fieldQueries);
|
77
|
|
78
|
}
|
79
|
|
80
|
|
81
|
@GET
|
82
|
@Path("/api/publications/count")
|
83
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
84
|
public Response fetchPublicationsCount(@QueryParam("q") String query,
|
85
|
@QueryParam("format") final String format,
|
86
|
@QueryParam("fq") final List<String> fieldQueries,
|
87
|
@Context final HttpServletRequest request) {
|
88
|
String fullQuery = buildSearchRequest(RequestResponseHandler.Entity.PUBLICATION, query, fieldQueries);
|
89
|
return getCount(request, fullQuery, format, fieldQueries);
|
90
|
}
|
91
|
|
92
|
|
93
|
@GET
|
94
|
@Path("/api/publications/{publicationid}")
|
95
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
96
|
public Response fetchPublicationsById(@PathParam("publicationid") String publicationid,
|
97
|
@QueryParam("format") final String format,
|
98
|
@QueryParam("fq") final List<String> fieldQueries,
|
99
|
@Context final HttpServletRequest request) {
|
100
|
return getResponseByEntityId(request, RequestResponseHandler.Entity.PUBLICATION, publicationid, format, fieldQueries);
|
101
|
}
|
102
|
|
103
|
|
104
|
@GET
|
105
|
@Path("/api/datasets")
|
106
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
107
|
public Response fetchDatasets(@QueryParam("q") String keywords,
|
108
|
@QueryParam("doi") final String doi,
|
109
|
@QueryParam("sortBy") final String sortBy,
|
110
|
@DefaultValue("0") @QueryParam("page") int offset,
|
111
|
@DefaultValue("10") @QueryParam("size") int limit,
|
112
|
@QueryParam("refine") @DefaultValue("false") boolean refine,
|
113
|
@QueryParam("fields") final List<String> fields,
|
114
|
@QueryParam("fq") final List<String> fieldQueries,
|
115
|
@QueryParam("format") final String format,
|
116
|
@Context HttpServletRequest request) {
|
117
|
|
118
|
String simpleQuery = buildSearchRequest(RequestResponseHandler.Entity.DATASET, keywords, doi, sortBy, fieldQueries);
|
119
|
return getResponseByEntity(simpleQuery, RequestResponseHandler.Entity.DATASET, offset, limit, format, request, refine, fields, fieldQueries);
|
120
|
}
|
121
|
|
122
|
@GET
|
123
|
@Path("/api/datasets/count")
|
124
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
125
|
public Response fetchDatesetsCount(@QueryParam("q") String query,
|
126
|
@QueryParam("format") final String format,
|
127
|
@QueryParam("fq") final List<String> fieldQueries,
|
128
|
@Context final HttpServletRequest request) {
|
129
|
String fullQuery = buildSearchRequest(RequestResponseHandler.Entity.DATASET, query, fieldQueries);
|
130
|
return getCount(request, fullQuery, format, fieldQueries);
|
131
|
}
|
132
|
|
133
|
|
134
|
@GET
|
135
|
@Path("/api/datasets/{datasetid}")
|
136
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
137
|
public Response fetchDatasets(@PathParam("datasetid") String datasetid,
|
138
|
@QueryParam("format") final String format,
|
139
|
@QueryParam("fq") final List<String> fieldQueries,
|
140
|
@Context final HttpServletRequest request) {
|
141
|
return getResponseByEntityId(request, RequestResponseHandler.Entity.DATASET, datasetid, format, fieldQueries);
|
142
|
}
|
143
|
|
144
|
@GET
|
145
|
@Path("/api/software")
|
146
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
147
|
public Response fetchSoftware(@QueryParam("q") String keywords,
|
148
|
@QueryParam("doi") final String doi,
|
149
|
@QueryParam("sortBy") final String sortBy,
|
150
|
@DefaultValue("0") @QueryParam("page") int offset,
|
151
|
@DefaultValue("10") @QueryParam("size") int limit,
|
152
|
@QueryParam("refine") @DefaultValue("false") boolean refine,
|
153
|
@QueryParam("fields") final List<String> fields,
|
154
|
@QueryParam("fq") final List<String> fieldQueries,
|
155
|
@QueryParam("format") final String format,
|
156
|
@Context HttpServletRequest request) {
|
157
|
|
158
|
String simpleQuery = buildSearchRequest(RequestResponseHandler.Entity.SOFTWARE, keywords, doi, sortBy, fieldQueries);
|
159
|
return getResponseByEntity(simpleQuery, RequestResponseHandler.Entity.SOFTWARE, offset, limit, format, request, refine, fields, fieldQueries);
|
160
|
}
|
161
|
|
162
|
@GET
|
163
|
@Path("/api/software/count")
|
164
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
165
|
public Response fetchSoftwareCount(@QueryParam("q") String query,
|
166
|
@QueryParam("format") final String format,
|
167
|
@QueryParam("fq") final List<String> fieldQueries,
|
168
|
@Context final HttpServletRequest request) {
|
169
|
String fullQuery = buildSearchRequest(RequestResponseHandler.Entity.SOFTWARE, query, fieldQueries);
|
170
|
return getCount(request, fullQuery, format, fieldQueries);
|
171
|
}
|
172
|
|
173
|
@GET
|
174
|
@Path("/api/software/{softwareid}")
|
175
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
176
|
public Response fetchSoftware(@PathParam("softwareid") String datasetid,
|
177
|
@QueryParam("format") final String format,
|
178
|
@QueryParam("fq") final List<String> fieldQueries,
|
179
|
@Context final HttpServletRequest request) {
|
180
|
return getResponseByEntityId(request, RequestResponseHandler.Entity.SOFTWARE, datasetid, format, fieldQueries);
|
181
|
}
|
182
|
|
183
|
@GET
|
184
|
@Path("/api/projects")
|
185
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
186
|
public Response fetchProjects(@QueryParam("q") String keywords,
|
187
|
@DefaultValue("0") @QueryParam("page") int offset,
|
188
|
@DefaultValue("10") @QueryParam("size") int limit,
|
189
|
@QueryParam("refine") @DefaultValue("false") boolean refine,
|
190
|
@QueryParam("fields") final List<String> fields,
|
191
|
@QueryParam("fq") final List<String> fieldQueries,
|
192
|
@QueryParam("format") final String format,
|
193
|
@Context HttpServletRequest request) {
|
194
|
|
195
|
String simpleQuery = buildSearchRequest(RequestResponseHandler.Entity.PROJECT, keywords, fieldQueries);
|
196
|
return getResponseByEntity(simpleQuery, RequestResponseHandler.Entity.PROJECT, offset, limit, format, request, refine, fields, fieldQueries);
|
197
|
}
|
198
|
|
199
|
|
200
|
@GET
|
201
|
@Path("/api/projects/count")
|
202
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
203
|
public Response fetchProjectsCount(@QueryParam("q") String query,
|
204
|
@QueryParam("format") final String format,
|
205
|
@QueryParam("fq") final List<String> fieldQueries,
|
206
|
@Context final HttpServletRequest request) {
|
207
|
String fullQuery = buildSearchRequest(RequestResponseHandler.Entity.PROJECT, query, fieldQueries);
|
208
|
return getCount(request, fullQuery, format, fieldQueries);
|
209
|
}
|
210
|
|
211
|
@GET
|
212
|
@Path("/api/projects/{projectid}")
|
213
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
214
|
public Response fetchProjects(@PathParam("projectid") String projectid,
|
215
|
@QueryParam("format") final String format,
|
216
|
@QueryParam("fq") final List<String> fieldQueries,
|
217
|
@Context final HttpServletRequest request) {
|
218
|
return getResponseByEntityId(request, RequestResponseHandler.Entity.PROJECT, projectid, format, fieldQueries);
|
219
|
}
|
220
|
|
221
|
@GET
|
222
|
@Path("/api/projects/{projectId}/publications")
|
223
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
224
|
public Response fetchProjectPublications(@PathParam("projectId") String projectId,
|
225
|
@DefaultValue("0") @QueryParam("page") int offset,
|
226
|
@DefaultValue("10") @QueryParam("size") int limit,
|
227
|
@QueryParam("format") final String format,
|
228
|
@QueryParam("fq") final List<String> fieldQueries,
|
229
|
@Context HttpServletRequest request) {
|
230
|
|
231
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.PUBLICATION, "relprojectid", projectId, fieldQueries);
|
232
|
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PUBLICATION, offset, limit, format, request, false, null, fieldQueries);
|
233
|
}
|
234
|
|
235
|
@GET
|
236
|
@Path("/api/projects/{projectId}/publications/count")
|
237
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
238
|
public Response fetchProjectPublicationsCount(@PathParam("projectId") String projectId,
|
239
|
@QueryParam("format") final String format,
|
240
|
@QueryParam("fq") final List<String> fieldQueries,
|
241
|
@Context final HttpServletRequest request) {
|
242
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.PUBLICATION, "relprojectid", projectId, fieldQueries);
|
243
|
return getCount(request, fullQuery, format, fieldQueries);
|
244
|
}
|
245
|
|
246
|
@GET
|
247
|
@Path("/api/projects/{projectId}/datasets")
|
248
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
249
|
public Response fetchProjectDatasets(@PathParam("projectId") String projectId,
|
250
|
@DefaultValue("0") @QueryParam("page") int offset,
|
251
|
@DefaultValue("10") @QueryParam("size") int limit,
|
252
|
@QueryParam("format") final String format,
|
253
|
@QueryParam("fq") final List<String> fieldQueries,
|
254
|
@Context HttpServletRequest request) {
|
255
|
|
256
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.DATASET, "relprojectid", projectId, fieldQueries);
|
257
|
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.DATASET, offset, limit, format, request, false, null, fieldQueries);
|
258
|
}
|
259
|
|
260
|
|
261
|
@GET
|
262
|
@Path("/api/projects/{projectId}/datasets/count")
|
263
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
264
|
public Response fetchProjectDatasetsCount(@PathParam("projectId") String projectId,
|
265
|
@DefaultValue("0") @QueryParam("page") int offset,
|
266
|
@DefaultValue("10") @QueryParam("size") int limit,
|
267
|
@QueryParam("format") final String format,
|
268
|
@QueryParam("fq") final List<String> fieldQueries,
|
269
|
@Context HttpServletRequest request) {
|
270
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.DATASET, "relprojectid", projectId, fieldQueries);
|
271
|
return getCount(request, fullQuery, format, fieldQueries);
|
272
|
}
|
273
|
|
274
|
|
275
|
|
276
|
@GET
|
277
|
@Path("/api/projects/{projectId}/software/count")
|
278
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
279
|
public Response fetchProjectSoftwareCount(@PathParam("projectId") String projectId,
|
280
|
@DefaultValue("0") @QueryParam("page") int offset,
|
281
|
@DefaultValue("10") @QueryParam("size") int limit,
|
282
|
@QueryParam("format") final String format,
|
283
|
@QueryParam("fq") final List<String> fieldQueries,
|
284
|
@Context HttpServletRequest request) {
|
285
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.SOFTWARE, "relprojectid", projectId, fieldQueries);
|
286
|
return getCount(request, fullQuery, format, fieldQueries);
|
287
|
}
|
288
|
|
289
|
@GET
|
290
|
@Path("/api/projects/{projectId}/software")
|
291
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
292
|
public Response fetchProjectSoftware(@PathParam("projectId") String projectId,
|
293
|
@DefaultValue("0") @QueryParam("page") int offset,
|
294
|
@DefaultValue("10") @QueryParam("size") int limit,
|
295
|
@QueryParam("format") final String format,
|
296
|
@QueryParam("fq") final List<String> fieldQueries,
|
297
|
@Context HttpServletRequest request) {
|
298
|
|
299
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.SOFTWARE, "relprojectid", projectId, fieldQueries);
|
300
|
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.SOFTWARE, offset, limit, format, request, false, null, fieldQueries);
|
301
|
}
|
302
|
|
303
|
@GET
|
304
|
@Path("/api/datasources")
|
305
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
306
|
public Response fetchDatasources(@QueryParam("q") String keywords,
|
307
|
@DefaultValue("0") @QueryParam("page") final int offset,
|
308
|
@DefaultValue("10") @QueryParam("size") final int limit,
|
309
|
@QueryParam("refine") @DefaultValue("false") final boolean refine,
|
310
|
@QueryParam("fields") final List<String> fields,
|
311
|
@QueryParam("fq") final List<String> fieldQueries,
|
312
|
@QueryParam("format") final String format,
|
313
|
@Context final HttpServletRequest request) {
|
314
|
|
315
|
String simpleQuery = buildSearchRequest(RequestResponseHandler.Entity.DATASOURCE, keywords, fieldQueries);
|
316
|
return getResponseByEntity(simpleQuery, RequestResponseHandler.Entity.DATASOURCE, offset, limit, format, request, refine, fields, fieldQueries);
|
317
|
}
|
318
|
|
319
|
@GET
|
320
|
@Path("/api/datasources/count")
|
321
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
322
|
public Response fetchDatasourcesCount(@QueryParam("q") String query,
|
323
|
@QueryParam("format") final String format,
|
324
|
@QueryParam("fq") final List<String> fieldQueries,
|
325
|
@Context final HttpServletRequest request) {
|
326
|
String fullQuery = buildSearchRequest(RequestResponseHandler.Entity.DATASOURCE, query, fieldQueries);
|
327
|
return getCount(request, fullQuery, format, fieldQueries);
|
328
|
}
|
329
|
|
330
|
@GET
|
331
|
@Path("/api/datasources/{datasourceid}")
|
332
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
333
|
public Response fetchDatasources(@PathParam("datasourceid") String datasourceid,
|
334
|
@QueryParam("format") final String format,
|
335
|
@QueryParam("fq") final List<String> fieldQueries,
|
336
|
@Context final HttpServletRequest request) {
|
337
|
return getResponseByEntityId(request, RequestResponseHandler.Entity.DATASOURCE, datasourceid, format, fieldQueries);
|
338
|
}
|
339
|
|
340
|
|
341
|
@GET
|
342
|
@Path("/api/organizations")
|
343
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
344
|
public Response fetchOrganisations(@QueryParam("q") String keywords,
|
345
|
@DefaultValue("0") @QueryParam("page") final int offset,
|
346
|
@DefaultValue("10") @QueryParam("size") final int limit,
|
347
|
@QueryParam("refine") @DefaultValue("false") final boolean refine,
|
348
|
@QueryParam("fields") final List<String> fields,
|
349
|
@QueryParam("fq") final List<String> fieldQueries,
|
350
|
@QueryParam("format") final String format,
|
351
|
@Context final HttpServletRequest request) {
|
352
|
|
353
|
String simpleQuery = buildSearchRequest(RequestResponseHandler.Entity.ORGANIZATION, keywords, fieldQueries);
|
354
|
return getResponseByEntity(simpleQuery, RequestResponseHandler.Entity.ORGANIZATION, offset, limit, format, request, refine, fields, fieldQueries);
|
355
|
}
|
356
|
|
357
|
|
358
|
@GET
|
359
|
@Path("/api/organizations/count")
|
360
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
361
|
public Response fetchOrganizationsCount(@QueryParam("q") String query,
|
362
|
@QueryParam("format") final String format,
|
363
|
@QueryParam("fq") final List<String> fieldQueries,
|
364
|
@Context final HttpServletRequest request) {
|
365
|
String fullQuery = buildSearchRequest(RequestResponseHandler.Entity.ORGANIZATION, query, fieldQueries);
|
366
|
return getCount(request, fullQuery, format, fieldQueries);
|
367
|
}
|
368
|
|
369
|
@GET
|
370
|
@Path("/api/organizations/{organizationid}")
|
371
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
372
|
public Response fetchOrganizations(@PathParam("organizationid") String organizationid,
|
373
|
@QueryParam("format") final String format,
|
374
|
@QueryParam("fq") final List<String> fieldQueries,
|
375
|
@Context final HttpServletRequest request) {
|
376
|
return getResponseByEntityId(request, RequestResponseHandler.Entity.ORGANIZATION, organizationid, format, fieldQueries);
|
377
|
}
|
378
|
|
379
|
|
380
|
@GET
|
381
|
@Path("/api/organizations/{organizationid}/datasources")
|
382
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
383
|
public Response fetchOrganizationDatasources(@PathParam("organizationid") String organizationid,
|
384
|
@DefaultValue("0") @QueryParam("page") int offset,
|
385
|
@DefaultValue("10") @QueryParam("size") int limit,
|
386
|
@QueryParam("format") final String format,
|
387
|
@QueryParam("fq") final List<String> fieldQueries,
|
388
|
@Context HttpServletRequest request) {
|
389
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.DATASOURCE, "relorganizationid", organizationid, fieldQueries);
|
390
|
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.DATASOURCE, offset, limit, format, request, false, null, fieldQueries);
|
391
|
}
|
392
|
|
393
|
@GET
|
394
|
@Path("/api/organizations/{organizationid}/datasources/count")
|
395
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
396
|
public Response fetchOrganizationDatasources(@PathParam("organizationid") String organizationid,
|
397
|
@QueryParam("format") final String format,
|
398
|
@QueryParam("fq") final List<String> fieldQueries,
|
399
|
@Context final HttpServletRequest request) {
|
400
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.DATASOURCE, "relorganizationid", organizationid, fieldQueries);
|
401
|
return getCount(request, fullQuery, format, fieldQueries);
|
402
|
}
|
403
|
|
404
|
|
405
|
@GET
|
406
|
@Path("/api/organizations/{organizationid}/publications")
|
407
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
408
|
public Response fetchOrganizationPublications(@PathParam("organizationid") String organizationid,
|
409
|
@DefaultValue("0") @QueryParam("page") int offset,
|
410
|
@DefaultValue("10") @QueryParam("size") int limit,
|
411
|
@QueryParam("format") final String format,
|
412
|
@QueryParam("fq") final List<String> fieldQueries,
|
413
|
@Context HttpServletRequest request) {
|
414
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.PUBLICATION, "relorganizationid", organizationid, fieldQueries);
|
415
|
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PUBLICATION, offset, limit, format, request, false, null, fieldQueries);
|
416
|
}
|
417
|
|
418
|
@GET
|
419
|
@Path("/api/organizations/{organizationid}/publications/count")
|
420
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
421
|
public Response fetchOrganizationPublicationsCount(@PathParam("organizationid") String organizationid,
|
422
|
@DefaultValue("0") @QueryParam("page") int offset,
|
423
|
@DefaultValue("10") @QueryParam("size") int limit,
|
424
|
@QueryParam("format") final String format,
|
425
|
@QueryParam("fq") final List<String> fieldQueries,
|
426
|
@Context HttpServletRequest request) {
|
427
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.PUBLICATION, "relorganizationid", organizationid, fieldQueries);
|
428
|
return getCount(request, fullQuery, format, fieldQueries);
|
429
|
}
|
430
|
|
431
|
@GET
|
432
|
@Path("/api/organizations/{organizationid}/datasets")
|
433
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
434
|
public Response fetchOrganizationDatsets(@PathParam("organizationid") String organizationid,
|
435
|
@DefaultValue("0") @QueryParam("page") int offset,
|
436
|
@DefaultValue("10") @QueryParam("size") int limit,
|
437
|
@QueryParam("format") final String format,
|
438
|
@QueryParam("fq") final List<String> fieldQueries,
|
439
|
@Context HttpServletRequest request) {
|
440
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.DATASET, "relorganizationid", organizationid, fieldQueries);
|
441
|
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.DATASET, offset, limit, format, request, false, null, fieldQueries);
|
442
|
}
|
443
|
|
444
|
@GET
|
445
|
@Path("/api/organizations/{organizationid}/datasets/count")
|
446
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
447
|
public Response fetchOrganizationDatsetsCount(@PathParam("organizationid") String organizationid,
|
448
|
@DefaultValue("0") @QueryParam("page") int offset,
|
449
|
@DefaultValue("10") @QueryParam("size") int limit,
|
450
|
@QueryParam("format") final String format,
|
451
|
@QueryParam("fq") final List<String> fieldQueries,
|
452
|
@Context HttpServletRequest request) {
|
453
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.DATASET, "relorganizationid", organizationid, fieldQueries);
|
454
|
return getCount(request, fullQuery, format, fieldQueries);
|
455
|
}
|
456
|
|
457
|
@GET
|
458
|
@Path("/api/organizations/{organizationid}/projects")
|
459
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
460
|
public Response fetchOrganizationProjects(@PathParam("organizationid") String organizationid,
|
461
|
@DefaultValue("0") @QueryParam("page") int offset,
|
462
|
@DefaultValue("10") @QueryParam("size") int limit,
|
463
|
@QueryParam("format") final String format,
|
464
|
@QueryParam("fq") final List<String> fieldQueries,
|
465
|
@Context HttpServletRequest request) {
|
466
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.PROJECT, "relorganizationid", organizationid, fieldQueries);
|
467
|
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PROJECT, offset, limit, format, request, false, null, fieldQueries);
|
468
|
}
|
469
|
|
470
|
@GET
|
471
|
@Path("/api/organizations/{organizationid}/projects/count")
|
472
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
473
|
public Response fetchOrganizationProjectsCount(@PathParam("organizationid") String organizationid,
|
474
|
@QueryParam("format") final String format,
|
475
|
@QueryParam("fq") final List<String> fieldQueries,
|
476
|
@Context final HttpServletRequest request) {
|
477
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.PROJECT, "relorganizationid", organizationid, fieldQueries);
|
478
|
return getCount(request, fullQuery,format, fieldQueries);
|
479
|
}
|
480
|
|
481
|
|
482
|
|
483
|
@GET
|
484
|
@Path("/api/people")
|
485
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
486
|
public Response fetchPeople(@QueryParam("q") String keywords,
|
487
|
@DefaultValue("0") @QueryParam("page") final int offset,
|
488
|
@DefaultValue("10") @QueryParam("size") final int limit,
|
489
|
@QueryParam("refine") @DefaultValue("false") final boolean refine,
|
490
|
@QueryParam("fields") final List<String> fields,
|
491
|
@QueryParam("fq") final List<String> fieldQueries,
|
492
|
@QueryParam("format") final String format,
|
493
|
@Context final HttpServletRequest request) {
|
494
|
|
495
|
String simpleQuery = buildSearchRequest(RequestResponseHandler.Entity.PERSON, keywords, fieldQueries);
|
496
|
return getResponseByEntity(simpleQuery, RequestResponseHandler.Entity.PERSON, offset, limit, format, request, refine, fields, fieldQueries);
|
497
|
}
|
498
|
|
499
|
|
500
|
@GET
|
501
|
@Path("/api/people/count")
|
502
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
503
|
public Response fetchPeopleCount(@QueryParam("q") String query,
|
504
|
@QueryParam("format") final String format,
|
505
|
@QueryParam("fq") final List<String> fieldQueries,
|
506
|
@Context final HttpServletRequest request) {
|
507
|
String fullQuery = buildSearchRequest(RequestResponseHandler.Entity.PERSON, query, fieldQueries);
|
508
|
return getCount(request, fullQuery, format, fieldQueries);
|
509
|
}
|
510
|
|
511
|
@GET
|
512
|
@Path("/api/people/{personid}")
|
513
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
514
|
public Response fetchPeople(@PathParam("personid") String personid,
|
515
|
@QueryParam("format") final String format,
|
516
|
@QueryParam("fq") final List<String> fieldQueries,
|
517
|
@Context final HttpServletRequest request) {
|
518
|
return getResponseByEntityId(request, RequestResponseHandler.Entity.PERSON, personid, format, fieldQueries);
|
519
|
}
|
520
|
|
521
|
|
522
|
@GET
|
523
|
@Path("/api/people/{personid}/publications")
|
524
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
525
|
public Response fetchPeoplePublications(@PathParam("personid") String personid,
|
526
|
@DefaultValue("0") @QueryParam("page") final int offset,
|
527
|
@DefaultValue("10") @QueryParam("size") final int limit,
|
528
|
@QueryParam("format") final String format,
|
529
|
@QueryParam("fq") final List<String> fieldQueries,
|
530
|
@Context final HttpServletRequest request) {
|
531
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.PUBLICATION, "relpersonid", personid, fieldQueries);
|
532
|
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PUBLICATION, offset, limit, format, request, false, null, fieldQueries);
|
533
|
}
|
534
|
|
535
|
@GET
|
536
|
@Path("/api/people/{personid}/publications/count")
|
537
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
538
|
public Response fetchPeoplePublicationsCount(@PathParam("personid") String personid,
|
539
|
@QueryParam("format") final String format,
|
540
|
@QueryParam("fq") final List<String> fieldQueries,
|
541
|
@Context final HttpServletRequest request) {
|
542
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.PUBLICATION, "relpersonid", personid, fieldQueries);
|
543
|
return getCount(request, fullQuery, format, fieldQueries);
|
544
|
}
|
545
|
|
546
|
|
547
|
@GET
|
548
|
@Path("/api/people/{personid}/datasets")
|
549
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
550
|
public Response fetchPeopleDatasets(@PathParam("personid") String personid,
|
551
|
@DefaultValue("0") @QueryParam("page") final int offset,
|
552
|
@DefaultValue("10") @QueryParam("size") final int limit,
|
553
|
@QueryParam("format") final String format,
|
554
|
@QueryParam("fq") final List<String> fieldQueries,
|
555
|
@Context final HttpServletRequest request) {
|
556
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.DATASET, "relpersonid", personid, fieldQueries);
|
557
|
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.DATASET, offset, limit, format, request, false, null, fieldQueries);
|
558
|
}
|
559
|
|
560
|
@GET
|
561
|
@Path("/api/people/{personid}/datasets/count")
|
562
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
563
|
public Response fetchPeopleDatasetsCount(@PathParam("personid") String personid,
|
564
|
@QueryParam("format") final String format,
|
565
|
@QueryParam("fq") final List<String> fieldQueries,
|
566
|
@Context final HttpServletRequest request) {
|
567
|
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.DATASET, "relpersonid", personid, fieldQueries);
|
568
|
return getCount(request, fullQuery, format, fieldQueries);
|
569
|
}
|
570
|
|
571
|
@GET
|
572
|
@Path("/reports")
|
573
|
@Produces(MediaType.TEXT_PLAIN)
|
574
|
public Response fetchReport(@QueryParam("query") String query,
|
575
|
@QueryParam("refine") @DefaultValue("false") boolean refine,
|
576
|
@QueryParam("fields") final List<String> fields,
|
577
|
@QueryParam("fq") final List<String> fieldQueries,
|
578
|
@QueryParam("type") final String type,
|
579
|
@QueryParam("format") final String format,
|
580
|
@Context final HttpServletResponse response) {
|
581
|
|
582
|
logger.debug("Reports request received");
|
583
|
|
584
|
if (type == null || type.isEmpty()) {
|
585
|
return Response.status(Response.Status.BAD_REQUEST).entity(APIResponseFormatter.
|
586
|
compose500Message(MediaType.APPLICATION_JSON, "Fail to fetch report.", "The 'type' parameter is missing.")).build();
|
587
|
}
|
588
|
|
589
|
long startTime = System.nanoTime();
|
590
|
final RequestResponseHandler.Entity entity = extractEntity(type);
|
591
|
final Transformer transformer = extractTransformer(format, extractEntity(type));
|
592
|
final boolean special = isSpecialFormat(format);
|
593
|
String responseType = extractResponseFormat(format);
|
594
|
|
595
|
final String fullQuery = buildSearchRequest(entity,"", fieldQueries);
|
596
|
|
597
|
try {
|
598
|
SearchResult sr = searchService.newSearch(fullQuery, Locale.getDefault().toString(), fields, fieldQueries,0, 0, format, transformer,null,false);
|
599
|
logger.debug("Total number of results " + sr.getTotal());
|
600
|
|
601
|
if (sr.getTotal() > 10000) {
|
602
|
return Response.status(Response.Status.BAD_REQUEST).entity(APIResponseFormatter.
|
603
|
compose500Message(MediaType.APPLICATION_JSON, "Fail to fetch report.", "You have exceeded the number of allowed returned results")).build();
|
604
|
}
|
605
|
|
606
|
} catch (SearchServiceException sse) {
|
607
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(APIResponseFormatter.
|
608
|
compose500Message(MediaType.APPLICATION_JSON, "Fail to fetch report.", sse.getMessage())).build();
|
609
|
}
|
610
|
|
611
|
StreamingOutput stream = new StreamingOutput() {
|
612
|
@Override
|
613
|
public void write(OutputStream os) throws IOException, WebApplicationException
|
614
|
{
|
615
|
if (hasTitle(format) && !format.equals("html")) {
|
616
|
os.write(CSVResponseFormat.appendTitle(entity, special).getBytes());
|
617
|
}
|
618
|
try {
|
619
|
searchService.cursorSearch(fullQuery, fields, fieldQueries, format, transformer, os);
|
620
|
|
621
|
} catch (SearchServiceException sse) {
|
622
|
os.write(("Fail to return report. " + sse.getMessage()).getBytes());
|
623
|
logger.warn("Fail to return report. ", sse);
|
624
|
}
|
625
|
|
626
|
//os.close();
|
627
|
}
|
628
|
|
629
|
};
|
630
|
|
631
|
long estimatedTime = System.nanoTime() - startTime;
|
632
|
logger.debug("Reports search time " + estimatedTime/1000000 + " milliseconds");
|
633
|
return Response.ok().entity(stream).type(responseType).build();
|
634
|
}
|
635
|
|
636
|
private String extractResponseFormat(String format) {
|
637
|
if (format != null && !format.isEmpty()) {
|
638
|
if (format.equalsIgnoreCase("json")) {
|
639
|
return MediaType.APPLICATION_JSON;
|
640
|
|
641
|
} else if (format.contains("csv")) {
|
642
|
return new MediaType("text", "csv").toString();
|
643
|
|
644
|
} else if (format.equalsIgnoreCase("tsv")) {
|
645
|
return new MediaType("text", "tsv").toString();
|
646
|
|
647
|
} else if (format.equalsIgnoreCase("html")) {
|
648
|
return MediaType.TEXT_HTML;
|
649
|
}
|
650
|
}
|
651
|
|
652
|
return MediaType.APPLICATION_XML;
|
653
|
}
|
654
|
|
655
|
private boolean isSpecialFormat(String format) {
|
656
|
if (format != null && format.toLowerCase().contains("special")) {
|
657
|
return true;
|
658
|
}
|
659
|
return false;
|
660
|
}
|
661
|
|
662
|
private boolean hasTitle(String format) {
|
663
|
if (format != null && format.toLowerCase().contains("notitle")) {
|
664
|
return false;
|
665
|
}
|
666
|
return true;
|
667
|
}
|
668
|
|
669
|
//TODO differentiate for html + add special controls for format support
|
670
|
private Transformer extractTransformer(String format, RequestResponseHandler.Entity entity) {
|
671
|
if (searchRegistry == null) { return null;}
|
672
|
|
673
|
if (format != null && (format.toLowerCase().contains("csv") || format.equalsIgnoreCase("html"))) {
|
674
|
logger.debug("Extracting transformer...." + format + "_" + entity);
|
675
|
return searchRegistry.getTransformer(format.replaceAll("-notitle","") + "_" + entity, Locale.getDefault());
|
676
|
|
677
|
}
|
678
|
|
679
|
return null;
|
680
|
}
|
681
|
|
682
|
//TODO old way of creating query see if needed.
|
683
|
/*private String buildSimpleSearchQuery(RequestResponseHandler.Entity entity, String keywords) {
|
684
|
StringBuilder queryBuilder = new StringBuilder();
|
685
|
enhanceQueryWithEntityType(queryBuilder, entity);
|
686
|
builtQueryKeywords(queryBuilder, cleanKeywords(keywords));
|
687
|
return queryBuilder.toString();
|
688
|
}*/
|
689
|
|
690
|
private String cleanKeywords(String keywords) {
|
691
|
if (keywords != null) {
|
692
|
return keywords.replaceAll("[\\\\/:*?<>|()]", "");
|
693
|
}
|
694
|
return keywords;
|
695
|
}
|
696
|
|
697
|
private String cleanId(String entityId) {
|
698
|
return entityId.replaceAll("\"", "");
|
699
|
}
|
700
|
|
701
|
private String buildSearchRequest(RequestResponseHandler.Entity entity, String keywords, String doi, String sortBy, List<String> fieldQueries) {
|
702
|
enhanceFieldQueryWithEntityType(entity, fieldQueries);
|
703
|
|
704
|
StringBuilder queryBuilder = new StringBuilder();
|
705
|
if (doi != null && !doi.trim().isEmpty()) {
|
706
|
queryBuilder.append("(pidclassid exact \"doi\" and pid exact " + doi + ")");
|
707
|
}
|
708
|
|
709
|
builtQueryKeywords(queryBuilder, keywords);
|
710
|
enhanceWithSortParameter(queryBuilder, sortBy);
|
711
|
|
712
|
return queryBuilder.toString();
|
713
|
}
|
714
|
|
715
|
private String buildSearchRequest(RequestResponseHandler.Entity entity, String keywords, List<String> fieldQueries) {
|
716
|
enhanceFieldQueryWithEntityType(entity, fieldQueries);
|
717
|
|
718
|
StringBuilder queryBuilder = new StringBuilder();
|
719
|
builtQueryKeywords(queryBuilder, keywords);
|
720
|
|
721
|
return queryBuilder.toString();
|
722
|
}
|
723
|
|
724
|
private void enhanceFieldQueryWithEntityType(RequestResponseHandler.Entity entity, List<String> fieldQueries) {
|
725
|
for (String fieldQuery: entity.getFieldQueries()) {
|
726
|
logger.debug("adding " + fieldQuery);
|
727
|
fieldQueries.add(fieldQuery);
|
728
|
}
|
729
|
}
|
730
|
|
731
|
private void builtQueryKeywords(StringBuilder queryBuilder, String keywords) {
|
732
|
if (keywords != null && !keywords.trim().isEmpty()) {
|
733
|
CQLQueryBuilder.appendKeywords(queryBuilder, cleanKeywords(keywords));
|
734
|
}
|
735
|
|
736
|
if (queryBuilder.toString().isEmpty()) {
|
737
|
queryBuilder.append("(*)");
|
738
|
}
|
739
|
}
|
740
|
|
741
|
private void builtEntityIdQuery(StringBuilder queryBuilder, RequestResponseHandler.Entity entity, String entityId, List<String> fieldQueries){
|
742
|
enhanceFieldQueryWithEntityType(entity, fieldQueries);
|
743
|
enhanceQueryWithEntityId(queryBuilder, entity, cleanId(entityId));
|
744
|
}
|
745
|
|
746
|
private String builtEntity2EntityRelationQuery(RequestResponseHandler.Entity entity, String relationfield, String entityId, List<String> fieldQueries){
|
747
|
StringBuilder queryBuilder = new StringBuilder();
|
748
|
//enhanceQueryWithEntityType(queryBuilder, entity);
|
749
|
enhanceFieldQueryWithEntityType(entity, fieldQueries);
|
750
|
enhanceQueryWithRelationId(queryBuilder, relationfield, cleanId(entityId));
|
751
|
return queryBuilder.toString();
|
752
|
}
|
753
|
|
754
|
private void enhanceQueryWithRelationId(StringBuilder queryBuilder, String relationfield, String entityId) {
|
755
|
if (!queryBuilder.toString().isEmpty()) {
|
756
|
CQLQueryBuilder.appendFieldTerm(queryBuilder, CQLQueryBuilder.Operator.AND, " " + relationfield + " ", CQLQueryBuilder.Operator.EXACT, entityId);
|
757
|
|
758
|
} else {
|
759
|
CQLQueryBuilder.appendFieldTerm(queryBuilder, CQLQueryBuilder.Operator.NONE, relationfield + " ", CQLQueryBuilder.Operator.EXACT, entityId);
|
760
|
}
|
761
|
}
|
762
|
|
763
|
private void enhanceQueryWithEntityId(StringBuilder queryBuilder, RequestResponseHandler.Entity entity, String entityId) {
|
764
|
if (queryBuilder.toString().isEmpty()) {
|
765
|
CQLQueryBuilder.appendFieldTerm(queryBuilder, CQLQueryBuilder.Operator.NONE, "objidentifier", CQLQueryBuilder.Operator.EXACT, entityId);
|
766
|
} else {
|
767
|
CQLQueryBuilder.appendFieldTerm(queryBuilder, CQLQueryBuilder.Operator.AND, "objidentifier", CQLQueryBuilder.Operator.EXACT, entityId);
|
768
|
}
|
769
|
|
770
|
if (entity == RequestResponseHandler.Entity.PUBLICATION || entity == RequestResponseHandler.Entity.DATASET) {
|
771
|
CQLQueryBuilder.appendFieldTerm(queryBuilder, CQLQueryBuilder.Operator.OR, "resultdupid", CQLQueryBuilder.Operator.EXACT, entityId);
|
772
|
}
|
773
|
|
774
|
}
|
775
|
|
776
|
private void enhanceWithSortParameter(StringBuilder queryBuilder, String sortBy) {
|
777
|
if (sortBy != null) {
|
778
|
String[] sortParams = sortBy.split(",");
|
779
|
|
780
|
if (sortParams.length != 2) {
|
781
|
throw new IllegalArgumentException("Invalid sort paremeter. 'sortBy' parameter format is <fieldName>[,asc|,desc].");
|
782
|
}
|
783
|
|
784
|
String sortByField = sortParams[0];
|
785
|
String order = sortParams[1];
|
786
|
|
787
|
if (!sortByField.equals("resultdateofacceptance")){
|
788
|
throw new IllegalArgumentException("'" + sortByField + "' is not a sortable field.");
|
789
|
}
|
790
|
|
791
|
if (!checkOrder(order)) {
|
792
|
throw new IllegalArgumentException("'" + order + "' is not a valid ordering. Please use one of {ascending, descending}");
|
793
|
}
|
794
|
|
795
|
addSortParameter(sortByField, order, queryBuilder);
|
796
|
}
|
797
|
}
|
798
|
|
799
|
private static boolean checkOrder(String order) {
|
800
|
if (order.matches("ascending|descending")) {
|
801
|
return true;
|
802
|
}
|
803
|
return false;
|
804
|
}
|
805
|
|
806
|
private static void addSortParameter(String indexField, String order, StringBuilder queryBuilder) {
|
807
|
queryBuilder.append(" sortBy " + indexField + "/sort." + order);
|
808
|
}
|
809
|
|
810
|
private Response getCount(HttpServletRequest request, String query, String format, List<String> fieldQueries) {
|
811
|
String responseFormat = extractResponseFormat(format);
|
812
|
|
813
|
try {
|
814
|
SearchResult searchResult = searchService.newSearch(query, Locale.getDefault().toString(), null, fieldQueries, 0, 0, responseFormat, null, null, false);
|
815
|
return Response.status(Response.Status.OK).entity(APIResponseFormatter.createCountMeta(request, query, searchResult.getTotal(), responseFormat)).type(responseFormat).build();
|
816
|
|
817
|
} catch (SearchServiceException sse) {
|
818
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(APIResponseFormatter.compose500Message(responseFormat, "Fail to fetch count for query " + query + ".", sse.getMessage())).build();
|
819
|
}
|
820
|
}
|
821
|
|
822
|
private Response getResponseByEntityId(HttpServletRequest request, RequestResponseHandler.Entity entity, String entityId, String format, List<String> fieldQueries) {
|
823
|
String responseType = extractResponseFormat(format);
|
824
|
|
825
|
try {
|
826
|
StringBuilder queryBuilder = new StringBuilder();
|
827
|
builtEntityIdQuery(queryBuilder, entity, entityId, fieldQueries);
|
828
|
String fullQuery = queryBuilder.toString();
|
829
|
|
830
|
if(fullQuery == null || fullQuery.isEmpty()) {
|
831
|
return Response.status(Response.Status.BAD_REQUEST).
|
832
|
entity(APIResponseFormatter.compose400Message(format, "The 'query' parameter is required")).
|
833
|
type(responseType).build();
|
834
|
}
|
835
|
|
836
|
SearchResult searchResult = searchService.newSearch(fullQuery, Locale.getDefault().toString(), null, fieldQueries, 0, 1, responseType, null, null, false);
|
837
|
|
838
|
if (searchResult.getSearchResults() == null || searchResult.getSearchResults().isEmpty() ||
|
839
|
searchResult.getSearchResults().size() == 0 || searchResult.getSearchResults().get(0) == null) {
|
840
|
return Response.status(Response.Status.NOT_FOUND).entity(APIResponseFormatter.compose404Message(responseType, "404 - " + entity +" with id "
|
841
|
+ entityId + " not found.")).type(responseType).build();
|
842
|
}
|
843
|
|
844
|
return Response.status(Response.Status.OK).entity(APIResponseFormatter.createEntityResponse(request, entity, searchResult.getSearchResults().get(0).toString(), responseType)).type(responseType).build();
|
845
|
|
846
|
} catch (SearchServiceException sse) {
|
847
|
logger.error("Fail to fetch "+ entity + " with id " + entityId, sse);
|
848
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(APIResponseFormatter.compose500Message(responseType, "Fail to fetch " + entity + " with id " + entityId, sse.getMessage())).build();
|
849
|
}
|
850
|
}
|
851
|
|
852
|
private Response getResponseByEntity(String fullQuery, RequestResponseHandler.Entity entity, int offset, int limit, String format, HttpServletRequest request, boolean refine, List<String> refineFields, List<String> fieldQueries) {
|
853
|
long startTime = System.nanoTime();
|
854
|
Transformer transformer = extractTransformer(format, entity);
|
855
|
boolean special = isSpecialFormat(format);
|
856
|
boolean hasTitle = hasTitle(format);
|
857
|
String responseType = extractResponseFormat(format);
|
858
|
|
859
|
if(fullQuery == null || fullQuery.isEmpty()) {
|
860
|
return Response.status(Response.Status.BAD_REQUEST).
|
861
|
entity(APIResponseFormatter.compose400Message(responseType, "The 'query' parameter is required")).
|
862
|
type(responseType).build();
|
863
|
}
|
864
|
|
865
|
try {
|
866
|
SearchResult solrResults = searchService.newSearch(fullQuery, Locale.getDefault().toString(), refineFields, fieldQueries, offset, limit, responseType, transformer, null, false);
|
867
|
long estimatedTime = System.nanoTime() - startTime;
|
868
|
logger.debug("Publications search time " + estimatedTime/1000000 + " milliseconds");
|
869
|
return Response.status(Response.Status.OK).entity(APIResponseFormatter.createEntitiesResponse(request, entity, fullQuery, fieldQueries, solrResults, refine, responseType, special, hasTitle)).type(responseType).build();
|
870
|
|
871
|
} catch (SearchServiceException sse) {
|
872
|
logger.error("Fail to get " + entity.getPlural() , sse);
|
873
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).
|
874
|
entity(APIResponseFormatter.compose500Message(responseType, "Fail to fetch " + entity.getPlural(), sse.getMessage())).type(responseType).build();
|
875
|
}
|
876
|
}
|
877
|
|
878
|
private RequestResponseHandler.Entity extractEntity(String type) {
|
879
|
|
880
|
if (type == null) {
|
881
|
return RequestResponseHandler.Entity.NONE;
|
882
|
}
|
883
|
|
884
|
if (type.equalsIgnoreCase(RequestResponseHandler.Entity.PUBLICATION.getPlural())) {
|
885
|
return RequestResponseHandler.Entity.PUBLICATION;
|
886
|
|
887
|
} else if (type.equalsIgnoreCase(RequestResponseHandler.Entity.DATASET.getPlural())) {
|
888
|
return RequestResponseHandler.Entity.DATASET;
|
889
|
|
890
|
} else if (type.equalsIgnoreCase(RequestResponseHandler.Entity.SOFTWARE.getPlural())) {
|
891
|
return RequestResponseHandler.Entity.SOFTWARE;
|
892
|
|
893
|
} else if (type.equalsIgnoreCase(RequestResponseHandler.Entity.PROJECT.getPlural())) {
|
894
|
return RequestResponseHandler.Entity.PROJECT;
|
895
|
|
896
|
} else if (type.equalsIgnoreCase(RequestResponseHandler.Entity.DATASOURCE.getPlural())) {
|
897
|
return RequestResponseHandler.Entity.DATASOURCE;
|
898
|
|
899
|
} else if (type.equalsIgnoreCase(RequestResponseHandler.Entity.PERSON.getPlural())) {
|
900
|
return RequestResponseHandler.Entity.PERSON;
|
901
|
|
902
|
} else if (type.equalsIgnoreCase(RequestResponseHandler.Entity.ORGANIZATION.getPlural())) {
|
903
|
return RequestResponseHandler.Entity.ORGANIZATION;
|
904
|
}
|
905
|
|
906
|
return RequestResponseHandler.Entity.NONE;
|
907
|
}
|
908
|
|
909
|
|
910
|
/*
|
911
|
public static void main(String[] args) {
|
912
|
String json = " {\"result\":{\"xmlns:oaf\":\"http://namespace.openaire.eu/oaf\",\"xmlns:xsi\":\"http://www.w3.o" +
|
913
|
"rg/2001/XMLSchema-instance\",\"header\":{\"dri:dateOfCollection\":\"2016-05-20T03:34:07Z\",\"dri:dateOfTransformation\":\"\",\"counters\":{\"counter_dedup\":{\"value\":2},\"counter_doi\":{\"value\":2},\"counter_authorship\":{\"va" +
|
914
|
"lue\":1},\"counter_authorship_inferred\":{\"value\":1}},\"dri:objIdentifier\":\"dedup_wf_001::0f5604702b32f59d9789573113279e69\"},\"metadata\":{\"oaf:entity\":{\"oaf:result\":{\"datainfo\":{\"provenanceaction\":{\"schemename\"" +
|
915
|
":\"dnet:provenanceActions\",\"classname\":\"sysimport:dedup\",\"schemeid\":\"dnet:provenanceActions\",\"classid\":\"sysimport:dedup\"},\"trust\":0.9,\"inferenceprovenance\":\"dedup-similarity-result\",\"deletedbyinference\":fal" +
|
916
|
"se,\"inferred\":true},\"rels\":{\"rel\":{\"to\":{\"content\":\"dedup_wf_001::884d8c367553a8de98ec90ece1b1be4a\",\"scheme\":\"dnet:person_result_relations\",\"class\":\"hasAuthor\",\"type\":\"person\"},\"provenanceaction\":\"sysimpor" +
|
917
|
"t:crosswalk:repository\",\"trust\":0.9,\"inferenceprovenance\":\"\",\"ranking\":1,\"inferred\":true,\"fullname\":\"Krafft, Alexander\"}},\"subject\":[{\"content\":\"Clinic for Obstetrics\",\"schemename\":\"dnet:result_subject\",\"c" +
|
918
|
"lassname\":\"keyword\",\"schemeid\":\"dnet:result_subject\",\"classid\":\"keyword\"},{\"content\":\"610 Medicine & health\",\"schemename\":\"dnet:result_subject\",\"classname\":\"keyword\",\"schemeid\":\"dnet:result_subject\",\"class" +
|
919
|
"id\":\"keyword\"},{\"content\":\"News\",\"schemename\":\"dnet:result_subject\",\"classname\":\"keyword\",\"schemeid\":\"dnet:result_subject\",\"classid\":\"keyword\"}],\"collectedfrom\":[{\"id\":\"opendoar____::0efe32849d230d7f53049d" +
|
920
|
"dc4a4b0c60\",\"name\":\"Zurich Open Repository and Archive\"},{\"id\":\"opendoar____::8b6dd7db9af49e67306feb59a8bdc52c\",\"name\":\"Europe PubMed Central\"}],\"children\":{\"result\":[{\"title\":{\"content\":\"Iron supplementat" +
|
921
|
"ion in pregnancy\",\"schemename\":\"dnet:dataCite_title\",\"classname\":\"main title\",\"schemeid\":\"dnet:dataCite_title\",\"classid\":\"main title\"},\"resulttype\":{\"schemename\":\"dnet:result_typologies\",\"classname\":\"publi" +
|
922
|
"cation\",\"schemeid\":\"dnet:result_typologies\",\"classid\":\"publication\"},\"dateofacceptance\":\"1981-09-01\",\"objidentifier\":\"od_______908::f27f9e68f0b9f510ebf572ba295344e1\"},{\"title\":{\"content\":\"Iron supplementat" +
|
923
|
"ion in pregnancy\",\"schemename\":\"dnet:dataCite_title\",\"classname\":\"main title\",\"schemeid\":\"dnet:dataCite_title\",\"classid\":\"main title\"},\"resulttype\":{\"schemename\":\"dnet:result_typologies\",\"classname\":\"publi" +
|
924
|
"cation\",\"schemeid\":\"dnet:result_typologies\",\"classid\":\"publication\"},\"dateofacceptance\":\"2013-01-01\",\"publisher\":\"BMJ Publishing Group\",\"objidentifier\":\"od_______885::0f5604702b32f59d9789573113279e69\"}],\"i" +
|
925
|
"nstance\":[{\"webresource\":{\"url\":\"http://europepmc.org/articles/PMC2306130\"},\"id\":\"opendoar____::8b6dd7db9af49e67306feb59a8bdc52c\",\"licence\":{\"schemename\":\"dnet:access_modes\",\"classname\":\"Open Access\",\"sche" +
|
926
|
"meid\":\"dnet:access_modes\",\"classid\":\"OPEN\"},\"hostedby\":{\"id\":\"opendoar____::8b6dd7db9af49e67306feb59a8bdc52c\",\"name\":\"Europe PubMed Central\"},\"instancetype\":{\"schemename\":\"dnet:publication_resource\",\"class" +
|
927
|
"name\":\"Article\",\"schemeid\":\"dnet:publication_resource\",\"classid\":\"0001\"}},{\"webresource\":{\"url\":\"http://www.zora.uzh.ch/87059/1/30_13_Krafft_2013bmj_f4399_full.pdf\"},\"id\":\"opendoar____::0efe32849d230d7f530" +
|
928
|
"49ddc4a4b0c60\",\"licence\":{\"schemename\":\"dnet:access_modes\",\"classname\":\"Open Access\",\"schemeid\":\"dnet:access_modes\",\"classid\":\"OPEN\"},\"hostedby\":{\"id\":\"opendoar____::0efe32849d230d7f53049ddc4a4b0c60\",\"name" +
|
929
|
"\":\"Zurich Open Repository and Archive\"},\"instancetype\":{\"schemename\":\"dnet:publication_resource\",\"classname\":\"Article\",\"schemeid\":\"dnet:publication_resource\",\"classid\":\"0001\"}}]},\"pid\":[{\"content\":\"10.5167" +
|
930
|
"/uzh-87059\",\"schemename\":\"dnet:pid_types\",\"classname\":\"doi\",\"schemeid\":\"dnet:pid_types\",\"classid\":\"doi\"},{\"content\":\"PMC2306130\",\"schemename\":\"dnet:pid_types\",\"classname\":\"pmc\",\"schemeid\":\"dnet:pid_types\"," +
|
931
|
"\"classid\":\"pmc\"},{\"content\":\"10.1136/bmj.f4399\",\"schemename\":\"dnet:pid_types\",\"classname\":\"doi\",\"schemeid\":\"dnet:pid_types\",\"classid\":\"doi\"}],\"resourcetype\":{\"schemename\":\"\",\"classname\":\"\",\"schemeid\":\"\",\"c" +
|
932
|
"lassid\":\"\"},\"format\":\"\",\"dateofacceptance\":\"2013-01-01\",\"embargoenddate\":\"\",\"version\":\"\",\"size\":\"\",\"publisher\":\"BMJ Publishing Group\",\"country\":{\"schemename\":\"\",\"classname\":\"\",\"schemeid\":\"\",\"classid\":\"\"},\"" +
|
933
|
"metadataversionnumber\":\"\",\"title\":{\"content\":\"Iron supplementation in pregnancy\",\"schemename\":\"dnet:dataCite_title\",\"classname\":\"main title\",\"schemeid\":\"dnet:dataCite_title\",\"classid\":\"main title\"},\"result" +
|
934
|
"type\":{\"schemename\":\"dnet:result_typologies\",\"classname\":\"publication\",\"schemeid\":\"dnet:result_typologies\",\"classid\":\"publication\"},\"source\":\"Krafft, Alexander (2013). Iron supplementation in pregnancy. Br" +
|
935
|
"itish Medical Journal, 347:f4399.\",\"contributor\":\"\",\"description\":\"\",\"originalId\":[\"oai:europepmc.org:1052305\",\"oai:www.zora.uzh.ch:87059\"],\"lastmetadataupdate\":\"\",\"relevantdate\":{\"schemename\":\"\",\"classnam" +
|
936
|
"e\":\"\",\"schemeid\":\"\",\"classid\":\"\"},\"device\":\"\",\"bestlicense\":{\"schemename\":\"dnet:access_modes\",\"classname\":\"Open Access\",\"schemeid\":\"dnet:access_modes\",\"classid\":\"OPEN\"},\"language\":{\"schemename\":\"dnet:langu" +
|
937
|
"ages\",\"classname\":\"English\",\"schemeid\":\"dnet:languages\",\"classid\":\"eng\"},\"storagedate\":\"\",\"fulltext\":\"\"},\"xsi:schemaLocation\":\"http://namespace.openaire.eu/oaf https://www.openaire.eu/schema/0.2/oaf-0.2.xs" +
|
938
|
"d\"}},\"xmlns:dri\":\"http://www.driver-repository.eu/namespace/dri\"}}";
|
939
|
|
940
|
String s = "{\"result\":{\"xmlns:oaf\":\"http://namespace.openaire.eu/oaf\",\"xmlns:xsi\":\"http://www.w3.org/2001" +
|
941
|
"/XMLSchema-instance\",\"header\":{\"dri:dateOfCollection\":\"2014-04-10T13:35:33Z\",\"dri:dateOfTransformation\":\"\",\"counters\":{\"counter_dedup\":{\"value\":2},\"counter_authorship\":{\"value\":1},\"counter_authorship_infer" +
|
942
|
"red\":{\"value\":1}},\"dri:objIdentifier\":\"dedup_wf_001::4d934888dee33a62dece6fa36192c710\"},\"metadata\":{\"oaf:entity\":{\"oaf:result\":{\"datainfo\":{\"provenanceaction\":{\"schemename\":\"dnet:provenanceActions\",\"classn" +
|
943
|
"ame\":\"sysimport:dedup\",\"schemeid\":\"dnet:provenanceActions\",\"classid\":\"sysimport:dedup\"},\"trust\":0.9,\"inferenceprovenance\":\"dedup-similarity-result\",\"deletedbyinference\":false,\"inferred\":true},\"rels\":{\"rel\"" +
|
944
|
":{\"to\":{\"content\":\"od______1232::d4f9cea02c7762da24cd71ea0b9791de\",\"scheme\":\"dnet:person_result_relations\",\"class\":\"hasAuthor\",\"type\":\"person\"},\"provenanceaction\":\"sysimport:crosswalk:repository\",\"trust\":0" +
|
945
|
".9,\"inferenceprovenance\":\"\",\"ranking\":1,\"inferred\":true,\"fullname\":\"Anonymous\"}},\"subject\":{\"content\":\"Señoríos-Legislación\",\"schemename\":\"dnet:result_subject\",\"classname\":\"keyword\",\"schemeid\":\"dnet:result" +
|
946
|
"_subject\",\"classid\":\"keyword\"},\"collectedfrom\":[{\"id\":\"opendoar____::e53a0a2978c28872a4505bdb51db06dc\",\"name\":\"Biblioteca Virtual del Patrimonio Bibliográfico (Virtual Library of Bibliographical Heritage)\"" +
|
947
|
"},{\"id\":\"opendoar____::7aee26c309def8c5a2a076eb250b8f36\",\"name\":\"Biblioteca Virtual de Aragón\"}],\"children\":{\"result\":[{\"title\":{\"content\":\"Discurso sobre los señoríos : necesidad y justicia del decreto de" +
|
948
|
" Cortes de 6 de Agosto de 1811 y ventaja que debe causar a la Nación\",\"schemename\":\"dnet:dataCite_title\",\"classname\":\"main title\",\"schemeid\":\"dnet:dataCite_title\",\"classid\":\"main title\"},\"resulttype\":{\"sch" +
|
949
|
"emename\":\"dnet:result_typologies\",\"classname\":\"publication\",\"schemeid\":\"dnet:result_typologies\",\"classid\":\"publication\"},\"dateofacceptance\":\"1820-01-01\",\"objidentifier\":\"od______2719::bc2326a31763719dc5ebd" +
|
950
|
"38f16a25404\"},{\"title\":{\"content\":\"Discurso sobre los señoríos : necesidad y justicia del decreto de Cortes de 6 de Agosto de 1811 y ventaja que debe causar a la Nación\",\"schemename\":\"dnet:dataCite_title\"," +
|
951
|
"\"classname\":\"main title\",\"schemeid\":\"dnet:dataCite_title\",\"classid\":\"main title\"},\"resulttype\":{\"schemename\":\"dnet:result_typologies\",\"classname\":\"publication\",\"schemeid\":\"dnet:result_typologies\",\"classid\"" +
|
952
|
":\"publication\"},\"dateofacceptance\":\"1820-01-01\",\"objidentifier\":\"od______1232::4d934888dee33a62dece6fa36192c710\"}],\"instance\":[{\"webresource\":[{\"url\":\"http://bvpb.mcu.es/es/consulta/registro.cmd?id=403864\"" +
|
953
|
"},{\"url\":\"http://bvpb.mcu.es/es/catalogo_imagenes/grupo.cmd?path=2755\"}],\"id\":\"opendoar____::e53a0a2978c28872a4505bdb51db06dc\",\"licence\":{\"schemename\":\"dnet:access_modes\",\"classname\":\"Open Access\",\"schemei" +
|
954
|
"d\":\"dnet:access_modes\",\"classid\":\"OPEN\"},\"hostedby\":{\"id\":\"opendoar____::e53a0a2978c28872a4505bdb51db06dc\",\"name\":\"Biblioteca Virtual del Patrimonio Bibliográfico (Virtual Library of Bibliographical Herita" +
|
955
|
"ge)\"},\"instancetype\":{\"schemename\":\"dnet:publication_resource\",\"classname\":\"Book\",\"schemeid\":\"dnet:publication_resource\",\"classid\":\"0002\"}},{\"webresource\":{\"url\":\"http://bibliotecavirtual.aragon.es/bva/i18" +
|
956
|
"n/consulta/registro.cmd?id=2878\"},\"id\":\"opendoar____::7aee26c309def8c5a2a076eb250b8f36\",\"licence\":{\"schemename\":\"dnet:access_modes\",\"classname\":\"Open Access\",\"schemeid\":\"dnet:access_modes\",\"classid\":\"OPEN\"" +
|
957
|
"},\"hostedby\":{\"id\":\"opendoar____::7aee26c309def8c5a2a076eb250b8f36\",\"name\":\"Biblioteca Virtual de Aragón\"},\"instancetype\":{\"schemename\":\"dnet:publication_resource\",\"classname\":\"Book\",\"schemeid\":\"dnet:publi" +
|
958
|
"cation_resource\",\"classid\":\"0002\"}}]},\"pid\":{\"schemename\":\"\",\"classname\":\"\",\"schemeid\":\"\",\"classid\":\"\"},\"resourcetype\":{\"schemename\":\"\",\"classname\":\"\",\"schemeid\":\"\",\"classid\":\"\"},\"format\":\"\",\"dateofaccepta" +
|
959
|
"nce\":\"1820-01-01\",\"embargoenddate\":\"\",\"version\":\"\",\"size\":\"\",\"country\":{\"schemename\":\"\",\"classname\":\"\",\"schemeid\":\"\",\"classid\":\"\"},\"publisher\":\"\",\"metadataversionnumber\":\"\",\"title\":{\"content\":\"Discurso sob" +
|
960
|
"re los señoríos : necesidad y justicia del decreto de Cortes de 6 de Agosto de 1811 y ventaja que debe causar a la Nación\",\"schemename\":\"dnet:dataCite_title\",\"classname\":\"main title\",\"schemeid\":\"dnet:dataC" +
|
961
|
"ite_title\",\"classid\":\"main title\"},\"resulttype\":{\"schemename\":\"dnet:result_typologies\",\"classname\":\"publication\",\"schemeid\":\"dnet:result_typologies\",\"classid\":\"publication\"},\"source\":\"\",\"contributor\":\"Impr" +
|
962
|
"enta de Heras, imp.\",\"description\":\"\",\"originalId\":[\"oai:bvpb.mcu.es:403864\",\"oai:bibliotecavirtual.aragon.es:2878\"],\"lastmetadataupdate\":\"\",\"relevantdate\":{\"schemename\":\"\",\"classname\":\"\",\"schemeid\":\"\",\"cl" +
|
963
|
"assid\":\"\"},\"device\":\"\",\"bestlicense\":{\"schemename\":\"dnet:access_modes\",\"classname\":\"Open Access\",\"schemeid\":\"dnet:access_modes\",\"classid\":\"OPEN\"},\"language\":{\"schemename\":\"dnet:languages\",\"classname\":\"Span" +
|
964
|
"ish\",\"schemeid\":\"dnet:languages\",\"classid\":\"esl/spa\"},\"storagedate\":\"\",\"fulltext\":\"\"},\"xsi:schemaLocation\":\"http://namespace.openaire.eu/oaf https://www.openaire.eu/schema/0.2/oaf-0.2.xsd\"}},\"xmlns:dri\":\"h" +
|
965
|
"ttp://www.driver-repository.eu/namespace/dri\"}}\n";
|
966
|
|
967
|
JSONObject finalOject = new JSONObject();
|
968
|
finalOject.put("dri:objIdentifier", new JSONObject(json).getJSONObject("result").getJSONObject("header").getString("dri:objIdentifier"));
|
969
|
finalOject.put("content", new JSONObject(new JSONObject(json).getJSONObject("result").getJSONObject("metadata").getJSONObject("oaf:entity").getJSONObject("oaf:result"),
|
970
|
new String[]{"title", "description", "language", "bestlicense", "pid", "rels"}));
|
971
|
|
972
|
System.out.println(finalOject);
|
973
|
|
974
|
JSONObject jsonObject = new JSONObject(s).getJSONObject("result").getJSONObject("metadata").getJSONObject("oaf:entity").getJSONObject("oaf:result");
|
975
|
System.out.println("title --> " + jsonObject.getJSONObject("title").getString("content"));
|
976
|
System.out.println("pid --> " + jsonObject.getJSONArray("pid"));
|
977
|
|
978
|
JSONObject newJsonObject1 = new JSONObject(jsonObject, new String[]{"title", "description", "language", "bestlicense", "pid", "rels"});
|
979
|
|
980
|
}
|
981
|
*/
|
982
|
/*
|
983
|
|
984
|
public static void main(String[] args) throws IOException, CQLParseException {
|
985
|
CqlTranslator translator = new CqlTranslatorImpl();
|
986
|
System.out.println(translator.toLucene("relfundinglevel0_id = ec__________::EC::FP7"));
|
987
|
}
|
988
|
*/
|
989
|
|
990
|
}
|