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();
}
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;
}
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);
}
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;
}
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;
}
Aggregations