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("registering profile context " + desc.getId());
|
63
|
return serviceLocator.getService(ISRegistryService.class).updateProfile(profId, doc.asXML(), "ContextDSResourceType");
|
64
|
}
|
65
|
|
66
|
private void updateContextParams(final Element ctxElement, ContextDesc desc) {
|
67
|
//removing old PARAMs
|
68
|
List<Node> oldParams = ctxElement.selectNodes("param");
|
69
|
for (Node n : oldParams) {
|
70
|
n.detach();
|
71
|
}
|
72
|
//adding new params
|
73
|
for (Element param : desc.getParamsAsElements()) {
|
74
|
ctxElement.add(param);
|
75
|
}
|
76
|
}
|
77
|
|
78
|
private String registerNewProfile(final ContextDesc desc) throws DocumentException, ISRegistryException {
|
79
|
final SAXReader reader = new SAXReader();
|
80
|
final Document doc = reader.read(getClass().getResourceAsStream(
|
81
|
"/eu/dnetlib/msro/openaireplus/workflows/repo-hi/entityreg-contexts/xml/contextProfile.xml"));
|
82
|
((Element) doc.selectSingleNode("//CONFIGURATION")).add(desc.asDomElement());
|
83
|
log.info("updating profile context " + desc.getId());
|
84
|
return serviceLocator.getService(ISRegistryService.class).registerProfile(doc.asXML());
|
85
|
}
|
86
|
|
87
|
public String getContextObj() {
|
88
|
return contextObj;
|
89
|
}
|
90
|
|
91
|
public void setContextObj(final String contextObj) {
|
92
|
this.contextObj = contextObj;
|
93
|
}
|
94
|
|
95
|
}
|