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;
18
19 import com.atlassian.jira.rest.client.domain.Visibility;
20 import org.codehaus.jettison.json.JSONException;
21 import org.codehaus.jettison.json.JSONObject;
22
23 import javax.annotation.Nullable;
24
25 public class VisibilityJsonParser implements JsonObjectParser<Visibility> {
26 private static final String ROLE_TYPE = "ROLE";
27 private static final String GROUP_TYPE = "GROUP";
28
29 @Override
30 public Visibility parse(JSONObject json) throws JSONException {
31 final String type = json.getString("type");
32 final Visibility.Type visibilityType;
33 if (ROLE_TYPE.equalsIgnoreCase(type)) {
34 visibilityType = Visibility.Type.ROLE;
35 } else if (GROUP_TYPE.equalsIgnoreCase(type)) {
36 visibilityType = Visibility.Type.GROUP;
37 } else {
38 throw new JSONException("[" + type + "] does not represent a valid visibility type. Expected ["
39 + ROLE_TYPE + "] or [" + GROUP_TYPE + "].");
40 }
41 final String value = json.getString("value");
42 return new Visibility(visibilityType, value);
43 }
44
45 @Nullable
46 public Visibility parseVisibility(JSONObject parentObject) throws JSONException {
47 if (parentObject.has(CommentJsonParser.VISIBILITY_KEY)) {
48 return parse(parentObject.getJSONObject(CommentJsonParser.VISIBILITY_KEY));
49 }
50
51 String roleLevel = parentObject.optString("roleLevel", null);
52
53 if (roleLevel == null) {
54 roleLevel = JsonParseUtil.getOptionalString(parentObject, "role");
55 }
56
57 if (roleLevel != null) {
58 return Visibility.role(roleLevel);
59 }
60
61 final String groupLevel = parentObject.optString("groupLevel", null);
62 if (groupLevel != null) {
63 return Visibility.group(groupLevel);
64 }
65 return null;
66 }
67 }