Search in sources :

Example 1 with FlightInfo

use of org.switchyard.quickstarts.camel.sap.binding.jaxb.FlightInfo in project quickstarts by jboss-switchyard.

the class CreateFlightTripMessageComposer method compose.

@Override
public Message compose(CamelBindingData source, Exchange exchange) throws Exception {
    Message response = exchange.createMessage();
    // map context properties
    getContextMapper().mapFrom(source, exchange.getContext(response));
    Set<String> attachements = source.getMessage().getAttachmentNames();
    if (!attachements.isEmpty()) {
        for (Entry<String, DataHandler> entry : source.getMessage().getAttachments().entrySet()) {
            response.addAttachment(entry.getKey(), entry.getValue().getDataSource());
        }
    }
    // Retrieve flight connection and passenger info from exchange context property.
    FlightTripRequestInfo flightTripRequestInfo = (FlightTripRequestInfo) exchange.getContext().getProperty(FLIGHT_TRIP_REQUEST_INFO).getValue();
    FlightConnectionInfo flightConnectionInfo = flightTripRequestInfo.getFlightConnectionInfo();
    PassengerInfo passengerInfo = flightTripRequestInfo.getPassengerInfo();
    // Retrieve SAP response object from body of exchange message.
    Structure flightTripCreateResponse = source.getMessage().getBody(Structure.class);
    if (flightTripCreateResponse == null) {
        throw new Exception("No Flight Trip Create Response");
    }
    // Check BAPI return parameter for errors 
    @SuppressWarnings("unchecked") Table<Structure> bapiReturn = flightTripCreateResponse.get("RETURN", Table.class);
    Structure bapiReturnEntry = bapiReturn.get(0);
    if (!bapiReturnEntry.get("TYPE", String.class).equals("S")) {
        String message = bapiReturnEntry.get("MESSAGE", String.class);
        throw new Exception("BAPI call failed: " + message);
    }
    // Create bean to hold Flight Booking data.
    BookFlightResponse bookFlightResponse = new BookFlightResponse();
    // Trip Number
    String tripNumber = flightTripCreateResponse.get("TRIPNUMBER", String.class);
    if (tripNumber != null) {
        bookFlightResponse.setTripNumber(tripNumber);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Added TRIPNUMBER = '{}' to request", tripNumber);
        }
    } else {
        throw new Exception("No Flight Booking Trip Number");
    }
    // Pricing Info
    Structure ticketPrice = flightTripCreateResponse.get("TICKET_PRICE", Structure.class);
    if (ticketPrice != null) {
        // Ticket Price
        BigDecimal tripPrice = ticketPrice.get("TRIPPRICE", BigDecimal.class);
        bookFlightResponse.setTicketPrice(tripPrice);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Added TICKET_PRICE = '{}' to request", tripPrice);
        }
        // Ticket Tax
        BigDecimal tripTax = ticketPrice.get("TRIPTAX", BigDecimal.class);
        bookFlightResponse.setTicketTax(tripTax);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Added TICKET_TAX = '{}' to request", tripTax);
        }
        // Currency
        String currency = ticketPrice.get("CURR", String.class);
        bookFlightResponse.setCurrency(currency);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Added CURRENCY = '{}' to request", currency);
        }
    } else {
        throw new Exception("No Flight Booking Ticket Price");
    }
    // Passenger Info
    //  Form
    bookFlightResponse.setPassengerFormOfAddress(passengerInfo.getFormOfAddress());
    //  Name
    bookFlightResponse.setPassengerName(passengerInfo.getName());
    //  DOB
    bookFlightResponse.setPassengerDateOfBirth(passengerInfo.getDateOfBirth());
    // Flight Info
    FlightInfo flightInfo = new FlightInfo();
    //  Flight Time
    flightInfo.setFlightTime(flightConnectionInfo.getFlightTime());
    //  Departure City
    flightInfo.setCityFrom(flightConnectionInfo.getDepartureCity());
    //  Departure Date
    flightInfo.setDepartureDate(flightConnectionInfo.getDepartureDate());
    //  Departure Time
    flightInfo.setDepartureTime(flightConnectionInfo.getDepartureTime());
    //  Arrival City
    flightInfo.setCityTo(flightConnectionInfo.getArrivalCity());
    //  Arrival Date
    flightInfo.setArrivalDate(flightConnectionInfo.getArrivalDate());
    //  Arrival Time
    flightInfo.setArrivalTime(flightConnectionInfo.getArrivalTime());
    bookFlightResponse.setFlightInfo(flightInfo);
    ConnectionInfoTable connectionInfoTable = new ConnectionInfoTable();
    List<ConnectionInfo> rows = new ArrayList<ConnectionInfo>();
    for (FlightHop flightHop : flightConnectionInfo.getFlightHopList()) {
        // Connection Info
        ConnectionInfo connection = new ConnectionInfo();
        //  Connection ID
        connection.setConnectionId(flightHop.getHopNumber());
        //  Airline
        connection.setAirline(flightHop.getAirlineName());
        //  Plane Type
        connection.setPlaneType(flightHop.getAircraftType());
        //  Departure City
        connection.setCityFrom(flightHop.getDepatureCity());
        //  Departure Date
        connection.setDepartureDate(flightHop.getDepatureDate());
        //  Departure Time
        connection.setDepartureTime(flightHop.getDepatureTime());
        //  Arrival City
        connection.setCityTo(flightHop.getArrivalCity());
        //  Arrival Date
        connection.setArrivalDate(flightHop.getArrivalDate());
        //  Arrival Time
        connection.setArrivalTime(flightHop.getArrivalTime());
        rows.add(connection);
    }
    connectionInfoTable.setRows(rows);
    bookFlightResponse.setConnectionInfo(connectionInfoTable);
    response.setContent(bookFlightResponse);
    return response;
}
Also used : Message(org.switchyard.Message) ConnectionInfoTable(org.switchyard.quickstarts.camel.sap.binding.jaxb.ConnectionInfoTable) ArrayList(java.util.ArrayList) FlightConnectionInfo(org.switchyard.quickstarts.camel.sap.binding.bean.FlightConnectionInfo) PassengerInfo(org.switchyard.quickstarts.camel.sap.binding.bean.PassengerInfo) DataHandler(javax.activation.DataHandler) BigDecimal(java.math.BigDecimal) FlightHop(org.switchyard.quickstarts.camel.sap.binding.bean.FlightHop) FlightInfo(org.switchyard.quickstarts.camel.sap.binding.jaxb.FlightInfo) BookFlightResponse(org.switchyard.quickstarts.camel.sap.binding.jaxb.BookFlightResponse) FlightTripRequestInfo(org.switchyard.quickstarts.camel.sap.binding.bean.FlightTripRequestInfo) ConnectionInfo(org.switchyard.quickstarts.camel.sap.binding.jaxb.ConnectionInfo) FlightConnectionInfo(org.switchyard.quickstarts.camel.sap.binding.bean.FlightConnectionInfo) Structure(org.fusesource.camel.component.sap.model.rfc.Structure)

Aggregations

BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 DataHandler (javax.activation.DataHandler)1 Structure (org.fusesource.camel.component.sap.model.rfc.Structure)1 Message (org.switchyard.Message)1 FlightConnectionInfo (org.switchyard.quickstarts.camel.sap.binding.bean.FlightConnectionInfo)1 FlightHop (org.switchyard.quickstarts.camel.sap.binding.bean.FlightHop)1 FlightTripRequestInfo (org.switchyard.quickstarts.camel.sap.binding.bean.FlightTripRequestInfo)1 PassengerInfo (org.switchyard.quickstarts.camel.sap.binding.bean.PassengerInfo)1 BookFlightResponse (org.switchyard.quickstarts.camel.sap.binding.jaxb.BookFlightResponse)1 ConnectionInfo (org.switchyard.quickstarts.camel.sap.binding.jaxb.ConnectionInfo)1 ConnectionInfoTable (org.switchyard.quickstarts.camel.sap.binding.jaxb.ConnectionInfoTable)1 FlightInfo (org.switchyard.quickstarts.camel.sap.binding.jaxb.FlightInfo)1