]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java
Support for space notifications for pizzeria and group agents. Change table model...
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / group / gui / GroupOverviewModel.java
1 package at.ac.tuwien.sbc.valesriegler.group.gui;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Set;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import at.ac.tuwien.sbc.valesriegler.DEP_Table;
13 import at.ac.tuwien.sbc.valesriegler.common.TableModel;
14 import at.ac.tuwien.sbc.valesriegler.common.Util;
15 import at.ac.tuwien.sbc.valesriegler.group.Group;
16 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
17 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
18
19 public class GroupOverviewModel extends TableModel<Group> {
20         private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
21
22         private static final String STATE = "State";
23         private static final String PIZZAS = "Pizzas";
24         private static final String SIZE = "Size";
25         private static final String ID = "ID";
26         private static final String[] COLUMNS = new String[] { ID, SIZE, PIZZAS, STATE };
27
28         public GroupOverviewModel() {
29                 super();
30 //              super.items.add(new Group()); // TODO: remove, debuggroup!
31         }
32
33         @Override
34         protected String[] getColumns() {
35                 return COLUMNS;
36         }
37
38         public List<Group> getItems() {
39                 return new ArrayList<>(items.values());
40         }
41
42         public Group getGroupByID(int id) {
43                 return items.get(id);
44         }
45
46         @Override
47         public Object getValueAt(int rowIndex, int columnIndex) {
48                 List<Group> values = new ArrayList<>(items.values());
49                 Group group = values.get(rowIndex);
50                 String wantedColumn = COLUMNS[columnIndex];
51                 switch (wantedColumn) {
52                 case ID:
53                         int groupId = group.getGroupData().getId();
54                         log.info("This is the group id : {}", groupId);
55                         return groupId;
56                 case SIZE:
57                         int groupSize = group.getGroupData().getSize();
58                         log.info("This is the size : {}", groupSize);
59                         return groupSize;
60                 case PIZZAS:
61                         if (group.getGroupData().getOrder() == null)
62                                 return "none";
63                         return pizzaDisplay(group.getGroupData().getOrder().getOrderedPizzas());
64                 case STATE:
65                         return group.getGroupData().getState();
66                 default:
67                         throw new RuntimeException(UNHANDLEDCOLUMN);
68                 }
69         }
70
71         /**
72          * 
73          * @return A display string for PizzaTypes.
74          *         <p />
75          *         e.g. Cardinale, Cardinale, Margherita is displayed as
76          *         "2xCARDINALE,1xMARGHERITA"
77          */
78         private String pizzaDisplay(List<PizzaOrder> list) {
79                 HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
80                 for (PizzaOrder pizzaOrder : list) {
81                         PizzaType pizzaType = pizzaOrder.getPizzaType();
82                         if (pizzaCount.containsKey(pizzaType)) {
83                                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
84                         } else
85                                 pizzaCount.put(pizzaType, 1);
86                 }
87                 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
88                 StringBuilder sb = new StringBuilder();
89
90                 boolean multiplePizzas = false;
91                 for (PizzaType pizzaType : pizzaTypes) {
92                         if (multiplePizzas)
93                                 sb.append(", ");
94                         else
95                                 multiplePizzas = true;
96
97                         sb.append(pizzaCount.get(pizzaType) + "x");
98                         sb.append(pizzaType.toString());
99                 }
100
101                 return sb.toString();
102         }
103
104         @Override
105         public void addItems(List<Group> newItems) {
106                 log.info("addItems()");
107                 super.addItems(newItems);
108                 for (Group g : newItems) {
109                         if(Util.useJMS) g.goGrabSomeFood();
110                 }
111         }
112 }