/
Bugs.java
109 lines (97 loc) · 4.28 KB
/
Bugs.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
package demo;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.Path;
import org.json.JSONException;
import org.json.JSONObject;
import com.centurylink.mdw.common.service.ServiceException;
import com.centurylink.mdw.demo.bugs.Bug;
import com.centurylink.mdw.model.Status;
import com.centurylink.mdw.model.StatusResponse;
import com.centurylink.mdw.model.task.TaskInstance;
import com.centurylink.mdw.services.ServiceLocator;
import com.centurylink.mdw.services.TaskServices;
import com.centurylink.mdw.services.rest.JsonRestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@Path("/bugs")
@Api("Bugs API")
public class Bugs extends JsonRestService {
@Override
@ApiOperation(value="Report a bug",
notes="We expect this service never to be used :)", response=StatusResponse.class)
@ApiImplicitParams({
@ApiImplicitParam(name="bug", paramType="body", required=true, dataType="com.centurylink.mdw.demo.bugs.Bug")
})
public JSONObject post(String path, JSONObject content, Map<String,String> headers)
throws ServiceException, JSONException {
String requestId = Long.toHexString(System.nanoTime());
return invokeServiceProcess("com.centurylink.mdw.demo.bugs/Create Bug", new Bug(content), requestId,
null, headers);
}
@Override
@Path("/{id}")
@ApiOperation(value="Retrieve a bug by id", response=Bug.class)
public JSONObject get(String path, Map<String,String> headers)
throws ServiceException, JSONException {
TaskInstance bugTask = getBugTask(path);
Bug bug = new Bug(bugTask.getJson());
bug.setSeverity(bugTask.getPriority());
bug.setDescription(bugTask.getComments());
return bug.getJson();
}
@Override
@Path("/{id}")
@ApiOperation(value="Update a bug", response=StatusResponse.class)
@ApiImplicitParams({
@ApiImplicitParam(name="bug", paramType="body", required=true, dataType="com.centurylink.mdw.demo.bugs.Bug")
})
public JSONObject put(String path, JSONObject content, Map<String,String> headers)
throws ServiceException, JSONException {
TaskInstance bugTask = getBugTask(path);
Bug bug = new Bug(content);
TaskServices taskServices = ServiceLocator.getTaskServices();
// self-assign if needed
if (bugTask.getAssignee() == null) {
String user = getAuthUser(headers);
performAction("Assign", bugTask.getTaskInstanceId(), user, user);
}
// update header info
bugTask.setPriority(bug.getSeverity());
bugTask.setComments(bug.getDescription());
taskServices.updateTask(getAuthUser(headers), bugTask);
// update runtime values
Map<String,String> values = new HashMap<>();
values.put("bug", bug.toString());
taskServices.applyValues(bugTask.getTaskInstanceId(), values);
return null; // sends a standard 200 response
}
@Override
@Path("/{id}")
@ApiOperation(value="Delete (cancel) a bug by id", response=StatusResponse.class)
public JSONObject delete(String path, JSONObject content, Map<String,String> headers)
throws ServiceException, JSONException {
TaskInstance bugTask = getBugTask(path);
performAction("Cancel", bugTask.getTaskInstanceId(), getAuthUser(headers), null);
return null;
}
/**
* Return a bug task according to the {id} path param.
*/
private TaskInstance getBugTask(String path) throws ServiceException {
String id = getSegment(path, 2);
if (id == null)
throw new ServiceException(Status.BAD_REQUEST.getCode(), "Missing path parameter: {id}");
TaskServices taskServices = ServiceLocator.getTaskServices();
TaskInstance bugTask = taskServices.getInstance(Long.parseLong(id));
if (bugTask == null)
throw new ServiceException(Status.NOT_FOUND.getCode(), "Bug not found: " + id);
return bugTask;
}
private void performAction(String action, Long taskInstanceId, String user, String assignee)
throws ServiceException {
ServiceLocator.getTaskServices().performAction(taskInstanceId, action, user, assignee, null, null, true);
}
}