Project

General

Profile

1 1557 antonis.le
package eu.dnetlib.domain.enabling;
2 1288 antonis.le
3 1383 antonis.le
import java.io.ByteArrayInputStream;
4 1288 antonis.le
import java.util.Date;
5
6 1383 antonis.le
import javax.xml.parsers.DocumentBuilder;
7
import javax.xml.parsers.DocumentBuilderFactory;
8
import javax.xml.parsers.ParserConfigurationException;
9
import javax.xml.xpath.XPath;
10
import javax.xml.xpath.XPathConstants;
11
import javax.xml.xpath.XPathExpression;
12
import javax.xml.xpath.XPathFactory;
13
14
import org.apache.log4j.Logger;
15
import org.w3c.dom.Document;
16
import org.w3c.dom.Node;
17
18 1557 antonis.le
import eu.dnetlib.domain.ActionType;
19
import eu.dnetlib.domain.ResourceType;
20
21 1288 antonis.le
public class Notification {
22 1383 antonis.le
	private static Logger logger = Logger.getLogger(Notification.class);
23
24 1288 antonis.le
	private String subscriptionId = null;
25
	private String message = null;
26
	private String topic = null;
27
	private String isId = null;
28 6639 antonis.le
	private Date date = new Date();
29 1383 antonis.le
30
	private ActionType actionType = null;
31
	private ResourceType resourceType = null;
32
	private String resource = null;
33
	private String resourceId = null;
34
35
	public Notification(String subscriptionId, String message, String topic,
36 1557 antonis.le
			String isId) throws Exception  {
37 1383 antonis.le
		super();
38
39
		this.subscriptionId = subscriptionId;
40
		this.message = message;
41
		this.topic = topic;
42
		this.isId = isId;
43
44
		this.resource = message;
45
		this.resourceId = this.getField(message, "RESOURCE_IDENTIFIER");
46 1643 antonis.le
		this.resourceType = ResourceType.valueOf(ResourceType.class, this.getField(message, "RESOURCE_TYPE").toUpperCase());
47 1383 antonis.le
		this.actionType = ActionType.valueOf(topic.split("\\.")[0]);
48 1288 antonis.le
	}
49
50 6639 antonis.le
	public Notification(String subscriptionId, String resource,
51
			String resourceId, ResourceType resourceType, ActionType actionType) {
52
		this.resource = resource;
53
		this.resourceId = resourceId;
54
		this.resourceType = resourceType;
55
		this.actionType = actionType;
56
57
		this.subscriptionId = subscriptionId;
58
		this.message = resource;
59
		this.topic = actionType.getValue() + "." + resourceType.getValue();
60
	}
61
62 1288 antonis.le
	public String getSubscriptionId() {
63
		return subscriptionId;
64
	}
65
66
	public String getMessage() {
67
		return message;
68
	}
69
70
	public String getTopic() {
71
		return topic;
72
	}
73
74
	public String getIsId() {
75
		return isId;
76
	}
77
78 1383 antonis.le
	public Date getDate() {
79
		return date;
80 1288 antonis.le
	}
81 1383 antonis.le
82
	public ActionType getActionType() {
83
		return actionType;
84
	}
85
86 1378 antonis.le
	public ResourceType getResourceType() {
87 1383 antonis.le
		return resourceType;
88 1378 antonis.le
	}
89 1383 antonis.le
90
	public String getResource() {
91
		return resource;
92 1378 antonis.le
	}
93 1288 antonis.le
94 1378 antonis.le
	public String getResourceId() {
95 1383 antonis.le
		return resourceId;
96 1378 antonis.le
	}
97 1383 antonis.le
98
	private String getField(String xml, String fieldName) throws Exception {
99
		ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());
100
		DocumentBuilderFactory domFactory = DocumentBuilderFactory
101
				.newInstance();
102
103
		domFactory.setNamespaceAware(true); // never forget this!
104
105
		try {
106
			DocumentBuilder builder = domFactory.newDocumentBuilder();
107
			Document doc = builder.parse(bis);
108
			XPathFactory factory = XPathFactory.newInstance();
109
			XPath xpath = factory.newXPath();
110
111
			XPathExpression expression = xpath.compile("//*[local-name()='"
112
					+ fieldName + "']");
113
114
			Node node = (Node) expression.evaluate(doc, XPathConstants.NODE);
115
			String value = node.getAttributes().getNamedItem("value").getTextContent();
116
117
			logger.debug("Field : " + fieldName + ":" + value);
118
119
			return value;
120
		} catch (ParserConfigurationException e) {
121
			throw new Exception("XML Parser configuration Error", e);
122
		}
123 1378 antonis.le
	}
124 1383 antonis.le
}