]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java
Add wizard for creating delivery groups to GroupFrame
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / group / GroupAgent.java
1 package at.ac.tuwien.sbc.valesriegler.group;
2
3 import at.ac.tuwien.sbc.valesriegler.common.Util;
4 import at.ac.tuwien.sbc.valesriegler.group.gui.DeliveryOverviewModel;
5 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel;
6 import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector;
7 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
8 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import javax.swing.*;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16
17 /**
18  * The Main class of the Group component.
19  * <p/>
20  * Start the communication and the group GUI:
21  *
22  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
23  * @author jan
24  */
25 public class GroupAgent {
26     private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
27
28     private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
29     private GroupOverviewModel groupModel;
30     private AbstractGroupConnector groupconn;
31     private DeliveryOverviewModel deliveryOverviewModel;
32     private GroupAgentXVSM xvsm;
33
34     /* Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
35     notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
36     selecting for which pizzeria the customer groups are created
37     In case of XVSM the identifier string is a space URI*/
38     private List<String> pizzeriaIdentifiers = Collections.synchronizedList(new ArrayList<String>());
39
40     public static GroupAgent groupAgent;
41
42     public static void main(String[] args) {
43         if (args.length != 1) {
44             throw new IllegalArgumentException(USAGE);
45         }
46
47         String mw = args[0];
48         log.info("Middleware: " + mw);
49
50         if ("JMS".equalsIgnoreCase(mw)) {
51             groupAgent = new GroupAgent(new JMSGroupConnector());
52             Util.useJMS = true;
53         } else if ("XVSM".equalsIgnoreCase(mw)) {
54             // TODO: XVSM Connector?
55             Util.useJMS = false;
56             groupAgent = new GroupAgent(null);
57         } else {
58             throw new IllegalArgumentException(USAGE);
59         }
60
61         SwingUtilities.invokeLater(new GroupGUI());
62     }
63
64     public GroupAgent(AbstractGroupConnector groupconn) {
65         groupModel = new GroupOverviewModel();
66         if (Util.useJMS) {
67             this.groupconn = groupconn;
68             groupconn.init();
69         } else {
70             xvsm = new GroupAgentXVSM();
71
72             xvsm.listenForTableAssigned();
73             xvsm.listenForOrdersTaken();
74             xvsm.listenForDeliveredOrders();
75             xvsm.listenForPaymentRequest();
76             xvsm.listenForPaymentDone();
77         }
78     }
79
80     public static GroupAgent getInstance() {
81         return groupAgent;
82     }
83
84     public AbstractGroupConnector getGroupcomm() {
85         return groupconn;
86     }
87
88     public List<String> getPizzeriaIdentifiers() {
89         return pizzeriaIdentifiers;
90     }
91
92     public GroupOverviewModel getGroupModel() {
93         return groupModel;
94     }
95
96     public DeliveryOverviewModel getDeliveryModel() {
97         return deliveryOverviewModel;
98     }
99
100     public void onGroupsCreated(List<Group> newGroups, boolean isDeliveryGroup) {
101         if (!Util.useJMS) {
102             List<GroupData> groupData = new ArrayList<>();
103
104             for (Group group : newGroups) {
105                 groupData.add(group.getGroupData());
106             }
107             xvsm.sendNewGroupsToSpace(groupData);
108
109             log.info("New Groups were sent to the space");
110
111             // start the space group in a new thread
112             for (GroupData group : groupData) {
113                 new Thread(new SpaceGroup(group.getId())).start();
114             }
115         }
116
117     }
118
119
120 }