Project

General

Profile

« Previous | Next » 

Revision 41480

Repo HI

View differences:

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

  
3
import java.io.StringReader;
4
import java.lang.reflect.Type;
5
import java.util.HashMap;
6
import java.util.Map;
7

  
8
import javax.annotation.Resource;
9

  
10
import org.apache.commons.lang.StringEscapeUtils;
11
import org.apache.commons.lang.StringUtils;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.dom4j.Document;
15
import org.dom4j.Element;
16
import org.dom4j.Node;
17
import org.dom4j.io.SAXReader;
18

  
19
import com.google.gson.Gson;
20
import com.google.gson.reflect.TypeToken;
21

  
22
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
23
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
24
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
25
import eu.dnetlib.msro.rmi.MSROException;
26
import eu.dnetlib.msro.workflows.graph.Arc;
3 27
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
4 28
import eu.dnetlib.msro.workflows.procs.Env;
29
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
5 30

  
6 31
public class UpdateWfParametersJobNode extends SimpleJobNode {
7 32

  
33
	private String envParamsJson;
34
	private String simpleParamsJson;
35

  
36
	@Resource
37
	private UniqueServiceLocator serviceLocator;
38

  
39
	private static final Log log = LogFactory.getLog(UpdateWfParametersJobNode.class);
40

  
8 41
	@Override
9 42
	protected String execute(final Env env) throws Exception {
10
		// TODO Auto-generated method stub
11
		return null;
43

  
44
		final Gson gson = new Gson();
45

  
46
		final Map<String, Object> params = new HashMap<String, Object>();
47

  
48
		final Type type = new TypeToken<Map<String, Object>>() {}.getType();
49

  
50
		if (StringUtils.isNotBlank(this.simpleParamsJson)) {
51
			final Map<String, Object> map = gson.fromJson(this.simpleParamsJson, type);
52
			for (final Map.Entry<String, Object> e : map.entrySet()) {
53
				params.put(e.getKey(), e.getValue());
54
			}
55
		}
56
		if (StringUtils.isNotBlank(this.envParamsJson)) {
57
			final Map<String, Object> map = gson.fromJson(this.envParamsJson, type);
58
			for (final Map.Entry<String, Object> e : map.entrySet()) {
59
				params.put(e.getKey(), env.getAttribute(e.getValue().toString()));
60
			}
61
		}
62

  
63
		if (!params.isEmpty()) {
64
			final String profId = env.getAttribute("repoWfId", String.class);
65
			final String profile = this.serviceLocator.getService(ISLookUpService.class).getResourceProfile(profId);
66
			final Document doc = new SAXReader().read(new StringReader(profile));
67
			final Node paramsNode = doc.selectSingleNode("//CONFIGURATION/PARAMETERS");
68
			for (final Map.Entry<String, Object> e : params.entrySet()) {
69
				final Element node = (Element) paramsNode.selectSingleNode("./PARAM[@name = '" + StringEscapeUtils.escapeXml(e.getKey()) + "']");
70
				if (node != null) {
71
					node.setText("" + e.getValue());
72
					log.debug(String.format("Workflow %s, param %s = %s", profId, e.getKey(), "" + e.getValue()));
73
				} else {
74
					log.error(String.format("Invalid param %s for %s", e.getKey(), profId));
75
					throw new MSROException(String.format("Invalid param %s for %s", e.getKey(), profId));
76
				}
77
			}
78

  
79
			if (paramsNode.selectNodes("//PARAM[@managedBy='system' and @required='true' and not(text())]").size() > 0) {
80
				((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowsConstants.WorkflowStatus.WAIT_SYS_SETTINGS.toString());
81
				throw new MSROException("Error: some system parameters are empty");
82
			} else if (paramsNode.selectNodes("//PARAM[@managedBy='user' and @required='true' and not(text())]").size() > 0) {
83
				((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowsConstants.WorkflowStatus.WAIT_USER_SETTINGS.toString());
84
			} else {
85
				((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowsConstants.WorkflowStatus.EXECUTABLE.toString());
86
			}
87

  
88
			this.serviceLocator.getService(ISRegistryService.class).updateProfile(profId, doc.asXML(), doc.valueOf("//RESOURCE_TYPE/@value"));
89
		}
90

  
91
		return Arc.DEFAULT_ARC;
12 92
	}
13 93

  
94
	public String getEnvParamsJson() {
95
		return this.envParamsJson;
96
	}
97

  
98
	public void setEnvParamsJson(final String envParamsJson) {
99
		this.envParamsJson = envParamsJson;
100
	}
101

  
102
	public String getSimpleParamsJson() {
103
		return this.simpleParamsJson;
104
	}
105

  
106
	public void setSimpleParamsJson(final String simpleParamsJson) {
107
		this.simpleParamsJson = simpleParamsJson;
108
	}
109

  
14 110
}

Also available in: Unified diff