Revision 51020
Added by Michele Artini about 6 years ago
FreezeMDStorePlugin.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.mdstore.plugins; |
2 | 2 |
|
3 |
import java.io.StringReader; |
|
3 | 4 |
import java.util.Map; |
4 | 5 |
|
6 |
import org.apache.commons.logging.Log; |
|
7 |
import org.apache.commons.logging.LogFactory; |
|
5 | 8 |
import org.bson.Document; |
9 |
import org.dom4j.io.SAXReader; |
|
6 | 10 |
|
11 |
import com.google.gson.Gson; |
|
7 | 12 |
import com.mongodb.BasicDBObject; |
8 | 13 |
import com.mongodb.DBObject; |
9 | 14 |
import com.mongodb.client.MongoCollection; |
10 | 15 |
|
11 | 16 |
import eu.dnetlib.data.mdstore.modular.mongodb.MongoMDStore; |
17 |
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord; |
|
18 |
import eu.dnetlib.data.mdstore.plugins.objects.MdRecordConvertUtils; |
|
19 |
import eu.dnetlib.data.utils.BibTexConverter; |
|
20 |
import eu.dnetlib.data.utils.CsvConverter; |
|
12 | 21 |
import eu.dnetlib.rmi.data.MDStoreServiceException; |
13 | 22 |
|
14 | 23 |
public class FreezeMDStorePlugin extends AbstractIstiMDStorePlugin { |
15 | 24 |
|
25 |
private static final Log log = LogFactory.getLog(FreezeMDStorePlugin.class); |
|
26 |
|
|
16 | 27 |
@Override |
17 | 28 |
protected void process(final MongoMDStore store, final Map<String, String> params) throws MDStoreServiceException { |
18 | 29 |
|
... | ... | |
20 | 31 |
|
21 | 32 |
freezeColl.drop(); |
22 | 33 |
|
34 |
final Gson gson = new Gson(); |
|
35 |
final SAXReader reader = new SAXReader(); |
|
36 |
|
|
23 | 37 |
for (final DBObject obj : store.getCollection().find()) { |
24 |
final Document doc = new Document(); |
|
25 |
doc.append("id", obj.get("id")); |
|
26 |
doc.append("body", obj.get("body")); |
|
27 |
freezeColl.insertOne(doc); |
|
38 |
final String id = obj.get("id").toString(); |
|
39 |
try { |
|
40 |
final Document doc = new Document(); |
|
41 |
|
|
42 |
final String xml = reader.read(new StringReader(obj.get("body").toString())) |
|
43 |
.selectSingleNode("//*[local-name() = 'metadata']/record") |
|
44 |
.asXML(); |
|
45 |
|
|
46 |
final MdRecord record = MdRecordConvertUtils.fromString(xml); |
|
47 |
doc.append("id", id); |
|
48 |
doc.append("xml", xml); |
|
49 |
doc.append("bibtex", BibTexConverter.asBibTex(obj.get("body").toString())); |
|
50 |
doc.append("csv", CsvConverter.asCsv(obj.get("body").toString())); |
|
51 |
doc.append("json", gson.toJson(record)); |
|
52 |
|
|
53 |
freezeColl.insertOne(doc); |
|
54 |
} catch (final Exception e) { |
|
55 |
log.error("Error saving record " + id, e); |
|
56 |
} |
|
28 | 57 |
} |
29 | 58 |
|
30 | 59 |
freezeColl.createIndex(new BasicDBObject("id", 1)); |
Also available in: Unified diff
refactoring using jaxb