1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.atlassian.jira.rest.client.internal.json.gen;
18
19 import com.atlassian.jira.rest.client.api.domain.Comment;
20 import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
21 import com.atlassian.jira.rest.client.api.domain.ServerInfo;
22 import com.atlassian.jira.rest.client.api.domain.Visibility;
23 import com.atlassian.jira.rest.client.internal.json.CommentJsonParser;
24 import org.codehaus.jettison.json.JSONException;
25 import org.codehaus.jettison.json.JSONObject;
26
27 public class CommentJsonGenerator implements JsonGenerator<Comment> {
28
29 private final ServerInfo serverInfo;
30
31 public CommentJsonGenerator(ServerInfo serverInfo) {
32 this.serverInfo = serverInfo;
33 }
34
35 @Override
36 public JSONObject generate(Comment comment) throws JSONException {
37 JSONObject res = new JSONObject();
38 if (comment.getBody() != null) {
39 res.put("body", comment.getBody());
40 }
41
42 final Visibility commentVisibility = comment.getVisibility();
43 if (commentVisibility != null) {
44
45 final int buildNumber = serverInfo.getBuildNumber();
46 if (buildNumber >= ServerVersionConstants.BN_JIRA_4_3) {
47 JSONObject visibilityJson = new JSONObject();
48 final String commentVisibilityType;
49 if (buildNumber >= ServerVersionConstants.BN_JIRA_5) {
50 commentVisibilityType = commentVisibility.getType() == Visibility.Type.GROUP ? "group" : "role";
51 } else {
52 commentVisibilityType = commentVisibility.getType() == Visibility.Type.GROUP ? "GROUP" : "ROLE";
53 }
54 visibilityJson.put("type", commentVisibilityType);
55 visibilityJson.put("value", commentVisibility.getValue());
56 res.put(CommentJsonParser.VISIBILITY_KEY, visibilityJson);
57 } else {
58 if (commentVisibility.getType() == Visibility.Type.ROLE) {
59 res.put("role", commentVisibility.getValue());
60 } else {
61 res.put("group", commentVisibility.getValue());
62 }
63 }
64 }
65
66 return res;
67 }
68 }