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.GroupState;
16 import at.ac.tuwien.sbc.valesriegler.types.Order;
17 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
18 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
19 import at.ac.tuwien.sbc.valesriegler.types.Table;
22 * Might not the nicest way to store global data, but this is an elemental
23 * Object, storing ALL the Group's data. Might consider splitting those two
29 public class GroupOverviewModel extends TableModel<Group> {
30 private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
32 private static final String STATE = "State";
33 private static final String PIZZAS = "Pizzas";
34 private static final String SIZE = "Size";
35 private static final String ID = "ID";
36 private static final String[] COLUMNS = new String[] { ID, SIZE, PIZZAS, STATE };
40 protected String[] getColumns() {
44 public List<Group> getItems() {
45 return new ArrayList<>(items.values());
48 public Group getGroupByID(int id) {
53 public Object getValueAt(int rowIndex, int columnIndex) {
54 List<Group> values = new ArrayList<>(items.values());
55 Group group = values.get(rowIndex);
56 String wantedColumn = COLUMNS[columnIndex];
57 switch (wantedColumn) {
59 int groupId = group.getGroupData().getId();
62 int groupSize = group.getGroupData().getSize();
65 if (group.getGroupData().getOrder() == null)
67 return pizzaDisplay(group.getGroupData().getOrder().getOrderedPizzas());
69 return group.getGroupData().getState();
71 throw new RuntimeException(UNHANDLEDCOLUMN);
77 * @return A display string for PizzaTypes.
79 * e.g. Cardinale, Cardinale, Margherita is displayed as
80 * "2xCARDINALE,1xMARGHERITA"
82 private String pizzaDisplay(List<PizzaOrder> list) {
83 HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
84 for (PizzaOrder pizzaOrder : list) {
85 PizzaType pizzaType = pizzaOrder.getPizzaType();
86 if (pizzaCount.containsKey(pizzaType)) {
87 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
89 pizzaCount.put(pizzaType, 1);
91 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
92 StringBuilder sb = new StringBuilder();
94 boolean multiplePizzas = false;
95 for (PizzaType pizzaType : pizzaTypes) {
99 multiplePizzas = true;
101 sb.append(pizzaCount.get(pizzaType) + "x");
102 sb.append(pizzaType.toString());
105 return sb.toString();
109 public void addItems(List<Group> newItems) {
110 log.info("addItems()");
111 super.addItems(newItems);
112 for (Group g : newItems) {
113 if(Util.useJMS) g.goGrabSomeFood();
119 * This is necessary as in the space version GroupData objects get written to the space. In order to
120 * translate from Group data to the actual group object the group object has to be looked up by the id.
122 * @param newItems the group data of the corresponding groups
125 public void addGroupData(List<GroupData> newItems) {
126 List<Group> groups = new ArrayList<>();
127 for (GroupData groupData : newItems) {
128 Group group = items.get(groupData.getId());
129 group.setGroupData(groupData);
132 super.addItems(groups);
135 public void setGroupEating(int groupId) {
136 // if(stateIs(groupId, GroupState.GONE) || stateIs(groupId, GroupState.PAY)) return;
137 changeStateOfGroup(groupId, GroupState.EATING);
139 fireTableDataChanged();
142 public void setGroupWantsToPay(int groupId) {
143 // if(stateIs(groupId, GroupState.GONE)) return;
144 changeStateOfGroup(groupId, GroupState.PAY);
146 fireTableDataChanged();
149 private boolean stateIs(int groupId, GroupState state) {
150 return items.get(groupId).getGroupData().getState() == state;
154 public void setGroupHasPaid(int groupId) {
155 changeStateOfGroup(groupId, GroupState.GONE);
157 fireTableDataChanged();
160 private void changeStateOfGroup(int groupId, GroupState state) {
161 items.get(groupId).getGroupData().setState(state);
164 public void setGroupsSitting(List<Table> tables) {
165 synchronized (items) {
166 for (Table table : tables) {
167 changeStateOfGroup(table.getGroupId(), GroupState.SITTING);
171 fireTableDataChanged();
174 public void setOrderTaken(GroupData group) {
175 changeStateOfGroup(group.getId(), GroupState.ORDERED);
177 fireTableDataChanged();