1 package at.ac.tuwien.sbc.valesriegler.common;
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;
20 import javax.swing.border.TitledBorder;
22 public abstract class Util {
23 private static final Logger log = LoggerFactory.getLogger(Util.class);
25 // TODO: solve the switch between mom by command-line arguments
26 public static boolean useJMS = true;
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";
43 private static Random random = new Random();
45 public static final long SPACE_TRANSACTION_TIMEOUT = 1500;
47 public static final String SERVER_ADDR = "xvsm://localhost:9876";
49 public static final String JMS_CONNECTSTRING = "tcp://localhost:61616?jms.prefetchPolicy.all=1";
51 public static String getId(int id) {
52 return (id != 0 && id != -1) ? String.valueOf(id) : "";
55 public static int getIntSafe(Integer nr) {
56 return nr == null ? 0 : nr;
59 public static <T extends HasId> Map<Integer, T> intoMapById(List<T> hasIds) {
61 return Collections.emptyMap();
64 Map<Integer, T> myMap = new HashMap<>();
65 for (T hasId : hasIds) {
66 myMap.put(hasId.getId(), hasId);
71 public static int getRandom(int min, int max) {
72 return random.nextInt(max - min + 1) + min;
75 public static int parseId(String[] args, String usage) {
76 if (args.length != 1) {
77 throw new IllegalArgumentException(usage);
82 parsedId = Integer.parseInt(args[0]);
83 } catch (NumberFormatException e) {
84 throw new IllegalArgumentException(usage);
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();
97 c.insets = new Insets(10, 10, 10, 10);
98 c.fill = GridBagConstraints.BOTH;
99 JScrollPane scrollPane = new JScrollPane(table);
101 wrapperPanel.setBorder(new TitledBorder(title));
102 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
103 wrapperPanel.add(scrollPane, c);
110 * @return A display string for PizzaTypes.
112 * e.g. Cardinale, Cardinale, Margherita is displayed as
113 * "2xCARDINALE,1xMARGHERITA"
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);
122 pizzaCount.put(pizzaType, 1);
124 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
125 StringBuilder sb = new StringBuilder();
127 boolean multiplePizzas = false;
128 for (PizzaType pizzaType : pizzaTypes) {
132 multiplePizzas = true;
134 sb.append(pizzaCount.get(pizzaType) + "x");
135 sb.append(pizzaType.toString());
138 return sb.toString();