1
0
Fork 0

implemented field update operations

master
Bob Carroll 2013-09-26 14:17:04 -07:00
parent 8d3e14106a
commit 1583879d63
3 changed files with 92 additions and 3 deletions

View File

@ -107,6 +107,12 @@ public class Example {
}})
.execute();
/* You can also update values with field operations. */
issue.update()
.fieldAdd(Field.LABELS, "baz")
.fieldRemove(Field.LABELS, "foo")
.execute();
/* Print the summary. We have to refresh first to pickup the new value. */
issue.refresh();
System.out.println("New Summary: " + issue.getSummary());
@ -145,6 +151,7 @@ public class Example {
.field("customfield_5678", new HashMap() {{
put("value", "foo");
put("value", "bar");
put("id", "1234"); /* you can also update using the value ID */
}})
.execute();

View File

@ -50,6 +50,25 @@ public final class Field {
public int customId;
}
/**
* Field update operation.
*/
public static final class Operation {
public String name;
public Object value;
/**
* Initialises a new update operation.
*
* @param name Operation name
* @param value Field value
*/
public Operation(String name, Object value) {
this.name = name;
this.value = value;
}
}
public static final String ASSIGNEE = "assignee";
public static final String ATTACHMENT = "attachment";
public static final String COMMENT = "comment";
@ -442,7 +461,25 @@ public final class Field {
if (!(value instanceof Iterable))
throw new JiraException("Field expects an Iterable value");
return toArray((Iterable)value, m.items);
boolean isOper = false;
for (Object v : (Iterable)value) {
isOper = v instanceof Operation;
break;
}
if (isOper) {
List results = new ArrayList();
for (Object v : (Iterable)value) {
Operation oper = (Operation)v;
JSONObject json = new JSONObject();
json.put(oper.name, oper.value.toString());
results.add(json.toString());
}
return toArray(results, m.items);
} else
return toArray((Iterable)value, m.items);
} else if (m.type.equals("date")) {
Date d = toDate(value);

View File

@ -21,6 +21,7 @@ package net.rcarz.jiraclient;
import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@ -103,6 +104,7 @@ public class Issue extends Resource {
public final class FluentUpdate {
Map<String, Object> fields = new HashMap<String, Object>();
Map<String, List> fieldOpers = new HashMap<String, List>();
JSONObject editmeta = null;
private FluentUpdate(JSONObject editmeta) {
@ -116,8 +118,9 @@ public class Issue extends Resource {
*/
public void execute() throws JiraException {
JSONObject fieldmap = new JSONObject();
JSONObject updatemap = new JSONObject();
if (fields.size() == 0)
if (fields.size() == 0 && fieldOpers.size() == 0)
throw new JiraException("No fields were given for update");
for (Map.Entry<String, Object> ent : fields.entrySet()) {
@ -125,8 +128,18 @@ public class Issue extends Resource {
fieldmap.put(ent.getKey(), newval);
}
for (Map.Entry<String, List> ent : fieldOpers.entrySet()) {
Object newval = Field.toJson(ent.getKey(), ent.getValue(), editmeta);
updatemap.put(ent.getKey(), newval);
}
JSONObject req = new JSONObject();
req.put("fields", fieldmap);
if (fieldmap.size() > 0)
req.put("fields", fieldmap);
if (updatemap.size() > 0)
req.put("update", updatemap);
try {
restclient.put(getRestUri(key), req);
@ -147,6 +160,38 @@ public class Issue extends Resource {
fields.put(name, value);
return this;
}
private FluentUpdate fieldOperation(String oper, String name, Object value) {
if (!fieldOpers.containsKey(name))
fieldOpers.put(name, new ArrayList());
fieldOpers.get(name).add(new Field.Operation(oper, value));
return this;
}
/**
* Adds a field value to the existing value set.
*
* @param name Name of the field
* @param value Field value to append
*
* @return the current fluent update instance
*/
public FluentUpdate fieldAdd(String name, Object value) {
return fieldOperation("add", name, value);
}
/**
* Removes a field value from the existing value set.
*
* @param name Name of the field
* @param value Field value to remove
*
* @return the current fluent update instance
*/
public FluentUpdate fieldRemove(String name, Object value) {
return fieldOperation("remove", name, value);
}
}
/**