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  	// this has to be "configurable" as JIRA 4.2 and JIRA 4.3 EAP differently name
32  	// this attribute for transitions and for issue linking
33  	private final String groupLevelAttribute;
34  
35  	public CommentJsonGenerator(ServerInfo serverInfo) {
36  		this(serverInfo, "groupLevel");
37  	}
38  
39  	public CommentJsonGenerator(ServerInfo serverInfo, String groupLevelAttribute) {
40  		this.serverInfo = serverInfo;
41  		this.groupLevelAttribute = groupLevelAttribute;
42  	}
43  
44  	@Override
45  	public JSONObject generate(Comment comment) throws JSONException {
46  		JSONObject res = new JSONObject();
47  		if (comment.getBody() != null) {
48  			res.put("body", comment.getBody());
49  		}
50  
51  		final Visibility commentVisibility = comment.getVisibility();
52  		if (commentVisibility != null) {
53  
54  			final int buildNumber = serverInfo.getBuildNumber();
55  			if (buildNumber >= ServerVersionConstants.BN_JIRA_4_3) {
56  				JSONObject visibilityJson = new JSONObject();
57  				final String commentVisibilityType;
58  				if (buildNumber >= ServerVersionConstants.BN_JIRA_5) {
59  					commentVisibilityType = commentVisibility.getType() == Visibility.Type.GROUP ? "group" : "role";
60  				} else {
61  					commentVisibilityType = commentVisibility.getType() == Visibility.Type.GROUP ? "GROUP" : "ROLE";
62  				}
63  				visibilityJson.put("type", commentVisibilityType);
64  				visibilityJson.put("value", commentVisibility.getValue());
65  				res.put(CommentJsonParser.VISIBILITY_KEY, visibilityJson);
66  			} else {
67  				if (commentVisibility.getType() == Visibility.Type.ROLE) {
68  					res.put("role", commentVisibility.getValue());
69  				} else {
70  					res.put(getGroupLevelAttribute(), commentVisibility.getValue());
71  				}
72  			}
73  		}
74  
75  		return res;
76  	}
77  
78  	protected String getGroupLevelAttribute() {
79  		return groupLevelAttribute;
80  	}
81  }