-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
98 lines (79 loc) · 3.24 KB
/
Controller.java
File metadata and controls
98 lines (79 loc) · 3.24 KB
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
// Copyright 2019 doubleSlash Net Business GmbH
//
// This file is part of KeepTime.
// KeepTime is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package de.doubleslash.keeptask.controller;
import de.doubleslash.keeptask.model.Model;
import de.doubleslash.keeptask.model.WorkItem;
import de.doubleslash.keeptask.model.repos.WorkItemRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PreDestroy;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class Controller {
private static final Logger LOG = LoggerFactory.getLogger(Controller.class);
private final Model model;
private WorkItemRepository workItemRepository;
@Autowired
public Controller(final Model model, final WorkItemRepository workItemRepository) {
this.model = model;
this.workItemRepository = workItemRepository;
}
public void init() {
reloadWorkItemsFromRepo();
}
public void addWorkItem(WorkItem workItem) {
WorkItem savedWorkItem = workItemRepository.save(workItem);
reloadWorkItemsFromRepo();
}
public void deleteWorkItem(WorkItem workItem) {
workItemRepository.delete(workItem);
reloadWorkItemsFromRepo();
}
public void toggleWorkItemCompleted(WorkItem workItem) {
workItem.setFinished(!workItem.isFinished());
if (workItem.isFinished()) workItem.setCompletedDateTime(LocalDateTime.now());
else workItem.setCompletedDateTime(null);
workItemRepository.save(workItem);
reloadWorkItemsFromRepo();
}
public void editWorkItem(WorkItem oldItem, WorkItem newItem) {
oldItem.setFinished(newItem.isFinished());
oldItem.setTodo(newItem.getTodo());
oldItem.setDueDateTime(newItem.getDueDateTime());
oldItem.setPriority(newItem.getPriority());
oldItem.setProject(newItem.getProject());
oldItem.setCreatedDateTime(newItem.getCreatedDateTime());
oldItem.setCompletedDateTime(newItem.getCompletedDateTime());
oldItem.setNote(newItem.getNote());
workItemRepository.save(oldItem);
reloadWorkItemsFromRepo();
}
private void reloadWorkItemsFromRepo() {
// TODO manage list internal and dont refetch everything all the time
List<WorkItem> workItems = workItemRepository.findAll();
model.setWorkItems(workItems);
}
@PreDestroy
public void shutdown() {
LOG.info("Controller shutdown");
}
public void setLatestSelectedProject(String projectName) {
model.setLatestSelectedProject(projectName);
}
}