From 480b7a7a5778b64d139b8a7617d5f17bf0e6c8a1 Mon Sep 17 00:00:00 2001 From: Alfred Sawatzky Date: Mon, 1 Feb 2016 15:25:11 -0700 Subject: [PATCH] added support for creating a new Version for a project. --- .../java/net/rcarz/jiraclient/Version.java | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/src/main/java/net/rcarz/jiraclient/Version.java b/src/main/java/net/rcarz/jiraclient/Version.java index da7b247..d997c03 100644 --- a/src/main/java/net/rcarz/jiraclient/Version.java +++ b/src/main/java/net/rcarz/jiraclient/Version.java @@ -22,6 +22,9 @@ package net.rcarz.jiraclient; import net.sf.json.JSON; import net.sf.json.JSONObject; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.Map; /** @@ -29,6 +32,109 @@ import java.util.Map; */ public class Version extends Resource { + /** + * Used to chain fields to a create action. + */ + public static final class FluentCreate { + /** + * The Jira rest client. + */ + RestClient restclient = null; + + /** + * The JSON request that will be built incrementally as fluent methods + * are invoked. + */ + JSONObject req = new JSONObject(); + + /** + * Creates a new fluent. + * @param restclient the REST client + * @param project the project key + */ + private FluentCreate(RestClient restclient, String project) { + this.restclient = restclient; + req.put("project", project); + } + + /** + * Sets the name of the version. + * @param name the name + * @return this + */ + public FluentCreate name(String name) { + req.put("name", name); + return this; + } + + /** + * Sets the description of the version. + * @param description the description + * @return this + */ + public FluentCreate description(String description) { + req.put("description", description); + return this; + } + + /** + * Sets the archived status of the version. + * @param isArchived archived status + * @return this + */ + public FluentCreate archived(boolean isArchived) { + req.put("archived", isArchived); + return this; + } + + /** + * Sets the released status of the version. + * @param isReleased released status + * @return this + */ + public FluentCreate released(boolean isReleased) { + req.put("released", isReleased); + return this; + } + + /** + * Sets the release date of the version. + * @param releaseDate release Date + * @return this + */ + public FluentCreate releaseDate(Date releaseDate) { + DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); + req.put("releaseDate", df.format(releaseDate)); + return this; + } + + + + + /** + * Executes the create action. + * @return the created Version + * + * @throws JiraException when the create fails + */ + public Version execute() throws JiraException { + JSON result = null; + + try { + result = restclient.post(getRestUri(null), req); + } catch (Exception ex) { + throw new JiraException("Failed to create version", ex); + } + + if (!(result instanceof JSONObject) || !((JSONObject) result).containsKey("id") + || !(((JSONObject) result).get("id") instanceof String)) { + throw new JiraException("Unexpected result on create version"); + } + + return new Version(restclient, (JSONObject) result); + } + } + private String name = null; private boolean archived = false; private boolean released = false; @@ -156,5 +262,22 @@ public class Version extends Resource { return description; } + + private static String getRestUri(String id) { + return getBaseUri() + "version/" + (id != null ? id : ""); + } + + /** + * Creates a new JIRA Version. + * + * @param restclient REST client instance + * @param project Key of the project to create the version in + * + * @return a fluent create instance + */ + public static FluentCreate create(RestClient restclient, String project) { + FluentCreate fc = new FluentCreate(restclient, project); + return fc; + } }