1
|
package eu.dnetlib.data.mdstore.plugins;
|
2
|
|
3
|
import java.net.URI;
|
4
|
import java.net.URISyntaxException;
|
5
|
|
6
|
import org.apache.commons.logging.Log;
|
7
|
import org.apache.commons.logging.LogFactory;
|
8
|
import org.bson.Document;
|
9
|
|
10
|
import com.mongodb.BasicDBObject;
|
11
|
import com.mongodb.client.MongoCollection;
|
12
|
|
13
|
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
|
14
|
import eu.dnetlib.data.utils.HttpFetcher;
|
15
|
|
16
|
public abstract class GenericDoiMdstorePlugin extends MdRecordPlugin {
|
17
|
|
18
|
private static final Log log = LogFactory.getLog(GenericDoiMdstorePlugin.class);
|
19
|
|
20
|
@Override
|
21
|
protected final boolean updateRecord(final String recordId, final MdRecord record) {
|
22
|
for (final String doi : record.getDois()) {
|
23
|
log.debug(" Record " + record.getId() + " has doi " + doi);
|
24
|
final String response = obtainData(doi);
|
25
|
if ((response != null) && updateDocument(record, response)) { return true; }
|
26
|
}
|
27
|
return false;
|
28
|
}
|
29
|
|
30
|
abstract protected boolean updateDocument(MdRecord doc, String response);
|
31
|
|
32
|
abstract protected URI prepareURI(String doi) throws URISyntaxException;
|
33
|
|
34
|
abstract protected MongoCollection<Document> getCacheCollection();
|
35
|
|
36
|
private String obtainData(final String doi) {
|
37
|
final MongoCollection<Document> cacheColl = getCacheCollection();
|
38
|
|
39
|
final Document row = cacheColl.find(new BasicDBObject("doi", doi)).first();
|
40
|
if (row != null) {
|
41
|
return row.get("data") != null ? row.get("data").toString() : null;
|
42
|
} else {
|
43
|
final String res = download(doi);
|
44
|
|
45
|
final Document doc = new Document();
|
46
|
doc.append("doi", doi);
|
47
|
doc.append("data", res);
|
48
|
cacheColl.insertOne(doc);
|
49
|
|
50
|
return res;
|
51
|
}
|
52
|
}
|
53
|
|
54
|
private String download(final String doi) {
|
55
|
try {
|
56
|
return HttpFetcher.fetch(prepareURI(doi));
|
57
|
} catch (final URISyntaxException e) {
|
58
|
log.error("Error resolving doi: " + doi, e);
|
59
|
return null;
|
60
|
}
|
61
|
}
|
62
|
|
63
|
}
|