Blame view

src/main/java/pl/it_crowd/youtrack/api/IssueWrapper.java 2.12 KB
bernard authored
1
package pl.it_crowd.youtrack.api;
bernard authored
2 3 4

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
bernard authored
5 6 7 8
import pl.it_crowd.youtrack.api.defaults.Fields;
import pl.it_crowd.youtrack.api.rest.Comment;
import pl.it_crowd.youtrack.api.rest.Field;
import pl.it_crowd.youtrack.api.rest.Issue;
bernard authored
9 10 11

import java.io.Serializable;
import java.util.ArrayList;
bernard authored
12
import java.util.Collections;
bernard authored
13 14 15 16
import java.util.HashMap;
import java.util.List;
import java.util.Map;
bernard authored
17
public class IssueWrapper implements Serializable {
bernard authored
18 19
// ------------------------------ FIELDS ------------------------------
bernard authored
20
    private static final Log log = LogFactory.getLog(IssueWrapper.class);
bernard authored
21
bernard authored
22
    private List<Comment> comments;
bernard authored
23
bernard authored
24
    private Map<String, Field> fieldMap;
bernard authored
25
bernard authored
26
    private Issue issue;
bernard authored
27 28 29

// --------------------------- CONSTRUCTORS ---------------------------
bernard authored
30
    public IssueWrapper(Issue issue)
bernard authored
31
    {
bernard authored
32
        this.issue = issue;
bernard authored
33 34
        fieldMap = new HashMap<String, Field>();
        comments = new ArrayList<Comment>();
bernard authored
35
        for (Object o : issue.getFieldOrComment()) {
bernard authored
36 37
            if (o instanceof Field) {
                Field field = (Field) o;
bernard authored
38
                fieldMap.put(field.getName().toLowerCase(), field);
bernard authored
39 40
            } else if (o instanceof Comment) {
                comments.add((Comment) o);
bernard authored
41 42 43 44 45 46 47 48
            } else {
                log.warn("Object " + o + " is not Field nor Coment");
            }
        }
    }

// -------------------------- OTHER METHODS --------------------------
bernard authored
49 50 51 52 53
    public List<Comment> getComments()
    {
        return Collections.unmodifiableList(comments);
    }
bernard authored
54
    public Field getField(String field)
bernard authored
55
    {
bernard authored
56 57 58
        return fieldMap.get(field);
    }
bernard authored
59 60
    public String getFieldValue(String fieldName)
    {
bernard authored
61
        Field field = getField(fieldName.toLowerCase());
bernard authored
62 63 64
        if (field == null) {
            return null;
        }
bernard authored
65
        List<Field.Value> values = field.getValues();
bernard authored
66 67 68
        return values.isEmpty() ? null : values.get(0).getContent();
    }
bernard authored
69
    public String getFieldValue(Fields field)
bernard authored
70
    {
bernard authored
71
        return getFieldValue(field.name());
bernard authored
72
    }
bernard authored
73
bernard authored
74 75 76 77
    public String getId()
    {
        return issue.getId();
    }
bernard authored
78
}