1 |
torben |
2903 |
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 |
torben |
2919 |
@Context ServletContext context;
|
21 |
torben |
2903 |
|
22 |
torben |
2919 |
|
23 |
torben |
2903 |
@Path("/list")
|
24 |
|
|
@GET
|
25 |
|
|
@Produces(MediaType.APPLICATION_JSON)
|
26 |
torben |
2919 |
public String listTasks( ) {
|
27 |
torben |
2903 |
TaskManager taskManager = (TaskManager) context.getAttribute(TaskManager.TASKMANAGER);
|
28 |
|
|
List<TaskBean> beans = taskManager.getTasks();
|
29 |
|
|
|
30 |
|
|
Gson gson = new Gson();
|
31 |
|
|
String json = gson.toJson(beans);
|
32 |
|
|
|
33 |
|
|
return json;
|
34 |
|
|
}
|
35 |
|
|
|
36 |
|
|
@Path("/log/{id}")
|
37 |
|
|
@GET
|
38 |
|
|
@Produces(MediaType.TEXT_PLAIN)
|
39 |
torben |
2919 |
public String getLog( @PathParam("id") int taskId ) {
|
40 |
torben |
2903 |
TaskManager taskManager = (TaskManager) context.getAttribute(TaskManager.TASKMANAGER);
|
41 |
|
|
Task t = taskManager.getTaskById(taskId);
|
42 |
|
|
|
43 |
|
|
if (t == null ){
|
44 |
|
|
return "No task with id: " + taskId;
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
return t.getLog();
|
48 |
|
|
}
|
49 |
|
|
|
50 |
torben |
2918 |
@Path("/remove/{id}")
|
51 |
|
|
@GET
|
52 |
|
|
@Produces(MediaType.TEXT_PLAIN)
|
53 |
torben |
2919 |
public String removeTask( @PathParam("id") int taskId ) {
|
54 |
torben |
2918 |
TaskManager taskManager = (TaskManager) context.getAttribute(TaskManager.TASKMANAGER);
|
55 |
|
|
Task removeTask = taskManager.getTaskById(taskId);
|
56 |
torben |
2903 |
|
57 |
torben |
2918 |
if (removeTask == null ){
|
58 |
|
|
return "No task with id: " + taskId;
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
taskManager.removeTask(removeTask);
|
62 |
|
|
|
63 |
|
|
return "OK";
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
|
67 |
torben |
2903 |
|
68 |
|
|
}
|