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.stream.Collectors;
|
8
|
|
9
|
import org.apache.commons.lang3.StringEscapeUtils;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
import org.dom4j.Document;
|
13
|
import org.dom4j.Element;
|
14
|
import org.dom4j.Node;
|
15
|
import org.dom4j.io.SAXReader;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.springframework.context.annotation.Scope;
|
18
|
import org.springframework.stereotype.Component;
|
19
|
|
20
|
import eu.dnetlib.clients.is.InformationServiceClient;
|
21
|
import eu.dnetlib.miscutils.templates.TemplateUtil;
|
22
|
import eu.dnetlib.msro.annotations.ProcessNode;
|
23
|
import eu.dnetlib.msro.workflows.Arc;
|
24
|
import eu.dnetlib.msro.workflows.WorkflowStatus;
|
25
|
import eu.dnetlib.msro.workflows.nodes.AbstractProcessNode;
|
26
|
import eu.dnetlib.msro.workflows.procs.ProcessAware;
|
27
|
import eu.dnetlib.msro.workflows.procs.Token;
|
28
|
import eu.dnetlib.msro.workflows.procs.WorkflowProcess;
|
29
|
|
30
|
@Component
|
31
|
@Scope("prototype")
|
32
|
@ProcessNode("RegisterWorkflowFromTemplate")
|
33
|
public class RegisterWorkflowFromTemplateJobNode extends AbstractProcessNode implements ProcessAware {
|
34
|
|
35
|
private String wfName;
|
36
|
private String wfDescription;
|
37
|
private String wfTemplate;
|
38
|
private Map<String, String> envParams;
|
39
|
private Map<String, String> simpleParams;
|
40
|
|
41
|
@Autowired
|
42
|
private TemplateUtil templateUtil;
|
43
|
|
44
|
@Autowired
|
45
|
private InformationServiceClient isClient;
|
46
|
|
47
|
private WorkflowProcess process;
|
48
|
|
49
|
private static final Log log = LogFactory.getLog(RegisterWorkflowFromTemplateJobNode.class);
|
50
|
|
51
|
@Override
|
52
|
public void execute(final Token token) {
|
53
|
try {
|
54
|
final Map<String, Object> map = new HashMap<>();
|
55
|
|
56
|
if (simpleParams != null) {
|
57
|
envParams.forEach((k, v) -> map.put(k, StringEscapeUtils.escapeXml11(v)));
|
58
|
}
|
59
|
|
60
|
if (envParams != null) {
|
61
|
envParams.forEach((k, v) -> {
|
62
|
if (token.getEnv().hasAttribute(v)) {
|
63
|
final Object rv = token.getEnv().getAttribute(v);
|
64
|
if (rv != null) {
|
65
|
log.info(String.format("%s -> env(%s) = %s", k, v, rv));
|
66
|
map.put(k, StringEscapeUtils.escapeXml11(rv.toString()));
|
67
|
} else {
|
68
|
log.warn(String.format("Env param %s is NULL", v));
|
69
|
}
|
70
|
} else {
|
71
|
log.warn(String.format("Env param %s is missing, valid params are %s", v, token.getEnv().getAttributes().keySet()));
|
72
|
}
|
73
|
});
|
74
|
}
|
75
|
|
76
|
map.put("name", StringEscapeUtils.escapeXml11(wfName));
|
77
|
map.put("desc", StringEscapeUtils.escapeXml11(wfDescription));
|
78
|
map.put("worker", StringEscapeUtils.escapeXml11(process.getWorkerId()));
|
79
|
map.put("dsId", StringEscapeUtils.escapeXml11(process.getDsId()));
|
80
|
map.put("iface", StringEscapeUtils.escapeXml11(process.getDsInterface()));
|
81
|
|
82
|
final String wfProfile = templateUtil.processTemplateProfile(wfTemplate, map).get("wf");
|
83
|
|
84
|
final Document doc = new SAXReader().read(new StringReader(wfProfile));
|
85
|
final Node paramsNode = doc.selectSingleNode("//CONFIGURATION/PARAMETERS");
|
86
|
|
87
|
final List<?> emptySysParams = paramsNode.selectNodes("//PARAM[@managedBy='system' and @required='true' and not(text())]");
|
88
|
if (emptySysParams.size() > 0) {
|
89
|
((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowStatus.WAIT_SYS_SETTINGS.toString());
|
90
|
log.warn("The following system parameters are empty: "
|
91
|
+ emptySysParams.stream().map(p -> ((Node) p).valueOf("@name")).collect(Collectors.joining()));
|
92
|
} else if (paramsNode.selectNodes("//PARAM[@managedBy='user' and @required='true' and not(text())]").size() > 0) {
|
93
|
((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowStatus.WAIT_USER_SETTINGS.toString());
|
94
|
} else {
|
95
|
((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowStatus.EXECUTABLE.toString());
|
96
|
}
|
97
|
|
98
|
final String wfId = isClient.register(doc.asXML());
|
99
|
|
100
|
token.getEnv().setAttribute("wfId", wfId);
|
101
|
|
102
|
token.release(Arc.DEFAULT_ARC);
|
103
|
} catch (final Throwable e) {
|
104
|
token.releaseAsFailed(e);
|
105
|
}
|
106
|
}
|
107
|
|
108
|
public String getWfName() {
|
109
|
return wfName;
|
110
|
}
|
111
|
|
112
|
public void setWfName(final String wfName) {
|
113
|
this.wfName = wfName;
|
114
|
}
|
115
|
|
116
|
public String getWfDescription() {
|
117
|
return wfDescription;
|
118
|
}
|
119
|
|
120
|
public void setWfDescription(final String wfDescription) {
|
121
|
this.wfDescription = wfDescription;
|
122
|
}
|
123
|
|
124
|
public String getWfTemplate() {
|
125
|
return wfTemplate;
|
126
|
}
|
127
|
|
128
|
public void setWfTemplate(final String wfTemplate) {
|
129
|
this.wfTemplate = wfTemplate;
|
130
|
}
|
131
|
|
132
|
public Map<String, String> getEnvParams() {
|
133
|
return envParams;
|
134
|
}
|
135
|
|
136
|
public void setEnvParams(final Map<String, String> envParams) {
|
137
|
this.envParams = envParams;
|
138
|
}
|
139
|
|
140
|
public Map<String, String> getSimpleParams() {
|
141
|
return simpleParams;
|
142
|
}
|
143
|
|
144
|
public void setSimpleParams(final Map<String, String> simpleParams) {
|
145
|
this.simpleParams = simpleParams;
|
146
|
}
|
147
|
|
148
|
@Override
|
149
|
public void setProcess(final WorkflowProcess process) {
|
150
|
this.process = process;
|
151
|
}
|
152
|
|
153
|
}
|