1 |
package dk.daoas.adressevedligehold.tasks;
|
2 |
|
3 |
import java.util.List;
|
4 |
|
5 |
import javax.servlet.ServletContext;
|
6 |
import javax.ws.rs.GET;
|
7 |
import javax.ws.rs.Path;
|
8 |
import javax.ws.rs.PathParam;
|
9 |
import javax.ws.rs.Produces;
|
10 |
import javax.ws.rs.core.Context;
|
11 |
import javax.ws.rs.core.MediaType;
|
12 |
|
13 |
import com.google.gson.Gson;
|
14 |
|
15 |
import dk.daoas.adressevedligehold.tasks.Task.TaskBean;
|
16 |
|
17 |
@Path("/tasks")
|
18 |
public class TaskRestService {
|
19 |
|
20 |
|
21 |
@Path("/list")
|
22 |
@GET
|
23 |
@Produces(MediaType.APPLICATION_JSON)
|
24 |
public String listTasks(@Context ServletContext context ) {
|
25 |
TaskManager taskManager = (TaskManager) context.getAttribute(TaskManager.TASKMANAGER);
|
26 |
List<TaskBean> beans = taskManager.getTasks();
|
27 |
|
28 |
Gson gson = new Gson();
|
29 |
String json = gson.toJson(beans);
|
30 |
|
31 |
return json;
|
32 |
}
|
33 |
|
34 |
@Path("/log/{id}")
|
35 |
@GET
|
36 |
@Produces(MediaType.TEXT_PLAIN)
|
37 |
public String getLog(@Context ServletContext context, @PathParam("id") int taskId ) {
|
38 |
TaskManager taskManager = (TaskManager) context.getAttribute(TaskManager.TASKMANAGER);
|
39 |
Task t = taskManager.getTaskById(taskId);
|
40 |
|
41 |
if (t == null ){
|
42 |
return "No task with id: " + taskId;
|
43 |
}
|
44 |
|
45 |
return t.getLog();
|
46 |
}
|
47 |
|
48 |
@Path("/remove/{id}")
|
49 |
@GET
|
50 |
@Produces(MediaType.TEXT_PLAIN)
|
51 |
public String removeTask(@Context ServletContext context, @PathParam("id") int taskId ) {
|
52 |
TaskManager taskManager = (TaskManager) context.getAttribute(TaskManager.TASKMANAGER);
|
53 |
Task removeTask = taskManager.getTaskById(taskId);
|
54 |
|
55 |
if (removeTask == null ){
|
56 |
return "No task with id: " + taskId;
|
57 |
}
|
58 |
|
59 |
taskManager.removeTask(removeTask);
|
60 |
|
61 |
return "OK";
|
62 |
}
|
63 |
|
64 |
|
65 |
|
66 |
}
|