Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit d1cc9fb

Browse files
author
Chris Mihaly
committed
Add batch support
1 parent cc9c3af commit d1cc9fb

3 files changed

Lines changed: 221 additions & 0 deletions

File tree

src/BatchExample.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import java.net.URL;
2+
import java.util.ArrayList;
3+
import java.util.HashMap;
4+
import java.util.Iterator;
5+
import java.util.Map;
6+
import java.util.Properties;
7+
8+
import com.shotgunsoftware.*;
9+
10+
class BatchExample {
11+
private static final String SHOTGUN_SERVER;
12+
private static final String SCRIPT_KEY;
13+
private static final String SCRIPT_NAME;
14+
15+
private static final String DEFAULT_SHOTGUN_SERVER = "http://shotgun-dev.fas.fa.disney.com/api3";
16+
private static final String DEFAULT_SHOTGUN_KEY = "4b5a0063ed0bba6fea045b7eff7b77a9375353d5";
17+
private static final String DEFAULT_SCRIPT_NAME = "testScript";
18+
19+
static {
20+
SHOTGUN_SERVER = (System.getProperty("SHOTGUN_SERVER") != null) ? (String) System.getProperty("SHOTGUN_SERVER") : DEFAULT_SHOTGUN_SERVER;
21+
SCRIPT_KEY = (System.getProperty("SCRIPT_KEY") != null) ? (String) System.getProperty("SCRIPT_KEY") : DEFAULT_SHOTGUN_KEY;
22+
SCRIPT_NAME = (System.getProperty("SCRIPT_NAME") != null) ? (String) System.getProperty("SCRIPT_NAME") : DEFAULT_SCRIPT_NAME;
23+
}
24+
25+
26+
public static void main(String[] args) {
27+
try {
28+
URL u = new URL(SHOTGUN_SERVER);
29+
Shotgun s = new Shotgun(u, SCRIPT_NAME, SCRIPT_KEY);
30+
31+
BatchRequest[] req = new BatchRequest[3];
32+
33+
HashMap asset = new HashMap();
34+
asset.put("type", "Asset");
35+
asset.put("id", new Integer(23182));
36+
37+
HashMap step = new HashMap();
38+
step.put("type", "Step");
39+
step.put("id", new Integer(10));
40+
41+
HashMap project = new HashMap();
42+
project.put("type", "Project");
43+
project.put("id", new Integer(77));
44+
45+
HashMap data = new HashMap();
46+
data.put("content", "New Batch Test Tast1");
47+
data.put("entity", asset);
48+
data.put("sg_status_list", "pre");
49+
data.put("step", step);
50+
data.put("project", project);
51+
52+
req[0] = new BatchRequest("Task");
53+
req[0].create(data);
54+
req[0] = new BatchRequest("Task");
55+
req[0].create(data);
56+
57+
data = new HashMap();
58+
data.put("content", "New Batch Test Tast2");
59+
data.put("entity", asset);
60+
data.put("sg_status_list", "wrk");
61+
data.put("step", step);
62+
data.put("project", project);
63+
64+
req[1] = new BatchRequest("Task");
65+
req[1].create(data);
66+
67+
data = new HashMap();
68+
data.put("content", "New Batch Test Tast3");
69+
data.put("entity", asset);
70+
data.put("sg_status_list", "cmpt");
71+
data.put("step", step);
72+
data.put("project", project);
73+
74+
req[2] = new BatchRequest("Task");
75+
req[2].create(data);
76+
77+
Object[] r = s.batch(req);
78+
data = new HashMap();
79+
data.put("sg_status_list", "omt");
80+
for (int index = 0; index < r.length; index++) {
81+
req[index] = new BatchRequest("Task");
82+
req[index].update((Integer) ((Map) r[index]).get("id"), data);
83+
}
84+
r = s.batch(req);
85+
for (int index = 0; index < r.length; index++) {
86+
req[index] = new BatchRequest("Task");
87+
req[index].delete((Integer) ((Map)r[index]).get("id"));
88+
}
89+
r = s.batch(req);
90+
91+
int i = 4;
92+
93+
// Object[] assets = (Object[])r.get("assets");
94+
// for (int index = 0; index < assets.length; index++)
95+
// System.out.print(assets[index].toString());
96+
// System.out.println();
97+
} catch ( Exception e ) {
98+
System.out.println(e.getMessage());
99+
}
100+
}
101+
}
102+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.shotgunsoftware;
2+
3+
import java.util.Map;
4+
import java.util.HashMap;
5+
import java.util.Set;
6+
7+
/**
8+
* The specification for an Update request.
9+
*
10+
* @author Matt Daw
11+
* @version 1.0
12+
*/
13+
public class BatchRequest {
14+
String entityType;
15+
Integer entityId;
16+
Map data;
17+
Set returnFields;
18+
String requestType;
19+
20+
/**
21+
* Sole Constructor
22+
*
23+
* @param entityType the type of entity to update
24+
* @param entityId the id of entity to update
25+
*/
26+
public BatchRequest(String entityType) {
27+
this.entityType = entityType;
28+
this.entityId = null;
29+
this.data = new HashMap();
30+
this.requestType = null;
31+
}
32+
33+
/**
34+
* Specify entity values to create.
35+
*
36+
* @param data map of field name to field value
37+
*/
38+
public void create(Map data, Set returnFields) {
39+
this.data = data;
40+
this.requestType = "create";
41+
this.returnFields = returnFields;
42+
}
43+
44+
public void create(Map data) {
45+
create(data, null);
46+
}
47+
48+
/**
49+
* Specify entity values to create.
50+
*
51+
* @param data map of field name to field value
52+
*/
53+
public void update(Integer entityId, Map data) {
54+
this.entityId = entityId;
55+
this.data = data;
56+
this.requestType = "update";
57+
}
58+
/**
59+
* Specify entity values to create.
60+
*
61+
* @param data map of field name to field value
62+
*/
63+
public void delete(Integer entityId) {
64+
this.entityId = entityId;
65+
this.requestType = "delete";
66+
67+
}
68+
69+
}

src/com/shotgunsoftware/Shotgun.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55
import java.util.ArrayList;
66
import java.util.Map;
7+
import java.util.Vector;
78
import java.util.Map.Entry;
89
import java.util.HashMap;
910
import java.util.Iterator;
@@ -195,6 +196,55 @@ public boolean delete(DeleteRequest dr) throws XmlRpcException {
195196
return (didDelete.equals(Boolean.TRUE));
196197
}
197198

199+
/**
200+
* Batch a series of update requests
201+
*
202+
* @param br A list of Batch Requests
203+
* @return a List of Map of field names to field values for the created entity
204+
* @throws XmlRpcException if the request failed
205+
*/
206+
public Object[] batch(BatchRequest[] br) throws XmlRpcException {
207+
List req = new Vector();
208+
for (int index = 0; index < br.length; index++) {
209+
Map hr = new HashMap();
210+
hr.put("request_type", br[index].requestType);
211+
hr.put("type", br[index].entityType);
212+
if (br[index].requestType.equals("create")) {
213+
List fields = new Vector();
214+
for (Iterator entry_it = br[index].data.entrySet().iterator(); entry_it.hasNext(); ) {
215+
Map.Entry entry = (Map.Entry) entry_it.next();
216+
Map e = new HashMap();
217+
e.put("field_name", entry.getKey());
218+
e.put("value", entry.getValue());
219+
fields.add(e);
220+
}
221+
hr.put("fields", fields.toArray());
222+
} else if (br[index].requestType.equals("update")) {
223+
hr.put("id", br[index].entityId);
224+
List fields = new Vector();
225+
for (Iterator entry_it = br[index].data.entrySet().iterator(); entry_it.hasNext(); ) {
226+
Map.Entry entry = (Map.Entry) entry_it.next();
227+
Map e = new HashMap();
228+
e.put("field_name", entry.getKey());
229+
e.put("value", entry.getValue());
230+
fields.add(e);
231+
}
232+
hr.put("fields", fields.toArray());
233+
} else if (br[index].requestType.equals("delete")) {
234+
hr.put("id", br[index].entityId);
235+
}
236+
req.add(hr);
237+
}
238+
Object[] params = new Object[] { this.auth, req.toArray() };
239+
Map response = (Map)this.client.execute("batch", params);
240+
Object[] results = (Object[]) response.get("results");
241+
return results;
242+
}
243+
244+
public Object[] batch(List br) throws XmlRpcException {
245+
return batch((BatchRequest[]) br.toArray());
246+
}
247+
198248
/**
199249
* Return the shotgun schema
200250
*

0 commit comments

Comments
 (0)