forked from nmusaelian-rally/rally-java-rest-apps
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathClearCustomUserTextField.java
More file actions
78 lines (64 loc) · 3.16 KB
/
ClearCustomUserTextField.java
File metadata and controls
78 lines (64 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//This code toggles Custom Text field value on a User from empty to current milliseconds, and from a non-empty value to empty
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.request.UpdateRequest;
import com.rallydev.rest.response.UpdateResponse;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.util.QueryFilter;
import java.util.Date;
public class ClearCustomUserTextField{
public static void main(String[] args) throws URISyntaxException, IOException {
String host = "https://rally1.rallydev.com";
String apiKey = "_abc123"; //use your ApiKey
String applicationName = "ClearCustomUserTextField";
RallyRestApi restApi = new RallyRestApi(new URI(host),apiKey);
restApi.setApplicationName(applicationName);
try {
QueryRequest userRequest = new QueryRequest("User");
userRequest.setFetch(new Fetch("UserName", "DisplayName", "c_Comments"));
userRequest.setQueryFilter(new QueryFilter("UserName", "=", "nick@denver.com"));
QueryResponse userQueryResponse = restApi.query(userRequest);
JsonArray userQueryResults = userQueryResponse.getResults();
JsonElement userQueryElement = userQueryResults.get(0);
JsonObject userObject = userQueryElement.getAsJsonObject();
String userRef = userObject.get("_ref").getAsString();
System.out.println(userObject.get("UserName") + " Comments field value: " + userObject.get("c_Comments"));
if(userObject.get("c_Comments").isJsonNull()){
String newComment = new Date().getTime()+""; //converting long to string
System.out.println("Comments field is empty. Updating to: " + newComment);
update(restApi, userRef, newComment);
}
else{
System.out.println("not empty");
System.out.println("Comments field is not empty. Clearing the value...");
update(restApi, userRef, "");
}
} finally {
//Release all resources
restApi.close();
}
}
private static void update(RallyRestApi restApi, String userRef, String comment ) throws URISyntaxException,IOException {
JsonObject userUpdate = new JsonObject();
userUpdate.addProperty("c_Comments", comment);
UpdateRequest updateRequest = new UpdateRequest(userRef,userUpdate);
UpdateResponse updateResponse = restApi.update(updateRequest);
if (updateResponse.wasSuccessful()) {
System.out.println("Successfully updated user");
} else {
System.out.println("Error occurred attempting to update user");
String[] errorList;
errorList = updateResponse.getErrors();
for (int e=0;e<errorList.length;e++) {
System.out.println(errorList[e]);
}
}
}
}