1 package at.ac.tuwien.sbc.valesriegler.group.gui;
3 import java.util.ArrayList;
4 import java.util.HashMap;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
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;
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
26 public class GroupOverviewModel extends TableModel<Group> {
27 private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
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 };
37 protected String[] getColumns() {
41 public List<Group> getItems() {
42 return new ArrayList<>(items.values());
45 public Group getGroupByID(int id) {
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) {
56 int groupId = group.getGroupData().getId();
57 // log.info("This is the group id : {}", groupId);
60 int groupSize = group.getGroupData().getSize();
61 // log.info("This is the size : {}", groupSize);
64 if (group.getGroupData().getOrder() == null)
66 return pizzaDisplay(group.getGroupData().getOrder().getOrderedPizzas());
68 return group.getGroupData().getState();
70 throw new RuntimeException(UNHANDLEDCOLUMN);
76 * @return A display string for PizzaTypes.
78 * e.g. Cardinale, Cardinale, Margherita is displayed as
79 * "2xCARDINALE,1xMARGHERITA"
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);
88 pizzaCount.put(pizzaType, 1);
90 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
91 StringBuilder sb = new StringBuilder();
93 boolean multiplePizzas = false;
94 for (PizzaType pizzaType : pizzaTypes) {
98 multiplePizzas = true;
100 sb.append(pizzaCount.get(pizzaType) + "x");
101 sb.append(pizzaType.toString());
104 return sb.toString();
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();
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.
121 * @param newItems the group data of the corresponding groups
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 group.setGroupData(groupData);
130 super.addItems(groups);