James Moger
2012-09-10 fabe060d3a435f116128851f828e35c2af5fde67
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
/*
 * Copyright 2012 gitblit.com.
 *
 * 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 com.gitblit.models;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
 
import com.gitblit.utils.ArrayUtils;
import com.gitblit.utils.StringUtils;
import com.gitblit.utils.TimeUtils;
 
/**
 * The Gitblit Issue model, its component classes, and enums.
 * 
 * @author James Moger
 * 
 */
public class IssueModel implements Serializable, Comparable<IssueModel> {
 
    private static final long serialVersionUID = 1L;
 
    public String id;
 
    public Type type;
 
    public Status status;
 
    public Priority priority;
 
    public Date created;
 
    public String summary;
 
    public String description;
 
    public String reporter;
 
    public String owner;
 
    public String milestone;
 
    public List<Change> changes;
 
    public IssueModel() {
        // the first applied change set the date appropriately
        created = new Date(0);
 
        type = Type.Defect;
        status = Status.New;
        priority = Priority.Medium;
 
        changes = new ArrayList<Change>();
    }
 
    public String getStatus() {
        String s = status.toString();
        if (!StringUtils.isEmpty(owner))
            s += " (" + owner + ")";
        return s;
    }
 
    public boolean hasLabel(String label) {
        return getLabels().contains(label);
    }
 
    public List<String> getLabels() {
        List<String> list = new ArrayList<String>();
        String labels = null;
        for (Change change : changes) {
            if (change.hasField(Field.Labels)) {
                labels = change.getString(Field.Labels);
            }
        }
        if (!StringUtils.isEmpty(labels)) {
            list.addAll(StringUtils.getStringsFromValue(labels, " "));
        }
        return list;
    }
 
    public Attachment getAttachment(String name) {
        Attachment attachment = null;
        for (Change change : changes) {
            if (change.hasAttachments()) {
                Attachment a = change.getAttachment(name);
                if (a != null) {
                    attachment = a;
                }
            }
        }
        return attachment;
    }
 
    public List<Attachment> getAttachments() {
        List<Attachment> list = new ArrayList<Attachment>();
        for (Change change : changes) {
            if (change.hasAttachments()) {
                list.addAll(change.attachments);
            }
        }
        return list;
    }
 
    public void applyChange(Change change) {
        if (changes.size() == 0) {
            // first change created the issue
            created = change.created;
        }
        changes.add(change);
 
        if (change.hasFieldChanges()) {
            for (FieldChange fieldChange : change.fieldChanges) {
                switch (fieldChange.field) {
                case Id:
                    id = fieldChange.value.toString();
                    break;
                case Type:
                    type = IssueModel.Type.fromObject(fieldChange.value);
                    break;
                case Status:
                    status = IssueModel.Status.fromObject(fieldChange.value);
                    break;
                case Priority:
                    priority = IssueModel.Priority.fromObject(fieldChange.value);
                    break;
                case Summary:
                    summary = fieldChange.value.toString();
                    break;
                case Description:
                    description = fieldChange.value.toString();
                    break;
                case Reporter:
                    reporter = fieldChange.value.toString();
                    break;
                case Owner:
                    owner = fieldChange.value.toString();
                    break;
                case Milestone:
                    milestone = fieldChange.value.toString();
                    break;
                }
            }
        }
    }
 
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("issue ");
        sb.append(id.substring(0, 8));
        sb.append(" (" + summary + ")\n");
        for (Change change : changes) {
            sb.append(change);
            sb.append('\n');
        }
        return sb.toString();
    }
 
    @Override
    public int compareTo(IssueModel o) {
        return o.created.compareTo(created);
    }
 
    @Override
    public boolean equals(Object o) {
        if (o instanceof IssueModel)
            return id.equals(((IssueModel) o).id);
        return super.equals(o);
    }
 
    @Override
    public int hashCode() {
        return id.hashCode();
    }
 
    public static class Change implements Serializable, Comparable<Change> {
 
        private static final long serialVersionUID = 1L;
 
        public final Date created;
 
        public final String author;
 
        public String id;
 
        public char code;
 
        public Comment comment;
 
        public Set<FieldChange> fieldChanges;
 
        public Set<Attachment> attachments;
 
        public Change(String author) {
            this.created = new Date((System.currentTimeMillis() / 1000) * 1000);
            this.author = author;
            this.id = StringUtils.getSHA1(created.toString() + author);
        }
 
        public boolean hasComment() {
            return comment != null && !comment.deleted;
        }
 
        public void comment(String text) {
            comment = new Comment(text);
            comment.id = StringUtils.getSHA1(created.toString() + author + text);
        }
 
        public boolean hasAttachments() {
            return !ArrayUtils.isEmpty(attachments);
        }
 
        public void addAttachment(Attachment attachment) {
            if (attachments == null) {
                attachments = new LinkedHashSet<Attachment>();
            }
            attachments.add(attachment);
        }
 
        public Attachment getAttachment(String name) {
            for (Attachment attachment : attachments) {
                if (attachment.name.equalsIgnoreCase(name)) {
                    return attachment;
                }
            }
            return null;
        }
 
        public boolean hasField(Field field) {
            return !StringUtils.isEmpty(getString(field));
        }
 
        public boolean hasFieldChanges() {
            return !ArrayUtils.isEmpty(fieldChanges);
        }
 
        public Object getField(Field field) {
            if (fieldChanges != null) {
                for (FieldChange fieldChange : fieldChanges) {
                    if (fieldChange.field == field) {
                        return fieldChange.value;
                    }
                }
            }
            return null;
        }
 
        public void setField(Field field, Object value) {
            FieldChange fieldChange = new FieldChange(field, value);
            if (fieldChanges == null) {
                fieldChanges = new LinkedHashSet<FieldChange>();
            }
            fieldChanges.add(fieldChange);
        }
 
        public String getString(Field field) {
            Object value = getField(field);
            if (value == null) {
                return null;
            }
            return value.toString();
        }
 
        @Override
        public int compareTo(Change c) {
            return created.compareTo(c.created);
        }
 
        @Override
        public int hashCode() {
            return id.hashCode();
        }
 
        @Override
        public boolean equals(Object o) {
            if (o instanceof Change) {
                return id.equals(((Change) o).id);
            }
            return false;
        }
 
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();            
            sb.append(new TimeUtils().timeAgo(created));
            switch (code) {
            case '+':
                sb.append(" created by ");
                break;
            default:
                if (hasComment()) {
                    sb.append(" commented on by ");
                } else {
                    sb.append(" changed by ");
                }
            }
            sb.append(author).append(" - ");
            if (hasComment()) {
                if (comment.deleted) {
                    sb.append("(deleted) ");
                }
                sb.append(comment.text).append(" ");
            }
            if (hasFieldChanges()) {
                switch (code) {
                case '+':
                    break;
                default:
                    for (FieldChange fieldChange : fieldChanges) {
                        sb.append("\n  ");
                        sb.append(fieldChange);
                    }
                    break;
                }
            }
            return sb.toString();
        }
    }
 
    public static class Comment implements Serializable {
 
        private static final long serialVersionUID = 1L;
 
        public String text;
 
        public String id;
 
        public boolean deleted;
 
        Comment(String text) {
            this.text = text;
        }
 
        @Override
        public String toString() {
            return text;
        }
    }
 
    public static class FieldChange implements Serializable {
 
        private static final long serialVersionUID = 1L;
 
        public final Field field;
 
        public final Object value;
 
        FieldChange(Field field, Object value) {
            this.field = field;
            this.value = value;
        }
 
        @Override
        public int hashCode() {
            return field.hashCode();
        }
 
        @Override
        public boolean equals(Object o) {
            if (o instanceof FieldChange) {
                return field.equals(((FieldChange) o).field);
            }
            return false;
        }
 
        @Override
        public String toString() {
            return field + ": " + value;
        }
    }
 
    public static class Attachment implements Serializable {
 
        private static final long serialVersionUID = 1L;
 
        public final String name;
        public String id;
        public long size;
        public byte[] content;
        public boolean deleted;
 
        public Attachment(String name) {
            this.name = name;
        }
 
        @Override
        public int hashCode() {
            return name.hashCode();
        }
 
        @Override
        public boolean equals(Object o) {
            if (o instanceof Attachment) {
                return name.equalsIgnoreCase(((Attachment) o).name);
            }
            return false;
        }
 
        @Override
        public String toString() {
            return name;
        }
    }
 
    public static enum Field {
        Id, Summary, Description, Reporter, Owner, Type, Status, Priority, Milestone, Component, Labels;
    }
 
    public static enum Type {
        Defect, Enhancement, Task, Review, Other;
 
        public static Type fromObject(Object o) {
            if (o instanceof Type) {
                // cast and return
                return (Type) o;
            } else if (o instanceof String) {
                // find by name
                for (Type type : values()) {
                    String str = o.toString();
                    if (type.toString().equalsIgnoreCase(str)) {
                        return type;
                    }
                }
            } else if (o instanceof Number) {
                // by ordinal
                int id = ((Number) o).intValue();
                if (id >= 0 && id < values().length) {
                    return values()[id];
                }
            }
            return null;
        }
    }
 
    public static enum Priority {
        Low, Medium, High, Critical;
 
        public static Priority fromObject(Object o) {
            if (o instanceof Priority) {
                // cast and return
                return (Priority) o;
            } else if (o instanceof String) {
                // find by name
                for (Priority priority : values()) {
                    String str = o.toString();
                    if (priority.toString().equalsIgnoreCase(str)) {
                        return priority;
                    }
                }
            } else if (o instanceof Number) {
                // by ordinal
                int id = ((Number) o).intValue();
                if (id >= 0 && id < values().length) {
                    return values()[id];
                }
            }
            return null;
        }
    }
 
    public static enum Status {
        New, Accepted, Started, Review, Queued, Testing, Done, Fixed, WontFix, Duplicate, Invalid;
 
        public static Status fromObject(Object o) {
            if (o instanceof Status) {
                // cast and return
                return (Status) o;
            } else if (o instanceof String) {
                // find by name
                for (Status status : values()) {
                    String str = o.toString();
                    if (status.toString().equalsIgnoreCase(str)) {
                        return status;
                    }
                }
            } else if (o instanceof Number) {
                // by ordinal
                int id = ((Number) o).intValue();
                if (id >= 0 && id < values().length) {
                    return values()[id];
                }
            }
            return null;
        }
 
        public boolean atLeast(Status status) {
            return ordinal() >= status.ordinal();
        }
 
        public boolean exceeds(Status status) {
            return ordinal() > status.ordinal();
        }
 
        public boolean isClosed() {
            return ordinal() >= Done.ordinal();
        }
 
        public Status next() {
            switch (this) {
            case New:
                return Started;
            case Accepted:
                return Started;
            case Started:
                return Testing;
            case Review:
                return Testing;
            case Queued:
                return Testing;
            case Testing:
                return Done;
            }
            return Accepted;
        }
    }
}