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.domain.Comment;
20 import com.atlassian.jira.rest.client.domain.ServerInfo;
21 import com.atlassian.jira.rest.client.domain.Visibility;
22 import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
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
32
33 private final String groupLevelAttribute;
34
35 public CommentJsonGenerator(ServerInfo serverInfo) {
36 this(serverInfo, "groupLevel");
37 }
38
39 public CommentJsonGenerator(ServerInfo serverInfo, String groupLevelAttribute) {
40 this.serverInfo = serverInfo;
41 this.groupLevelAttribute = groupLevelAttribute;
42 }
43
44 @Override
45 public JSONObject generate(Comment comment) throws JSONException {
46 JSONObject res = new JSONObject();
47 if (comment.getBody() != null) {
48 res.put("body", comment.getBody());
49 }
50
51 final Visibility commentVisibility = comment.getVisibility();
52 if (commentVisibility != null) {
53
54 final int buildNumber = serverInfo.getBuildNumber();
55 if (buildNumber >= ServerVersionConstants.BN_JIRA_4_3) {
56 JSONObject visibilityJson = new JSONObject();
57 final String commentVisibilityType;
58 if (buildNumber >= ServerVersionConstants.BN_JIRA_5) {
59 commentVisibilityType = commentVisibility.getType() == Visibility.Type.GROUP ? "group" : "role";
60 } else {
61 commentVisibilityType = commentVisibility.getType() == Visibility.Type.GROUP ? "GROUP" : "ROLE";
62 }
63 visibilityJson.put("type", commentVisibilityType);
64 visibilityJson.put("value", commentVisibility.getValue());
65 res.put(CommentJsonParser.VISIBILITY_KEY, visibilityJson);
66 } else {
67 if (commentVisibility.getType() == Visibility.Type.ROLE) {
68 res.put("role", commentVisibility.getValue());
69 } else {
70 res.put(getGroupLevelAttribute(), commentVisibility.getValue());
71 }
72 }
73 }
74
75 return res;
76 }
77
78 protected String getGroupLevelAttribute() {
79 return groupLevelAttribute;
80 }
81 }