1
|
package eu.dnetlib.enabling.resultset;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.List;
|
5
|
|
6
|
import com.google.gson.Gson;
|
7
|
import eu.dnetlib.enabling.resultset.listener.ResultSetListener;
|
8
|
import eu.dnetlib.enabling.resultset.registry.ResultSetRegistry;
|
9
|
import eu.dnetlib.rmi.common.ResultSetException;
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
11
|
import org.springframework.stereotype.Controller;
|
12
|
import org.springframework.web.bind.annotation.PathVariable;
|
13
|
import org.springframework.web.bind.annotation.RequestMapping;
|
14
|
import org.springframework.web.bind.annotation.RequestMethod;
|
15
|
import org.springframework.web.bind.annotation.ResponseBody;
|
16
|
|
17
|
@Controller
|
18
|
public class ResultSetController {
|
19
|
|
20
|
@Autowired
|
21
|
private ResultSetRegistry resultSetRegistry;
|
22
|
|
23
|
@RequestMapping(value = "/rs/{rsId}/next/{n}", method = RequestMethod.GET)
|
24
|
public @ResponseBody ResultSetResponse getNextResults(@PathVariable(value = "rsId") final String rsId, @PathVariable(value = "n") final int n)
|
25
|
throws ResultSetException {
|
26
|
final ResultSetListener<?> rs = this.resultSetRegistry.getResultSetById(rsId);
|
27
|
|
28
|
final int start = rs.getCount();
|
29
|
|
30
|
final List<String> elements = new ArrayList<>();
|
31
|
|
32
|
final Gson gson = new Gson();
|
33
|
while (rs.hasNext() && elements.size() < n) {
|
34
|
elements.add(gson.toJson(rs.next()));
|
35
|
}
|
36
|
|
37
|
final int from = start;
|
38
|
final int to = rs.getCount();
|
39
|
final boolean end = !rs.hasNext();
|
40
|
final int total = rs.getTotal();
|
41
|
|
42
|
return new ResultSetResponse(from, to, end, total, elements);
|
43
|
}
|
44
|
|
45
|
@RequestMapping(value = "/rs/{rsId}/info", method = RequestMethod.GET)
|
46
|
public @ResponseBody ResultSetInfo getRsInfo(@PathVariable(value = "rsId") final String rsId)
|
47
|
throws ResultSetException {
|
48
|
final ResultSetListener<?> rs = this.resultSetRegistry.getResultSetById(rsId);
|
49
|
return new ResultSetInfo(rsId, rs);
|
50
|
}
|
51
|
|
52
|
}
|