1
|
package eu.dnetlib.data.mdstore.plugins;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.Map;
|
5
|
|
6
|
import org.apache.commons.logging.Log;
|
7
|
import org.apache.commons.logging.LogFactory;
|
8
|
import org.bson.Document;
|
9
|
import org.dom4j.io.SAXReader;
|
10
|
|
11
|
import com.google.gson.Gson;
|
12
|
import com.mongodb.BasicDBObject;
|
13
|
import com.mongodb.DBObject;
|
14
|
import com.mongodb.client.MongoCollection;
|
15
|
|
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;
|
21
|
import eu.dnetlib.rmi.data.MDStoreServiceException;
|
22
|
|
23
|
public class FreezeMDStorePlugin extends AbstractIstiMDStorePlugin {
|
24
|
|
25
|
private static final Log log = LogFactory.getLog(FreezeMDStorePlugin.class);
|
26
|
|
27
|
@Override
|
28
|
protected void process(final MongoMDStore store, final Map<String, String> params) throws MDStoreServiceException {
|
29
|
|
30
|
final MongoCollection<Document> freezeColl = getDao().getDb().getCollection(params.get("collection"));
|
31
|
|
32
|
freezeColl.drop();
|
33
|
|
34
|
final Gson gson = new Gson();
|
35
|
final SAXReader reader = new SAXReader();
|
36
|
|
37
|
for (final DBObject obj : store.getCollection().find()) {
|
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(record));
|
50
|
doc.append("csv", CsvConverter.asCsv(record));
|
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
|
}
|
57
|
}
|
58
|
|
59
|
freezeColl.createIndex(new BasicDBObject("id", 1));
|
60
|
|
61
|
}
|
62
|
|
63
|
}
|