Revision 51020
Added by Michele Artini about 6 years ago
GenericDoiMdstorePlugin.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.mdstore.plugins; |
2 | 2 |
|
3 |
import java.io.StringReader; |
|
4 | 3 |
import java.net.URI; |
5 | 4 |
import java.net.URISyntaxException; |
6 | 5 |
import java.security.KeyManagementException; |
7 | 6 |
import java.security.KeyStoreException; |
8 | 7 |
import java.security.NoSuchAlgorithmException; |
9 |
import java.util.Map; |
|
10 | 8 |
import java.util.concurrent.TimeUnit; |
11 | 9 |
|
12 | 10 |
import org.apache.commons.logging.Log; |
... | ... | |
14 | 12 |
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
15 | 13 |
import org.apache.http.impl.client.HttpClientBuilder; |
16 | 14 |
import org.apache.http.ssl.SSLContextBuilder; |
17 |
import org.dom4j.Document; |
|
18 |
import org.dom4j.DocumentException; |
|
19 |
import org.dom4j.Node; |
|
20 |
import org.dom4j.io.SAXReader; |
|
21 | 15 |
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
22 | 16 |
import org.springframework.web.client.RestTemplate; |
23 | 17 |
|
24 |
import com.mongodb.BasicDBObject; |
|
25 |
import com.mongodb.DBObject; |
|
26 |
import com.mongodb.client.MongoCollection; |
|
18 |
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord; |
|
27 | 19 |
|
28 |
import eu.dnetlib.data.mdstore.modular.mongodb.MongoMDStore; |
|
29 |
import eu.dnetlib.rmi.data.MDStoreServiceException; |
|
20 |
public abstract class GenericDoiMdstorePlugin extends MdRecordPlugin { |
|
30 | 21 |
|
31 |
public abstract class GenericDoiMdstorePlugin extends AbstractIstiMDStorePlugin { |
|
32 |
|
|
33 | 22 |
private static final Log log = LogFactory.getLog(GenericDoiMdstorePlugin.class); |
34 | 23 |
|
35 | 24 |
private static final int MAX_NUMBER_OF_ATTEMPTS = 10; |
... | ... | |
57 | 46 |
.build()); |
58 | 47 |
|
59 | 48 |
@Override |
60 |
public void process(final MongoMDStore store, final Map<String, String> params) throws MDStoreServiceException { |
|
61 |
|
|
62 |
final MongoCollection<DBObject> collPubs = store.getCollection(); |
|
63 |
|
|
64 |
int count = 0; |
|
65 |
|
|
66 |
for (final DBObject obj : collPubs.find()) { |
|
67 |
|
|
68 |
try { |
|
69 |
final String recordId = obj.get("id").toString(); |
|
70 |
final Document doc = (new SAXReader()).read(new StringReader(obj.get("body").toString())); |
|
71 |
for (final Object o : doc.selectNodes("//*[local-name()='alternateIdentifier' and @alternateIdentifierType='doi']")) { |
|
72 |
final String doi = ((Node) o).getText().trim(); |
|
73 |
|
|
74 |
log.debug(" Record " + recordId + " has doi " + doi); |
|
75 |
final String response = download(doi); |
|
76 |
if ((response != null) && updateDocument(doc, response)) { |
|
77 |
collPubs.updateOne(new BasicDBObject("id", recordId), new BasicDBObject("$set", new BasicDBObject("body", doc.asXML()))); |
|
78 |
count++; |
|
79 |
} |
|
80 |
} |
|
81 |
} catch (final DocumentException e) { |
|
82 |
log.warn("Problem parsing a mdstore record"); |
|
83 |
} |
|
49 |
protected final boolean updateRecord(final MdRecord record) { |
|
50 |
for (final String doi : record.getDois()) { |
|
51 |
log.debug(" Record " + record.getId() + " has doi " + doi); |
|
52 |
final String response = download(doi); |
|
53 |
if ((response != null) && updateDocument(record, response)) { return true; } |
|
84 | 54 |
} |
85 |
|
|
86 |
log.info("Number of patched records: " + count); |
|
87 |
|
|
88 |
touch(store); |
|
55 |
return false; |
|
89 | 56 |
} |
90 | 57 |
|
91 |
abstract protected boolean updateDocument(Document doc, String response);
|
|
58 |
abstract protected boolean updateDocument(MdRecord doc, String response);
|
|
92 | 59 |
|
93 | 60 |
abstract protected URI prepareURI(String doi) throws URISyntaxException; |
94 | 61 |
|
... | ... | |
116 | 83 |
} |
117 | 84 |
} |
118 | 85 |
} |
86 |
|
|
119 | 87 |
} |
Also available in: Unified diff
refactoring using jaxb