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