View Javadoc

1   /*
2    * Copyright (C) 2011 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.atlassian.jira.rest.client.internal.json;
18  
19  import com.atlassian.jira.rest.client.api.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)) { // JIRA 4.3-rc1 and newer
48              return parse(parentObject.getJSONObject(CommentJsonParser.VISIBILITY_KEY));
49          }
50  
51          String roleLevel = parentObject.optString("roleLevel", null);
52          // in JIRA 4.2 "role" was used instead
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  }