-
Notifications
You must be signed in to change notification settings - Fork 62
/
Change.java
568 lines (506 loc) · 15.7 KB
/
Change.java
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
* The MIT License
*
* Copyright 2010 Sony Mobile Communications Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.sonymobile.tools.gerrit.gerritevents.dto.attr;
import com.sonymobile.tools.gerrit.gerritevents.GerritQueryHandler;
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritChangeStatus;
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritJsonDTO;
import com.sonymobile.tools.gerrit.gerritevents.dto.rest.Topic;
import com.sonymobile.tools.gerrit.gerritevents.helpers.FileHelper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import static com.sonymobile.tools.gerrit.gerritevents.GerritJsonEventFactory.getBoolean;
import static com.sonymobile.tools.gerrit.gerritevents.GerritJsonEventFactory.getString;
import static com.sonymobile.tools.gerrit.gerritevents.GerritJsonEventFactory.getDate;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.BRANCH;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.COMMENTS;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.COMMIT_MESSAGE;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.TOPIC;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.HASHTAGS;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.ID;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.NUMBER;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.OWNER;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.PROJECT;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.SUBJECT;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.URL;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.CREATED_ON;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.LAST_UPDATED;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.STATUS;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.WIP;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.PRIVATE;
/**
* Represents a Gerrit JSON Change DTO.
* The Gerrit change the event is related to.
* @author Robert Sandell <robert.sandell@sonyericsson.com>
*/
public class Change implements GerritJsonDTO {
/**
* Project path in Gerrit.
*/
private String project;
/**
* Branch name within project.
*/
private String branch;
/**
* Topic.
*/
private Topic topicObject;
/**
* Topic name.
*/
private transient String topic;
/**
* Hashtags.
*/
private List<String> hashtags;
/**
* Change identifier.
*/
private String id;
/**
* Change number (deprecated).
*/
private String number;
/**
* Description of change.
*/
private String subject;
/**
* The change's full commit message.
*/
private String commitMessage;
/**
* Owner in account attribute.
*/
private Account owner;
/**
* Canonical URL to reach this change.
*/
private String url;
/**
* The Date when this change was created.
*/
private Date createdOn;
/**
* The Date when this change was last updated.
*/
private Date lastUpdated;
private List<Comment> comments;
/**
* The changed files in this patchset.
*/
private List<String> files;
/**
* The change status.
*/
private GerritChangeStatus status;
/**
* Converts old serialized data to newer construct.
*
* @return itself
*/
@SuppressWarnings("unused")
private Object readResolve() {
if (StringUtils.isNotEmpty(topic)) {
topicObject = new Topic(topic);
topic = null;
}
if (topicObject != null && StringUtils.isEmpty(topicObject.getName())) {
topicObject = null;
}
return this;
}
/**
* Is this change a Work in progress.
*/
private boolean wip;
/**
* Is this change private.
*/
// CS IGNORE MemberName FOR NEXT 1 LINES. REASON: Private is a reserved keyword.
private boolean _private;
/**
* Default constructor.
*/
public Change() {
}
/**
* Constructor that fills with data directly.
* @param json the JSON Object with corresponding data.
* @see #fromJson(net.sf.json.JSONObject)
*/
public Change(JSONObject json) {
this.fromJson(json);
}
@Override
public void fromJson(JSONObject json) {
project = getString(json, PROJECT);
branch = getString(json, BRANCH);
id = getString(json, ID);
number = getString(json, NUMBER);
subject = getString(json, SUBJECT);
createdOn = getDate(json, CREATED_ON);
lastUpdated = getDate(json, LAST_UPDATED);
if (json.containsKey(OWNER)) {
owner = new Account(json.getJSONObject(OWNER));
}
if (json.containsKey(COMMENTS)) {
comments = new ArrayList<Comment>();
JSONArray eventApprovals = json.getJSONArray(COMMENTS);
for (int i = 0; i < eventApprovals.size(); i++) {
comments.add(new Comment(eventApprovals.getJSONObject(i)));
}
}
if (json.containsKey(COMMIT_MESSAGE)) {
commitMessage = getString(json, COMMIT_MESSAGE);
}
if (json.containsKey(TOPIC)) {
String topicName = getString(json, TOPIC);
if (StringUtils.isNotEmpty(topicName)) {
topicObject = new Topic(topicName);
}
}
if (json.containsKey(HASHTAGS)) {
JSONArray tags = json.getJSONArray(HASHTAGS);
hashtags = new ArrayList<String>(tags.size());
for (int i = 0; i < tags.size(); i++) {
hashtags.add(tags.getString(i));
}
} else {
hashtags = Collections.emptyList();
}
url = getString(json, URL);
status = GerritChangeStatus.fromString(getString(json, STATUS));
wip = getBoolean(json, WIP, false);
_private = getBoolean(json, PRIVATE, false);
}
/**
* Branch name within project.
* @return the branch.
*/
public String getBranch() {
return branch;
}
/**
* Branch name within project.
* @param branch the branch.
*/
public void setBranch(String branch) {
this.branch = branch;
}
/**
* Comments (only filled up if getComments equals true during query).
* @return the comments list.
*/
public List<Comment> getComments() {
return comments;
}
/**
* Change status.
* @return the change status.
*/
public GerritChangeStatus getStatus() {
return status;
}
/**
* Sets the change status.
* @param status the status.
*/
public void setStatus(GerritChangeStatus status) {
this.status = status;
}
/**
* The topic info related to this change. Can be null if there is no topic.
* @return the topic.
*/
public Topic getTopicObject() {
return topicObject;
}
/**
* Shortcut for {@code getTopicObject() != null ? getTopicObject().getName() : null}.
* @return the topic name if exists. null otherwise.
*/
public String getTopic() {
if (topicObject == null) {
return null;
}
return topicObject.getName();
}
/**
* Sets the topic info related to this change.
* @param topicName the topic name.
*/
public void setTopic(String topicName) {
topicObject = new Topic(topicName);
}
/**
* Sets the topic info related to this change.
* @param topicObject the topic.
*/
public void setTopicObject(Topic topicObject) {
this.topicObject = topicObject;
}
/**
* Change hashtags.
* @return the hashtags.
*/
public List<String> getHashtags() {
return hashtags;
}
/**
* Sets the hashtags to this change.
* @param hashtags the hashtags.
*/
public void setHashtags(List<String> hashtags) {
this.hashtags = hashtags;
}
/**
* Change identifier.
* @return the identifier.
*/
public String getId() {
return id;
}
/**
* Change identifier.
* @param id the identifier.
*/
public void setId(String id) {
this.id = id;
}
/**
* Change number (deprecated).
* @return the change number.
* @deprecated because the Gerrit documentation says so.
*/
public String getNumber() {
return number;
}
/**
* Change number (deprecated).
* @param number the change number.
* @deprecated because the Gerrit documentation says so.
*/
public void setNumber(String number) {
this.number = number;
}
/**
* Owner in account attribute.
* @return the owner.
*/
public Account getOwner() {
return owner;
}
/**
* Owner in account attribute.
* @param owner the owner.
*/
public void setOwner(Account owner) {
this.owner = owner;
}
/**
* Project path in Gerrit.
* @return the project.
*/
public String getProject() {
return project;
}
/**
* Project path in Gerrit.
* @param project the project.
*/
public void setProject(String project) {
this.project = project;
}
/**
* Description of change.
* @return the description.
*/
public String getSubject() {
return subject;
}
/**
* Description of change.
* @param subject the description.
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* Full commit message.
* @return the commit message.
*/
public String getCommitMessage() {
return commitMessage;
}
/**
* Full commit message.
* @param commitMessage the commit message.
*/
public void setCommitMessage(String commitMessage) {
this.commitMessage = commitMessage;
}
/**
* Canonical URL to reach this change.
* @return the URL.
*/
public String getUrl() {
return url;
}
/**
* Canonical URL to reach this change.
* @param url the URL.
*/
public void setUrl(String url) {
this.url = url;
}
/**
* The Date this change was created on.
* @return the Date.
*/
public Date getCreatedOn() {
return createdOn;
}
/**
* The Date this change was created on.
* @param date the Date.
*/
public void setCreatedOn(Date date) {
this.createdOn = date;
}
/**
* The Date this change was last updated.
* @return the Date.
*/
public Date getLastUpdated() {
return lastUpdated;
}
/**
* The Date this change was last updated.
* @param date the Date.
*/
public void setLastUpdated(Date date) {
this.lastUpdated = date;
}
/**
* Is this change a work in progress.
* @return change is in WIP state.
*/
public boolean isWip() { return wip; }
/**
* Is this change a work in progress.
* @param wip change is in WIP state.
*/
public void setWip(boolean wip) { this.wip = wip; }
/**
* Is this change private.
* @return change is in private state.
*/
public boolean isPrivate() { return _private; }
/**
* Is this change private.
* @param _private change is private.
*/
// CS IGNORE HiddenField FOR NEXT 2 LINES. REASON: Private is a reserved keyword.
// CS IGNORE ParameterName FOR NEXT 1 LINES. REASON: Private is a reserved keyword.
public void setPrivate(boolean _private) { this._private = _private; }
@Override
public boolean equals(Object obj) {
//CS IGNORE MagicNumber FOR NEXT 18 LINES. REASON: Autogenerated Code.
//CS IGNORE AvoidInlineConditionals FOR NEXT 18 LINES. REASON: Autogenerated Code.
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Change other = (Change)obj;
if ((this.project == null) ? (other.project != null) : !this.project.equals(other.project)) {
return false;
}
if ((this.branch == null) ? (other.branch != null) : !this.branch.equals(other.branch)) {
return false;
}
if ((this.number == null) ? (other.number != null) : !this.number.equals(other.number)) {
return false;
}
if ((this.id == null) ? (other.id != null) : !this.id.equals(other.id)) {
return false;
}
return true;
}
@Override
public int hashCode() {
//CS IGNORE MagicNumber FOR NEXT 6 LINES. REASON: Autogenerated Code.
//CS IGNORE AvoidInlineConditionals FOR NEXT 6 LINES. REASON: Autogenerated Code.
int hash = 7;
hash = 29 * hash + (this.project != null ? this.project.hashCode() : 0);
hash = 29 * hash + (this.branch != null ? this.branch.hashCode() : 0);
hash = 29 * hash + (this.number != null ? this.number.hashCode() : 0);
hash = 29 * hash + (this.id != null ? this.id.hashCode() : 0);
return hash;
}
@Override
public String toString() {
return "Change-Id for #" + getNumber() + ": " + getId();
}
/**
* Returns change's info in string format.
* @param preText the text before change info.
* @return change info.
*/
public String getChangeInfo(String preText) {
StringBuilder s = new StringBuilder();
s.append(preText + "\n");
s.append("Subject: " + getSubject() + "\n");
s.append("Project: " + getProject() + " " + getBranch() + " " + getId() + "\n");
s.append("Link: " + getUrl() + "\n");
return s.toString();
}
/**
* Sets the list of the files included in this patch set.
* @param files a list of files.
*/
public void setFiles(List<String> files) {
this.files = files;
}
/**
* Queries gerrit for the files included in this patch set.
*
* @param gerritQueryHandler the query handler, responsible for the queries to gerrit.
* @return a list of files that are part of this patch set.
*
*/
public List<String> getFiles(GerritQueryHandler gerritQueryHandler) {
if (files == null) {
files = FileHelper.getFilesByChange(gerritQueryHandler, id);
}
return files;
}
}