Project

General

Profile

1
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
		reconfigure();
38

    
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
				if (updateRecord(record)) {
49
					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
		touch(store);
63
	}
64

    
65
	abstract protected void reconfigure();
66

    
67
	abstract protected boolean updateRecord(MdRecord record);
68

    
69
}
(8-8/8)