1
|
package eu.dnetlib.msro.workflows.nodes.repobye;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.io.StringWriter;
|
5
|
|
6
|
import org.apache.commons.logging.Log;
|
7
|
import org.apache.commons.logging.LogFactory;
|
8
|
import org.dom4j.Document;
|
9
|
import org.dom4j.Node;
|
10
|
import org.dom4j.io.SAXReader;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
|
13
|
import com.googlecode.sarasvati.Arc;
|
14
|
import com.googlecode.sarasvati.NodeToken;
|
15
|
|
16
|
import eu.dnetlib.enabling.datasources.common.Api;
|
17
|
import eu.dnetlib.enabling.datasources.common.Datasource;
|
18
|
import eu.dnetlib.enabling.datasources.common.LocalDatasourceManager;
|
19
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
20
|
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
|
21
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
22
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
23
|
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
|
24
|
|
25
|
public class DeleteMetaWfJobNode extends SimpleJobNode {
|
26
|
|
27
|
private String metaWfId;
|
28
|
|
29
|
@Autowired
|
30
|
private UniqueServiceLocator serviceLocator;
|
31
|
|
32
|
@Autowired
|
33
|
private LocalDatasourceManager<Datasource<?, ?>, Api<?>> dsManager;
|
34
|
|
35
|
private static final Log log = LogFactory.getLog(DeleteMetaWfJobNode.class);
|
36
|
|
37
|
@Override
|
38
|
protected String execute(final NodeToken token) throws Exception {
|
39
|
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(metaWfId);
|
40
|
final Document doc = new SAXReader().read(new StringReader(profile));
|
41
|
|
42
|
final String dsId = doc.valueOf("//DATAPROVIDER/@id");
|
43
|
final String dsName = doc.valueOf("//DATAPROVIDER/text()");
|
44
|
final String ifaceId = doc.valueOf("//DATAPROVIDER/@interface");
|
45
|
final String destroyWfId = doc.valueOf("//CONFIGURATION/@destroyWorkflow");
|
46
|
|
47
|
log.info("Removing a MetaWf of dataprovider: " + dsId);
|
48
|
|
49
|
token.getEnv().setAttribute(WorkflowsConstants.DATAPROVIDER_ID, dsId);
|
50
|
token.getEnv().setAttribute(WorkflowsConstants.DATAPROVIDER_NAME, dsName);
|
51
|
token.getEnv().setAttribute(WorkflowsConstants.DATAPROVIDER_INTERFACE, ifaceId);
|
52
|
|
53
|
final ISRegistryService registry = serviceLocator.getService(ISRegistryService.class);
|
54
|
|
55
|
for (final Object o : doc.selectNodes("//WORKFLOW")) {
|
56
|
final String wfId = ((Node) o).valueOf("@id");
|
57
|
try {
|
58
|
registry.deleteProfile(wfId);
|
59
|
log.info(" - Deleted Workflow: " + wfId);
|
60
|
} catch (final Exception e) {
|
61
|
log.error(" - (ERR) Error deleting profile " + wfId);
|
62
|
}
|
63
|
}
|
64
|
registry.deleteProfile(metaWfId);
|
65
|
log.info(" - Deleted MetaWorkflow: " + metaWfId);
|
66
|
|
67
|
registry.deleteProfile(destroyWfId);
|
68
|
log.info(" - Deleted destroy workflow: " + destroyWfId);
|
69
|
|
70
|
verifyDatasource(dsId, ifaceId);
|
71
|
|
72
|
return Arc.DEFAULT_ARC;
|
73
|
}
|
74
|
|
75
|
private void verifyDatasource(final String dsId, final String ifaceId) throws Exception {
|
76
|
final StringWriter sw = new StringWriter();
|
77
|
|
78
|
sw.append("for $x in collection('/db/DRIVER/MetaWorkflowDSResources/MetaWorkflowDSResourceType') where");
|
79
|
sw.append(" $x//DATAPROVIDER/@id = '" + dsId + "' and ");
|
80
|
sw.append(" $x//DATAPROVIDER/@interface = '" + ifaceId + "' and ");
|
81
|
sw.append(" $x//RESOURCE_IDENTIFIER/@value != '" + metaWfId + "' ");
|
82
|
sw.append("return $x//RESOURCE_IDENTIFIER/@value/string()");
|
83
|
|
84
|
final boolean active = !serviceLocator.getService(ISLookUpService.class).quickSearchProfile(sw.toString()).isEmpty();
|
85
|
|
86
|
log.info(" - Updating iface, active status: " + active);
|
87
|
|
88
|
setActivationStatus(dsId, ifaceId, active);
|
89
|
}
|
90
|
|
91
|
protected void setActivationStatus(final String dsId, final String ifaceId, final boolean active) throws Exception {
|
92
|
dsManager.setActive(dsId, ifaceId, active);
|
93
|
}
|
94
|
|
95
|
public String getMetaWfId() {
|
96
|
return metaWfId;
|
97
|
}
|
98
|
|
99
|
public void setMetaWfId(final String metaWfId) {
|
100
|
this.metaWfId = metaWfId;
|
101
|
}
|
102
|
|
103
|
}
|