Project

General

Profile

1
package eu.dnetlib.functionality.modular.ui.workflows.util;
2

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

    
6
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
7
import eu.dnetlib.functionality.modular.ui.workflows.objects.WorkflowNotificationInfo;
8
import eu.dnetlib.functionality.modular.ui.workflows.objects.WorkflowUpdateInfo;
9
import eu.dnetlib.msro.workflows.util.WorkflowsConstants.WorkflowStatus;
10
import eu.dnetlib.rmi.enabling.ISRegistryException;
11
import eu.dnetlib.rmi.enabling.ISRegistryService;
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
import org.springframework.beans.factory.annotation.Autowired;
19

    
20
public class ISRegistryClient {
21

    
22
	private static final Log log = LogFactory.getLog(ISRegistryClient.class);
23
	@Autowired
24
	private UniqueServiceLocator serviceLocator;
25

    
26
	public String registerProfile(final String profile) throws ISRegistryException {
27
		return this.serviceLocator.getService(ISRegistryService.class).registerProfile(profile);
28
	}
29

    
30
	public void deleteProfile(final String id) throws ISRegistryException {
31
		this.serviceLocator.getService(ISRegistryService.class).deleteProfile(id);
32
	}
33

    
34
	public boolean updateWorkflowProfile(final String wfId, final String profile, final Map<String, String> map) throws Exception {
35
		final Document doc = new SAXReader().read(new StringReader(profile));
36
		for (final Map.Entry<String, String> e : map.entrySet()) {
37
			final Node node = doc.selectSingleNode("//CONFIGURATION/PARAMETERS/PARAM[@name='" + e.getKey() + "']");
38
			if (node == null) {
39
				log.error("Param " + e.getKey() + " not found in profile " + profile);
40
			} else {
41
				final String val = e.getValue() != null ? e.getValue() : "";
42
				if (node.valueOf("@managedBy").equalsIgnoreCase("user")) {
43
					node.setText(val);
44
				} else {
45
					log.debug("Param " + e.getKey() + " not updated, it is not editable by user");
46
				}
47

    
48
			}
49
		}
50

    
51
		// Update the Workflow status
52
		if (doc.selectNodes("//CONFIGURATION/PARAMETERS/PARAM[@required='true' and string-length(normalize-space(.)) = 0]").isEmpty()) {
53
			((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowStatus.EXECUTABLE.name());
54
		} else {
55
			((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowStatus.WAIT_USER_SETTINGS.name());
56
		}
57

    
58
		return this.serviceLocator.getService(ISRegistryService.class).updateProfile(wfId, doc.asXML(), doc.valueOf("//RESOURCE_TYPE/@value"));
59
	}
60

    
61
	public boolean updateWorkflowProfile(final String wfId, final String profile, final WorkflowUpdateInfo info) throws Exception {
62
		final Document doc = new SAXReader().read(new StringReader(profile));
63

    
64
		doc.selectSingleNode("//CONFIGURATION/@start").setText(info.getMode().toString());
65

    
66
		final Element notificationsNode = (Element) doc.selectSingleNode("//NOTIFICATIONS");
67
		notificationsNode.clearContent();
68
		if (info.getNotifications() != null) {
69
			for (final WorkflowNotificationInfo n : info.getNotifications()) {
70
				final Element emailNode = notificationsNode.addElement("EMAIL");
71
				emailNode.addAttribute("address", n.getEmail());
72
				emailNode.addAttribute("messageProfileId", n.getMessageProfileId());
73
				emailNode.addAttribute("condition", n.getCondition().toString());
74
			}
75
		}
76

    
77
		doc.selectSingleNode("//WORKFLOW_PRIORITY").setText(Integer.toString(info.getPriority()));
78

    
79
		final Node node = doc.selectSingleNode("//SCHEDULING");
80
		((Element) node).addAttribute("enabled", Boolean.toString(info.isScheduled()));
81
		if (info.isScheduled()) {
82
			node.selectSingleNode("./CRON").setText(info.getCron() != null ? info.getCron() : "");
83
			node.selectSingleNode("./MININTERVAL").setText(Integer.toString(info.getInterval()));
84
		}
85
		return this.serviceLocator.getService(ISRegistryService.class).updateProfile(wfId, doc.asXML(), doc.valueOf("//RESOURCE_TYPE/@value"));
86
	}
87

    
88
	public void updateWorkflowStatus(final String id, final WorkflowStatus status) throws ISRegistryException {
89
		this.serviceLocator.getService(ISRegistryService.class).updateProfileNode(id, "//CONFIGURATION/@status", "'" + status.toString() + "'");
90
	}
91

    
92
}
(2-2/2)