net.rcarz.jiraclient.Project Maven / Gradle / Ivy
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll ([email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient;
import java.util.List;
import java.util.Map;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Represents a JIRA project.
*/
public final class Project extends Resource {
private Map avatarUrls = null;
private String key = null;
private String name = null;
private String description = null;
private User lead = null;
private String assigneeType = null;
private List components = null;
private List issueTypes = null;
private List versions = null;
private Map roles = null;
/**
* Creates a project from a JSON payload.
*
* @param restclient REST client instance
* @param json JSON payload
*/
protected Project(RestClient restclient, JSONObject json) {
super(restclient);
if (json != null)
deserialise(json);
}
private void deserialise(JSONObject json) {
Map map = json;
self = Field.getString(map.get("self"));
id = Field.getString(map.get("id"));
avatarUrls = Field.getMap(String.class, String.class, map.get("avatarUrls"));
key = Field.getString(map.get("key"));
name = Field.getString(map.get("name"));
description = Field.getString(map.get("description"));
lead = Field.getResource(User.class, map.get("lead"), restclient);
assigneeType = Field.getString(map.get("assigneeType"));
components = Field.getResourceArray(Component.class, map.get("components"), restclient);
issueTypes = Field.getResourceArray(
IssueType.class,
map.containsKey("issueTypes") ? map.get("issueTypes") : map.get("issuetypes"),
restclient);
versions = Field.getResourceArray(Version.class, map.get("versions"), restclient);
roles = Field.getMap(String.class, String.class, map.get("roles"));
}
/**
* Retrieves the given project record.
*
* @param restclient REST client instance
* @param key Project key
*
* @return a project instance
*
* @throws JiraException when the retrieval fails
*/
public static Project get(RestClient restclient, String key)
throws JiraException {
JSON result = null;
try {
result = restclient.get(RESOURCE_URI + "project/" + key);
} catch (Exception ex) {
throw new JiraException("Failed to retrieve project " + key, ex);
}
if (!(result instanceof JSONObject))
throw new JiraException("JSON payload is malformed");
return new Project(restclient, (JSONObject)result);
}
/**
* Retrieves all project records visible to the session user.
*
* @param restclient REST client instance
*
* @return a list of projects
*
* @throws JiraException when the retrieval fails
*/
public static List getAll(RestClient restclient) throws JiraException {
JSON result = null;
try {
result = restclient.get(RESOURCE_URI + "project");
} catch (Exception ex) {
throw new JiraException("Failed to retrieve projects", ex);
}
if (!(result instanceof JSONArray))
throw new JiraException("JSON payload is malformed");
return Field.getResourceArray(Project.class, result, restclient);
}
@Override
public String toString() {
return getName();
}
public Map getAvatarUrls() {
return avatarUrls;
}
public String getKey() {
return key;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public User getLead() {
return lead;
}
public String getAssigneeType() {
return assigneeType;
}
public List getComponents() {
return components;
}
public List getIssueTypes() {
return issueTypes;
}
public List getVersions() {
return versions;
}
public Map getRoles() {
return roles;
}
}