Child: [042431] (diff)

Download this file

BugComments.java    186 lines (162 with data), 5.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright 2011 Thomas Golden
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.j2bugzilla.base.Bug;
import com.j2bugzilla.base.BugzillaMethod;
import utils.Comment;
/**
* This class allows clients to request a list of all public {@link Comment Comments} made on a
* specific {@link Bug} in a Bugzilla installation. The {@link Bug} must already
* exist in the installation.
*
* @author Tom
*
*/
public class BugComments implements BugzillaMethod {
/**
* The XML-RPC method Bugzilla will use
*/
private static final String METHOD_NAME = "Bug.comments";
/**
* A {@code Map} of parameter objects required by the XML-RPC method call.
*/
private final Map<Object, Object> params = new HashMap<Object, Object>();
/**
* A {@code Map} of objects returned by the XML-RPC method call.
*/
private Map<Object, Object> hash = new HashMap<Object, Object>();
/**
* The ID of the {@link Bug}
*/
// private final int id;
private final Date date;
private final List<Integer> bugIdList;
/**
* Creates a new {@link BugComments} object for the specified
* {@link Bug}
*
* @param bug A {@link Bug} to retrieve comments for
*/
public BugComments(Bug bug) {
bugIdList = new ArrayList<Integer>();
bugIdList.add(bug.getID());
params.put("ids", bugIdList);
date = null;
}
/**
* Creates a new {@link BugComments} object for the specified
* {@link Bug} ID.
* @param bugId An integer specifying which {@link Bug} to retrieve
* comments for
*/
public BugComments(int bugId) {
bugIdList = new ArrayList<Integer>();
bugIdList.add(bugId);
params.put("ids", bugIdList);
date = null;
}
public BugComments(Comment comment) {
bugIdList = null;
params.put("comment_ids", comment.getId());
date = null;
}
public BugComments(int bugId, Date date) {
bugIdList = new ArrayList<Integer>();
bugIdList.add(bugId);
params.put("ids", bugIdList);
this.date = date;
params.put("new_since", date);
}
public BugComments(List<Integer> bugIdList) { // int[] idArray) {
this.bugIdList = bugIdList;
params.put("ids", bugIdList);
date = null;
}
public BugComments(List<Integer> bugIdList, Date date) {
this.bugIdList = bugIdList;
params.put("ids", bugIdList);
this.date = date;
params.put("new_since", date);
}
/**
* Returns a <code>List</code> of all public comments made on the
* {@link Bug} requested from the installation
*
* @return A List of {@link Comment} objects representing user comments
*/
public List<Comment> getComments() {
List<Comment> commentList = new ArrayList<Comment>();
if(hash.containsKey("bugs")) {
/*
* Hideous, but it's the structure of the XML that
* Bugzilla returns. Since it's designed to return comments for
* multiple bugs at a time, there's extra nesting we don't need.
* TODO Allow requests for lists of Bugs
*/
@SuppressWarnings("unchecked")
Map<String, Map<String, Map<Object, Object>[]>> m =
(Map<String, Map<String, Map<Object, Object>[]>>)hash.get("bugs");
for (int id: bugIdList) {
Map<String, Map<Object, Object>[]> weird = m.get(String.valueOf(id));
Object[] comments = weird.get("comments");
if(comments.length == 0) { return commentList; } //Early return to prevent ClassCast
for(Object o : comments) {
@SuppressWarnings("unchecked")
Map<Object, Object> comment = (Map<Object, Object>)o;
// for (Entry<Object, Object> i:comment.entrySet())
// System.out.println("comment object:\t"+i.getKey()+"\t"+i.getValue());
Comment c = new Comment((Integer)comment.get("id"));
c.setText((String)comment.get("text"));
c.setTimestamp((Date)comment.get("time"));
c.setPrivate((Boolean)comment.get("is_private"));
c.setBugId((Integer)comment.get("bug_id"));
c.setCreatorId((Integer)comment.get("creator_id"));
c.setCreator((String)comment.get("creator"));
c.setAuthor((String)comment.get("author"));
commentList.add(c);
}
}
}
return commentList;
}
/**
* {@inheritDoc}
*/
@Override
public void setResultMap(Map<Object, Object> hash) {
this.hash = hash;
}
/**
* {@inheritDoc}
*/
@Override
public Map<Object, Object> getParameterMap() {
return Collections.unmodifiableMap(params);
}
/**
* {@inheritDoc}
*/
@Override
public String getMethodName() {
return METHOD_NAME;
}
}