View Javadoc

1   /*
2    * Copyright (C) 2012 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  package com.atlassian.jira.rest.client.internal.json;
17  
18  import com.atlassian.jira.rest.client.api.domain.ProjectRole;
19  import com.atlassian.jira.rest.client.api.domain.RoleActor;
20  import com.google.common.base.Optional;
21  import com.google.common.collect.ImmutableSet;
22  import org.codehaus.jettison.json.JSONArray;
23  import org.codehaus.jettison.json.JSONException;
24  import org.codehaus.jettison.json.JSONObject;
25  
26  import java.net.URI;
27  import java.util.Collection;
28  
29  public class ProjectRoleJsonParser implements JsonObjectParser<ProjectRole> {
30  
31      private final RoleActorJsonParser roleActorJsonParser;
32  
33      public ProjectRoleJsonParser(URI baseJiraUri) {
34          this.roleActorJsonParser = new RoleActorJsonParser(baseJiraUri);
35      }
36  
37      @Override
38      public ProjectRole parse(final JSONObject json) throws JSONException {
39          final URI self = JsonParseUtil.getSelfUri(json);
40          final long id = json.getLong("id");
41          final String name = json.getString("name");
42          final String description = json.getString("description");
43          final Optional<JSONArray> roleActorsOpt = JsonParseUtil.getOptionalArray(json, "actors");
44          final Collection<RoleActor> roleActors = roleActorsOpt.isPresent() ?
45                  JsonParseUtil.parseJsonArray(roleActorsOpt.get(), roleActorJsonParser) : ImmutableSet.<RoleActor>of();
46          return new ProjectRole(id, self, name, description, roleActors);
47      }
48  
49  }