1 |
49291
|
michele.ar
|
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 |
60864
|
michele.ar
|
import org.bson.Document;
|
9 |
49291
|
michele.ar
|
|
10 |
60864
|
michele.ar
|
import com.mongodb.BasicDBObject;
|
11 |
|
|
import com.mongodb.client.MongoCollection;
|
12 |
|
|
|
13 |
51020
|
michele.ar
|
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
|
14 |
51852
|
michele.ar
|
import eu.dnetlib.data.utils.HttpFetcher;
|
15 |
49291
|
michele.ar
|
|
16 |
51020
|
michele.ar
|
public abstract class GenericDoiMdstorePlugin extends MdRecordPlugin {
|
17 |
49291
|
michele.ar
|
|
18 |
|
|
private static final Log log = LogFactory.getLog(GenericDoiMdstorePlugin.class);
|
19 |
|
|
|
20 |
|
|
@Override
|
21 |
60388
|
michele.ar
|
protected final boolean updateRecord(final String recordId, final MdRecord record) {
|
22 |
51020
|
michele.ar
|
for (final String doi : record.getDois()) {
|
23 |
|
|
log.debug(" Record " + record.getId() + " has doi " + doi);
|
24 |
60864
|
michele.ar
|
final String response = obtainData(doi);
|
25 |
51020
|
michele.ar
|
if ((response != null) && updateDocument(record, response)) { return true; }
|
26 |
49291
|
michele.ar
|
}
|
27 |
51020
|
michele.ar
|
return false;
|
28 |
49291
|
michele.ar
|
}
|
29 |
|
|
|
30 |
51020
|
michele.ar
|
abstract protected boolean updateDocument(MdRecord doc, String response);
|
31 |
49291
|
michele.ar
|
|
32 |
|
|
abstract protected URI prepareURI(String doi) throws URISyntaxException;
|
33 |
|
|
|
34 |
60864
|
michele.ar
|
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 |
49291
|
michele.ar
|
private String download(final String doi) {
|
55 |
|
|
try {
|
56 |
51852
|
michele.ar
|
return HttpFetcher.fetch(prepareURI(doi));
|
57 |
49291
|
michele.ar
|
} catch (final URISyntaxException e) {
|
58 |
|
|
log.error("Error resolving doi: " + doi, e);
|
59 |
|
|
return null;
|
60 |
|
|
}
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
}
|