-
Notifications
You must be signed in to change notification settings - Fork 9
/
TaskService.java
134 lines (113 loc) · 4.35 KB
/
TaskService.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
package app.services;
import app.entities.Task;
import app.entities.User;
import app.entities.enums.TicketStatus;
import app.exceptions.service.BadRequestException;
import app.exceptions.service.ResourceNotFoundException;
import app.exceptions.service.UserIdDoesNotExistException;
import app.repositories.TaskRepository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.List;
@Service
public class TaskService implements app.services.Service
{
private final Log logger = LogFactory.getLog( getClass() );
@Autowired
private TaskRepository taskRepository;
@Autowired
private TicketService ticketService;
@Autowired
private UserService userService;
public Task create ( Task task )
{
this.ticketService.verifyTicketExists( task.getTicket().getId() );
if ( this.taskExistsForTicketId( task.getTicket().getId() ) )
{
throw new BadRequestException( "A task already exists for ticket with id : " + task.getTicket().getId() );
}
task.getUsers().forEach( user ->
{
if ( !userService.userExists( user.getId() ) )
{
throw new UserIdDoesNotExistException( "User with id " + user.getId() + " not found" );
}
} );
//Refreshing so that all associations in Task are populated from the database,
//since we only have the IDs
task = this.taskRepository.saveFlushAndRefresh( task );
this.changeTicketStatus( task.getTicket().getId(), TicketStatus.ASSIGNED );
return task;
}
public Task addUsersToTask ( Task task )
{
Collection<User> inUsers = task.getUsers();
Task taskFromDb = this.getById( task.getId() );
inUsers.forEach( ( inUser ->
{
if ( !this.userService.userExists( inUser.getId() ) )
{
throw new UserIdDoesNotExistException( "User with id " + inUser.getId() + " not found" );
}
if ( taskFromDb.getUsers().contains( inUser ) )
{
logger.info( "User: " + inUser.getUsername() + " was already assigned to task with id: " + taskFromDb.getId() );
} else
{
logger.warn( "Adding user: " + inUser.getId() + " to task with id: " + taskFromDb.getId() );
taskFromDb.getUsers().add( inUser );
}
} ) );
return this.taskRepository.save( taskFromDb );
}
public Task removeUsersFromTask ( Task task )
{
Collection<User> inUsers = task.getUsers();
Task taskFromDb = this.getById( task.getId() );
inUsers.forEach( ( user ->
{
if ( taskFromDb.getUsers().contains( user ) )
{
logger.info( "Removing user: " + user.getId() + " from task with id: " + taskFromDb.getId() );
this.taskRepository.removeUserFromTask( task.getId(), user.getId() );
} else
{
logger.warn( "User: " + user.getId() + " not found in task with id: " + taskFromDb.getId() );
}
} ) );
return this.getById( taskFromDb.getId() );
}
public void changeTicketStatus ( Long ticketId, TicketStatus status )
{
this.ticketService.changeStatus( ticketId, status );
}
public Task getById ( Long taskId )
{
this.verifyTaskExists( taskId );
return this.taskRepository.findOne( taskId );
}
public List<Task> getAll ()
{
return this.taskRepository.findAll();
}
public Task getTaskByTicketId ( Long ticketId )
{
return this.taskRepository.findByTicketId( ticketId )
.orElseThrow( () -> new ResourceNotFoundException( "There is no Task for given ticket with id : " + ticketId ) );
}
public boolean taskExistsForTicketId ( Long ticketId )
{
return this.taskRepository.findByTicketId( ticketId ).isPresent();
}
public void verifyTaskExists ( Long taskId ) throws ResourceNotFoundException
{
Task task = this.taskRepository.findOne( taskId );
if ( task == null )
{
throw new ResourceNotFoundException( "Task with id " + taskId + " not found" );
}
}
}