View Javadoc

1   /*
2    * Copyright (C) 2010-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  
17  package com.atlassian.jira.rest.client.internal.json;
18  
19  import com.atlassian.jira.rest.client.api.OptionalIterable;
20  import com.atlassian.jira.rest.client.api.domain.BasicComponent;
21  import com.atlassian.jira.rest.client.api.domain.BasicProjectRole;
22  import com.atlassian.jira.rest.client.api.domain.BasicUser;
23  import com.atlassian.jira.rest.client.api.domain.IssueType;
24  import com.atlassian.jira.rest.client.api.domain.Project;
25  import com.atlassian.jira.rest.client.api.domain.Version;
26  import com.google.common.base.Splitter;
27  import org.codehaus.jettison.json.JSONArray;
28  import org.codehaus.jettison.json.JSONException;
29  import org.codehaus.jettison.json.JSONObject;
30  
31  import java.net.URI;
32  import java.net.URISyntaxException;
33  import java.util.Collection;
34  import java.util.Collections;
35  
36  public class ProjectJsonParser implements JsonObjectParser<Project> {
37  
38      private final VersionJsonParser versionJsonParser = new VersionJsonParser();
39      private final BasicComponentJsonParser componentJsonParser = new BasicComponentJsonParser();
40      private final IssueTypeJsonParser issueTypeJsonParser = new IssueTypeJsonParser();
41      private final BasicProjectRoleJsonParser basicProjectRoleJsonParser = new BasicProjectRoleJsonParser();
42  
43      static Iterable<String> parseExpandos(final JSONObject json) throws JSONException {
44          if (json.has("expand")) {
45              final String expando = json.getString("expand");
46              return Splitter.on(',').split(expando);
47          } else {
48              return Collections.emptyList();
49          }
50      }
51  
52      @Override
53      public Project parse(JSONObject json) throws JSONException {
54          URI self = JsonParseUtil.getSelfUri(json);
55          final Iterable<String> expandos = parseExpandos(json);
56          final BasicUser lead = JsonParseUtil.parseBasicUser(json.getJSONObject("lead"));
57          final String key = json.getString("key");
58          final Long id = JsonParseUtil.getOptionalLong(json, "id");
59          final String name = JsonParseUtil.getOptionalString(json, "name");
60          final String urlStr = JsonParseUtil.getOptionalString(json, "url");
61          URI uri;
62          try {
63              uri = urlStr == null || "".equals(urlStr) ? null : new URI(urlStr);
64          } catch (URISyntaxException e) {
65              uri = null;
66          }
67          String description = JsonParseUtil.getOptionalString(json, "description");
68          if ("".equals(description)) {
69              description = null;
70          }
71          final Collection<Version> versions = JsonParseUtil.parseJsonArray(json.getJSONArray("versions"), versionJsonParser);
72          final Collection<BasicComponent> components = JsonParseUtil.parseJsonArray(json
73                  .getJSONArray("components"), componentJsonParser);
74          final JSONArray issueTypesArray = json.optJSONArray("issueTypes");
75          final OptionalIterable<IssueType> issueTypes = JsonParseUtil.parseOptionalJsonArray(issueTypesArray, issueTypeJsonParser);
76          final Collection<BasicProjectRole> projectRoles = basicProjectRoleJsonParser.parse(JsonParseUtil
77                  .getOptionalJsonObject(json, "roles"));
78          return new Project(expandos, self, key, id, name, description, lead, uri, versions, components, issueTypes, projectRoles);
79      }
80  
81  
82  }