1 package at.ac.tuwien.sbc.valesriegler.xvsm;
4 import at.ac.tuwien.sbc.valesriegler.common.Util;
5 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
6 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
7 import at.ac.tuwien.sbc.valesriegler.types.Order;
8 import at.ac.tuwien.sbc.valesriegler.types.Pizza;
9 import org.mozartspaces.core.MzsConstants;
10 import org.mozartspaces.core.TransactionReference;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
14 import java.io.Serializable;
15 import java.util.Arrays;
16 import java.util.List;
18 public class DriverXVSM extends AbstractXVSMConnector {
19 private static final Logger log = LoggerFactory.getLogger(DriverXVSM.class);
23 public DriverXVSM(int id, int port) {
28 this.deliverDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS);
32 public void listenForPreparedPizzas() {
34 * A waiter gets informed when a new pizza is complete. He takes the
35 * orderId of the pizza and looks up the corresponding order from which
36 * he gets the number of necessary pizzas of the order. He then tries to
37 * fetch all pizzas with the corresponding orderId and compares the
38 * number of those pizzas with the number of necessary pizzas. If all
39 * pizzas of an order are complete he then delivers them!
41 SpaceListener preparedPizzasListener = new SpaceListenerImpl(capi, deliverDeliveryPizzasContainer) {
44 void onEntriesWritten(List<? extends Serializable> entries)
47 List<Pizza> pizzas = castEntries(entries);
49 for (Pizza pizza : pizzas) {
50 int orderId = pizza.getOrderId();
51 Order order = new Order();
54 TransactionReference tx = getDefaultTransaction();
55 final boolean isDeliveryPizza = pizza.isDeliveryPizza();
58 DeliveryGroupData entity = new DeliveryGroupData();
59 entity.setDeliveryStatus(null);
60 entity.setOrder(order);
62 DeliveryGroupData groupData = takeMatchingEntity(entity,
63 orderTakenContainer, tx, MzsConstants.RequestTimeout.DEFAULT,
64 "Another driver just checks if the delivery order is complete");
65 int groupId = groupData.getId();
66 int numberOfPizzas = order.getNumberOfPizzas();
68 Pizza pizzaTemplate = new Pizza();
69 pizzaTemplate.setOrderId(orderId);
71 List<Pizza> pizzasOfOrder = takeMatchingEntities(
72 pizzaTemplate, deliverPizzasContainer, tx,
73 MzsConstants.RequestTimeout.DEFAULT,
74 "Cannot take the pizzas from the deliverDeliveryPizzasContainer");
76 if (pizzasOfOrder.size() == numberOfPizzas) {
77 DeliveryGroupData group = new DeliveryGroupData();
78 group.setDriverId(driverId);
79 Order completeOrder = new Order();
80 completeOrder.setId(orderId);
81 completeOrder.setGroupId(groupId);
82 group.setOrder(completeOrder);
83 final List<DeliveryGroupData> groupsWithCompleteOrder = Arrays.asList(group);
84 sendItemsToContainer(groupsWithCompleteOrder,
85 orderCompleteContainer, MzsConstants.RequestTimeout.DEFAULT,
88 capi.commitTransaction(tx);
90 log.info("Not yet all pizzas prepared! Order with id "
91 + orderId + " has " + numberOfPizzas
92 + " pizzas, but only " + pizzasOfOrder.size()
93 + " pizzas are ready by now!");
94 capi.rollbackTransaction(tx);
96 } catch (NullPointerException e) {
98 } catch (Exception e) {
99 capi.rollbackTransaction(tx);
105 createNotification(preparedPizzasListener, deliverDeliveryPizzasContainer);