1
|
package eu.dnetlib.msro.workflows.nodes.hostedby;
|
2
|
|
3
|
import java.util.Map.Entry;
|
4
|
import javax.annotation.Resource;
|
5
|
|
6
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
7
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
8
|
import eu.dnetlib.msro.workflows.graph.Arc;
|
9
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
10
|
import eu.dnetlib.msro.workflows.procs.Env;
|
11
|
import eu.dnetlib.rmi.datasource.DatasourceDesc;
|
12
|
import eu.dnetlib.rmi.datasource.DatasourceManagerService;
|
13
|
import eu.dnetlib.rmi.datasource.DatasourceManagerServiceException;
|
14
|
import eu.dnetlib.rmi.datasource.IfaceDesc;
|
15
|
import org.apache.commons.logging.Log;
|
16
|
import org.apache.commons.logging.LogFactory;
|
17
|
|
18
|
public class UpsertHostedByApisJobNode extends SimpleJobNode {
|
19
|
|
20
|
public static final String extraFieldsForTotal = "last_aggregation_total";
|
21
|
public static final String extraFieldForDate = "last_aggregation_date";
|
22
|
public static final String extraFieldForMdId = "last_aggregation_mdId";
|
23
|
private static final Log log = LogFactory.getLog(UpsertHostedByApisJobNode.class);
|
24
|
private static final String HOSTED_BY_COMPLIANCE = "hostedBy";
|
25
|
@Resource
|
26
|
private UniqueServiceLocator serviceLocator;
|
27
|
private String countersParam;
|
28
|
private String mdId;
|
29
|
|
30
|
@Override
|
31
|
protected String execute(final Env env) throws Exception {
|
32
|
final HostedByCounters counters = env.getAttribute(this.countersParam, HostedByCounters.class);
|
33
|
final DatasourceManagerService dsManager = this.serviceLocator.getService(DatasourceManagerService.class);
|
34
|
final String date = DateUtils.now_ISO8601();
|
35
|
final String namespacePrefix = env.getAttribute("namespacePrefix", String.class);
|
36
|
|
37
|
log.info(counters);
|
38
|
|
39
|
for (final Entry<String, Integer> e : counters.getCounters().entrySet()) {
|
40
|
updateHostedByApi(dsManager, e.getKey(), namespacePrefix, date, e.getValue());
|
41
|
}
|
42
|
|
43
|
return Arc.DEFAULT_ARC;
|
44
|
}
|
45
|
|
46
|
private void updateHostedByApi(final DatasourceManagerService dsManager,
|
47
|
final String dsId,
|
48
|
final String namepsacePrefix,
|
49
|
final String date,
|
50
|
final int size) {
|
51
|
log.info("Verifying hostedBy api in ds: " + dsId);
|
52
|
try {
|
53
|
final DatasourceDesc ds = dsManager.getDatasource(dsId);
|
54
|
|
55
|
for (final IfaceDesc iface : ds.getInterfaces()) {
|
56
|
if (HOSTED_BY_COMPLIANCE.equals(iface.getCompliance())) {
|
57
|
|
58
|
// dsManager.updateExtraField(dsId, iface.getId(), namepsacePrefix + ":" + extraFieldForDate, date, false);
|
59
|
// dsManager.updateExtraField(dsId, iface.getId(), namepsacePrefix + ":" + extraFieldsForTotal, Integer.toString(size),
|
60
|
// false);
|
61
|
// dsManager.updateExtraField(dsId, iface.getId(), namepsacePrefix + ":" + extraFieldForMdId, mdId, false);
|
62
|
return;
|
63
|
}
|
64
|
}
|
65
|
|
66
|
final IfaceDesc iface = new IfaceDesc();
|
67
|
iface.setId("api_________::" + dsId + "::hostedBy");
|
68
|
iface.setTypology(ds.getDatasourceClass());
|
69
|
iface.setCompliance(HOSTED_BY_COMPLIANCE);
|
70
|
iface.setAccessProtocol("UNKNOWN");
|
71
|
iface.setContentDescription("metadata");
|
72
|
iface.setBaseUrl("");
|
73
|
iface.setActive(false);
|
74
|
iface.setRemovable(true);
|
75
|
iface.getExtraFields().put(namepsacePrefix + ":" + extraFieldForDate, date);
|
76
|
iface.getExtraFields().put(namepsacePrefix + ":" + extraFieldsForTotal, Integer.toString(size));
|
77
|
iface.getExtraFields().put(namepsacePrefix + ":" + extraFieldForMdId, this.mdId);
|
78
|
dsManager.addInterface(dsId, iface);
|
79
|
} catch (final DatasourceManagerServiceException e) {
|
80
|
log.warn("Error setting hostedBy api of ds: " + dsId, e);
|
81
|
}
|
82
|
}
|
83
|
|
84
|
public String getMdId() {
|
85
|
return this.mdId;
|
86
|
}
|
87
|
|
88
|
public void setMdId(final String mdId) {
|
89
|
this.mdId = mdId;
|
90
|
}
|
91
|
|
92
|
public String getCountersParam() {
|
93
|
return this.countersParam;
|
94
|
}
|
95
|
|
96
|
public void setCountersParam(final String countersParam) {
|
97
|
this.countersParam = countersParam;
|
98
|
}
|
99
|
|
100
|
}
|