1
|
package eu.dnetlib.msro.workflows.nodes.info;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.HashMap;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
import java.util.Set;
|
8
|
import java.util.stream.Collectors;
|
9
|
|
10
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
11
|
import eu.dnetlib.msro.workflows.graph.Arc;
|
12
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
13
|
import eu.dnetlib.msro.workflows.procs.Env;
|
14
|
import eu.dnetlib.rmi.datasource.DatasourceManagerService;
|
15
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
16
|
import org.dom4j.Document;
|
17
|
import org.dom4j.Node;
|
18
|
import org.dom4j.io.SAXReader;
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
|
21
|
public class RemoveApiExtraFieldsJobNode extends SimpleJobNode {
|
22
|
|
23
|
private String datasourceId;
|
24
|
private String datasourceInterface;
|
25
|
private List<String> fields;
|
26
|
|
27
|
@Autowired
|
28
|
private UniqueServiceLocator serviceLocator;
|
29
|
|
30
|
@Override
|
31
|
protected String execute(final Env env) throws Exception {
|
32
|
if (this.fields != null) {
|
33
|
final Set<String> invalidFields = this.fields.stream().map(String::toLowerCase).collect(Collectors.toSet());
|
34
|
final Map<String, String> map = calculateValidExtraFields(this.datasourceId, this.datasourceInterface, invalidFields);
|
35
|
this.serviceLocator.getService(DatasourceManagerService.class).bulkUpdateApiExtraFields(this.datasourceId, this.datasourceInterface, map);
|
36
|
}
|
37
|
return Arc.DEFAULT_ARC;
|
38
|
}
|
39
|
|
40
|
private Map<String, String> calculateValidExtraFields(final String repoId, final String ifaceId, final Set<String> invalidFields) throws Exception {
|
41
|
final Map<String, String> res = new HashMap<>();
|
42
|
|
43
|
final String profile = this.serviceLocator.getService(ISLookUpService.class).getResourceProfile(repoId);
|
44
|
|
45
|
final SAXReader reader = new SAXReader();
|
46
|
final Document doc = reader.read(new StringReader(profile));
|
47
|
|
48
|
final Node ifcNode = doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']");
|
49
|
if (ifcNode != null) {
|
50
|
for (final Object o : ifcNode.selectNodes("./INTERFACE_EXTRA_FIELD")) {
|
51
|
final String name = ((Node) o).valueOf("@name");
|
52
|
if (!invalidFields.contains(name.toLowerCase())) {
|
53
|
res.put(name, ((Node) o).getText());
|
54
|
}
|
55
|
}
|
56
|
}
|
57
|
|
58
|
return res;
|
59
|
}
|
60
|
|
61
|
public List<String> getFields() {
|
62
|
return this.fields;
|
63
|
}
|
64
|
|
65
|
public void setFields(final List<String> fields) {
|
66
|
this.fields = fields;
|
67
|
}
|
68
|
|
69
|
public String getDatasourceId() {
|
70
|
return this.datasourceId;
|
71
|
}
|
72
|
|
73
|
public void setDatasourceId(final String datasourceId) {
|
74
|
this.datasourceId = datasourceId;
|
75
|
}
|
76
|
|
77
|
public String getDatasourceInterface() {
|
78
|
return this.datasourceInterface;
|
79
|
}
|
80
|
|
81
|
public void setDatasourceInterface(final String datasourceInterface) {
|
82
|
this.datasourceInterface = datasourceInterface;
|
83
|
}
|
84
|
|
85
|
}
|