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.gen;
18  
19  import com.atlassian.jira.rest.client.api.domain.AssigneeType;
20  import com.atlassian.jira.rest.client.internal.domain.AssigneeTypeConstants;
21  import com.atlassian.jira.rest.client.api.RestClientException;
22  import com.atlassian.jira.rest.client.internal.domain.input.ComponentInputWithProjectKey;
23  import org.codehaus.jettison.json.JSONException;
24  import org.codehaus.jettison.json.JSONObject;
25  
26  public class ComponentInputWithProjectKeyJsonGenerator implements JsonGenerator<ComponentInputWithProjectKey> {
27  
28  	@Override
29  	public JSONObject generate(ComponentInputWithProjectKey componentInput) throws JSONException {
30  		JSONObject res = new JSONObject();
31  		if (componentInput.getProjectKey() != null) {
32  			res.put("project", componentInput.getProjectKey());
33  		}
34  		if (componentInput.getName() != null) {
35  			res.put("name", componentInput.getName());
36  		}
37  		if (componentInput.getDescription() != null) {
38  			res.put("description", componentInput.getDescription());
39  		}
40  		if (componentInput.getLeadUsername() != null) {
41  			res.put("leadUserName", componentInput.getLeadUsername());
42  		}
43  		final AssigneeType assigneeType = componentInput.getAssigneeType();
44  		if (assigneeType != null) {
45  			final String assigneeTypeStr;
46  			switch (assigneeType) {
47  				case PROJECT_DEFAULT:
48  					assigneeTypeStr = AssigneeTypeConstants.PROJECT_DEFAULT;
49  					break;
50  				case COMPONENT_LEAD:
51  					assigneeTypeStr = AssigneeTypeConstants.COMPONENT_LEAD;
52  					break;
53  				case PROJECT_LEAD:
54  					assigneeTypeStr = AssigneeTypeConstants.PROJECT_LEAD;
55  					break;
56  				case UNASSIGNED:
57  					assigneeTypeStr = AssigneeTypeConstants.UNASSIGNED;
58  					break;
59  				default:
60  					throw new RestClientException("Unexpected assignee type [" + assigneeType + "]", null);
61  			}
62  			res.put("assigneeType", assigneeTypeStr);
63  		}
64  		return res;
65  	}
66  }