1
|
package eu.dnetlib.data.collector.plugins.datasets;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.List;
|
5
|
|
6
|
import org.apache.commons.logging.Log;
|
7
|
import org.apache.commons.logging.LogFactory;
|
8
|
|
9
|
import com.google.gson.JsonArray;
|
10
|
import com.google.gson.JsonElement;
|
11
|
import com.google.gson.JsonObject;
|
12
|
import com.google.gson.JsonParser;
|
13
|
|
14
|
public class ElasticSearchResponse {
|
15
|
|
16
|
/** The logger. */
|
17
|
private static final Log log = LogFactory.getLog(ElasticSearchResponse.class);
|
18
|
private long total;
|
19
|
private List<String> xmlRecords;
|
20
|
|
21
|
public static ElasticSearchResponse createNewResponse(final String response) {
|
22
|
ElasticSearchResponse item = new ElasticSearchResponse();
|
23
|
|
24
|
if (response == null) {
|
25
|
log.fatal("Error: null elasticsearch reponse");
|
26
|
return null;
|
27
|
|
28
|
}
|
29
|
JsonElement jElement = new JsonParser().parse(response);
|
30
|
JsonObject jobject = jElement.getAsJsonObject();
|
31
|
if (jobject.has("hits")) {
|
32
|
|
33
|
item.setTotal(jobject.get("hits").getAsJsonObject().get("total").getAsLong());
|
34
|
|
35
|
JsonElement hits = ((JsonObject) jobject.get("hits")).get("hits");
|
36
|
|
37
|
JsonArray hitsObject = hits.getAsJsonArray();
|
38
|
|
39
|
List<String> records = new ArrayList<String>();
|
40
|
|
41
|
for (JsonElement elem : hitsObject) {
|
42
|
JsonObject _source = (JsonObject) ((JsonObject) elem).get("_source");
|
43
|
String xml = _source.get("xml").getAsString();
|
44
|
records.add(xml);
|
45
|
}
|
46
|
item.setXmlRecords(records);
|
47
|
return item;
|
48
|
}
|
49
|
return null;
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* @return the xmlRecords
|
54
|
*/
|
55
|
public List<String> getXmlRecords() {
|
56
|
return xmlRecords;
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* @param xmlRecords
|
61
|
* the xmlRecords to set
|
62
|
*/
|
63
|
public void setXmlRecords(final List<String> xmlRecords) {
|
64
|
this.xmlRecords = xmlRecords;
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* @return the total
|
69
|
*/
|
70
|
public long getTotal() {
|
71
|
return total;
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* @param total
|
76
|
* the total to set
|
77
|
*/
|
78
|
public void setTotal(final long total) {
|
79
|
this.total = total;
|
80
|
}
|
81
|
|
82
|
}
|