All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.rcarz.jiraclient.greenhopper.Backlog Maven / Gradle / Ivy

There is a newer version: 0.5
Show newest version
/**
 * jira-client - a simple JIRA REST client
 * Copyright (c) 2013 Bob Carroll ([email protected])
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package net.rcarz.jiraclient.greenhopper;

import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.JiraException;
import net.rcarz.jiraclient.RestClient;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * GreenHopper backlog data.
 */
public class Backlog {

    private RestClient restclient = null;
    private List issues = null;
    private int rankCustomFieldId = 0;
    private List openSprints = null;
    private List projects = null;
    private List markers = null;
    private List epics = null;
    private boolean canEditEpics = false;
    private boolean canManageSprints = false;
    private boolean maxIssuesExceeded = false;
    private int queryResultLimit = 0;
    private Map> versionsPerProject = null;

    /**
     * Creates the backlog from a JSON payload.
     *
     * @param restclient REST client instance
     * @param json JSON payload
     */
    protected Backlog(RestClient restclient, JSONObject json) {
        this.restclient = restclient;

        if (json != null)
            deserialise(json);
    }

    private void deserialise(JSONObject json) {
        Map map = json;

        issues = GreenHopperField.getResourceArray(
            SprintIssue.class,
            map.get("issues"),
            restclient);
        rankCustomFieldId = Field.getInteger(map.get("rankCustomFieldId"));
        openSprints = GreenHopperField.getResourceArray(
            Sprint.class,
            map.get("openSprints"),
            restclient);
        projects = GreenHopperField.getResourceArray(
            RapidViewProject.class,
            map.get("projects"),
            restclient);
        markers = GreenHopperField.getResourceArray(
            Marker.class,
            map.get("markers"),
            restclient);
        canManageSprints = Field.getBoolean(map.get("canManageSprints"));
        maxIssuesExceeded = Field.getBoolean(map.get("maxIssuesExceeded"));
        queryResultLimit = Field.getInteger(map.get("queryResultLimit"));

        if (map.containsKey("epicData") && map.get("epicData") instanceof JSONObject) {
            Map epicData = (Map)map.get("epicData");

            epics = GreenHopperField.getResourceArray(Epic.class, epicData.get("epics"), restclient);
            canEditEpics = Field.getBoolean(epicData.get("canEditEpics"));
        }

        if (map.containsKey("versionData") && map.get("versionData") instanceof JSONObject) {
            Map verData = (JSONObject)map.get("versionData");

            if (verData.containsKey("versionsPerProject") &&
                verData.get("versionsPerProject") instanceof JSONObject) {

                Map verMap = (Map)verData.get("versionsPerProject");
                versionsPerProject = new HashMap>();

                for (Map.Entry kvp : 
                     (Iterable>)verMap.entrySet()) {

                    if (!(kvp.getValue() instanceof JSONArray))
                        continue;

                    List versions = new ArrayList();

                    for (Object item : (JSONArray)kvp.getValue()) {
                        if (!(item instanceof JSONObject))
                            continue;

                        RapidViewVersion ver = new RapidViewVersion(restclient, (JSONObject)item);
                        versions.add(ver);
                    }

                    versionsPerProject.put(kvp.getKey(), versions);
                }
            }
        }
    }

    /**
     * Retrieves the backlog data for the given rapid view.
     *
     * @param restclient REST client instance
     * @param rv Rapid View instance
     *
     * @return the backlog
     *
     * @throws JiraException when the retrieval fails
     */
    public static Backlog get(RestClient restclient, RapidView rv)
        throws JiraException {

        final int rvId = rv.getId();
        JSON result = null;

        try {
            URI reporturi = restclient.buildURI(
                GreenHopperResource.RESOURCE_URI + "xboard/plan/backlog/data",
                new HashMap() {{
                    put("rapidViewId", Integer.toString(rvId));
                }});
            result = restclient.get(reporturi);
        } catch (Exception ex) {
            throw new JiraException("Failed to retrieve backlog data", ex);
        }

        if (!(result instanceof JSONObject))
            throw new JiraException("JSON payload is malformed");

        return new Backlog(restclient, (JSONObject)result);
    }

    public List getIssues() {
        return issues;
    }

    public int getRankCustomFieldId() {
        return rankCustomFieldId;
    }

    public List getOpenSprints() {
        return openSprints;
    }

    public List getProjects() {
        return projects;
    }

    public List getMarkers() {
        return markers;
    }

    public List getEpics() {
        return epics;
    }

    public boolean canEditEpics() {
        return canEditEpics;
    }

    public boolean canManageSprints() {
        return canManageSprints;
    }

    public boolean maxIssuesExceeded() {
        return maxIssuesExceeded;
    }

    public int queryResultLimit() {
        return queryResultLimit;
    }

    public Map> getVersionsPerProject() {
        return versionsPerProject;
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy