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