Project

General

Profile

« Previous | Next » 

Revision 60864

API Cache

View differences:

GenericDoiMdstorePlugin.java
5 5

  
6 6
import org.apache.commons.logging.Log;
7 7
import org.apache.commons.logging.LogFactory;
8
import org.bson.Document;
8 9

  
10
import com.mongodb.BasicDBObject;
11
import com.mongodb.client.MongoCollection;
12

  
9 13
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
10 14
import eu.dnetlib.data.utils.HttpFetcher;
11 15

  
......
17 21
	protected final boolean updateRecord(final String recordId, final MdRecord record) {
18 22
		for (final String doi : record.getDois()) {
19 23
			log.debug("  Record " + record.getId() + " has doi " + doi);
20
			final String response = download(doi);
24
			final String response = obtainData(doi);
21 25
			if ((response != null) && updateDocument(record, response)) { return true; }
22 26
		}
23 27
		return false;
......
27 31

  
28 32
	abstract protected URI prepareURI(String doi) throws URISyntaxException;
29 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

  
30 54
	private String download(final String doi) {
31 55
		try {
32 56
			return HttpFetcher.fetch(prepareURI(doi));

Also available in: Unified diff