1 package at.ac.tuwien.sbc.valesriegler.common;
3 import java.awt.GridBagConstraints;
4 import java.awt.GridBagLayout;
5 import java.awt.Insets;
7 import java.util.Collections;
8 import java.util.HashMap;
11 import java.util.Random;
14 import javax.swing.JPanel;
15 import javax.swing.JScrollPane;
16 import javax.swing.JTable;
17 import javax.swing.ScrollPaneConstants;
18 import javax.swing.border.TitledBorder;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
24 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
26 public abstract class Util {
27 private static final Logger log = LoggerFactory.getLogger(Util.class);
28 public static boolean useJMS = true;
30 public static final String TABLE_ASSIGNED = "tables";
31 public static final String ASSIGN_TABLE = "assignTable";
32 public static final String TAKE_ORDER = "takeOrder";
33 public static final String ORDER_TAKEN = "order";
34 public static final String DELIVERY_ORDER_TAKEN = "deliverOrderTaken";
35 public static final String DELIVER_PIZZAS = "deliverPizzas";
36 public static final String DELIVER_DELIVERY_PIZZAS = "deliverDeliveryPizzas";
37 public static final String DELIVER_DELIVERY_ORDER = "deliverDeliveryOrderContainer";
38 public static final String PREPARE_PIZZAS = "preparePizzas";
39 public static final String PREPARE_DELIVERY_PIZZAS = "prepareDeliveryPizzas";
40 public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress";
41 public static final String ORDER_COMPLETE = "orderComplete";
42 public static final String PAYMENT_REQUEST = "payment";
43 public static final String FREE_TABLES = "freeTables";
44 public static final String PAYMENT_DONE = "hasPaid";
45 public static final String PIZZERIA_INFO = "pizzeriaInfo";
46 public static final String PHONE_CALLS = "phoneCalls";
47 public static final String GROUP_AGENT_INFO = "groupAgentInfo";
48 public static final String PIZZERIA_GROUP = "pizzeriaGroupContainer";
49 public static final String PIZZERIA_TABLE = "pizzeriaTableContainer";
50 public static final String PIZZERIA_DELIVERY = "pizzeriaDeliveryContainer";
52 private static Random random = new Random();
54 public static final long SPACE_TRANSACTION_TIMEOUT = 1500;
56 public static final String SERVER_ADDR = "xvsm://localhost:%d";
57 public static final int GROUP_AGENT_PORT = 9876;
58 public static final int DELIVERY_CUSTOMERS_PORT = 9877;
60 public static final String JMS_NAMING_SERVICE = "tcp://localhost:61616?jms.prefetchPolicy.all=1";
62 public static String getId(int id) {
63 return (id != 0 && id != -1) ? String.valueOf(id) : "";
66 public static int getIntSafe(Integer nr) {
67 return nr == null ? 0 : nr;
70 public static URI createURI(int port) {
71 return URI.create(String.format(SERVER_ADDR, port));
74 public static <T extends HasId> Map<Integer, T> intoMapById(List<T> hasIds) {
76 return Collections.emptyMap();
79 Map<Integer, T> myMap = new HashMap<>();
80 for (T hasId : hasIds) {
81 myMap.put(hasId.getId(), hasId);
86 public static int getRandom(int min, int max) {
87 return random.nextInt(max - min + 1) + min;
90 public static Tuple<Integer> parseIdAndSpacePort(String[] args, String usage) {
91 if (args.length != 2) {
92 throw new IllegalArgumentException(usage);
98 parsedId = Integer.parseInt(args[0]);
99 port = Integer.parseInt(args[1]);
100 } catch (NumberFormatException e) {
101 throw new IllegalArgumentException(usage);
104 return new Tuple<Integer>(parsedId, port);
107 public static JTable createTableInTitledPanel(JPanel wrapperPanel, TableModel<?> model, String title) {
108 JTable table = new JTable(model);
109 wrapperPanel.setLayout(new GridBagLayout());
110 GridBagConstraints c = new GridBagConstraints();
114 c.insets = new Insets(10, 10, 10, 10);
115 c.fill = GridBagConstraints.BOTH;
116 JScrollPane scrollPane = new JScrollPane(table);
118 wrapperPanel.setBorder(new TitledBorder(title));
119 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
120 wrapperPanel.add(scrollPane, c);
121 table.setAutoCreateRowSorter(true);
127 * @return A display string for PizzaTypes.
129 * e.g. Cardinale, Cardinale, Margherita is displayed as "2xCARDINALE,1xMARGHERITA"
131 public static String pizzaDisplay(List<PizzaOrder> list) {
132 HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
133 for (PizzaOrder pizzaOrder : list) {
134 PizzaType pizzaType = pizzaOrder.getPizzaType();
135 if (pizzaCount.containsKey(pizzaType)) {
136 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
138 pizzaCount.put(pizzaType, 1);
140 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
141 StringBuilder sb = new StringBuilder();
143 boolean multiplePizzas = false;
144 for (PizzaType pizzaType : pizzaTypes) {
148 multiplePizzas = true;
150 sb.append(pizzaCount.get(pizzaType) + "x");
151 sb.append(pizzaType.toString());
154 return sb.toString();
157 public static void handleSpaceErrorAndTerminate(Exception e) {
158 log.error(e.getMessage());