-
Notifications
You must be signed in to change notification settings - Fork 0
/
KanbanController.java
376 lines (366 loc) · 10.2 KB
/
KanbanController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package Controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import Model.Kanban;
import Model.Lane;
import Model.Ticket;
import Model.User;
/**
* @version 1.0
*
*/
public class KanbanController {
private Kanban kanbanBoard;
public KanbanController(Kanban bored){
kanbanBoard = bored;
}
/**
* Increments the ID and returns an Id to the user
* this is used whenever creating a ticket
* @return
*/
// @ TODO test
public float incrementID(){
float id = kanbanBoard.getUniqueID();
id++;
kanbanBoard.setUniqueID(id);
return id;
}
/**
* Gets a lane with a given name
* @param name The name of the lane you want to get
* @return A lane
*/
public Lane getALane(String name){
ArrayList<Lane> arr = kanbanBoard.getLaneArray();
int size = arr.size();
boolean found = false;
Lane x = null;
for (int i=0; i<size && found ==false; i++){
x = arr.get(i);
if (x.getName().equals(name)){
found = true;
}
}
return x;
}
/**
* Creates a default kanban with four lanes
*/
public void createDefaultKanban(){
ArrayList<Lane> arr = kanbanBoard.getLaneArray();
Lane backlog = new Lane ();
Lane ready = new Lane ();
Lane inProgress = new Lane ();
Lane done = new Lane ();
LaneController controlLane = new LaneController(backlog);
controlLane.createLane("backlog");
arr.add(backlog);
controlLane.changeControlTo(ready);
controlLane.createLane("ready");
arr.add(ready);
controlLane.changeControlTo(inProgress);
controlLane.createLane("in progress");
arr.add(inProgress);
controlLane.changeControlTo(done);
controlLane.createLane("done");
arr.add(done);
kanbanBoard.setLaneArray(arr);
}
/**
* Creates a user generated lane at a given point
* provided it does not have the same name as another a lane
* and provided it does not have a position less than zero or greater the the final
* element
* @param name The name of the lane your going to add
* @param pos Where you want to place it
* @return if outcome false operation failed
*/
// @ TODO test
public boolean createAUserLane(String name, int pos){
boolean outcome = false;
boolean badName = false;
ArrayList<Lane> arr = kanbanBoard.getLaneArray();
for (int i=0; i<arr.size() && badName == false; i++){
Lane check = arr.get(i);
if (check.getName().toLowerCase().equals(name.toLowerCase())){
badName = true;
outcome = false;
}
}
if (pos > 0 || pos >= arr.size()-1){
Lane adderLane = new Lane();
LaneController lc = new LaneController(adderLane);
lc.createLane(name);
arr.add(pos, adderLane);
kanbanBoard.setLaneArray(arr);
outcome = true;
}
else{
outcome = false;
}
return outcome;
}
/**
* Removes a lane by the name provided its a user created lane
* @param x
* @return
*/
// @ TODO test
public boolean removeALane(String name){
boolean outcome = false;
ArrayList<Lane> arr = kanbanBoard.getLaneArray();
for (int i=0; i<arr.size() && outcome == false; i++){
Lane l = arr.get(i);
if (l.getBooleanArr()[1] == true && l.getLaneArr().size()<1){
if (l.getName().equals(name)){
arr.remove(i);
kanbanBoard.setLaneArray(arr);
outcome = true;
}
}
}
return outcome;
}
//@ TODO TEST please seriously
public boolean moveATicket(Ticket t){
boolean outcome = false;
boolean found = false;
ArrayList<Lane> arr = kanbanBoard.getLaneArray();
for (int i=0; i<arr.size() && found == false; i++){
Lane lane = arr.get(i);
ArrayList <Ticket> tickArr = lane.getLaneArr();
int index = tickArr.indexOf(t);
if (index >= 0 && i< arr.size()-1){
//Setting found to true so it will exit the loop
found = true;
//Removing the ticket from its home array
tickArr.remove(index);
//Returning the lane with the changes made
lane.setLaneArr(tickArr);
//Getting the lane one step in front
lane = arr.get(i+1);
//Getting its ticket array
tickArr = lane.getLaneArr();
//Adding the ticket to it
tickArr.add(t);
//Returing this ticket array
lane.setLaneArr(tickArr);
outcome = true;
}
//Checking to see if it is inside sub-lane
else{
tickArr = lane.getSubLaneArr();
index = tickArr.indexOf(t);
if (index >= 0 && i<= arr.size()-1){
found = true;
tickArr.remove(index);
lane.setSubLaneArr(tickArr);
lane = arr.get(i+1);
tickArr = lane.getLaneArr();
tickArr.add(t);
lane.setLaneArr(tickArr);
outcome = true;
}
}
}
return outcome;
}
/**
* Pass in an argument with a tickt of the same ID and it
* will be replaced by ticket passed in
*
* @param ticket
* @return The outcome of the operation
*/
public boolean findATicketAndReplace(Ticket ticket){
boolean outcome = false;
boolean found = false;
ArrayList<Lane> listOfLane = kanbanBoard.getLaneArray();
for (int i=0; i<listOfLane.size() && found == false; i++){
Lane lane = listOfLane.get(i);
ArrayList<Ticket> listOfTicket = lane.getLaneArr();
for (int y=0; y<listOfTicket.size() && found == false; y++){
Ticket t = listOfTicket.get(y);
if (t.getTicketID() == ticket.getTicketID()){
found = true;
LaneController lc = new LaneController(lane);
listOfTicket.remove(y);
listOfTicket.add(y,ticket);
lane.setLaneArr(lc.sortByPriority(listOfTicket));
outcome = true;
}
}
//If not found in the main array then check sub
//First check if sub exists
if (lane.getBooleanArr()[3] == true){
listOfTicket = lane.getSubLaneArr();
for (int o=0; o<listOfTicket.size(); o++){
Ticket t = listOfTicket.get(o);
if (t.getTicketID() == ticket.getTicketID()){
found = true;
LaneController lc = new LaneController(lane);
listOfTicket.remove(o);
listOfTicket.add(o,ticket);
lane.setSubLaneArr(lc.sortByPriority(listOfTicket));
outcome = true;
}
}
}
}
return outcome;
}
/**
* Finds a lane matching the name passed in
* and returns it if it
*
* @param nameOfLane
* @return
*/
public Lane findALane(String nameOfLane){
ArrayList<Lane> arrayOfLanes = kanbanBoard.getLaneArray();
Lane lane = null;
boolean found = false;
for (int i=0; i<arrayOfLanes.size() && found == false; i++){
lane = arrayOfLanes.get(i);
if (lane.getName().equals(nameOfLane)){
found = true;
}
}
return lane;
}
/**
* Checks if a lane has a ticket and will return this lane
* If no lane containing the ticket can be found it will
* return a null value
* @param t The ticket that you want to search with
* @return
*/
public Lane laneHasTicket(Ticket t){
ArrayList<Lane> listOfLane = kanbanBoard.getLaneArray();
Lane lane = null;
boolean found = false;
for (int i=0; i<listOfLane.size() && found == false; i++){
lane = listOfLane.get(i);
if (lane.getLaneArr().contains(t)) {
found = true;
}
else if(lane.getSubLaneArr().contains(t)) {
found = true;
}
}
return lane;
}
/**
* Adds a ticket to a local done lane
* Will only work if the ticket is in a
* lane that has a local done lane
* @param ticket the ticket that is to be moved
* @return the outcome of the operation
*/
public boolean moveTicketToSub(Ticket ticket){
boolean outcome= false;
Lane lane = laneHasTicket(ticket);
if (lane.getBooleanArr()[3] == true){
LaneController lc = new LaneController(lane);
lc.addATicketToSub(ticket);
replaceALane(lane);
outcome = true;
}
return outcome;
}
public boolean replaceALane(Lane lane){
boolean outcome = false;
ArrayList<Lane> listOfLanes = kanbanBoard.getLaneArray();
boolean found = false;
for (int i=0; i<listOfLanes.size() && found == false; i++){
Lane x = listOfLanes.get(i);
if (x.getName().equals(lane.getName())){
listOfLanes.remove(i);
listOfLanes.add(i,lane);
kanbanBoard.setLaneArray(listOfLanes);
found = true;
outcome = true;
}
}
return outcome;
}
/**
* removes a ticket from the subLAne and places it in the main lane
* will only work if already in an local done lane
* @param ticket
* @return
*/
public boolean removeFromSub(Ticket ticket){
boolean outcome = false;
Lane lane = laneHasTicket(ticket);
if (lane != null){
LaneController lc = new LaneController(lane);
outcome = lc.moveATicketFromSubToMain(ticket);
if (outcome == true) {
replaceALane(lane);
}
}
return outcome;
}
public boolean saveKanban(File file){
boolean outcome = false;
try {
FileOutputStream fileOut =
new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(kanbanBoard);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in "+file.getName());
outcome = true;
}catch(IOException i) {
i.printStackTrace();
}
return outcome;
}
public boolean addUserToArray(User user) {
boolean outcome = true;
kanbanBoard.getUserArray().add(user);
int index = kanbanBoard.getUserArray().indexOf(user);
User x = kanbanBoard.getUserArray().get(index);
System.out.println(x.getUserName());
return outcome;
}
public void loginUser(User user) {
kanbanBoard.setCurrentUser(user);
}
public Kanban loadIn(File file){
Kanban e = null;
try {
FileInputStream fileIn = new FileInputStream(file.getAbsolutePath());
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Kanban) in.readObject();
in.close();
fileIn.close();
}catch(IOException i) {
i.printStackTrace();
}catch(ClassNotFoundException c) {
System.out.println("Kanban class not found");
c.printStackTrace();
}
return e;
}
public User findUserByID(String id){
ArrayList<User> list = kanbanBoard.getUserArray();
User user = null;
for (int i=0; i<list.size(); i++){
user = list.get(i);
if (user.getUserName().equals(id)){
break;
}
}
return user;
}
}