Project

General

Profile

1
package eu.dnetlib.msro.openaireplus.workflows.nodes.contexts;
2

    
3
import java.io.StringReader;
4
import java.util.List;
5
import javax.annotation.Resource;
6

    
7
import com.googlecode.sarasvati.Arc;
8
import com.googlecode.sarasvati.NodeToken;
9
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
10
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
11
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
12
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
13
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.dom4j.Document;
17
import org.dom4j.DocumentException;
18
import org.dom4j.Element;
19
import org.dom4j.Node;
20
import org.dom4j.io.SAXReader;
21

    
22
public class SaveContextProfileJobNode extends SimpleJobNode {
23

    
24
	private String contextObj;
25

    
26
	private static final Log log = LogFactory.getLog(SaveContextProfileJobNode.class);
27

    
28
	@Resource
29
	private UniqueServiceLocator serviceLocator;
30

    
31
	@Override
32
	protected String execute(final NodeToken token) throws Exception {
33
		final ContextDesc desc = (ContextDesc) token.getEnv().getTransientAttribute(contextObj);
34

    
35
		final String xquery = "for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType') " + "where $x//CONFIGURATION/context[@id='"
36
				+ desc.getId() + "' " + "and @type='" + desc.getType() + "'] " + "return $x";
37
		List<String> list = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery);
38

    
39
		if (list.isEmpty()) {
40
			registerNewProfile(desc);
41
		} else {
42
			updateProfile(list.get(0), desc);
43
		}
44

    
45
		return Arc.DEFAULT_ARC;
46
	}
47

    
48
	private boolean updateProfile(final String profile, final ContextDesc desc) throws DocumentException, ISRegistryException {
49
		final SAXReader reader = new SAXReader();
50
		final Document doc = reader.read(new StringReader(profile));
51
		final String profId = doc.valueOf("//HEADER/RESOURCE_IDENTIFIER/@value");
52
		final Element ctxElem = (Element) doc.selectSingleNode("//CONFIGURATION/context[@id='" + desc.getId() + "' and @type='" + desc.getType() + "']");
53
		updateContextParams(ctxElem, desc);
54

    
55
		for (ContextPart cat : desc.getCategories().values()) {
56
			final Node catElem = ctxElem.selectSingleNode("./category[@id='" + cat.getId() + "']");
57
			if (catElem != null) {
58
				catElem.detach();
59
			}
60
			ctxElem.add(cat.asDomElement("category"));
61
		}
62
		log.info("updating profile context " + desc.getId());
63
		String xml = doc.asXML();
64
		log.debug(xml);
65
		return serviceLocator.getService(ISRegistryService.class).updateProfile(profId, xml, "ContextDSResourceType");
66
	}
67

    
68
	private void updateContextParams(final Element ctxElement, ContextDesc desc) {
69
		//removing old PARAMs
70
		List<Node> oldParams = ctxElement.selectNodes("param");
71
		for (Node n : oldParams) {
72
			n.detach();
73
		}
74
		//adding new params
75
		for (Element param : desc.getParamsAsElements()) {
76
			ctxElement.add(param);
77
		}
78
	}
79

    
80
	private String registerNewProfile(final ContextDesc desc) throws DocumentException, ISRegistryException {
81
		final SAXReader reader = new SAXReader();
82
		final Document doc = reader.read(getClass().getResourceAsStream(
83
				"/eu/dnetlib/msro/openaireplus/workflows/repo-hi/entityreg-contexts/xml/contextProfile.xml"));
84
		((Element) doc.selectSingleNode("//CONFIGURATION")).add(desc.asDomElement());
85
		log.info("registering profile context " + desc.getId());
86
		String xml = doc.asXML();
87
		log.debug(xml);
88
		return serviceLocator.getService(ISRegistryService.class).registerProfile(xml);
89
	}
90

    
91
	public String getContextObj() {
92
		return contextObj;
93
	}
94

    
95
	public void setContextObj(final String contextObj) {
96
		this.contextObj = contextObj;
97
	}
98

    
99
}
(8-8/8)