]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java
[XVSM] Delivery in progress, Delivery done updates in UIs, Delivery logic
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / common / Util.java
1 package at.ac.tuwien.sbc.valesriegler.common;
2
3 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
4 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory;
7
8 import javax.swing.*;
9 import javax.swing.border.TitledBorder;
10 import java.awt.*;
11 import java.net.URI;
12 import java.util.*;
13 import java.util.List;
14
15 public abstract class Util {
16         private static final Logger log = LoggerFactory.getLogger(Util.class);
17         public static boolean useJMS = false;
18
19         public static final String TABLE_ASSIGNED = "tables";
20         public static final String ASSIGN_TABLE = "assignTable";
21         public static final String TAKE_ORDER = "takeOrder";
22         public static final String ORDER_TAKEN = "order";
23         public static final String DELIVERY_ORDER_TAKEN = "deliverOrderTaken";
24         public static final String DELIVER_PIZZAS = "deliverPizzas";
25         public static final String DELIVER_DELIVERY_PIZZAS = "deliverDeliveryPizzas";
26         public static final String DELIVER_DELIVERY_ORDER = "deliverDeliveryOrderContainer";
27         public static final String PREPARE_PIZZAS = "preparePizzas";
28         public static final String PREPARE_DELIVERY_PIZZAS = "prepareDeliveryPizzas";
29         public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress";
30         public static final String ORDER_COMPLETE = "orderComplete";
31         public static final String PAYMENT_REQUEST = "payment";
32         public static final String FREE_TABLES = "freeTables";
33         public static final String PAYMENT_DONE = "hasPaid";
34         public static final String PIZZERIA_INFO = "pizzeriaInfo";
35         public static final String PHONE_CALLS = "phoneCalls";
36         public static final String GROUP_AGENT_INFO = "groupAgentInfo";
37         public static final String PIZZERIA_GROUP = "pizzeriaGroupContainer";
38         public static final String PIZZERIA_TABLE = "pizzeriaTableContainer";
39         public static final String PIZZERIA_DELIVERY = "pizzeriaDeliveryContainer";
40         public static final String DELIVERY_PROGRESS = "deliveryInProgress";
41         public static final String DELIVERY_DONE = "deliveryDone";
42         public static final String BILL_PAID = "billPaid";
43
44
45     public static final String NUMBER_DISPLAY_FORMAT = String.format("%%0%dd", 3);
46
47         private static Random random = new Random();
48
49         public static final long SPACE_TRANSACTION_TIMEOUT = 1500;
50
51         public static final String SERVER_ADDR = "xvsm://localhost:%d";
52         public static final int GROUP_AGENT_PORT = 9876;
53         public static final int DELIVERY_CUSTOMERS_PORT = 9877;
54
55         public static final String JMS_NAMING_SERVICE = "tcp://localhost:61616?jms.prefetchPolicy.all=1";
56
57         public static String getId(int id) {
58                 return (id != 0 && id != -1) ? String.valueOf(id) : "";
59         }
60
61         public static int getIntSafe(Integer nr) {
62                 return nr == null ? 0 : nr;
63         }
64
65         public static URI createURI(int port) {
66                 return URI.create(String.format(SERVER_ADDR, port));
67         }
68
69         public static <T extends HasId> Map<Integer, T> intoMapById(List<T> hasIds) {
70                 if (hasIds == null) {
71                         return Collections.emptyMap();
72                 }
73
74                 Map<Integer, T> myMap = new HashMap<>();
75                 for (T hasId : hasIds) {
76                         myMap.put(hasId.getId(), hasId);
77                 }
78                 return myMap;
79         }
80
81         public static int getRandom(int min, int max) {
82                 return random.nextInt(max - min + 1) + min;
83         }
84
85         public static Tuple<Integer> parseIdAndSpacePort(String[] args, String usage) {
86                 if (args.length != 2) {
87                         throw new IllegalArgumentException(usage);
88                 }
89
90                 int parsedId = 0;
91                 int port = 0;
92                 try {
93                         parsedId = Integer.parseInt(args[0]);
94                         port = Integer.parseInt(args[1]);
95                 } catch (NumberFormatException e) {
96                         throw new IllegalArgumentException(usage);
97                 }
98
99                 return new Tuple<Integer>(parsedId, port);
100         }
101
102         public static JTable createTableInTitledPanel(JPanel wrapperPanel, TableModel<?> model, String title) {
103                 JTable table = new JTable(model);
104                 wrapperPanel.setLayout(new GridBagLayout());
105                 GridBagConstraints c = new GridBagConstraints();
106                 c.gridx = 0;
107                 c.weightx = 1;
108                 c.weighty = 1;
109                 c.insets = new Insets(10, 10, 10, 10);
110                 c.fill = GridBagConstraints.BOTH;
111                 JScrollPane scrollPane = new JScrollPane(table);
112
113                 wrapperPanel.setBorder(new TitledBorder(title));
114                 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
115                 wrapperPanel.add(scrollPane, c);
116                 table.setAutoCreateRowSorter(true);
117                 return table;
118         }
119
120         /**
121          * 
122          * @return A display string for PizzaTypes.
123          *         <p />
124          *         e.g. Cardinale, Cardinale, Margherita is displayed as "2xCARDINALE,1xMARGHERITA"
125          */
126         public static String pizzaDisplay(List<PizzaOrder> list) {
127                 HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
128                 for (PizzaOrder pizzaOrder : list) {
129                         PizzaType pizzaType = pizzaOrder.getPizzaType();
130                         if (pizzaCount.containsKey(pizzaType)) {
131                                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
132                         } else
133                                 pizzaCount.put(pizzaType, 1);
134                 }
135                 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
136                 StringBuilder sb = new StringBuilder();
137
138                 boolean multiplePizzas = false;
139                 for (PizzaType pizzaType : pizzaTypes) {
140                         if (multiplePizzas)
141                                 sb.append(", ");
142                         else
143                                 multiplePizzas = true;
144
145                         sb.append(pizzaCount.get(pizzaType) + "x");
146                         sb.append(pizzaType.toString());
147                 }
148
149                 return sb.toString();
150         }
151
152         public static void handleSpaceErrorAndTerminate(Exception e) {
153                 log.error(e.getMessage());
154                 e.printStackTrace();
155                 System.exit(1);
156         }
157 }