]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java
XVSMConnector refactoring
[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.HashMap;
5 import java.util.List;
6 import java.util.Set;
7
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import at.ac.tuwien.sbc.valesriegler.common.TableModel;
12 import at.ac.tuwien.sbc.valesriegler.common.Util;
13 import at.ac.tuwien.sbc.valesriegler.group.Group;
14 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
15 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
16 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
17
18 /**
19  * Might not the nicest way to store global data, but this is an elemental
20  * Object, storing ALL the Group's data. Might consider splitting those two
21  * usecases apart.
22  * 
23  * @author jan
24  * 
25  */
26 public class GroupOverviewModel extends TableModel<Group> {
27         private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
28
29         private static final String STATE = "State";
30         private static final String PIZZAS = "Pizzas";
31         private static final String SIZE = "Size";
32         private static final String ID = "ID";
33         private static final String[] COLUMNS = new String[] { ID, SIZE, PIZZAS, STATE };
34
35
36         @Override
37         protected String[] getColumns() {
38                 return COLUMNS;
39         }
40
41         public List<Group> getItems() {
42                 return new ArrayList<>(items.values());
43         }
44
45         public Group getGroupByID(int id) {
46                 return items.get(id);
47         }
48
49         @Override
50         public Object getValueAt(int rowIndex, int columnIndex) {
51                 List<Group> values = new ArrayList<>(items.values());
52                 Group group = values.get(rowIndex);
53                 String wantedColumn = COLUMNS[columnIndex];
54                 switch (wantedColumn) {
55                 case ID:
56                         int groupId = group.getGroupData().getId();
57                         log.info("This is the group id : {}", groupId);
58                         return groupId;
59                 case SIZE:
60                         int groupSize = group.getGroupData().getSize();
61                         log.info("This is the size : {}", groupSize);
62                         return groupSize;
63                 case PIZZAS:
64                         if (group.getGroupData().getOrder() == null)
65                                 return "none";
66                         return pizzaDisplay(group.getGroupData().getOrder().getOrderedPizzas());
67                 case STATE:
68                         return group.getGroupData().getState();
69                 default:
70                         throw new RuntimeException(UNHANDLEDCOLUMN);
71                 }
72         }
73
74         /**
75          * 
76          * @return A display string for PizzaTypes.
77          *         <p />
78          *         e.g. Cardinale, Cardinale, Margherita is displayed as
79          *         "2xCARDINALE,1xMARGHERITA"
80          */
81         private String pizzaDisplay(List<PizzaOrder> list) {
82                 HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
83                 for (PizzaOrder pizzaOrder : list) {
84                         PizzaType pizzaType = pizzaOrder.getPizzaType();
85                         if (pizzaCount.containsKey(pizzaType)) {
86                                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
87                         } else
88                                 pizzaCount.put(pizzaType, 1);
89                 }
90                 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
91                 StringBuilder sb = new StringBuilder();
92
93                 boolean multiplePizzas = false;
94                 for (PizzaType pizzaType : pizzaTypes) {
95                         if (multiplePizzas)
96                                 sb.append(", ");
97                         else
98                                 multiplePizzas = true;
99
100                         sb.append(pizzaCount.get(pizzaType) + "x");
101                         sb.append(pizzaType.toString());
102                 }
103
104                 return sb.toString();
105         }
106
107         @Override
108         public void addItems(List<Group> newItems) {
109                 log.info("addItems()");
110                 super.addItems(newItems);
111                 for (Group g : newItems) {
112                         if(Util.useJMS) g.goGrabSomeFood();
113                 }
114         }
115         
116         /**
117          * 
118          * This is necessary as in the space version GroupData objects get written to the space. In order to 
119          * translate from Group data to the actual group object the group object has to be looked up by the id.
120          * 
121          * @param newItems the group data of the corresponding groups
122          */
123         public void addGroupData(List<GroupData> newItems) {
124                 List<Group> groups = new ArrayList<>();
125                 for (GroupData groupData : newItems) {
126                         Group group = items.get(groupData.getId());
127                         groups.add(group);
128                 }
129                 super.addItems(groups);
130         }
131 }