1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.atlassian.jira.rest.client.internal.async;
17
18 import com.atlassian.jira.rest.client.api.ComponentRestClient;
19 import com.atlassian.jira.rest.client.api.domain.Component;
20 import com.atlassian.jira.rest.client.api.domain.input.ComponentInput;
21 import com.atlassian.jira.rest.client.internal.domain.input.ComponentInputWithProjectKey;
22 import com.atlassian.jira.rest.client.internal.json.ComponentJsonParser;
23 import com.atlassian.jira.rest.client.internal.json.JsonObjectParser;
24 import com.atlassian.jira.rest.client.internal.json.gen.ComponentInputWithProjectKeyJsonGenerator;
25 import com.atlassian.httpclient.api.HttpClient;
26 import com.atlassian.util.concurrent.Promise;
27 import org.codehaus.jettison.json.JSONException;
28 import org.codehaus.jettison.json.JSONObject;
29
30 import javax.annotation.Nullable;
31 import javax.ws.rs.core.UriBuilder;
32 import java.net.URI;
33
34
35
36
37
38
39 public class AsynchronousComponentRestClient extends AbstractAsynchronousRestClient implements ComponentRestClient {
40
41 private final ComponentJsonParser componentJsonParser = new ComponentJsonParser();
42 private final URI componentUri;
43
44 public AsynchronousComponentRestClient(final URI baseUri, final HttpClient client) {
45 super(client);
46 componentUri = UriBuilder.fromUri(baseUri).path("component").build();
47 }
48
49 @Override
50 public Promise<Component> getComponent(final URI componentUri) {
51 return getAndParse(componentUri, componentJsonParser);
52 }
53
54 @Override
55 public Promise<Component> createComponent(final String projectKey, final ComponentInput componentInput) {
56 final ComponentInputWithProjectKey helper = new ComponentInputWithProjectKey(projectKey, componentInput);
57 return postAndParse(componentUri, helper, new ComponentInputWithProjectKeyJsonGenerator(), componentJsonParser);
58 }
59
60 @Override
61 public Promise<Component> updateComponent(URI componentUri, ComponentInput componentInput) {
62 final ComponentInputWithProjectKey helper = new ComponentInputWithProjectKey(null, componentInput);
63 return putAndParse(componentUri, helper, new ComponentInputWithProjectKeyJsonGenerator(), componentJsonParser);
64 }
65
66 @Override
67 public Promise<Void> removeComponent(URI componentUri, @Nullable URI moveIssueToComponentUri) {
68 final UriBuilder uriBuilder = UriBuilder.fromUri(componentUri);
69 if (moveIssueToComponentUri != null) {
70 uriBuilder.queryParam("moveIssuesTo", moveIssueToComponentUri);
71 }
72 return delete(uriBuilder.build());
73 }
74
75 @Override
76 public Promise<Integer> getComponentRelatedIssuesCount(URI componentUri) {
77 final URI relatedIssueCountsUri = UriBuilder.fromUri(componentUri).path("relatedIssueCounts").build();
78 return getAndParse(relatedIssueCountsUri, new JsonObjectParser<Integer>() {
79 @Override
80 public Integer parse(JSONObject json) throws JSONException {
81 return json.getInt("issueCount");
82 }
83 });
84 }
85 }