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.gen;
18  
19  import com.atlassian.jira.rest.client.domain.Comment;
20  import com.atlassian.jira.rest.client.domain.ServerInfo;
21  import com.atlassian.jira.rest.client.domain.Visibility;
22  import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
23  import com.atlassian.jira.rest.client.internal.json.CommentJsonParser;
24  import org.codehaus.jettison.json.JSONException;
25  import org.codehaus.jettison.json.JSONObject;
26  
27  public class CommentJsonGenerator implements JsonGenerator<Comment> {
28  
29  	private final ServerInfo serverInfo;
30  
31  	public CommentJsonGenerator(ServerInfo serverInfo) {
32  		this.serverInfo = serverInfo;
33  	}
34  
35  	@Override
36  	public JSONObject generate(Comment comment) throws JSONException {
37  		JSONObject res = new JSONObject();
38  		if (comment.getBody() != null) {
39  			res.put("body", comment.getBody());
40  		}
41  
42  		final Visibility commentVisibility = comment.getVisibility();
43  		if (commentVisibility != null) {
44  
45  			final int buildNumber = serverInfo.getBuildNumber();
46  			if (buildNumber >= ServerVersionConstants.BN_JIRA_4_3) {
47  				JSONObject visibilityJson = new JSONObject();
48  				final String commentVisibilityType;
49  				if (buildNumber >= ServerVersionConstants.BN_JIRA_5) {
50  					commentVisibilityType = commentVisibility.getType() == Visibility.Type.GROUP ? "group" : "role";
51  				} else {
52  					commentVisibilityType = commentVisibility.getType() == Visibility.Type.GROUP ? "GROUP" : "ROLE";
53  				}
54  				visibilityJson.put("type", commentVisibilityType);
55  				visibilityJson.put("value", commentVisibility.getValue());
56  				res.put(CommentJsonParser.VISIBILITY_KEY, visibilityJson);
57  			} else {
58  				if (commentVisibility.getType() == Visibility.Type.ROLE) {
59  					res.put("role", commentVisibility.getValue());
60  				} else {
61  					res.put("group", commentVisibility.getValue());
62  				}
63  			}
64  		}
65  
66  		return res;
67  	}
68  }