View Javadoc

1   /*
2    * Copyright (C) 2010 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.AssigneeType;
20  import com.atlassian.jira.rest.client.api.domain.BasicComponent;
21  import com.atlassian.jira.rest.client.api.domain.BasicUser;
22  import com.atlassian.jira.rest.client.api.domain.Component;
23  import com.atlassian.jira.rest.client.internal.domain.AssigneeTypeConstants;
24  import org.codehaus.jettison.json.JSONException;
25  import org.codehaus.jettison.json.JSONObject;
26  
27  public class ComponentJsonParser implements JsonObjectParser<Component> {
28  	@Override
29  	public Component parse(JSONObject json) throws JSONException {
30  		final BasicComponent basicComponent = BasicComponentJsonParser.parseBasicComponent(json);
31  		final JSONObject leadJson = json.optJSONObject("lead");
32  		final BasicUser lead = leadJson != null ? JsonParseUtil.parseBasicUser(leadJson) : null;
33  		final String assigneeTypeStr = JsonParseUtil.getOptionalString(json, "assigneeType");
34  		final Component.AssigneeInfo assigneeInfo;
35  		if (assigneeTypeStr != null) {
36  			final AssigneeType assigneeType = parseAssigneeType(assigneeTypeStr);
37  			final JSONObject assigneeJson = json.optJSONObject("assignee");
38  			final BasicUser assignee = assigneeJson != null ? JsonParseUtil.parseBasicUser(assigneeJson) : null;
39  			final AssigneeType realAssigneeType = parseAssigneeType(json.getString("realAssigneeType"));
40  			final JSONObject realAssigneeJson = json.optJSONObject("realAssignee");
41  			final BasicUser realAssignee = realAssigneeJson != null ? JsonParseUtil.parseBasicUser(realAssigneeJson) : null;
42  			final boolean isAssigneeTypeValid = json.getBoolean("isAssigneeTypeValid");
43  			assigneeInfo = new Component.AssigneeInfo(assignee, assigneeType, realAssignee, realAssigneeType, isAssigneeTypeValid);
44  		} else {
45  			assigneeInfo = null;
46  		}
47  
48  		return new Component(basicComponent.getSelf(), basicComponent.getId(), basicComponent.getName(), basicComponent
49  				.getDescription(), lead, assigneeInfo);
50  	}
51  
52  	AssigneeType parseAssigneeType(String str) throws JSONException {
53  		// JIRA 4.4+ adds full assignee info to component resource
54  		if (AssigneeTypeConstants.COMPONENT_LEAD.equals(str)) {
55  			return AssigneeType.COMPONENT_LEAD;
56  		}
57  		if (AssigneeTypeConstants.PROJECT_DEFAULT.equals(str)) {
58  			return AssigneeType.PROJECT_DEFAULT;
59  		}
60  		if (AssigneeTypeConstants.PROJECT_LEAD.equals(str)) {
61  			return AssigneeType.PROJECT_LEAD;
62  		}
63  		if (AssigneeTypeConstants.UNASSIGNED.equals(str)) {
64  			return AssigneeType.UNASSIGNED;
65  		}
66  		throw new JSONException("Unexpected value of assignee type [" + str + "]");
67  	}
68  }