|
1 |
package eu.dnetlib.msro.workers.aggregation.repohi;
|
|
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 org.dom4j.Document;
|
|
11 |
import org.dom4j.Node;
|
|
12 |
import org.dom4j.io.SAXReader;
|
|
13 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
14 |
import org.springframework.context.annotation.Scope;
|
|
15 |
import org.springframework.stereotype.Component;
|
|
16 |
|
|
17 |
import eu.dnetlib.clients.dsManager.DsManagerClient;
|
|
18 |
import eu.dnetlib.clients.is.InformationServiceClient;
|
|
19 |
import eu.dnetlib.clients.locators.ServiceClientFactory;
|
|
20 |
import eu.dnetlib.msro.annotations.ProcessNode;
|
|
21 |
import eu.dnetlib.msro.workflows.Arc;
|
|
22 |
import eu.dnetlib.msro.workflows.nodes.AbstractParallelProcessNode;
|
|
23 |
|
|
24 |
@Component
|
|
25 |
@Scope("prototype")
|
|
26 |
@ProcessNode("RemoveApiExtraFields")
|
|
27 |
public class RemoveApiExtraFieldsJobNode extends AbstractParallelProcessNode {
|
|
28 |
|
|
29 |
private String datasourceId;
|
|
30 |
private String datasourceInterface;
|
|
31 |
private List<String> fields;
|
|
32 |
|
|
33 |
@Autowired
|
|
34 |
private ServiceClientFactory clientFactory;
|
|
35 |
|
|
36 |
@Override
|
|
37 |
protected String execute() throws Exception {
|
|
38 |
if (fields != null) {
|
|
39 |
final Set<String> invalidFields = fields.stream().map(String::toLowerCase).collect(Collectors.toSet());
|
|
40 |
final Map<String, String> map = calculateValidExtraFields(datasourceId, datasourceInterface, invalidFields);
|
|
41 |
clientFactory.getClient(DsManagerClient.class).bulkUpdateApiExtraFields(datasourceId, datasourceInterface, map);
|
|
42 |
}
|
|
43 |
return Arc.DEFAULT_ARC;
|
|
44 |
}
|
|
45 |
|
|
46 |
private Map<String, String> calculateValidExtraFields(final String repoId, final String ifaceId, final Set<String> invalidFields) throws Exception {
|
|
47 |
final Map<String, String> res = new HashMap<>();
|
|
48 |
|
|
49 |
final String profile = clientFactory.getClient(InformationServiceClient.class).getProfile(repoId);
|
|
50 |
|
|
51 |
final SAXReader reader = new SAXReader();
|
|
52 |
final Document doc = reader.read(new StringReader(profile));
|
|
53 |
|
|
54 |
final Node ifcNode = doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']");
|
|
55 |
if (ifcNode != null) {
|
|
56 |
for (final Object o : ifcNode.selectNodes("./INTERFACE_EXTRA_FIELD")) {
|
|
57 |
final String name = ((Node) o).valueOf("@name");
|
|
58 |
if (!invalidFields.contains(name.toLowerCase())) {
|
|
59 |
res.put(name, ((Node) o).getText());
|
|
60 |
}
|
|
61 |
}
|
|
62 |
}
|
|
63 |
|
|
64 |
return res;
|
|
65 |
}
|
|
66 |
|
|
67 |
public List<String> getFields() {
|
|
68 |
return fields;
|
|
69 |
}
|
|
70 |
|
|
71 |
public void setFields(final List<String> fields) {
|
|
72 |
this.fields = fields;
|
|
73 |
}
|
|
74 |
|
|
75 |
public String getDatasourceId() {
|
|
76 |
return datasourceId;
|
|
77 |
}
|
|
78 |
|
|
79 |
public void setDatasourceId(final String datasourceId) {
|
|
80 |
this.datasourceId = datasourceId;
|
|
81 |
}
|
|
82 |
|
|
83 |
public String getDatasourceInterface() {
|
|
84 |
return datasourceInterface;
|
|
85 |
}
|
|
86 |
|
|
87 |
public void setDatasourceInterface(final String datasourceInterface) {
|
|
88 |
this.datasourceInterface = datasourceInterface;
|
|
89 |
}
|
|
90 |
|
|
91 |
}
|
repobye