1
|
package eu.dnetlib.data.collector.plugins.fairsharing;
|
2
|
|
3
|
import java.util.HashMap;
|
4
|
import java.util.Iterator;
|
5
|
import java.util.Map;
|
6
|
import java.util.Queue;
|
7
|
import java.util.concurrent.PriorityBlockingQueue;
|
8
|
|
9
|
import org.apache.commons.io.IOUtils;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
13
|
import org.apache.http.client.methods.HttpGet;
|
14
|
import org.apache.http.impl.client.CloseableHttpClient;
|
15
|
import org.apache.http.impl.client.HttpClients;
|
16
|
import org.dom4j.Document;
|
17
|
import org.dom4j.DocumentHelper;
|
18
|
import org.dom4j.Element;
|
19
|
import org.dom4j.Node;
|
20
|
import org.json.JSONObject;
|
21
|
import org.json.XML;
|
22
|
|
23
|
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
|
24
|
|
25
|
public class FairSharingIterator implements Iterator<String> {
|
26
|
|
27
|
private static final Log log = LogFactory.getLog(FairSharingIterator.class); // NOPMD by marko on 11/24/08 5:02 PM
|
28
|
|
29
|
private final Queue<String> queue = new PriorityBlockingQueue<>();
|
30
|
|
31
|
private final String baseUrl;
|
32
|
private final String authCode;
|
33
|
private final int pageSize;
|
34
|
private final Map<String, Element> savedOrgs;
|
35
|
|
36
|
private String nextUrl;
|
37
|
private boolean started;
|
38
|
|
39
|
public FairSharingIterator(final String baseUrl, final String authCode, final int pageSize) {
|
40
|
this.baseUrl = baseUrl;
|
41
|
this.authCode = authCode;
|
42
|
this.pageSize = pageSize;
|
43
|
this.started = false;
|
44
|
this.savedOrgs = new HashMap<>();
|
45
|
}
|
46
|
|
47
|
private void verifyStarted() {
|
48
|
if (!this.started) {
|
49
|
this.started = true;
|
50
|
try {
|
51
|
final String url = baseUrl + "/fairsharing_records/?page%5Bnumber%5D=1&page%5Bsize%5D=" + pageSize;
|
52
|
this.nextUrl = downloadPage(url);
|
53
|
} catch (final CollectorServiceException e) {
|
54
|
throw new RuntimeException(e);
|
55
|
}
|
56
|
}
|
57
|
}
|
58
|
|
59
|
@Override
|
60
|
public boolean hasNext() {
|
61
|
synchronized (queue) {
|
62
|
verifyStarted();
|
63
|
return !queue.isEmpty();
|
64
|
}
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
public String next() {
|
69
|
synchronized (queue) {
|
70
|
verifyStarted();
|
71
|
final String res = queue.poll();
|
72
|
while (queue.isEmpty() && nextUrl != null && !nextUrl.isEmpty()) {
|
73
|
try {
|
74
|
nextUrl = downloadPage(nextUrl);
|
75
|
} catch (final CollectorServiceException e) {
|
76
|
throw new RuntimeException(e);
|
77
|
}
|
78
|
}
|
79
|
return res;
|
80
|
}
|
81
|
}
|
82
|
|
83
|
@Override
|
84
|
public void remove() {}
|
85
|
|
86
|
private String downloadPage(final String url) throws CollectorServiceException {
|
87
|
log.debug("Fetching url: " + url);
|
88
|
|
89
|
final HttpGet req = new HttpGet(url);
|
90
|
req.addHeader("Accept", "application/json");
|
91
|
req.addHeader("Content-Type", "application/json");
|
92
|
req.addHeader("Authorization", "Bearer " + authCode);
|
93
|
|
94
|
try (final CloseableHttpClient client = HttpClients.createDefault()) {
|
95
|
try (final CloseableHttpResponse response = client.execute(req)) {
|
96
|
final String content = IOUtils.toString(response.getEntity().getContent());
|
97
|
final JSONObject obj = new JSONObject(content);
|
98
|
|
99
|
for (final Object record : obj.getJSONArray("data")) {
|
100
|
final String xml = XML.toString(record, "record");
|
101
|
final Document doc = DocumentHelper.parseText(xml);
|
102
|
for (final Object o : doc.selectNodes("//grants")) {
|
103
|
addOrganizationInfo((Element) o);
|
104
|
}
|
105
|
fix_null_values(doc);
|
106
|
queue.add(doc.asXML());
|
107
|
}
|
108
|
|
109
|
final JSONObject links = obj.getJSONObject("links");
|
110
|
|
111
|
return links.isNull("next") ? null : links.getString("next");
|
112
|
}
|
113
|
} catch (final Exception e) {
|
114
|
throw new CollectorServiceException("Error perfoming call for page", e);
|
115
|
}
|
116
|
}
|
117
|
|
118
|
private void addOrganizationInfo(final Element n) throws CollectorServiceException {
|
119
|
final String orgId = n.valueOf("./organisation_id");
|
120
|
|
121
|
if (!savedOrgs.containsKey(orgId)) {
|
122
|
final String url = baseUrl + "/organisations/" + orgId;
|
123
|
final HttpGet req = new HttpGet(url);
|
124
|
req.addHeader("Accept", "application/json");
|
125
|
req.addHeader("Content-Type", "application/json");
|
126
|
req.addHeader("Authorization", "Bearer " + authCode);
|
127
|
|
128
|
try (final CloseableHttpClient client = HttpClients.createDefault()) {
|
129
|
try (final CloseableHttpResponse response = client.execute(req)) {
|
130
|
final String content = IOUtils.toString(response.getEntity().getContent());
|
131
|
final JSONObject obj = new JSONObject(content);
|
132
|
|
133
|
final Document doc = DocumentHelper.parseText(XML.toString(obj.getJSONObject("data"), "extended_info"));
|
134
|
for (final Object o : doc.selectNodes("//organisation_links")) {
|
135
|
((Node) o).detach();
|
136
|
}
|
137
|
fix_null_values(doc);
|
138
|
|
139
|
savedOrgs.put(orgId, doc.getRootElement());
|
140
|
}
|
141
|
} catch (final Exception e) {
|
142
|
throw new CollectorServiceException("Error perfoming call for organization", e);
|
143
|
}
|
144
|
}
|
145
|
|
146
|
n.add(savedOrgs.get(orgId).createCopy());
|
147
|
}
|
148
|
|
149
|
private void fix_null_values(final Document doc) {
|
150
|
for (final Object o : doc.selectNodes("//*[text() = 'null']")) {
|
151
|
((Element) o).setText("");
|
152
|
}
|
153
|
}
|
154
|
|
155
|
}
|