Project

General

Profile

1
package eu.dnetlib.functionality.modular.ui.patcheditor.record;
2

    
3
import com.mongodb.client.MongoCollection;
4
import eu.dnetlib.data.mdstore.modular.connector.MDStoreManagerInfo;
5
import eu.dnetlib.data.mdstore.modular.mongodb.MDStoreTransactionManagerImpl;
6
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
7
import eu.dnetlib.functionality.modular.ui.patcheditor.exceptions.PatchEditorException;
8
import eu.dnetlib.miscutils.datetime.DateUtils;
9
import eu.dnetlib.rmi.data.MDStoreServiceException;
10
import eu.dnetlib.rmi.enabling.ISLookUpException;
11
import eu.dnetlib.rmi.enabling.ISLookUpService;
12
import java.io.StringReader;
13
import java.util.List;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.bson.Document;
17
import org.dom4j.Element;
18
import org.dom4j.io.SAXReader;
19
import org.springframework.beans.factory.annotation.Autowired;
20

    
21
public class PatchManagerImpl implements PatchManager {
22

    
23
	@Autowired
24
	private UniqueServiceLocator serviceLocator;
25

    
26
	@Autowired
27
	private MDStoreTransactionManagerImpl mdstoreTransactionManager;
28

    
29
	private final SAXReader reader = new SAXReader();
30

    
31
	private static final Log log = LogFactory.getLog(PatchManager.class); // NOPMD by marko on 11/24/08 5:02 PM
32

    
33
	@Override
34
	public void commit(PatchedRecord record) throws PatchEditorException {
35

    
36
		try {
37
			final String patchMdStoreId = getPatchMDStoreId(record.getRepositoryId());
38
			final MongoCollection<Document> collection = getPatchMdStoreMongoCollection(patchMdStoreId);
39
			final Document mongoQuery = new Document().append("id", record.getId());
40
			final Document store_patch = collection.find(mongoQuery).first();
41
			if (store_patch == null) {
42
				final Document patched_document = mongoQuery
43
						.append("body", record.asXML())
44
						.append("timestamp", System.currentTimeMillis());
45
				collection.insertOne(patched_document);
46
			} else {
47
				final String oldPatch = store_patch.getString("body");
48

    
49
				collection.findOneAndUpdate(mongoQuery, new Document("$set", new Document("body", mergePatches(record.asXML(), oldPatch))));
50
			}
51

    
52
			log.info("patchMdStoreId" + patchMdStoreId);
53
		} catch (final Throwable e) {
54
			throw new PatchEditorException(e);
55
		}
56

    
57
	}
58

    
59
	public String mergePatches(String newPatch, final String oldPatch) {
60
		try {
61
			final org.dom4j.Document newDoc = reader.read(new StringReader(newPatch));
62

    
63
			final org.dom4j.Document oldDoc = reader.read(new StringReader(oldPatch));
64
			final Element e = (Element) oldDoc.selectSingleNode("//patches");
65
			for (final Object o : newDoc.selectNodes("//patch")) {
66
				e.add(((Element) o).createCopy());
67
			}
68
			oldDoc.selectSingleNode("//*[local-name()='dateOfCollection']").setText(DateUtils.now_ISO8601());
69
			return oldDoc.asXML();
70
		} catch (final Exception e) {
71
			return newPatch;
72
		}
73
	}
74

    
75
	private String getPatchMDStoreId(final String repositoryId) throws ISLookUpException {
76

    
77
		final String getPatchMdstoreIdQuery = "for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType') where "
78
				+ "($x//DATASOURCE/@id/string()='%s') return $x//PARAM[./@name='patchMdstoreId']/text()";
79

    
80
		final ISLookUpService lookUpClient = serviceLocator.getService(ISLookUpService.class);
81

    
82
		final List<String> patchMdStoreIds = lookUpClient.quickSearchProfile(String.format(getPatchMdstoreIdQuery, repositoryId));
83

    
84
		if (patchMdStoreIds == null || patchMdStoreIds.size() != 1) { throw new RuntimeException(
85
				"Unexpected number of result executing query " + String.format(getPatchMdstoreIdQuery, patchMdStoreIds.get(0)) + " expected: 1"); }
86

    
87
		return patchMdStoreIds.get(0);
88
	}
89

    
90
	private MongoCollection<Document> getPatchMdStoreMongoCollection(final String mdStoreId) throws MDStoreServiceException {
91

    
92
		final MDStoreManagerInfo info = mdstoreTransactionManager.getInfoForCurrentMdStore(mdStoreId);
93
		return mdstoreTransactionManager.getDb().getCollection(info.getCurrentId());
94
	}
95

    
96
}
(5-5/8)