]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java
Create DeliveryGroup, DeliveryGroupData, DeliveryStatus. Add Deliveries overview...
[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 java.awt.*;
4 import java.net.URI;
5 import java.util.*;
6 import java.util.List;
7
8 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
9 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
10 import org.mozartspaces.capi3.Coordinator;
11 import org.mozartspaces.core.Capi;
12 import org.mozartspaces.core.ContainerReference;
13 import org.mozartspaces.core.MzsConstants.Container;
14 import org.mozartspaces.core.MzsConstants.RequestTimeout;
15 import org.mozartspaces.core.MzsCoreException;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import javax.swing.*;
20 import javax.swing.border.TitledBorder;
21
22 public abstract class Util {
23         private static final Logger log = LoggerFactory.getLogger(Util.class);
24
25         // TODO: solve the switch between mom by command-line arguments
26         public static boolean useJMS = true;
27
28         public static final String TABLE_ASSIGNED = "tables";
29         public static final String GROUPS_CONTAINER = "groups";
30         public static final String ASSIGN_TABLE = "assignTable";
31         public static final String TAKE_ORDER = "takeOrder";
32         public static final String ORDER_TAKEN = "order";
33         public static final String DELIVER_PIZZAS = "deliverPizzas";
34         public static final String PREPARE_PIZZAS = "preparePizzas";
35         public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress";
36         public static final String ORDER_COMPLETE = "orderComplete";
37         public static final String PAYMENT_REQUEST = "payment";
38         public static final String FREE_TABLES = "freeTables";
39         public static final String IS_EATING = "isEating";
40         public static final String PAYMENT_DONE = "hasPaid";
41         public static final String INFO = "info";
42
43         private static Random random = new Random();
44
45         public static final long SPACE_TRANSACTION_TIMEOUT = 1500;
46
47         public static final String SERVER_ADDR = "xvsm://localhost:9876";
48
49         public static final String JMS_CONNECTSTRING = "tcp://localhost:61616?jms.prefetchPolicy.all=1";
50
51         public static String getId(int id) {
52                 return (id != 0 && id != -1) ? String.valueOf(id) : "";
53         }
54
55         public static int getIntSafe(Integer nr) {
56                 return nr == null ? 0 : nr;
57         }
58
59         public static <T extends HasId> Map<Integer, T> intoMapById(List<T> hasIds) {
60                 if (hasIds == null) {
61                         return Collections.emptyMap();
62                 }
63
64                 Map<Integer, T> myMap = new HashMap<>();
65                 for (T hasId : hasIds) {
66                         myMap.put(hasId.getId(), hasId);
67                 }
68                 return myMap;
69         }
70
71         public static int getRandom(int min, int max) {
72                 return random.nextInt(max - min + 1) + min;
73         }
74
75     public static int parseId(String[] args, String usage) {
76         if (args.length != 1) {
77             throw new IllegalArgumentException(usage);
78         }
79
80         int parsedId = 0;
81         try {
82             parsedId = Integer.parseInt(args[0]);
83         } catch (NumberFormatException e) {
84             throw new IllegalArgumentException(usage);
85         }
86
87         return parsedId;
88     }
89
90     public static JTable createTableInTitledPanel(JPanel wrapperPanel, TableModel<?> model, String title) {
91         JTable table = new JTable(model);
92         wrapperPanel.setLayout(new GridBagLayout());
93         GridBagConstraints c = new GridBagConstraints();
94         c.gridx = 0;
95         c.weightx = 1;
96         c.weighty = 1;
97         c.insets = new Insets(10, 10, 10, 10);
98         c.fill = GridBagConstraints.BOTH;
99         JScrollPane scrollPane = new JScrollPane(table);
100
101         wrapperPanel.setBorder(new TitledBorder(title));
102         scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
103         wrapperPanel.add(scrollPane, c);
104
105         return table;
106     }
107
108     /**
109      *
110      * @return A display string for PizzaTypes.
111      *         <p />
112      *         e.g. Cardinale, Cardinale, Margherita is displayed as
113      *         "2xCARDINALE,1xMARGHERITA"
114      */
115     public static String pizzaDisplay(List<PizzaOrder> list) {
116         HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
117         for (PizzaOrder pizzaOrder : list) {
118             PizzaType pizzaType = pizzaOrder.getPizzaType();
119             if (pizzaCount.containsKey(pizzaType)) {
120                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
121             } else
122                 pizzaCount.put(pizzaType, 1);
123         }
124         Set<PizzaType> pizzaTypes = pizzaCount.keySet();
125         StringBuilder sb = new StringBuilder();
126
127         boolean multiplePizzas = false;
128         for (PizzaType pizzaType : pizzaTypes) {
129             if (multiplePizzas)
130                 sb.append(", ");
131             else
132                 multiplePizzas = true;
133
134             sb.append(pizzaCount.get(pizzaType) + "x");
135             sb.append(pizzaType.toString());
136         }
137
138         return sb.toString();
139     }
140 }