1
|
package eu.dnetlib.wds.nodes;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
5
|
import eu.dnetlib.enabling.resultset.client.ResultSetClient;
|
6
|
import eu.dnetlib.msro.workflows.graph.Arc;
|
7
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
8
|
import eu.dnetlib.msro.workflows.procs.Env;
|
9
|
import eu.dnetlib.rmi.common.ResultSet;
|
10
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
11
|
import eu.dnetlib.rmi.enabling.ISRegistryService;
|
12
|
import eu.dnetlib.wds.collector.plugins.GenericDatasource;
|
13
|
import org.antlr.stringtemplate.StringTemplate;
|
14
|
import org.apache.commons.logging.Log;
|
15
|
import org.apache.commons.logging.LogFactory;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.springframework.beans.factory.annotation.Required;
|
18
|
|
19
|
import java.util.List;
|
20
|
|
21
|
public class SaveRepositoryProfileJobNode extends SimpleJobNode {
|
22
|
|
23
|
final static String query = "for $x in collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType') where $x//DATASOURCE_ORIGINAL_ID/text() ='%s' return $x//RESOURCE_IDENTIFIER/@value/string()";
|
24
|
|
25
|
private static final Log log = LogFactory.getLog(SaveRepositoryProfileJobNode.class);
|
26
|
|
27
|
@Autowired
|
28
|
private UniqueServiceLocator serviceLocator;
|
29
|
|
30
|
private String inputEprParam;
|
31
|
|
32
|
@Autowired
|
33
|
private ResultSetClient resultSetClient;
|
34
|
|
35
|
|
36
|
private StringTemplate xmlTemplate;
|
37
|
|
38
|
@Override
|
39
|
protected String execute(Env env) throws Exception {
|
40
|
|
41
|
final ISRegistryService registryService = serviceLocator.getService(ISRegistryService.class);
|
42
|
|
43
|
final ISLookUpService lookUpService = serviceLocator.getService(ISLookUpService.class);
|
44
|
|
45
|
final ResultSet<?> rsIn = env.getAttribute(inputEprParam, ResultSet.class);
|
46
|
final Iterable<String> profiles = resultSetClient.iter(rsIn, String.class);
|
47
|
final Gson g = new Gson();
|
48
|
if (profiles != null) {
|
49
|
profiles.forEach(it -> {
|
50
|
final GenericDatasource ds = g.fromJson(it, GenericDatasource.class);
|
51
|
xmlTemplate.removeAttribute("datasource");
|
52
|
xmlTemplate.setAttribute("datasource", ds);
|
53
|
final String currentProfile = xmlTemplate.toString();
|
54
|
try {
|
55
|
final List<String> resourceIds = lookUpService.quickSearchProfile(String.format(query, ds.getId()));
|
56
|
if (resourceIds != null && resourceIds.size() > 0) {
|
57
|
registryService.updateProfile(resourceIds.get(0), currentProfile, "RepositoryServiceResourceType");
|
58
|
} else
|
59
|
registryService.registerProfile(currentProfile);
|
60
|
} catch (Exception e) {
|
61
|
log.error("Error on saving profile " + it, e);
|
62
|
throw new RuntimeException(e);
|
63
|
}
|
64
|
});
|
65
|
}
|
66
|
return Arc.DEFAULT_ARC;
|
67
|
}
|
68
|
|
69
|
public String getInputEprParam() {
|
70
|
return inputEprParam;
|
71
|
}
|
72
|
|
73
|
public void setInputEprParam(String inputEprParam) {
|
74
|
this.inputEprParam = inputEprParam;
|
75
|
}
|
76
|
|
77
|
public StringTemplate getXmlTemplate() {
|
78
|
return xmlTemplate;
|
79
|
}
|
80
|
|
81
|
@Required
|
82
|
public void setXmlTemplate(StringTemplate xmlTemplate) {
|
83
|
this.xmlTemplate = xmlTemplate;
|
84
|
}
|
85
|
}
|