Project

General

Profile

1 51020 michele.ar
package eu.dnetlib.data.mdstore.plugins;
2
3
import java.io.StringReader;
4
import java.util.Map;
5
6
import javax.xml.bind.JAXBException;
7
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10
import org.dom4j.Document;
11
import org.dom4j.DocumentException;
12
import org.dom4j.Element;
13
import org.dom4j.io.SAXReader;
14
15
import com.mongodb.BasicDBObject;
16
import com.mongodb.DBObject;
17
import com.mongodb.client.MongoCollection;
18
19
import eu.dnetlib.data.mdstore.modular.mongodb.MongoMDStore;
20
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
21
import eu.dnetlib.data.mdstore.plugins.objects.MdRecordConvertUtils;
22
import eu.dnetlib.rmi.data.MDStoreServiceException;
23
24
public abstract class MdRecordPlugin extends AbstractIstiMDStorePlugin {
25
26
	private static final Log log = LogFactory.getLog(MdRecordPlugin.class);
27
28
	@Override
29
	public final void process(final MongoMDStore store, final Map<String, String> params) throws MDStoreServiceException {
30
31
		final MongoCollection<DBObject> collPubs = store.getCollection();
32
33
		int count = 0;
34
35
		final SAXReader reader = new SAXReader();
36
37 51852 michele.ar
		reconfigure(params);
38 51020 michele.ar
39
		for (final DBObject obj : collPubs.find()) {
40
41
			try {
42
				final String recordId = obj.get("id").toString();
43
				final Document doc = reader.read(new StringReader(obj.get("body").toString()));
44
				final Element mdNode = (Element) doc.selectSingleNode("//*[local-name() = 'metadata']");
45
				final Element xmlRecord = (Element) mdNode.selectSingleNode("./record").detach();
46
				final MdRecord record = MdRecordConvertUtils.fromString(xmlRecord.asXML());
47
48 60388 michele.ar
				if (updateRecord(recordId, record)) {
49 51020 michele.ar
					final Document docPart = reader.read(new StringReader(MdRecordConvertUtils.toString(record)));
50
					mdNode.add(docPart.getRootElement());
51
					collPubs.updateOne(new BasicDBObject("id", recordId), new BasicDBObject("$set", new BasicDBObject("body", doc.asXML())));
52
					count++;
53
				}
54
55
			} catch (final DocumentException | JAXBException e) {
56
				log.warn("Problem parsing a mdstore record", e);
57
			}
58
		}
59
60
		log.info("Number of patched records: " + count);
61
62 54968 michele.ar
		resetConfiguration();
63
64 51020 michele.ar
		touch(store);
65
	}
66
67 51852 michele.ar
	abstract protected void reconfigure(Map<String, String> params);
68 51020 michele.ar
69 54968 michele.ar
	abstract protected void resetConfiguration();
70
71 60388 michele.ar
	abstract protected boolean updateRecord(String recordId, MdRecord record);
72 51020 michele.ar
73
}