]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DeliveryGroupXVSM.java
[XVSM] Some fixes for making sure that Cook only prepares normal pizzas when there...
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / xvsm / DeliveryGroupXVSM.java
1 package at.ac.tuwien.sbc.valesriegler.xvsm;
2
3
4 import at.ac.tuwien.sbc.valesriegler.common.Bill;
5 import at.ac.tuwien.sbc.valesriegler.common.Util;
6 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
7 import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus;
8 import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction;
9 import org.mozartspaces.core.ContainerReference;
10 import org.mozartspaces.core.MzsConstants;
11 import org.mozartspaces.core.TransactionReference;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import java.io.Serializable;
16 import java.util.Arrays;
17 import java.util.List;
18
19 public class DeliveryGroupXVSM extends AbstractXVSMConnector {
20     private static final Logger log = LoggerFactory.getLogger(DeliveryGroupXVSM.class);
21
22     private final int pizzeriaSpacePort;
23     private final int groupId;
24
25     private final ContainerReference myContainer;
26
27     public DeliveryGroupXVSM(int groupId, int pizzeriaSpacePort, String address) {
28         super(pizzeriaSpacePort);
29         this.groupId = groupId;
30         this.pizzeriaSpacePort = pizzeriaSpacePort;
31
32
33         myContainer = useContainerOfSpaceWithPort(address, Util.DELIVERY_CUSTOMERS_PORT);
34         pizzeriaDeliveryContainer = useContainerOfSpaceWithPort(Util.PIZZERIA_DELIVERY, pizzeriaSpacePort);
35     }
36
37     public void waitForMyOrder() {
38         getDefaultBuilder().setCref(myContainer).setSpaceAction(new SpaceAction() {
39             @Override
40             public void onEntriesWritten(List<? extends Serializable> entries) throws Exception {
41
42                 final List<Serializable> entities = castEntries(entries);
43                 try {
44                     for (Serializable entity : entities) {
45                         if (entity instanceof Bill) {
46                             Bill bill = ((Bill) entity);
47                             if (bill.groupId == groupId) {
48
49                                 final DeliveryGroupData template = new DeliveryGroupData(groupId);
50                                 template.setDeliveryStatus(null);
51                                 final TransactionReference tx = getDefaultTransaction();
52                                 final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, MzsConstants.RequestTimeout.INFINITE, "Cannot get the delivery order!");
53                                 group.setDeliveryStatus(DeliveryStatus.PAID);
54                                 sendItemsToContainer(Arrays.asList(group), pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.DEFAULT, tx);
55                                 capi.commitTransaction(tx);
56
57                                 notificationMgr.shutdown();
58                             }
59                         }
60                     }
61                 } catch (Exception e) {
62                     log.error("Delivery group exception!");
63                     e.printStackTrace();
64                 }
65
66             }
67         }).createSpaceListenerImpl();
68     }
69 }