1
|
package eu.dnetlib.dli.collector.plugin;
|
2
|
|
3
|
import com.google.gson.JsonArray;
|
4
|
import com.google.gson.JsonElement;
|
5
|
import com.google.gson.JsonObject;
|
6
|
import com.google.gson.JsonParser;
|
7
|
import eu.dnetlib.dli.resolver.model.DLIObjectProvenance;
|
8
|
import eu.dnetlib.dli.resolver.model.DLIObjectRelation;
|
9
|
import eu.dnetlib.dli.resolver.model.DLIResolvedObject;
|
10
|
import eu.dnetlib.pid.resolver.AbstractPIDResolver;
|
11
|
import eu.dnetlib.pid.resolver.model.ObjectType;
|
12
|
import eu.dnetlib.pid.resolver.model.PID;
|
13
|
import org.antlr.stringtemplate.NoIndentWriter;
|
14
|
import org.antlr.stringtemplate.StringTemplate;
|
15
|
import org.apache.commons.logging.Log;
|
16
|
import org.apache.commons.logging.LogFactory;
|
17
|
|
18
|
import java.io.StringWriter;
|
19
|
import java.util.ArrayList;
|
20
|
import java.util.Collections;
|
21
|
import java.util.Iterator;
|
22
|
import java.util.List;
|
23
|
|
24
|
public class CrossRefIterator implements Iterator<String> {
|
25
|
|
26
|
private static final Log log = LogFactory.getLog(CrossRefIterator.class);
|
27
|
|
28
|
private int current = 0;
|
29
|
private int total =0;
|
30
|
private String nextCursor;
|
31
|
|
32
|
|
33
|
private final StringTemplate resolverSerializer;
|
34
|
|
35
|
private List<JsonElement> buffer = new ArrayList<>();
|
36
|
private String nextItem;
|
37
|
|
38
|
|
39
|
public CrossRefIterator(final StringTemplate resolverSerializer) {
|
40
|
this.resolverSerializer = resolverSerializer;
|
41
|
nextItem = calculateNextItem();
|
42
|
}
|
43
|
|
44
|
|
45
|
@Override
|
46
|
public boolean hasNext() {
|
47
|
return nextItem != null;
|
48
|
}
|
49
|
|
50
|
@Override
|
51
|
public String next() {
|
52
|
final String tmp = nextItem;
|
53
|
nextItem = calculateNextItem();
|
54
|
current ++;
|
55
|
return tmp;
|
56
|
}
|
57
|
|
58
|
|
59
|
private String calculateNextItem() {
|
60
|
|
61
|
if (buffer.isEmpty())
|
62
|
{
|
63
|
if (!requestNextBuffer()) return null;
|
64
|
}
|
65
|
if (buffer.isEmpty()) return null;
|
66
|
final JsonObject item = buffer.remove(0).getAsJsonObject();
|
67
|
try {
|
68
|
final DLIResolvedObject object = new DLIResolvedObject();
|
69
|
final String id = item.get("Source").getAsJsonObject().get("Identifier").getAsJsonObject().get("ID").getAsString();
|
70
|
final String id_type = item.get("Source").getAsJsonObject().get("Identifier").getAsJsonObject().get("IDScheme").getAsString();
|
71
|
object.setPid(id);
|
72
|
object.setPidType(id_type);
|
73
|
final JsonElement licenseURL = item.get("LicenseURL");
|
74
|
|
75
|
|
76
|
|
77
|
final String type = item.get("Source").getAsJsonObject().get("Type").getAsJsonObject().get("Name").isJsonNull()?"unknownw":item.get("Source").getAsJsonObject().get("Type").getAsJsonObject().get("Name").getAsString();
|
78
|
object.setType(type.equals("literature") ? ObjectType.publication : ObjectType.dataset);
|
79
|
final String relType = item.get("RelationshipType").getAsJsonObject().get("Name").getAsString();
|
80
|
object.setDatasourceProvenance(Collections.singletonList(new DLIObjectProvenance().setDatasource("Crossref").setDatasourceId("dli_________::crossref")));
|
81
|
DLIObjectRelation relation = new DLIObjectRelation();
|
82
|
relation.setRelationSemantics(relType);
|
83
|
final String t_id = item.get("Target").getAsJsonObject().get("Identifier").getAsJsonObject().get("ID").getAsString();
|
84
|
final String t_id_type = item.get("Target").getAsJsonObject().get("Identifier").getAsJsonObject().get("IDScheme").getAsString();
|
85
|
|
86
|
final String t_type = item.get("Target").getAsJsonObject().get("Type").getAsJsonObject().get("Name").isJsonNull()?"unknown":item.get("Target").getAsJsonObject().get("Type").getAsJsonObject().get("Name").getAsString();
|
87
|
|
88
|
if (licenseURL!= null && !licenseURL.isJsonNull())
|
89
|
relation.setLicense(licenseURL.getAsString());
|
90
|
relation.setTargetPID(new PID(t_id, t_id_type));
|
91
|
relation.setTargetType(t_type.equals("literature") ? ObjectType.publication : ObjectType.dataset);
|
92
|
object.setRelations(Collections.singletonList(relation));
|
93
|
relation.setCompletionStatus("incomplete");
|
94
|
relation.setRelationProvenance(Collections.singletonList(new DLIObjectProvenance().setDatasource("Crossref").setDatasourceId("dli_________::crossref")));
|
95
|
try {
|
96
|
StringWriter writer = new StringWriter(16);
|
97
|
NoIndentWriter out = new NoIndentWriter(writer);
|
98
|
resolverSerializer.removeAttribute("object");
|
99
|
resolverSerializer.setAttribute("object", object);
|
100
|
resolverSerializer.write(out);
|
101
|
String result = writer.toString();
|
102
|
writer.close();
|
103
|
return result;
|
104
|
} catch (Throwable e) {
|
105
|
throw new RuntimeException(e);
|
106
|
}
|
107
|
}catch (Throwable e) {
|
108
|
System.out.println(item.toString());
|
109
|
throw new RuntimeException(e);
|
110
|
}
|
111
|
}
|
112
|
|
113
|
private boolean requestNextBuffer() {
|
114
|
String templateUrlWithCursor = "https://api.eventdata.crossref.org/v1/events/scholix?mailto=sandro.labruzzo@isti.cnr.it&rows=1000&cursor=%s";
|
115
|
String templateUrl = "https://api.eventdata.crossref.org/v1/events/scholix?mailto=sandro.labruzzo@isti.cnr.it&rows=1000";
|
116
|
final String s = AbstractPIDResolver.requestURL(nextCursor == null ? templateUrl : String.format(templateUrlWithCursor, nextCursor), 30, 2);
|
117
|
if (s!= null)
|
118
|
{
|
119
|
JsonElement jElement = new JsonParser().parse(s);
|
120
|
if (jElement.getAsJsonObject().has("message"))
|
121
|
{
|
122
|
JsonObject message = jElement.getAsJsonObject().get("message").getAsJsonObject();
|
123
|
if (message.has("next-cursor") && !message.get("next-cursor").isJsonNull())
|
124
|
nextCursor = message.get("next-cursor").getAsString();
|
125
|
else
|
126
|
nextCursor = null;
|
127
|
total = message.get("total-results").getAsInt();
|
128
|
final JsonArray items = message.get("link-packages").getAsJsonArray();
|
129
|
if (items== null || items.size() ==0) {
|
130
|
return requestNextBuffer();
|
131
|
}
|
132
|
|
133
|
items.forEach(buffer::add);
|
134
|
return nextCursor!=null;
|
135
|
}
|
136
|
}
|
137
|
return false;
|
138
|
}
|
139
|
|
140
|
public int getTotal() {
|
141
|
return total;
|
142
|
}
|
143
|
|
144
|
public void setTotal(int total) {
|
145
|
this.total = total;
|
146
|
}
|
147
|
|
148
|
public int getCurrent() {
|
149
|
return current;
|
150
|
}
|
151
|
|
152
|
public void setCurrent(int current) {
|
153
|
this.current = current;
|
154
|
}
|
155
|
}
|