]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java
[JMS] GG should be able to launch deliveryorders now.
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / group / DeliveryGroup.java
1 package at.ac.tuwien.sbc.valesriegler.group;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5
6 import at.ac.tuwien.sbc.valesriegler.common.HasId;
7 import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest;
8 import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector;
9 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
10 import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus;
11
12 public class DeliveryGroup implements Runnable, HasId {
13         private static final Logger log = LoggerFactory.getLogger(DeliveryGroup.class);
14         private static int idNext = 0;
15
16         private DeliveryGroupData deliveryGroupData;
17
18         public DeliveryGroup() {
19                 deliveryGroupData = new DeliveryGroupData(++idNext);
20         }
21
22         public DeliveryGroup(int id) {
23                 deliveryGroupData = new DeliveryGroupData(id);
24         }
25
26         public DeliveryGroupData getDeliveryGroupData() {
27                 return deliveryGroupData;
28         }
29
30         @Override
31         public int getId() {
32                 return deliveryGroupData.getId();
33         }
34
35         public void setDeliveryGroupData(DeliveryGroupData deliveryGroupData) {
36                 this.deliveryGroupData = deliveryGroupData;
37         }
38
39         @Override
40         public void run() {
41                 log.debug("Thread started for:" + this);
42                 try {
43                         Thread.sleep((long) (Math.random() * 10000));
44                         switch (deliveryGroupData.getDeliveryStatus()) {
45                                 case START : // should not happen.
46                                         break;
47                                 case ORDER_PENDING :
48                                         JMSGroupConnector conn = JMSGroupConnector.getConnector(deliveryGroupData.getPizzeriaId());
49                                         conn.send(new DeliveryOrderRequest(deliveryGroupData));
50                                         break;
51                                 case IN_PROGRESS : // do nothing
52                                         break;
53                                 case DELIVERED : // do nothing
54                                         break;
55                                 case PAID : // do nothing
56                                         break;
57                                 case IS_DELIVERED : // do nothing
58                                         break;
59                                 case DELIVERY_FAILED : // do nothing
60                                         break;
61                                 default : // do nothing
62                                         break;
63                         }
64
65                 } catch (InterruptedException e) {
66                         e.printStackTrace();
67                 }
68         }
69
70         // lets order at the pizzaria.
71         public void orderSomeFood() {
72                 log.debug("orderSomeFood(): " + this);
73                 deliveryGroupData.setDeliveryStatus(DeliveryStatus.ORDER_PENDING);
74                 GroupAgent.getInstance().getGroupModel().fireTableDataChanged();
75                 new Thread(this).start();
76         }
77 }