]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java
Adjust Pizzeria GUI to display delivery orders. Show pizzeria identifier in Group...
[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.List;
5
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9 import at.ac.tuwien.sbc.valesriegler.common.TableModel;
10 import at.ac.tuwien.sbc.valesriegler.common.Util;
11 import at.ac.tuwien.sbc.valesriegler.group.Group;
12 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
13 import at.ac.tuwien.sbc.valesriegler.types.GroupState;
14 import at.ac.tuwien.sbc.valesriegler.types.Table;
15
16 /**
17  * Might not the nicest way to store global data, but this is an elemental
18  * Object, storing ALL the Group's data. Might consider splitting those two
19  * usecases apart.
20  * 
21  * @author jan
22  * 
23  */
24 public class GroupOverviewModel extends TableModel<Group> {
25         private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
26
27         private static final String STATE = "State";
28         private static final String PIZZAS = "Pizzas";
29         private static final String SIZE = "Size";
30         private static final String ID = "ID";
31         private static final String PIZZERIA = "Pizzeria";
32         private static final String[] COLUMNS = new String[] { ID, SIZE, PIZZAS, STATE, PIZZERIA };
33
34
35         @Override
36         protected String[] getColumns() {
37                 return COLUMNS;
38         }
39
40         public List<Group> getItems() {
41                 return new ArrayList<>(items.values());
42         }
43
44         public Group getGroupByID(int id) {
45                 return items.get(id);
46         }
47
48         @Override
49         public Object getValueAt(int rowIndex, int columnIndex) {
50                 List<Group> values = new ArrayList<>(items.values());
51                 Group group = values.get(rowIndex);
52                 String wantedColumn = COLUMNS[columnIndex];
53                 switch (wantedColumn) {
54                 case ID:
55                         int groupId = group.getGroupData().getId();
56                         return groupId;
57                 case SIZE:
58                         int groupSize = group.getGroupData().getSize();
59                         return groupSize;
60                 case PIZZAS:
61                         if (group.getGroupData().getOrder() == null)
62                                 return "none";
63                         return Util.pizzaDisplay(group.getGroupData().getOrder().getOrderedPizzas());
64                 case STATE:
65                         return group.getGroupData().getState();
66         case PIZZERIA:
67             return group.getGroupData().getPizzeriaId();
68                 default:
69                         throw new RuntimeException(UNHANDLEDCOLUMN);
70                 }
71         }
72
73     @Override
74         public void addItems(List<Group> newItems) {
75                 log.info("addItems()");
76                 super.addItems(newItems);
77                 for (Group g : newItems) {
78                         if(Util.useJMS) g.goGrabSomeFood();
79                 }
80         }
81         
82         /**
83          * 
84          * This is necessary as in the space version GroupData objects get written to the space. In order to 
85          * translate from Group data to the actual group object the group object has to be looked up by the id.
86          * 
87          * @param newItems the group data of the corresponding groups
88          */
89         @Deprecated
90         public void addGroupData(List<GroupData> newItems) {
91                 List<Group> groups = new ArrayList<>();
92                 for (GroupData groupData : newItems) {
93                         Group group = items.get(groupData.getId());
94                         group.setGroupData(groupData);
95                         groups.add(group);
96                 }
97                 super.addItems(groups);
98         }
99
100         public void setGroupEating(int groupId) {
101 //              if(stateIs(groupId, GroupState.GONE) || stateIs(groupId, GroupState.PAY)) return;
102                 changeStateOfGroup(groupId, GroupState.EATING);
103                 
104                 fireTableDataChanged();
105         }
106
107         public void setGroupWantsToPay(int groupId) {
108 //              if(stateIs(groupId, GroupState.GONE)) return;
109                 changeStateOfGroup(groupId, GroupState.PAY);
110                 
111                 fireTableDataChanged();
112         }
113
114         private boolean stateIs(int groupId, GroupState state) {
115                 return items.get(groupId).getGroupData().getState() == state;
116         }
117
118
119         public void setGroupHasPaid(int groupId) {
120                 changeStateOfGroup(groupId, GroupState.GONE);
121                 
122                 fireTableDataChanged();
123         }
124         
125         private void changeStateOfGroup(int groupId, GroupState state) {
126                 items.get(groupId).getGroupData().setState(state);
127         }
128
129         public void setGroupsSitting(List<Table> tables) {
130                 synchronized (items) {
131                         for (Table table : tables) {
132                                 changeStateOfGroup(table.getGroupId(), GroupState.SITTING);
133                         }
134                 }
135                 
136                 fireTableDataChanged();
137         }
138
139         public void setOrderTaken(GroupData group) {
140                 changeStateOfGroup(group.getId(), GroupState.ORDERED);
141                 
142                 fireTableDataChanged();
143         }
144
145 }