IssueWrapper.java 2.11 KB
package pl.itcrowd.youtrack.api;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pl.itcrowd.youtrack.api.defaults.Fields;
import pl.itcrowd.youtrack.api.rest.Comment;
import pl.itcrowd.youtrack.api.rest.Field;
import pl.itcrowd.youtrack.api.rest.Issue;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IssueWrapper implements Serializable {
// ------------------------------ FIELDS ------------------------------

    private static final Log log = LogFactory.getLog(IssueWrapper.class);

    private List<Comment> comments;

    private Map<String, Field> fieldMap;

    private Issue issue;

// --------------------------- CONSTRUCTORS ---------------------------

    public IssueWrapper(Issue issue)
    {
        this.issue = issue;
        fieldMap = new HashMap<String, Field>();
        comments = new ArrayList<Comment>();
        for (Object o : issue.getFieldOrComment()) {
            if (o instanceof Field) {
                Field field = (Field) o;
                fieldMap.put(field.getName().toLowerCase(), field);
            } else if (o instanceof Comment) {
                comments.add((Comment) o);
            } else {
                log.warn("Object " + o + " is not Field nor Coment");
            }
        }
    }

// -------------------------- OTHER METHODS --------------------------

    public List<Comment> getComments()
    {
        return Collections.unmodifiableList(comments);
    }

    public Field getField(String field)
    {
        return fieldMap.get(field);
    }

    public String getFieldValue(String fieldName)
    {
        Field field = getField(fieldName.toLowerCase());
        if (field == null) {
            return null;
        }
        List<Field.Value> values = field.getValues();
        return values.isEmpty() ? null : values.get(0).getContent();
    }

    public String getFieldValue(Fields field)
    {
        return getFieldValue(field.name());
    }

    public String getId()
    {
        return issue.getId();
    }
}