Search in sources :

Example 1 with Booking

use of io.narayana.sra.demo.model.Booking in project narayana by jbosstm.

the class TripController method bookTrip.

/**
 * The quickstart scenario is:
 *
 * start LRA 1
 *   Book hotel
 *   start LRA 2
 *     start LRA 3
 *       Book flight option 1
 *     start LRA 4
 *       Book flight option 2
 *
 * @param hotelName hotel name
 * @param hotelGuests number of beds required
 * @param flightSeats number of people flying
 */
@POST
@Path("/book")
@Produces(MediaType.APPLICATION_JSON)
// delayClose because we want the LRA to be associated with a booking until the user confirms the booking
@SRA(value = SRA.Type.REQUIRED, delayCommit = true)
public Response bookTrip(@HeaderParam(SRA_HTTP_HEADER) String SRAId, @QueryParam(HotelController.HOTEL_NAME_PARAM) @DefaultValue("") String hotelName, @QueryParam(HotelController.HOTEL_BEDS_PARAM) @DefaultValue("1") Integer hotelGuests, @QueryParam(FlightController.FLIGHT_NUMBER_PARAM) @DefaultValue("") String flightNumber, @QueryParam(FlightController.ALT_FLIGHT_NUMBER_PARAM) @DefaultValue("") String altFlightNumber, @QueryParam(FlightController.FLIGHT_SEATS_PARAM) @DefaultValue("1") Integer flightSeats, @QueryParam("mstimeout") @DefaultValue("500") Long timeout) throws BookingException {
    Booking hotelBooking = bookHotel(hotelName, hotelGuests);
    Booking flightBooking1 = bookFlight(flightNumber, flightSeats);
    Booking flightBooking2 = bookFlight(altFlightNumber, flightSeats);
    Booking tripBooking = new Booking(SRAId, "Trip", hotelBooking, flightBooking1, flightBooking2);
    return Response.status(Response.Status.CREATED).entity(tripBooking).build();
}
Also used : Booking(io.narayana.sra.demo.model.Booking) Path(javax.ws.rs.Path) SRA(io.narayana.sra.annotation.SRA) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 2 with Booking

use of io.narayana.sra.demo.model.Booking in project narayana by jbosstm.

the class TripService method mergeBookingResponse.

private Booking mergeBookingResponse(Booking tripBooking) {
    URL bookingId = SRAClient.lraToURL(tripBooking.getId());
    // each string is a json encoded tripBooking
    List<String> bookingDetails = lraClient.getResponseData(bookingId);
    // List<Booking> bookings = bookingDetails.stream().map(Booking::fromJson).collect(Collectors.toList());
    // convert the list of bookings into a map keyed by Booking::getId()
    Map<String, Booking> bookings = bookingDetails.stream().map(Booking::fromJson).collect(Collectors.toMap(Booking::getId, Function.identity()));
    // update tripBooking with bookings returned in the data returned from the trip setConfirmed request
    // the array of bookings in this trip booking
    Arrays.stream(tripBooking.getDetails()).filter(// pick out bookings for which we have updated data
    b -> bookings.containsKey(b.getId())).forEach(// merge in the changes (returned from the setConfirmed request)
    b -> b.merge(bookings.get(b.getId())));
    return tripBooking;
}
Also used : Arrays(java.util.Arrays) BookingStatus(io.narayana.sra.demo.model.BookingStatus) URL(java.net.URL) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Inject(javax.inject.Inject) List(java.util.List) SRAClientAPI(io.narayana.sra.client.SRAClientAPI) Response(javax.ws.rs.core.Response) Map(java.util.Map) WebApplicationException(javax.ws.rs.WebApplicationException) ApplicationScoped(javax.enterprise.context.ApplicationScoped) SRAClient(io.narayana.sra.client.SRAClient) Booking(io.narayana.sra.demo.model.Booking) Booking(io.narayana.sra.demo.model.Booking) URL(java.net.URL)

Example 3 with Booking

use of io.narayana.sra.demo.model.Booking in project narayana by jbosstm.

the class TripService method confirmBooking.

public Booking confirmBooking(Booking tripBooking) {
    System.out.printf("Confirming tripBooking id %s (%s) status: %s%n", tripBooking.getId(), tripBooking.getName(), tripBooking.getStatus());
    if (tripBooking.getStatus() == BookingStatus.CANCEL_REQUESTED)
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Trying to setConfirmed a tripBooking which needs to be cancelled").build());
    Booking prev = add(tripBooking);
    if (prev != null)
        System.out.printf("Seen this tripBooking before%n");
    // check the booking to see if the client wants to requestCancel any dependent bookings
    Arrays.stream(tripBooking.getDetails()).filter(Booking::isCancelPending).forEach(b -> {
        lraClient.cancelSRA(SRAClient.lraToURL(b.getId(), "Invalid " + b.getType() + " tripBooking id format"));
        b.setCanceled();
    });
    tripBooking.setConfirming();
    lraClient.commitSRA(SRAClient.lraToURL(tripBooking.getId()));
    tripBooking.setConfirmed();
    return mergeBookingResponse(tripBooking);
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) Booking(io.narayana.sra.demo.model.Booking)

Example 4 with Booking

use of io.narayana.sra.demo.model.Booking in project narayana by jbosstm.

the class HotelService method book.

public Booking book(String bid, String hotel, Integer beds) {
    ;
    Booking booking = new Booking(bid, hotel, beds, "Hotel");
    add(booking);
    return booking;
}
Also used : Booking(io.narayana.sra.demo.model.Booking)

Example 5 with Booking

use of io.narayana.sra.demo.model.Booking in project narayana by jbosstm.

the class FlightService method book.

public Booking book(String bid, String flightNumber, Integer seats) {
    Booking booking = new Booking(bid, flightNumber, seats, "Flight");
    add(booking);
    return booking;
}
Also used : Booking(io.narayana.sra.demo.model.Booking)

Aggregations

Booking (io.narayana.sra.demo.model.Booking)6 WebApplicationException (javax.ws.rs.WebApplicationException)3 SRA (io.narayana.sra.annotation.SRA)1 SRAClient (io.narayana.sra.client.SRAClient)1 SRAClientAPI (io.narayana.sra.client.SRAClientAPI)1 BookingStatus (io.narayana.sra.demo.model.BookingStatus)1 URL (java.net.URL)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Inject (javax.inject.Inject)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Response (javax.ws.rs.core.Response)1