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.Direction;
|
23
|
import eu.dnetlib.msro.annotations.EnvParam;
|
24
|
import eu.dnetlib.msro.annotations.ProcessNode;
|
25
|
import eu.dnetlib.msro.workflows.Arc;
|
26
|
import eu.dnetlib.msro.workflows.WorkflowStatus;
|
27
|
import eu.dnetlib.msro.workflows.nodes.AbstractParallelProcessNode;
|
28
|
import eu.dnetlib.msro.workflows.procs.ProcessAware;
|
29
|
import eu.dnetlib.msro.workflows.procs.WorkflowProcess;
|
30
|
|
31
|
@Component
|
32
|
@Scope("prototype")
|
33
|
@ProcessNode("RegisterWorkflowFromTemplate")
|
34
|
public class RegisterWorkflowFromTemplateJobNode extends AbstractParallelProcessNode implements ProcessAware {
|
35
|
|
36
|
private String wfName;
|
37
|
private String wfDescription;
|
38
|
private String wfTemplate;
|
39
|
private Map<String, String> envParams;
|
40
|
private Map<String, String> simpleParams;
|
41
|
|
42
|
@Autowired
|
43
|
private TemplateUtil templateUtil;
|
44
|
|
45
|
@Autowired
|
46
|
private InformationServiceClient isClient;
|
47
|
|
48
|
@EnvParam(value = "wfId", direction = Direction.OUT)
|
49
|
private String wfId;
|
50
|
|
51
|
private WorkflowProcess process;
|
52
|
|
53
|
private static final Log log = LogFactory.getLog(RegisterWorkflowFromTemplateJobNode.class);
|
54
|
|
55
|
@Override
|
56
|
protected String execute() throws Exception {
|
57
|
final Map<String, Object> map = new HashMap<>();
|
58
|
|
59
|
if (simpleParams != null) {
|
60
|
envParams.forEach((k, v) -> map.put(k, StringEscapeUtils.escapeXml11(v)));
|
61
|
}
|
62
|
|
63
|
if (envParams != null) {
|
64
|
envParams.forEach((k, v) -> map.put(k, StringEscapeUtils.escapeXml11(process.getEnv().getAttribute(v).toString())));
|
65
|
}
|
66
|
|
67
|
map.put("name", StringEscapeUtils.escapeXml11(wfName));
|
68
|
map.put("desc", StringEscapeUtils.escapeXml11(wfDescription));
|
69
|
map.put("worker", StringEscapeUtils.escapeXml11(process.getWorkerId()));
|
70
|
map.put("dsId", StringEscapeUtils.escapeXml11(process.getDsId()));
|
71
|
map.put("iface", StringEscapeUtils.escapeXml11(process.getDsInterface()));
|
72
|
|
73
|
final String wfProfile = templateUtil.processTemplateProfile(wfTemplate, map).get("wf");
|
74
|
|
75
|
final Document doc = new SAXReader().read(new StringReader(wfProfile));
|
76
|
final Node paramsNode = doc.selectSingleNode("//CONFIGURATION/PARAMETERS");
|
77
|
|
78
|
final List<?> emptySysParams = paramsNode.selectNodes("//PARAM[@managedBy='system' and @required='true' and not(text())]");
|
79
|
if (emptySysParams.size() > 0) {
|
80
|
((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowStatus.WAIT_SYS_SETTINGS.toString());
|
81
|
log.warn("The following system parameters are empty: "
|
82
|
+ emptySysParams.stream().map(p -> ((Node) p).valueOf("@name")).collect(Collectors.joining()));
|
83
|
} else if (paramsNode.selectNodes("//PARAM[@managedBy='user' and @required='true' and not(text())]").size() > 0) {
|
84
|
((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowStatus.WAIT_USER_SETTINGS.toString());
|
85
|
} else {
|
86
|
((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowStatus.EXECUTABLE.toString());
|
87
|
}
|
88
|
|
89
|
wfId = isClient.register(doc.asXML());
|
90
|
|
91
|
return Arc.DEFAULT_ARC;
|
92
|
}
|
93
|
|
94
|
public String getWfName() {
|
95
|
return wfName;
|
96
|
}
|
97
|
|
98
|
public void setWfName(final String wfName) {
|
99
|
this.wfName = wfName;
|
100
|
}
|
101
|
|
102
|
public String getWfDescription() {
|
103
|
return wfDescription;
|
104
|
}
|
105
|
|
106
|
public void setWfDescription(final String wfDescription) {
|
107
|
this.wfDescription = wfDescription;
|
108
|
}
|
109
|
|
110
|
public String getWfTemplate() {
|
111
|
return wfTemplate;
|
112
|
}
|
113
|
|
114
|
public void setWfTemplate(final String wfTemplate) {
|
115
|
this.wfTemplate = wfTemplate;
|
116
|
}
|
117
|
|
118
|
public Map<String, String> getEnvParams() {
|
119
|
return envParams;
|
120
|
}
|
121
|
|
122
|
public void setEnvParams(final Map<String, String> envParams) {
|
123
|
this.envParams = envParams;
|
124
|
}
|
125
|
|
126
|
public Map<String, String> getSimpleParams() {
|
127
|
return simpleParams;
|
128
|
}
|
129
|
|
130
|
public void setSimpleParams(final Map<String, String> simpleParams) {
|
131
|
this.simpleParams = simpleParams;
|
132
|
}
|
133
|
|
134
|
@Override
|
135
|
public void setProcess(final WorkflowProcess process) {
|
136
|
this.process = process;
|
137
|
}
|
138
|
|
139
|
public String getWfId() {
|
140
|
return wfId;
|
141
|
}
|
142
|
|
143
|
public void setWfId(final String wfId) {
|
144
|
this.wfId = wfId;
|
145
|
}
|
146
|
|
147
|
}
|