Project

General

Profile

1
package eu.dnetlib.msro.workflows.nodes.repohi;
2

    
3
import java.io.StringReader;
4
import java.util.HashMap;
5
import java.util.Map;
6

    
7
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
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.msro.workflows.util.WorkflowsConstants;
12
import eu.dnetlib.rmi.enabling.ISLookUpService;
13
import eu.dnetlib.rmi.enabling.ISRegistryService;
14
import eu.dnetlib.rmi.manager.MSROException;
15
import org.apache.commons.lang3.StringEscapeUtils;
16
import org.apache.commons.logging.Log;
17
import org.apache.commons.logging.LogFactory;
18
import org.dom4j.Document;
19
import org.dom4j.Element;
20
import org.dom4j.Node;
21
import org.dom4j.io.SAXReader;
22
import org.springframework.beans.factory.annotation.Autowired;
23

    
24
public class UpdateWfParametersJobNode extends SimpleJobNode {
25

    
26
	private static final Log log = LogFactory.getLog(UpdateWfParametersJobNode.class);
27
	private Map<String, String> envParams;
28
	private Map<String, String> simpleParams;
29
	@Autowired
30
	private UniqueServiceLocator serviceLocator;
31

    
32
	@Override
33
	protected String execute(final Env env) throws Exception {
34

    
35
		final Map<String, String> params = new HashMap<String, String>();
36

    
37
		if (this.simpleParams != null) {
38
			for (final Map.Entry<String, String> e : this.simpleParams.entrySet()) {
39
				params.put(e.getKey(), e.getValue());
40
			}
41
		}
42
		if (this.envParams != null) {
43
			for (final Map.Entry<String, String> e : this.envParams.entrySet()) {
44
				params.put(e.getKey(), env.getAttribute(e.getValue(), String.class));
45
			}
46
		}
47

    
48
		if (!params.isEmpty()) {
49
			final String profId = env.getAttribute("repoWfId", String.class);
50
			final String profile = this.serviceLocator.getService(ISLookUpService.class).getResourceProfile(profId);
51
			final Document doc = new SAXReader().read(new StringReader(profile));
52
			final Node paramsNode = doc.selectSingleNode("//CONFIGURATION/PARAMETERS");
53
			for (final Map.Entry<String, String> e : params.entrySet()) {
54
				final Element node = (Element) paramsNode.selectSingleNode("./PARAM[@name = '" + StringEscapeUtils.escapeXml11(e.getKey()) + "']");
55
				if (node != null) {
56
					node.setText("" + e.getValue());
57
					log.debug(String.format("Workflow %s, param %s = %s", profId, e.getKey(), "" + e.getValue()));
58
				} else {
59
					log.error(String.format("Invalid param %s for %s", e.getKey(), profId));
60
					throw new MSROException(String.format("Invalid param %s for %s", e.getKey(), profId));
61
				}
62
			}
63

    
64
			if (paramsNode.selectNodes("//PARAM[@managedBy='system' and @required='true' and not(text())]").size() > 0) {
65
				((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowsConstants.WorkflowStatus.WAIT_SYS_SETTINGS.toString());
66
				throw new MSROException("Error: some system parameters are empty");
67
			} else if (paramsNode.selectNodes("//PARAM[@managedBy='user' and @required='true' and not(text())]").size() > 0) {
68
				((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowsConstants.WorkflowStatus.WAIT_USER_SETTINGS.toString());
69
			} else {
70
				((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowsConstants.WorkflowStatus.EXECUTABLE.toString());
71
			}
72

    
73
			this.serviceLocator.getService(ISRegistryService.class).updateProfile(profId, doc.asXML(), doc.valueOf("//RESOURCE_TYPE/@value"));
74
		}
75

    
76
		return Arc.DEFAULT_ARC;
77
	}
78

    
79
	public Map<String, String> getEnvParams() {
80
		return this.envParams;
81
	}
82

    
83
	public void setEnvParams(final Map<String, String> envParams) {
84
		this.envParams = envParams;
85
	}
86

    
87
	public Map<String, String> getSimpleParams() {
88
		return this.simpleParams;
89
	}
90

    
91
	public void setSimpleParams(final Map<String, String> simpleParams) {
92
		this.simpleParams = simpleParams;
93
	}
94

    
95
}
(2-2/2)