Upgraded to JDOM 2.0.1
This commit is contained in:
@@ -16,7 +16,9 @@
|
||||
|
||||
package com.mycompany.hr.ws;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -25,10 +27,12 @@ import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
|
||||
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
|
||||
|
||||
import com.mycompany.hr.service.HumanResourceService;
|
||||
import org.jdom.Element;
|
||||
import org.jdom.JDOMException;
|
||||
import org.jdom.Namespace;
|
||||
import org.jdom.xpath.XPath;
|
||||
import org.jdom2.Element;
|
||||
import org.jdom2.JDOMException;
|
||||
import org.jdom2.Namespace;
|
||||
import org.jdom2.filter.Filters;
|
||||
import org.jdom2.xpath.XPathExpression;
|
||||
import org.jdom2.xpath.XPathFactory;
|
||||
|
||||
/**
|
||||
* This endpoint handles holiday requests. It uses a combination of JDOM and XPath to extract interesting pieces of XML
|
||||
@@ -41,11 +45,13 @@ public class HolidayEndpoint {
|
||||
|
||||
private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas";
|
||||
|
||||
private XPath startDateExpression;
|
||||
private XPathExpression<Element> startDateExpression;
|
||||
|
||||
private XPath endDateExpression;
|
||||
private XPathExpression<Element> endDateExpression;
|
||||
|
||||
private XPath nameExpression;
|
||||
private XPathExpression<Element> firstNameExpression;
|
||||
|
||||
private XPathExpression<Element> lastNameExpression;
|
||||
|
||||
private HumanResourceService humanResourceService;
|
||||
|
||||
@@ -53,22 +59,30 @@ public class HolidayEndpoint {
|
||||
public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException {
|
||||
this.humanResourceService = humanResourceService;
|
||||
Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI);
|
||||
startDateExpression = XPath.newInstance("//hr:StartDate");
|
||||
startDateExpression.addNamespace(namespace);
|
||||
endDateExpression = XPath.newInstance("//hr:EndDate");
|
||||
endDateExpression.addNamespace(namespace);
|
||||
nameExpression = XPath.newInstance("concat(//hr:FirstName,' ',//hr:LastName)");
|
||||
nameExpression.addNamespace(namespace);
|
||||
XPathFactory xPathFactory = XPathFactory.instance();
|
||||
startDateExpression = xPathFactory.compile("//hr:StartDate", Filters.element(), null, namespace);
|
||||
endDateExpression = xPathFactory.compile("//hr:EndDate", Filters.element(), null, namespace);
|
||||
firstNameExpression = xPathFactory.compile("//hr:FirstName", Filters.element(), null, namespace);
|
||||
lastNameExpression = xPathFactory.compile("//hr:LastName", Filters.element(), null, namespace);
|
||||
}
|
||||
|
||||
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "HolidayRequest")
|
||||
public void handleHolidayRequest(@RequestPayload Element holidayRequest) throws Exception {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
Date startDate = dateFormat.parse(startDateExpression.valueOf(holidayRequest));
|
||||
Date endDate = dateFormat.parse(endDateExpression.valueOf(holidayRequest));
|
||||
String name = nameExpression.valueOf(holidayRequest);
|
||||
Date startDate = parseDate(startDateExpression, holidayRequest);
|
||||
Date endDate = parseDate(endDateExpression, holidayRequest);
|
||||
String name = firstNameExpression.evaluateFirst(holidayRequest).getText() + " " + lastNameExpression.evaluateFirst(holidayRequest).getText();
|
||||
|
||||
humanResourceService.bookHoliday(startDate, endDate, name);
|
||||
}
|
||||
|
||||
private Date parseDate(XPathExpression<Element> expression, Element element) throws ParseException {
|
||||
Element result = expression.evaluateFirst(element);
|
||||
if (result != null) {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
return dateFormat.parse(result.getText());
|
||||
} else {
|
||||
throw new IllegalArgumentException("Could not evaluate [" + expression + "] on [" + element + "]");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user