Revision 44602
Added by Katerina Iatropoulou over 7 years ago
SearchApiService.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import eu.dnetlib.api.data.SearchServiceException; |
4 | 4 |
import eu.dnetlib.data.search.app.SearchServiceImpl; |
5 |
import eu.dnetlib.data.search.utils.SolrResultsFormatter; |
|
5 | 6 |
import eu.dnetlib.data.search.web.utils.RequestResponseHandler; |
6 | 7 |
import eu.dnetlib.domain.data.SearchResult; |
8 |
import eu.dnetlib.functionality.index.cql.CqlTranslator; |
|
9 |
import eu.dnetlib.functionality.index.cql.CqlTranslatorImpl; |
|
7 | 10 |
import org.apache.log4j.Logger; |
8 |
import org.json.JSONObject; |
|
9 | 11 |
import org.json.XML; |
10 | 12 |
import org.springframework.beans.factory.annotation.Autowired; |
11 | 13 |
import org.springframework.stereotype.Component; |
14 |
import org.z3950.zing.cql.CQLParseException; |
|
12 | 15 |
|
13 | 16 |
import javax.servlet.http.HttpServletRequest; |
14 | 17 |
import javax.ws.rs.*; |
15 | 18 |
import javax.ws.rs.core.Context; |
16 | 19 |
import javax.ws.rs.core.MediaType; |
17 | 20 |
import javax.ws.rs.core.Response; |
18 |
import java.util.*; |
|
21 |
import java.io.IOException; |
|
22 |
import java.util.Arrays; |
|
23 |
import java.util.List; |
|
24 |
import java.util.Locale; |
|
25 |
import java.util.Map; |
|
19 | 26 |
|
20 | 27 |
/** |
21 | 28 |
* Created by kiatrop on 2/6/2016. |
... | ... | |
31 | 38 |
private SearchServiceImpl searchService = null; |
32 | 39 |
|
33 | 40 |
@GET |
41 |
@Path("/api/resources") |
|
42 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) |
|
43 |
public Response fetchResources(@QueryParam("query") String query, |
|
44 |
@DefaultValue("1") @QueryParam("page") final int offset, |
|
45 |
@DefaultValue("10") @QueryParam("size") final int limit, |
|
46 |
@QueryParam("refine") @DefaultValue("false") final boolean refine, |
|
47 |
@QueryParam("fields") final List<String> fields, |
|
48 |
@QueryParam("fq") final List<String> fieldQueries, |
|
49 |
@QueryParam("format") final String format, |
|
50 |
@Context final HttpServletRequest request) { |
|
51 |
String type = null; |
|
52 |
if (format != null && !format.isEmpty() && format.equalsIgnoreCase("json")) { |
|
53 |
type = MediaType.APPLICATION_JSON; |
|
54 |
|
|
55 |
} else { |
|
56 |
type = MediaType.APPLICATION_XML; |
|
57 |
} |
|
58 |
|
|
59 |
if(query == null || query.isEmpty()) { |
|
60 |
return Response.status(Response.Status.BAD_REQUEST). |
|
61 |
entity(APIResponseFormatter.compose400Message(type, "The 'query' parameter is required")). |
|
62 |
type(type).build(); |
|
63 |
} |
|
64 |
|
|
65 |
return getResponseByEntity(query, RequestResponseHandler.Entity.PUBLICATION, offset, limit, format, request, refine, fields, fieldQueries); |
|
66 |
} |
|
67 |
/* |
|
68 |
@GET |
|
34 | 69 |
@Path("/api/publications") |
35 |
@Produces(MediaType.APPLICATION_JSON) |
|
36 |
public Response fetchPublications(@QueryParam("q") List<String> keywords, |
|
37 |
@QueryParam("op") final List<String> keywordOperators, |
|
70 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) |
|
71 |
public Response fetchPublications(@QueryParam("q") String keywords, |
|
38 | 72 |
@DefaultValue("1") @QueryParam("page") final int offset, |
39 | 73 |
@DefaultValue("10") @QueryParam("size") final int limit, |
40 | 74 |
@QueryParam("refine") @DefaultValue("false") final boolean refine, |
41 | 75 |
@QueryParam("fields") final List<String> fields, |
76 |
@QueryParam("format") final String format, |
|
42 | 77 |
@Context final HttpServletRequest request) { |
78 |
|
|
79 |
String simpleQuery = createSimpleSearchQuery(RequestResponseHandler.Entity.PUBLICATION, keywords); |
|
80 |
return getResponseByEntity(simpleQuery, RequestResponseHandler.Entity.PUBLICATION, offset, limit, format, request, refine, fields); |
|
43 | 81 |
|
44 |
String fullQuery = builtFullQuery(keywords, keywordOperators, RequestResponseHandler.Entity.PUBLICATION, request); |
|
45 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PUBLICATION, offset, limit, request, refine, fields); |
|
46 | 82 |
} |
47 | 83 |
|
48 |
private String builtFullQuery(List<String> keywords, List<String> keywordOperators, RequestResponseHandler.Entity entity, @Context HttpServletRequest request) { |
|
84 |
private String createSimpleSearchQuery(RequestResponseHandler.Entity entity, String keywords) { |
|
85 |
StringBuilder queryBuilder = new StringBuilder(); |
|
86 |
enhanceQueryWithEntityType(queryBuilder,entity); |
|
87 |
enhanceQueryWithKeywords(queryBuilder, keywords); |
|
88 |
return queryBuilder.toString(); |
|
89 |
} |
|
90 |
|
|
91 |
private void enhanceQueryWithKeywords(StringBuilder queryBuilder, String keywords) { |
|
92 |
queryBuilder.append(' ').append(CQLQueryGeneration.Operator.AND).append(' ').append(keywords); |
|
93 |
} |
|
94 |
|
|
95 |
private String builtFullQuery(List<String> keywords, List<String> keywordOperators, RequestResponseHandler.Entity entity, HttpServletRequest request) { |
|
49 | 96 |
Map<String, String[]> parameterMap = request.getParameterMap(); |
50 | 97 |
|
51 | 98 |
Map<String, String[]> operators = new HashMap<String, String[]>(); |
... | ... | |
55 | 102 |
return enhanceQuery(keywords, keywordOperators, operators, parameters, entity); |
56 | 103 |
} |
57 | 104 |
|
105 |
/* public static void buildFq(RequestResponseHandler.Entity entity, HttpServletRequest request, List<String> fields, List<FieldQuery> fieldQueries) { |
|
106 |
Map<String, String[]> parameterMap = request.getParameterMap(); |
|
58 | 107 |
|
108 |
Map<String, String> indexParameterMap = entity.geIndexParameterMap(); |
|
109 |
Map<String, String> parameterOperatorMap = entity.getParameterOperatorMap(); |
|
110 |
//logger.debug(parameterOperatorMap.keySet()); |
|
111 |
|
|
112 |
for (String field:fields) { |
|
113 |
logger.debug("field: " + field); |
|
114 |
String parameter = indexParameterMap.get(field); |
|
115 |
logger.debug("parameter: " + parameter); |
|
116 |
if (indexParameterMap.get(field) != null ) { |
|
117 |
String[] refineValues = request.getParameterValues(parameter); |
|
118 |
String operatorParameter = parameterOperatorMap.get(parameter); |
|
119 |
String operatorValue = null; |
|
120 |
if (operatorParameter != null && !operatorParameter.trim().isEmpty()) { |
|
121 |
operatorValue = request.getParameter(operatorParameter); |
|
122 |
if (operatorValue == null || operatorValue.trim().isEmpty()) { |
|
123 |
operatorValue = "AND"; |
|
124 |
} |
|
125 |
fieldQueries.add(new FieldQuery(field, operatorValue, refineValues)); |
|
126 |
|
|
127 |
} else { |
|
128 |
//TODO throw exception? |
|
129 |
} |
|
130 |
} |
|
131 |
} |
|
132 |
} |
|
133 |
*/ |
|
134 |
|
|
135 |
/* |
|
136 |
|
|
137 |
public static String createFqParameter(List<FieldQuery> fieldQueries) { |
|
138 |
StringBuilder fq = new StringBuilder(); |
|
139 |
for (FieldQuery fieldQuery:fieldQueries) { |
|
140 |
fq.append(fieldQuery.getIndexField()); |
|
141 |
if(fieldQuery.getValues() != null && fieldQuery.getValues().length == 0) { |
|
142 |
for(int i=0; i< fieldQuery.getValues().length; i++) { |
|
143 |
fq.append(":").append(fieldQuery.getValues()[i] + " "); |
|
144 |
if(i < fieldQuery.getValues().length-1) { |
|
145 |
fq.append(" " + fieldQuery.getOperator() + " "); |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
} |
|
150 |
return fq.toString(); |
|
151 |
} |
|
152 |
|
|
59 | 153 |
@GET |
60 | 154 |
@Path("/api/publications/count") |
61 |
@Produces(MediaType.APPLICATION_JSON)
|
|
155 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
62 | 156 |
public Response fetchPublicationsCount(@QueryParam("q") String query) { |
63 | 157 |
String fullQuery = builtQueryByEntity(query, RequestResponseHandler.Entity.PUBLICATION); |
64 | 158 |
return getCount(fullQuery); |
... | ... | |
67 | 161 |
|
68 | 162 |
@GET |
69 | 163 |
@Path("/api/publications/{publicationid}") |
70 |
@Produces(MediaType.APPLICATION_JSON)
|
|
164 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
71 | 165 |
public Response fetchPublicationsById(@PathParam("publicationid") String publicationid) { |
72 | 166 |
return getResponseByEntityId(RequestResponseHandler.Entity.PUBLICATION, publicationid); |
73 | 167 |
} |
... | ... | |
75 | 169 |
|
76 | 170 |
@GET |
77 | 171 |
@Path("/api/datasets") |
78 |
@Produces(MediaType.APPLICATION_JSON)
|
|
172 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
79 | 173 |
public Response fetchDatasets(@QueryParam("q") List<String> keywords, |
80 | 174 |
@QueryParam("op") final List<String> keywordOperators, |
81 | 175 |
@DefaultValue("1") @QueryParam("page") int offset, |
82 | 176 |
@DefaultValue("10") @QueryParam("size") int limit, |
83 | 177 |
@QueryParam("refine") @DefaultValue("false") boolean refine, |
84 | 178 |
@QueryParam("fields") final List<String> fields, |
179 |
@QueryParam("format") final String format, |
|
85 | 180 |
@Context HttpServletRequest request) { |
86 | 181 |
|
87 | 182 |
String fullQuery = builtFullQuery(keywords, keywordOperators, RequestResponseHandler.Entity.PUBLICATION, request); |
88 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.DATASET, offset, limit, request, refine, fields); |
|
183 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.DATASET, offset, limit, format, request, refine, fields);
|
|
89 | 184 |
} |
90 | 185 |
|
91 | 186 |
@GET |
92 | 187 |
@Path("/api/datasets/count") |
93 |
@Produces(MediaType.APPLICATION_JSON)
|
|
188 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
94 | 189 |
public Response fetchDatesetsCount(@QueryParam("q") String query) { |
95 | 190 |
String fullQuery = builtQueryByEntity(query, RequestResponseHandler.Entity.DATASET); |
96 | 191 |
return getCount(fullQuery); |
... | ... | |
98 | 193 |
|
99 | 194 |
@GET |
100 | 195 |
@Path("/api/datasets/{datasetid}") |
101 |
@Produces(MediaType.APPLICATION_JSON)
|
|
196 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
102 | 197 |
public Response fetchDatasets(@PathParam("datasetid") String datasetid) { |
103 | 198 |
return getResponseByEntityId(RequestResponseHandler.Entity.DATASET, datasetid); |
104 | 199 |
} |
... | ... | |
106 | 201 |
|
107 | 202 |
@GET |
108 | 203 |
@Path("/api/projects") |
109 |
@Produces(MediaType.APPLICATION_JSON)
|
|
204 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
110 | 205 |
public Response fetchProjects(@QueryParam("q") List<String> keywords, |
111 | 206 |
@QueryParam("op") final List<String> keywordOperators, |
112 | 207 |
@DefaultValue("1") @QueryParam("page") final int offset, |
113 | 208 |
@DefaultValue("10") @QueryParam("size") final int limit, |
114 | 209 |
@QueryParam("refine") @DefaultValue("false") final boolean refine, |
210 |
@QueryParam("format") final String format, |
|
115 | 211 |
@QueryParam("fields") final List<String> fields, |
116 | 212 |
@Context final HttpServletRequest request) { |
117 | 213 |
|
118 | 214 |
String fullQuery = builtFullQuery(keywords, keywordOperators, RequestResponseHandler.Entity.PROJECT, request); |
119 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PROJECT, offset, limit, request, refine, fields); |
|
215 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PROJECT, offset, limit, format, request, refine, fields);
|
|
120 | 216 |
} |
121 | 217 |
|
122 | 218 |
|
123 | 219 |
@GET |
124 | 220 |
@Path("/api/projects/count") |
125 |
@Produces(MediaType.APPLICATION_JSON)
|
|
221 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
126 | 222 |
public Response fetchProjectsCount(@QueryParam("q") String query){ |
127 | 223 |
String fullQuery = builtQueryByEntity(query, RequestResponseHandler.Entity.PROJECT); |
128 | 224 |
return getCount(fullQuery); |
... | ... | |
130 | 226 |
|
131 | 227 |
@GET |
132 | 228 |
@Path("/api/projects/{projectid}") |
133 |
@Produces(MediaType.APPLICATION_JSON)
|
|
229 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
134 | 230 |
public Response fetchProjects(@PathParam("projectid") String projectid) { |
135 | 231 |
return getResponseByEntityId(RequestResponseHandler.Entity.PROJECT, projectid); |
136 | 232 |
} |
137 | 233 |
|
138 | 234 |
@GET |
139 | 235 |
@Path("/api/projects/{projectId}/publications") |
140 |
@Produces(MediaType.APPLICATION_JSON)
|
|
236 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
141 | 237 |
public Response fetchProjectPublications(@PathParam("projectId") String projectId, |
142 | 238 |
@DefaultValue("1") @QueryParam("page") int offset, |
143 | 239 |
@DefaultValue("10") @QueryParam("size") int limit, |
240 |
@QueryParam("format") final String format, |
|
144 | 241 |
@Context HttpServletRequest request) { |
145 | 242 |
|
146 | 243 |
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.PUBLICATION, "relprojectid", projectId); |
147 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PUBLICATION, offset, limit, request, false, null); |
|
244 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PUBLICATION, offset, limit, format, request, false, null);
|
|
148 | 245 |
} |
149 | 246 |
|
150 | 247 |
@GET |
151 | 248 |
@Path("/api/projects/{projectId}/publications/count") |
152 |
@Produces(MediaType.APPLICATION_JSON)
|
|
249 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
153 | 250 |
public Response fetchProjectPublicationsCount(@PathParam("projectId") String projectId) { |
154 | 251 |
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.PUBLICATION, "relprojectid", projectId); |
155 | 252 |
return getCount(fullQuery); |
... | ... | |
157 | 254 |
|
158 | 255 |
@GET |
159 | 256 |
@Path("/api/projects/{projectId}/datasets") |
160 |
@Produces(MediaType.APPLICATION_JSON)
|
|
257 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
161 | 258 |
public Response fetchProjectDatasets(@PathParam("projectId") String projectId, |
162 | 259 |
@DefaultValue("1") @QueryParam("page") int offset, |
163 | 260 |
@DefaultValue("10") @QueryParam("size") int limit, |
261 |
@QueryParam("format") final String format, |
|
164 | 262 |
@Context HttpServletRequest request) { |
165 | 263 |
|
166 | 264 |
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.DATASET, "relprojectid", projectId); |
167 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.DATASET, offset, limit, request, false, null); |
|
265 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.DATASET, offset, limit, format, request, false, null);
|
|
168 | 266 |
} |
169 | 267 |
|
170 | 268 |
@GET |
171 | 269 |
@Path("/api/projects/{projectId}/datasets/count") |
172 |
@Produces(MediaType.APPLICATION_JSON)
|
|
270 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
173 | 271 |
public Response fetchProjectDatasetsCount(@PathParam("projectId") String projectId, |
174 | 272 |
@DefaultValue("1") @QueryParam("page") int offset, |
175 | 273 |
@DefaultValue("10") @QueryParam("size") int limit, |
... | ... | |
182 | 280 |
|
183 | 281 |
@GET |
184 | 282 |
@Path("/api/datasources") |
185 |
@Produces(MediaType.APPLICATION_JSON)
|
|
283 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
186 | 284 |
public Response fetchDatasources(@QueryParam("q") List<String> keywords, |
187 | 285 |
@QueryParam("op") final List<String> keywordOperators, |
188 | 286 |
@DefaultValue("1") @QueryParam("page") final int offset, |
189 | 287 |
@DefaultValue("10") @QueryParam("size") final int limit, |
190 | 288 |
@QueryParam("refine") @DefaultValue("false") final boolean refine, |
191 | 289 |
@QueryParam("fields") final List<String> fields, |
290 |
@QueryParam("format") final String format, |
|
192 | 291 |
@Context final HttpServletRequest request) { |
193 | 292 |
|
194 | 293 |
String fullQuery = builtFullQuery(keywords, keywordOperators, RequestResponseHandler.Entity.DATASOURCE, request); |
195 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.DATASOURCE, offset, limit, request, refine, fields); |
|
294 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.DATASOURCE, offset, limit, format, request, refine, fields);
|
|
196 | 295 |
} |
197 | 296 |
|
198 | 297 |
@GET |
199 | 298 |
@Path("/api/datasources/count") |
200 |
@Produces(MediaType.APPLICATION_JSON)
|
|
299 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
201 | 300 |
public Response fetchDatasourcesCount(@QueryParam("q") String query){ |
202 | 301 |
String fullQuery = builtQueryByEntity(query, RequestResponseHandler.Entity.DATASOURCE); |
203 | 302 |
return getCount(fullQuery); |
... | ... | |
205 | 304 |
|
206 | 305 |
@GET |
207 | 306 |
@Path("/api/datasources/{datasourceid}") |
208 |
@Produces(MediaType.APPLICATION_JSON)
|
|
307 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
209 | 308 |
public Response fetchDatasources(@PathParam("datasourceid") String datasourceid) { |
210 | 309 |
return getResponseByEntityId(RequestResponseHandler.Entity.DATASOURCE, datasourceid); |
211 | 310 |
} |
212 |
|
|
311 |
*/ |
|
213 | 312 |
/* |
214 | 313 |
@Path("/api/datasources/{datasourceid}/publications") |
215 | 314 |
@Path("/api/datasources/{datasourceid}/publications/count") |
... | ... | |
220 | 319 |
|
221 | 320 |
*/ |
222 | 321 |
|
223 |
|
|
322 |
/* |
|
224 | 323 |
@GET |
225 | 324 |
@Path("/api/organizations") |
226 |
@Produces(MediaType.APPLICATION_JSON)
|
|
325 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
227 | 326 |
public Response fetchOrganisations(@QueryParam("q") List<String> keywords, |
228 | 327 |
@QueryParam("op") final List<String> keywordOperators, |
229 | 328 |
@DefaultValue("1") @QueryParam("page") final int offset, |
230 | 329 |
@DefaultValue("10") @QueryParam("size") final int limit, |
231 | 330 |
@QueryParam("refine") @DefaultValue("false") final boolean refine, |
232 | 331 |
@QueryParam("fields") final List<String> fields, |
332 |
@QueryParam("format") final String format, |
|
233 | 333 |
@Context final HttpServletRequest request) { |
234 | 334 |
|
235 | 335 |
String fullQuery = builtFullQuery(keywords, keywordOperators, RequestResponseHandler.Entity.ORGANIZATION, request); |
236 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.ORGANIZATION, offset, limit, request, refine, fields); |
|
336 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.ORGANIZATION, offset, limit, format, request, refine, fields);
|
|
237 | 337 |
} |
238 | 338 |
|
239 | 339 |
|
240 | 340 |
@GET |
241 | 341 |
@Path("/api/organizations/count") |
242 |
@Produces(MediaType.APPLICATION_JSON)
|
|
342 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
243 | 343 |
public Response fetchOrganizationsCount(@QueryParam("q") String query){ |
244 | 344 |
String fullQuery = builtQueryByEntity(query, RequestResponseHandler.Entity.ORGANIZATION); |
245 | 345 |
return getCount(fullQuery); |
... | ... | |
247 | 347 |
|
248 | 348 |
@GET |
249 | 349 |
@Path("/api/organizations/{organizationid}") |
250 |
@Produces(MediaType.APPLICATION_JSON)
|
|
350 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
251 | 351 |
public Response fetchOrganizations(@PathParam("organizationid") String organizationid) { |
252 | 352 |
return getResponseByEntityId(RequestResponseHandler.Entity.ORGANIZATION, organizationid); |
253 | 353 |
} |
254 | 354 |
|
255 | 355 |
@GET |
256 | 356 |
@Path("/api/organizations/{organizationid}/datasources") |
257 |
@Produces(MediaType.APPLICATION_JSON)
|
|
357 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
258 | 358 |
public Response fetchOrganizationDatasources(@PathParam("organizationid") String organizationid, |
259 | 359 |
@DefaultValue("1") @QueryParam("page") int offset, |
260 | 360 |
@DefaultValue("10") @QueryParam("size") int limit, |
361 |
@QueryParam("format") final String format, |
|
261 | 362 |
@Context HttpServletRequest request) { |
262 | 363 |
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.DATASOURCE, "relorganizationid", organizationid); |
263 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.DATASOURCE, offset, limit, request, false, null); |
|
364 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.DATASOURCE, offset, limit, format, request, false, null);
|
|
264 | 365 |
} |
265 | 366 |
|
266 | 367 |
@GET |
267 | 368 |
@Path("/api/organizations/{organizationid}/datasources/count") |
268 |
@Produces(MediaType.APPLICATION_JSON)
|
|
369 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
269 | 370 |
public Response fetchOrganizationDatasources(@PathParam("organizationid") String organizationid) { |
270 | 371 |
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.DATASOURCE, "relorganizationid", organizationid); |
271 | 372 |
return getCount(fullQuery); |
... | ... | |
274 | 375 |
|
275 | 376 |
@GET |
276 | 377 |
@Path("/api/organizations/{organizationid}/projects") |
277 |
@Produces(MediaType.APPLICATION_JSON)
|
|
378 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
278 | 379 |
public Response fetchOrganizationProjects(@PathParam("organizationid") String organizationid, |
279 | 380 |
@DefaultValue("1") @QueryParam("page") int offset, |
280 | 381 |
@DefaultValue("10") @QueryParam("size") int limit, |
382 |
@QueryParam("format") final String format, |
|
281 | 383 |
@Context HttpServletRequest request) { |
282 | 384 |
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.PROJECT, "relprojectid", organizationid); |
283 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PROJECT, offset, limit, request, false, null); |
|
385 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PROJECT, offset, limit, format, request, false, null);
|
|
284 | 386 |
} |
285 | 387 |
|
286 | 388 |
@GET |
287 | 389 |
@Path("/api/organizations/{organizationid}/projects/count") |
288 |
@Produces(MediaType.APPLICATION_JSON)
|
|
390 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
289 | 391 |
public Response fetchOrganizationProjectsCount(@PathParam("organizationid") String organizationid) { |
290 | 392 |
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.PROJECT, "relprojectid", organizationid); |
291 | 393 |
return getCount(fullQuery); |
... | ... | |
295 | 397 |
|
296 | 398 |
@GET |
297 | 399 |
@Path("/api/people") |
298 |
@Produces(MediaType.APPLICATION_JSON)
|
|
400 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
299 | 401 |
public Response fetchPeople(@QueryParam("q") List<String> keywords, |
300 | 402 |
@QueryParam("op") final List<String> keywordOperators, |
301 | 403 |
@DefaultValue("1") @QueryParam("page") final int offset, |
302 | 404 |
@DefaultValue("10") @QueryParam("size") final int limit, |
303 | 405 |
@QueryParam("refine") @DefaultValue("false") final boolean refine, |
304 | 406 |
@QueryParam("fields") final List<String> fields, |
407 |
@QueryParam("format") final String format, |
|
305 | 408 |
@Context final HttpServletRequest request) { |
306 | 409 |
|
307 | 410 |
String fullQuery = builtFullQuery(keywords, keywordOperators, RequestResponseHandler.Entity.PERSON, request); |
308 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PERSON, offset, limit, request, refine, fields); |
|
411 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PERSON, offset, limit, format, request, refine, fields);
|
|
309 | 412 |
} |
310 | 413 |
|
311 | 414 |
|
312 | 415 |
@GET |
313 | 416 |
@Path("/api/people/count") |
314 |
@Produces(MediaType.APPLICATION_JSON)
|
|
417 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
315 | 418 |
public Response fetchPeopleCount(@QueryParam("q") String query){ |
316 | 419 |
String fullQuery = builtQueryByEntity(query, RequestResponseHandler.Entity.PERSON); |
317 | 420 |
return getCount(fullQuery); |
... | ... | |
319 | 422 |
|
320 | 423 |
@GET |
321 | 424 |
@Path("/api/people/{personid}") |
322 |
@Produces(MediaType.APPLICATION_JSON)
|
|
425 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
323 | 426 |
public Response fetchPeople(@PathParam("personid") String personid) { |
324 | 427 |
return getResponseByEntityId(RequestResponseHandler.Entity.PERSON, personid); |
325 | 428 |
} |
326 | 429 |
|
327 | 430 |
@GET |
328 | 431 |
@Path("/api/people/{personid}/publications") |
329 |
@Produces(MediaType.APPLICATION_JSON)
|
|
432 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
330 | 433 |
public Response fetchPeoplePublications(@PathParam("personid") String personid, |
331 | 434 |
@DefaultValue("1") @QueryParam("page") final int offset, |
332 | 435 |
@DefaultValue("10") @QueryParam("size") final int limit, |
436 |
@QueryParam("format") final String format, |
|
333 | 437 |
@Context final HttpServletRequest request) { |
334 | 438 |
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.PUBLICATION, "relpersonid", personid); |
335 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PUBLICATION, offset, limit, request, false, null); |
|
439 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.PUBLICATION, offset, limit, format, request, false, null);
|
|
336 | 440 |
} |
337 | 441 |
|
338 | 442 |
@GET |
339 | 443 |
@Path("/api/people/{personid}/publications/count") |
340 |
@Produces(MediaType.APPLICATION_JSON)
|
|
444 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
341 | 445 |
public Response fetchPeoplePublicationsCount(@PathParam("personid") String personid) { |
342 | 446 |
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.PUBLICATION, "relpersonid", personid); |
343 | 447 |
return getCount(fullQuery); |
... | ... | |
346 | 450 |
|
347 | 451 |
@GET |
348 | 452 |
@Path("/api/people/{personid}/datasets") |
349 |
@Produces(MediaType.APPLICATION_JSON)
|
|
453 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
350 | 454 |
public Response fetchPeopleDatasets(@PathParam("personid") String personid, |
351 | 455 |
@DefaultValue("1") @QueryParam("page") final int offset, |
352 | 456 |
@DefaultValue("10") @QueryParam("size") final int limit, |
457 |
@QueryParam("format") final String format, |
|
353 | 458 |
@Context final HttpServletRequest request) { |
354 | 459 |
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.DATASET, "relpersonid", personid); |
355 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.DATASET, offset, limit, request, false, null); |
|
460 |
return getResponseByEntity(fullQuery, RequestResponseHandler.Entity.DATASET, offset, limit, format, request, false, null);
|
|
356 | 461 |
} |
357 | 462 |
|
358 | 463 |
@GET |
359 | 464 |
@Path("/api/people/{personid}/datasets/count") |
360 |
@Produces(MediaType.APPLICATION_JSON)
|
|
465 |
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
|
361 | 466 |
public Response fetchPeopleDatasetsCount(@PathParam("personid") String personid) { |
362 | 467 |
String fullQuery = builtEntity2EntityRelationQuery(RequestResponseHandler.Entity.DATASET, "relpersonid", personid); |
363 | 468 |
return getCount(fullQuery); |
... | ... | |
377 | 482 |
|
378 | 483 |
return queryBuilder.toString(); |
379 | 484 |
} |
380 |
|
|
485 |
*/ |
|
486 |
@Deprecated |
|
381 | 487 |
private void enhanceQueryWithKeywords(StringBuilder queryBuilder, List<String> keywordOperators, List<String> keywords) { |
382 | 488 |
if (keywordOperators != null && !keywordOperators.isEmpty()) { |
383 | 489 |
if (keywords != null && !keywords.isEmpty()) { |
... | ... | |
434 | 540 |
} |
435 | 541 |
} |
436 | 542 |
|
543 |
/* |
|
437 | 544 |
private boolean isFirstTerm(StringBuilder queryBuilder) { |
438 | 545 |
return queryBuilder.toString().trim().equals(""); |
439 |
} |
|
546 |
}*/
|
|
440 | 547 |
|
441 | 548 |
public void builtEntityIdQuery(StringBuilder queryBuilder, RequestResponseHandler.Entity entity, String entityId){ |
442 | 549 |
enhanceQueryWithEntityType(queryBuilder, entity); |
... | ... | |
549 | 656 |
} |
550 | 657 |
} |
551 | 658 |
|
659 |
/* TODO remove? |
|
552 | 660 |
private Response getCountByEntity2EntityRelation(String query, RequestResponseHandler.Entity entity, String relationfield, String entityId) { |
553 | 661 |
String fullQuery = builtEntity2EntityRelationQuery(entity, relationfield, entityId); |
554 | 662 |
|
... | ... | |
559 | 667 |
} catch (SearchServiceException sse) { |
560 | 668 |
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ResponseFormatter.compose500Message("Fail to fetch count for query " + fullQuery.replace("\"", "\\\"") + ".", sse.getMessage())).build(); |
561 | 669 |
} |
562 |
} |
|
670 |
} */
|
|
563 | 671 |
|
564 | 672 |
private Response getResponseByEntityId(RequestResponseHandler.Entity entity, String entityId) { |
565 | 673 |
try { |
... | ... | |
584 | 692 |
} |
585 | 693 |
} |
586 | 694 |
|
587 |
private Response getResponseByEntity(String fullQuery, RequestResponseHandler.Entity entity, int offset, int limit, HttpServletRequest request, boolean refine, List<String> refineFields) {
|
|
695 |
private Response getResponseByEntity(String fullQuery, RequestResponseHandler.Entity entity, int offset, int limit, String format, HttpServletRequest request, boolean refine, List<String> refineFields, List<String> fieldQueries) {
|
|
588 | 696 |
try { |
589 | 697 |
|
590 |
SearchResult searchResult = searchService.search(fullQuery, null, Locale.getDefault().toString(), offset, limit); |
|
591 |
StringBuilder entitesBuilder = formatSearchResults(searchResult.getSearchResults()); |
|
698 |
SearchResult solrResults = searchService.newSearch(fullQuery, Locale.getDefault().toString(), refineFields, fieldQueries, offset, limit, format); |
|
592 | 699 |
|
593 |
StringBuilder refineBuilder = null; |
|
594 |
if(refine) { |
|
595 |
refineBuilder = new StringBuilder(); |
|
596 |
SearchResult refineResults = searchService.refine(fullQuery, null, Locale.getDefault().toString(), refineFields); |
|
597 |
formatRefineResults(refineBuilder, refineResults); |
|
598 |
} |
|
599 |
|
|
600 | 700 |
return Response.status(Response.Status.OK).entity(ResponseFormatter.createEntitiesResponse(request, entity, fullQuery.replace("\"", "\\\""), |
601 |
entitesBuilder.toString(), searchResult.getTotal()+"", offset+"", limit+"", refine, refineBuilder)).type(MediaType.APPLICATION_JSON).build();
|
|
701 |
solrResults.getSearchResults().toString(), solrResults.getTotal()+"", offset+"", limit+"", refine, SolrResultsFormatter.jsonList2Json(solrResults.getBrowseResults()))).type(MediaType.APPLICATION_JSON).build();
|
|
602 | 702 |
|
603 | 703 |
} catch (SearchServiceException sse) { |
604 | 704 |
logger.error("Fail to get " + entity.getPlural() , sse); |
... | ... | |
619 | 719 |
} |
620 | 720 |
} |
621 | 721 |
|
622 |
private static StringBuilder formatSearchResults(List<String> resultXmls) { |
|
623 |
StringBuilder entitesBuilder = new StringBuilder(); |
|
624 | 722 |
|
625 |
int i = 0; |
|
626 |
for (String xml:resultXmls) { |
|
627 |
if (i > 0) { |
|
628 |
entitesBuilder.append(","); |
|
629 |
} |
|
630 |
entitesBuilder.append(XML.toJSONObject(xml)); |
|
631 |
i++; |
|
632 |
} |
|
633 |
return entitesBuilder; |
|
634 |
} |
|
635 |
|
|
723 |
/* |
|
636 | 724 |
public static void main(String[] args) { |
637 | 725 |
String json = " {\"result\":{\"xmlns:oaf\":\"http://namespace.openaire.eu/oaf\",\"xmlns:xsi\":\"http://www.w3.o" + |
638 | 726 |
"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" + |
... | ... | |
702 | 790 |
|
703 | 791 |
JSONObject newJsonObject1 = new JSONObject(jsonObject, new String[]{"title", "description", "language", "bestlicense", "pid", "rels"}); |
704 | 792 |
|
793 |
} |
|
794 |
*/ |
|
705 | 795 |
|
796 |
public static void main(String[] args) throws IOException, CQLParseException { |
|
797 |
CqlTranslator translator = new CqlTranslatorImpl(); |
|
798 |
System.out.println(translator.toLucene("relfundinglevel0_id = ec__________::EC::FP7")); |
|
799 |
} |
|
706 | 800 |
|
707 |
} |
|
708 | 801 |
} |
Also available in: Unified diff
Added methods for single solr query for search and browse. Size does not trigger extra solr query. Refine uses field queries. Pending APIs for different entities - only the genarla resources api is currently enabled. Pending handling format for both XML and JSON. All these related to #1432 and #2479