From 7edf3fb9e44d0953490641c49a27a7791b6c2439 Mon Sep 17 00:00:00 2001 From: Chris Mihaly Date: Tue, 5 Jul 2011 08:55:30 -0700 Subject: [PATCH 1/6] Add schema methods to Java API --- src/SchemaExample.java | 54 ++++++++++++++++ src/com/shotgunsoftware/Shotgun.java | 95 +++++++++++++++++++++++++++- 2 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 src/SchemaExample.java diff --git a/src/SchemaExample.java b/src/SchemaExample.java new file mode 100644 index 0000000..58ac11b --- /dev/null +++ b/src/SchemaExample.java @@ -0,0 +1,54 @@ +import java.net.URL; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Properties; + +import com.shotgunsoftware.*; + +class SchemaExample { + private static final String SHOTGUN_SERVER; + private static final String SCRIPT_KEY; + private static final String SCRIPT_NAME; + + private static final String DEFAULT_SHOTGUN_SERVER = "http://shotgun-dev.fas.fa.disney.com/api3"; + private static final String DEFAULT_SHOTGUN_KEY = "4b5a0063ed0bba6fea045b7eff7b77a9375353d5"; + private static final String DEFAULT_SCRIPT_NAME = "testScript"; + + static { + SHOTGUN_SERVER = (System.getProperty("SHOTGUN_SERVER") != null) ? (String) System.getProperty("SHOTGUN_SERVER") : DEFAULT_SHOTGUN_SERVER; + SCRIPT_KEY = (System.getProperty("SCRIPT_KEY") != null) ? (String) System.getProperty("SCRIPT_KEY") : DEFAULT_SHOTGUN_KEY; + SCRIPT_NAME = (System.getProperty("SCRIPT_NAME") != null) ? (String) System.getProperty("SCRIPT_NAME") : DEFAULT_SCRIPT_NAME; + } + + + public static void main(String[] args) { + try { + URL u = new URL(SHOTGUN_SERVER); + Shotgun s = new Shotgun(u, SCRIPT_NAME, SCRIPT_KEY); + + Map schema = s.schema_read(); + System.out.println(schema); + Map fields = s.schema_field_read("Shot", null); + int i = 4; + + for (Iterator it = fields.keySet().iterator(); it.hasNext(); ) { + String key = (String) it.next(); + System.out.println("Key: " + key + " Value: " + fields.get(key)); + } + String field_name = s.schema_field_create("Shot", "number", "schema_create_test", null); + System.out.println(field_name); + Map props = new HashMap(); + props.put("name", "Test Number Field Renamed"); + props.put("description","this is only a test"); + boolean success = s.schema_field_update("Shot", field_name, props); + System.out.println(Boolean.toString(success)); + success = s.schema_field_delete("Shot", field_name); + System.out.println(Boolean.toString(success)); +// Map schema = s.schema_read(); + } catch ( Exception e ) { + System.out.println(e.getMessage()); + } + } +} + diff --git a/src/com/shotgunsoftware/Shotgun.java b/src/com/shotgunsoftware/Shotgun.java index d089cd8..af7cd66 100644 --- a/src/com/shotgunsoftware/Shotgun.java +++ b/src/com/shotgunsoftware/Shotgun.java @@ -15,6 +15,8 @@ import org.apache.ws.commons.util.NamespaceContextImpl; import org.apache.xmlrpc.XmlRpcException; +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + /** * Shotgun XML-RPC API Wrapper * @@ -194,7 +196,97 @@ public boolean delete(DeleteRequest dr) throws XmlRpcException { return (didDelete.equals(Boolean.TRUE)); } - + + /** + * Return the shotgun schema + * + * Well, it would but can't figure out how to pass no arguments through java xmlrpc + * @return + * @throws XmlRpcException if the request failed + */ + public Map schema_read() throws XmlRpcException { + throw new NotImplementedException(); + /** + Object[] params = new Object[] { this.auth, new HashMap()}; + Map response = (Map)this.client.execute("schema_read", params); + Map results = (Map)response.get("results"); + return results; + **/ + } + + public Map schema_field_read(String entity_type, String field_name) throws XmlRpcException { + HashMap req = new HashMap(); + req.put("type", entity_type); + if (field_name != null) + req.put("field_name", field_name); + Object[] params = new Object[] { this.auth, req }; + Map response = (Map)this.client.execute("schema_field_read", params); + Map results = (Map)response.get("results"); + return results; + } + + public String schema_field_create(String entity_type, String data_type, String display_name, + Map properties) throws XmlRpcException { + if (properties == null) + properties = new HashMap(); + List fields = new ArrayList(); + + HashMap rf = new HashMap(); + rf.put("property_name", "name"); + rf.put("value", display_name); + fields.add(rf); + for ( Iterator iter = properties.entrySet().iterator(); iter.hasNext(); ) { + Entry e = (Entry)iter.next(); + rf = new HashMap(); + String key = (String)e.getKey(); + rf.put("property_name", key); + rf.put("value", e.getValue()); + fields.add(rf); + } + HashMap req = new HashMap(); + req.put("type", entity_type); + req.put("data_type", data_type); + req.put("properties", fields); + Object[] params = new Object[] { this.auth, req }; + Map response = (Map)this.client.execute("schema_field_create", params); + String field_name = (String)response.get("results"); + return field_name; + } + + public boolean schema_field_update(String entity_type, String field_name, Map properties) throws XmlRpcException { + if (properties == null) + properties = new HashMap(); + List fields = new ArrayList(); + + HashMap rf = new HashMap(); + for ( Iterator iter = properties.entrySet().iterator(); iter.hasNext(); ) { + Entry e = (Entry)iter.next(); + rf = new HashMap(); + String key = (String)e.getKey(); + rf.put("property_name", key); + rf.put("value", e.getValue()); + fields.add(rf); + } + HashMap req = new HashMap(); + req.put("type", entity_type); + req.put("field_name", field_name); + req.put("properties", fields); + Object[] params = new Object[] { this.auth, req }; + Map response = (Map)this.client.execute("schema_field_update", params); + Boolean didDelete = (Boolean)response.get("results"); + return (didDelete.equals(Boolean.TRUE)); + } + + public boolean schema_field_delete(String entity_type, String field_name) throws XmlRpcException { + HashMap req = new HashMap(); + req.put("type", entity_type); + req.put("field_name", field_name); + Object[] params = new Object[] { this.auth, req }; + Map response = (Map)this.client.execute("schema_field_delete", params); + Boolean didDelete = (Boolean)response.get("results"); + return (didDelete.equals(Boolean.TRUE)); + } + class MyTypeFactory extends TypeFactoryImpl { public MyTypeFactory(XmlRpcController pController) { super(pController); @@ -210,4 +302,5 @@ public TypeParser getParser(XmlRpcStreamConfig pConfig, } } } + } From 5a141c7f4bd34fe39b7531b0d1bbd18434b53a61 Mon Sep 17 00:00:00 2001 From: Chris Mihaly Date: Tue, 5 Jul 2011 09:03:53 -0700 Subject: [PATCH 2/6] Java Build File and git ignore defaults for Eclipse project files --- .gitignore | 5 +++++ build.xml | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 .gitignore create mode 100644 build.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4b4796d --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +classes +.classpath +*.jar +.project +.settings diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..c9e3cf0 --- /dev/null +++ b/build.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 029582478618dd2a3341160b89d83743808a7ca0 Mon Sep 17 00:00:00 2001 From: Chris Mihaly Date: Tue, 5 Jul 2011 09:12:35 -0700 Subject: [PATCH 3/6] Use Java Standard UnsupportedOperationException --- src/com/shotgunsoftware/Shotgun.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/com/shotgunsoftware/Shotgun.java b/src/com/shotgunsoftware/Shotgun.java index af7cd66..e43571a 100644 --- a/src/com/shotgunsoftware/Shotgun.java +++ b/src/com/shotgunsoftware/Shotgun.java @@ -15,8 +15,6 @@ import org.apache.ws.commons.util.NamespaceContextImpl; import org.apache.xmlrpc.XmlRpcException; -import sun.reflect.generics.reflectiveObjects.NotImplementedException; - /** * Shotgun XML-RPC API Wrapper * @@ -205,7 +203,7 @@ public boolean delete(DeleteRequest dr) throws XmlRpcException { * @throws XmlRpcException if the request failed */ public Map schema_read() throws XmlRpcException { - throw new NotImplementedException(); + throw new UnsupportedOperationException(); /** Object[] params = new Object[] { this.auth, new HashMap()}; Map response = (Map)this.client.execute("schema_read", params); From cc9c3aff398e2dc5539e8be8c24e42a3e541a232 Mon Sep 17 00:00:00 2001 From: Chris Mihaly Date: Wed, 3 Aug 2011 12:30:13 -0700 Subject: [PATCH 4/6] Java 1.4 base code changes --- src/UpdateExample.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/UpdateExample.java b/src/UpdateExample.java index 38c5283..29e6f0a 100644 --- a/src/UpdateExample.java +++ b/src/UpdateExample.java @@ -33,21 +33,27 @@ public static void main(String[] args) { Map r = s.update(ur); Object[] assets = (Object[])r.get("assets"); - System.out.println(java.util.Arrays.toString(assets)); + for (int index = 0; index < assets.length; index++) + System.out.print(assets[index].toString()); + System.out.println(); // Now let's re-run the update in "remove" mode. modes.put("assets", "remove"); r = s.update(ur); assets = (Object[])r.get("assets"); - System.out.println(java.util.Arrays.toString(assets)); + for (int index = 0; index < assets.length; index++) + System.out.print(assets[index].toString()); + System.out.println(); // Add it back in for the next step. modes.put("assets", "add"); r = s.update(ur); assets = (Object[])r.get("assets"); - System.out.println(java.util.Arrays.toString(assets)); + for (int index = 0; index < assets.length; index++) + System.out.print(assets[index].toString()); + System.out.println(); // Now set a field on the connection between the Shot and Asset. To do this, // we need to pass the desired asset to the request. From d1cc9fb8cf46460cdfdeeca2f40ef740e4da1116 Mon Sep 17 00:00:00 2001 From: Chris Mihaly Date: Wed, 3 Aug 2011 12:30:48 -0700 Subject: [PATCH 5/6] Add batch support --- src/BatchExample.java | 102 ++++++++++++++++++++++ src/com/shotgunsoftware/BatchRequest.java | 69 +++++++++++++++ src/com/shotgunsoftware/Shotgun.java | 50 +++++++++++ 3 files changed, 221 insertions(+) create mode 100644 src/BatchExample.java create mode 100644 src/com/shotgunsoftware/BatchRequest.java diff --git a/src/BatchExample.java b/src/BatchExample.java new file mode 100644 index 0000000..753f2f8 --- /dev/null +++ b/src/BatchExample.java @@ -0,0 +1,102 @@ +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Properties; + +import com.shotgunsoftware.*; + +class BatchExample { + private static final String SHOTGUN_SERVER; + private static final String SCRIPT_KEY; + private static final String SCRIPT_NAME; + + private static final String DEFAULT_SHOTGUN_SERVER = "http://shotgun-dev.fas.fa.disney.com/api3"; + private static final String DEFAULT_SHOTGUN_KEY = "4b5a0063ed0bba6fea045b7eff7b77a9375353d5"; + private static final String DEFAULT_SCRIPT_NAME = "testScript"; + + static { + SHOTGUN_SERVER = (System.getProperty("SHOTGUN_SERVER") != null) ? (String) System.getProperty("SHOTGUN_SERVER") : DEFAULT_SHOTGUN_SERVER; + SCRIPT_KEY = (System.getProperty("SCRIPT_KEY") != null) ? (String) System.getProperty("SCRIPT_KEY") : DEFAULT_SHOTGUN_KEY; + SCRIPT_NAME = (System.getProperty("SCRIPT_NAME") != null) ? (String) System.getProperty("SCRIPT_NAME") : DEFAULT_SCRIPT_NAME; + } + + + public static void main(String[] args) { + try { + URL u = new URL(SHOTGUN_SERVER); + Shotgun s = new Shotgun(u, SCRIPT_NAME, SCRIPT_KEY); + + BatchRequest[] req = new BatchRequest[3]; + + HashMap asset = new HashMap(); + asset.put("type", "Asset"); + asset.put("id", new Integer(23182)); + + HashMap step = new HashMap(); + step.put("type", "Step"); + step.put("id", new Integer(10)); + + HashMap project = new HashMap(); + project.put("type", "Project"); + project.put("id", new Integer(77)); + + HashMap data = new HashMap(); + data.put("content", "New Batch Test Tast1"); + data.put("entity", asset); + data.put("sg_status_list", "pre"); + data.put("step", step); + data.put("project", project); + + req[0] = new BatchRequest("Task"); + req[0].create(data); + req[0] = new BatchRequest("Task"); + req[0].create(data); + + data = new HashMap(); + data.put("content", "New Batch Test Tast2"); + data.put("entity", asset); + data.put("sg_status_list", "wrk"); + data.put("step", step); + data.put("project", project); + + req[1] = new BatchRequest("Task"); + req[1].create(data); + + data = new HashMap(); + data.put("content", "New Batch Test Tast3"); + data.put("entity", asset); + data.put("sg_status_list", "cmpt"); + data.put("step", step); + data.put("project", project); + + req[2] = new BatchRequest("Task"); + req[2].create(data); + + Object[] r = s.batch(req); + data = new HashMap(); + data.put("sg_status_list", "omt"); + for (int index = 0; index < r.length; index++) { + req[index] = new BatchRequest("Task"); + req[index].update((Integer) ((Map) r[index]).get("id"), data); + } + r = s.batch(req); + for (int index = 0; index < r.length; index++) { + req[index] = new BatchRequest("Task"); + req[index].delete((Integer) ((Map)r[index]).get("id")); + } + r = s.batch(req); + + int i = 4; + +// Object[] assets = (Object[])r.get("assets"); +// for (int index = 0; index < assets.length; index++) +// System.out.print(assets[index].toString()); +// System.out.println(); + } catch ( Exception e ) { + System.out.println(e.getMessage()); + } + } +} + diff --git a/src/com/shotgunsoftware/BatchRequest.java b/src/com/shotgunsoftware/BatchRequest.java new file mode 100644 index 0000000..538cbc0 --- /dev/null +++ b/src/com/shotgunsoftware/BatchRequest.java @@ -0,0 +1,69 @@ +package com.shotgunsoftware; + +import java.util.Map; +import java.util.HashMap; +import java.util.Set; + +/** + * The specification for an Update request. + * + * @author Matt Daw + * @version 1.0 + */ +public class BatchRequest { + String entityType; + Integer entityId; + Map data; + Set returnFields; + String requestType; + + /** + * Sole Constructor + * + * @param entityType the type of entity to update + * @param entityId the id of entity to update + */ + public BatchRequest(String entityType) { + this.entityType = entityType; + this.entityId = null; + this.data = new HashMap(); + this.requestType = null; + } + + /** + * Specify entity values to create. + * + * @param data map of field name to field value + */ + public void create(Map data, Set returnFields) { + this.data = data; + this.requestType = "create"; + this.returnFields = returnFields; + } + + public void create(Map data) { + create(data, null); + } + + /** + * Specify entity values to create. + * + * @param data map of field name to field value + */ + public void update(Integer entityId, Map data) { + this.entityId = entityId; + this.data = data; + this.requestType = "update"; + } + /** + * Specify entity values to create. + * + * @param data map of field name to field value + */ + public void delete(Integer entityId) { + this.entityId = entityId; + this.requestType = "delete"; + + } + +} diff --git a/src/com/shotgunsoftware/Shotgun.java b/src/com/shotgunsoftware/Shotgun.java index e43571a..63f1adc 100644 --- a/src/com/shotgunsoftware/Shotgun.java +++ b/src/com/shotgunsoftware/Shotgun.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.ArrayList; import java.util.Map; +import java.util.Vector; import java.util.Map.Entry; import java.util.HashMap; import java.util.Iterator; @@ -195,6 +196,55 @@ public boolean delete(DeleteRequest dr) throws XmlRpcException { return (didDelete.equals(Boolean.TRUE)); } + /** + * Batch a series of update requests + * + * @param br A list of Batch Requests + * @return a List of Map of field names to field values for the created entity + * @throws XmlRpcException if the request failed + */ + public Object[] batch(BatchRequest[] br) throws XmlRpcException { + List req = new Vector(); + for (int index = 0; index < br.length; index++) { + Map hr = new HashMap(); + hr.put("request_type", br[index].requestType); + hr.put("type", br[index].entityType); + if (br[index].requestType.equals("create")) { + List fields = new Vector(); + for (Iterator entry_it = br[index].data.entrySet().iterator(); entry_it.hasNext(); ) { + Map.Entry entry = (Map.Entry) entry_it.next(); + Map e = new HashMap(); + e.put("field_name", entry.getKey()); + e.put("value", entry.getValue()); + fields.add(e); + } + hr.put("fields", fields.toArray()); + } else if (br[index].requestType.equals("update")) { + hr.put("id", br[index].entityId); + List fields = new Vector(); + for (Iterator entry_it = br[index].data.entrySet().iterator(); entry_it.hasNext(); ) { + Map.Entry entry = (Map.Entry) entry_it.next(); + Map e = new HashMap(); + e.put("field_name", entry.getKey()); + e.put("value", entry.getValue()); + fields.add(e); + } + hr.put("fields", fields.toArray()); + } else if (br[index].requestType.equals("delete")) { + hr.put("id", br[index].entityId); + } + req.add(hr); + } + Object[] params = new Object[] { this.auth, req.toArray() }; + Map response = (Map)this.client.execute("batch", params); + Object[] results = (Object[]) response.get("results"); + return results; + } + + public Object[] batch(List br) throws XmlRpcException { + return batch((BatchRequest[]) br.toArray()); + } + /** * Return the shotgun schema * From 0e05879bc023af015c6e50416548ec740b74b80d Mon Sep 17 00:00:00 2001 From: Chris Mihaly Date: Wed, 3 Aug 2011 12:35:39 -0700 Subject: [PATCH 6/6] Use 1.4 java compiler --- build.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.xml b/build.xml index c9e3cf0..7d38bd4 100644 --- a/build.xml +++ b/build.xml @@ -13,7 +13,7 @@ - +