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.domain.BasicComponent;
20  import com.atlassian.jira.rest.client.api.domain.BasicUser;
21  import com.atlassian.jira.rest.client.api.domain.IssueType;
22  import com.atlassian.jira.rest.client.api.domain.Project;
23  import com.atlassian.jira.rest.client.api.OptionalIterable;
24  import com.atlassian.jira.rest.client.api.domain.BasicProjectRole;
25  import com.atlassian.jira.rest.client.api.domain.Version;
26  import org.codehaus.jettison.json.JSONArray;
27  import org.codehaus.jettison.json.JSONException;
28  import org.codehaus.jettison.json.JSONObject;
29  
30  import java.net.URI;
31  import java.net.URISyntaxException;
32  import java.util.Collection;
33  
34  public class ProjectJsonParser implements JsonObjectParser<Project> {
35  
36  	private final VersionJsonParser versionJsonParser = new VersionJsonParser();
37  	private final BasicComponentJsonParser componentJsonParser = new BasicComponentJsonParser();
38  	private final IssueTypeJsonParser issueTypeJsonParser = new IssueTypeJsonParser();
39  	private final BasicProjectRoleJsonParser basicProjectRoleJsonParser = new BasicProjectRoleJsonParser();
40  
41  	@Override
42  	public Project parse(JSONObject json) throws JSONException {
43  		URI self = JsonParseUtil.getSelfUri(json);
44  		final BasicUser lead = JsonParseUtil.parseBasicUser(json.getJSONObject("lead"));
45  		final String key = json.getString("key");
46  		final String name = JsonParseUtil.getOptionalString(json, "name");
47  		final String urlStr = JsonParseUtil.getOptionalString(json, "url");
48  		URI uri;
49  		try {
50  			uri = urlStr == null || "".equals(urlStr) ? null : new URI(urlStr);
51  		} catch (URISyntaxException e) {
52  			uri = null;
53  		}
54  		String description = JsonParseUtil.getOptionalString(json, "description");
55  		if ("".equals(description)) {
56  			description = null;
57  		}
58  		final Collection<Version> versions = JsonParseUtil.parseJsonArray(json.getJSONArray("versions"), versionJsonParser);
59  		final Collection<BasicComponent> components = JsonParseUtil.parseJsonArray(json
60  				.getJSONArray("components"), componentJsonParser);
61  		final JSONArray issueTypesArray = json.optJSONArray("issueTypes");
62  		final OptionalIterable<IssueType> issueTypes = JsonParseUtil.parseOptionalJsonArray(issueTypesArray, issueTypeJsonParser);
63  		final Collection<BasicProjectRole> projectRoles = basicProjectRoleJsonParser.parse(JsonParseUtil
64  				.getOptionalJsonObject(json, "roles"));
65  		return new Project(self, key, name, description, lead, uri, versions, components, issueTypes, projectRoles);
66  	}
67  
68  
69  }