UP | HOME

ContainerManagementEndpoint.java

package de.botzenhart.springws.cm.ws;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.xpath.XPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import de.botzenhart.springws.cm.service.ServiceContainerManagement;

@Endpoint
public class EndpointContainerManagement {

   private XPath routeExpression;
   private XPath startDateExpression;
   private XPath endDateExpression;

   private XPath containerCodeExpression;
   private XPath containerNameExpression;

   private static final String NAMESPACE_URI = "http://springws.botzenhart.de/cm/schemas";
   private final ServiceContainerManagement containerManagementService;

   @Autowired
   public EndpointContainerManagement(
         ServiceContainerManagement containerManagementService)
         throws JDOMException {
      this.containerManagementService = containerManagementService;
      Namespace namespace = Namespace.getNamespace("cm", NAMESPACE_URI);
      routeExpression = XPath.newInstance("//cm:Route");
      routeExpression.addNamespace(namespace);
      startDateExpression = XPath.newInstance("//cm:StartDate");
      startDateExpression.addNamespace(namespace);
      endDateExpression = XPath.newInstance("//cm:EndDate");
      endDateExpression.addNamespace(namespace);
      containerCodeExpression = XPath.newInstance("//cm:ContainerCode");
      containerCodeExpression.addNamespace(namespace);
      containerNameExpression = XPath.newInstance("//cm:ContainerName");
      containerNameExpression.addNamespace(namespace);
   }

   @PayloadRoot(namespace = NAMESPACE_URI, localPart = "containerRequest")
   @ResponsePayload
   public Element handleContainerRequest(
         @RequestPayload Element containerRequest) throws Exception {
      // parse request body
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
      Integer route = Integer.parseInt(routeExpression
            .valueOf(containerRequest));
      Date startDate = dateFormat.parse(startDateExpression
            .valueOf(containerRequest));
      Date endDate = dateFormat.parse(endDateExpression
            .valueOf(containerRequest));
      Integer containerCode = Integer.parseInt(containerCodeExpression
            .valueOf(containerRequest));
      String containerName = containerNameExpression
            .valueOf(containerRequest);
      // invoke business logic
      boolean isAvailable = containerManagementService.containerRequest(route, startDate, endDate,
            containerCode, containerName);
      // generate response body
      Element elem = new Element("containerResponse", "cm", NAMESPACE_URI);
      Element available = new Element("available", "cm", NAMESPACE_URI);
      available.addContent(Boolean.toString(isAvailable));
      elem.addContent(available);
      return elem;
   }
}

Author: Rainer Schuler

Date: 2011-02-11 Fr

HTML generated by org-mode 7.4 in emacs 22