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

Commit 7edf3fb

Browse files
author
Chris Mihaly
committed
Add schema methods to Java API
1 parent e7904ed commit 7edf3fb

2 files changed

Lines changed: 148 additions & 1 deletion

File tree

src/SchemaExample.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.net.URL;
2+
import java.util.HashMap;
3+
import java.util.Iterator;
4+
import java.util.Map;
5+
import java.util.Properties;
6+
7+
import com.shotgunsoftware.*;
8+
9+
class SchemaExample {
10+
private static final String SHOTGUN_SERVER;
11+
private static final String SCRIPT_KEY;
12+
private static final String SCRIPT_NAME;
13+
14+
private static final String DEFAULT_SHOTGUN_SERVER = "http://shotgun-dev.fas.fa.disney.com/api3";
15+
private static final String DEFAULT_SHOTGUN_KEY = "4b5a0063ed0bba6fea045b7eff7b77a9375353d5";
16+
private static final String DEFAULT_SCRIPT_NAME = "testScript";
17+
18+
static {
19+
SHOTGUN_SERVER = (System.getProperty("SHOTGUN_SERVER") != null) ? (String) System.getProperty("SHOTGUN_SERVER") : DEFAULT_SHOTGUN_SERVER;
20+
SCRIPT_KEY = (System.getProperty("SCRIPT_KEY") != null) ? (String) System.getProperty("SCRIPT_KEY") : DEFAULT_SHOTGUN_KEY;
21+
SCRIPT_NAME = (System.getProperty("SCRIPT_NAME") != null) ? (String) System.getProperty("SCRIPT_NAME") : DEFAULT_SCRIPT_NAME;
22+
}
23+
24+
25+
public static void main(String[] args) {
26+
try {
27+
URL u = new URL(SHOTGUN_SERVER);
28+
Shotgun s = new Shotgun(u, SCRIPT_NAME, SCRIPT_KEY);
29+
30+
Map schema = s.schema_read();
31+
System.out.println(schema);
32+
Map fields = s.schema_field_read("Shot", null);
33+
int i = 4;
34+
35+
for (Iterator it = fields.keySet().iterator(); it.hasNext(); ) {
36+
String key = (String) it.next();
37+
System.out.println("Key: " + key + " Value: " + fields.get(key));
38+
}
39+
String field_name = s.schema_field_create("Shot", "number", "schema_create_test", null);
40+
System.out.println(field_name);
41+
Map props = new HashMap();
42+
props.put("name", "Test Number Field Renamed");
43+
props.put("description","this is only a test");
44+
boolean success = s.schema_field_update("Shot", field_name, props);
45+
System.out.println(Boolean.toString(success));
46+
success = s.schema_field_delete("Shot", field_name);
47+
System.out.println(Boolean.toString(success));
48+
// Map schema = s.schema_read();
49+
} catch ( Exception e ) {
50+
System.out.println(e.getMessage());
51+
}
52+
}
53+
}
54+

src/com/shotgunsoftware/Shotgun.java

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.apache.ws.commons.util.NamespaceContextImpl;
1616
import org.apache.xmlrpc.XmlRpcException;
1717

18+
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
19+
1820
/**
1921
* Shotgun XML-RPC API Wrapper
2022
*
@@ -194,7 +196,97 @@ public boolean delete(DeleteRequest dr) throws XmlRpcException {
194196

195197
return (didDelete.equals(Boolean.TRUE));
196198
}
197-
199+
200+
/**
201+
* Return the shotgun schema
202+
*
203+
* Well, it would but can't figure out how to pass no arguments through java xmlrpc
204+
* @return
205+
* @throws XmlRpcException if the request failed
206+
*/
207+
public Map schema_read() throws XmlRpcException {
208+
throw new NotImplementedException();
209+
/**
210+
Object[] params = new Object[] { this.auth, new HashMap()};
211+
Map response = (Map)this.client.execute("schema_read", params);
212+
Map results = (Map)response.get("results");
213+
return results;
214+
**/
215+
}
216+
217+
public Map schema_field_read(String entity_type, String field_name) throws XmlRpcException {
218+
HashMap req = new HashMap();
219+
req.put("type", entity_type);
220+
if (field_name != null)
221+
req.put("field_name", field_name);
222+
Object[] params = new Object[] { this.auth, req };
223+
Map response = (Map)this.client.execute("schema_field_read", params);
224+
Map results = (Map)response.get("results");
225+
return results;
226+
}
227+
228+
public String schema_field_create(String entity_type, String data_type, String display_name,
229+
Map properties) throws XmlRpcException {
230+
if (properties == null)
231+
properties = new HashMap();
232+
List fields = new ArrayList();
233+
234+
HashMap rf = new HashMap();
235+
rf.put("property_name", "name");
236+
rf.put("value", display_name);
237+
fields.add(rf);
238+
for ( Iterator iter = properties.entrySet().iterator(); iter.hasNext(); ) {
239+
Entry e = (Entry)iter.next();
240+
rf = new HashMap();
241+
String key = (String)e.getKey();
242+
rf.put("property_name", key);
243+
rf.put("value", e.getValue());
244+
fields.add(rf);
245+
}
246+
HashMap req = new HashMap();
247+
req.put("type", entity_type);
248+
req.put("data_type", data_type);
249+
req.put("properties", fields);
250+
Object[] params = new Object[] { this.auth, req };
251+
Map response = (Map)this.client.execute("schema_field_create", params);
252+
String field_name = (String)response.get("results");
253+
return field_name;
254+
}
255+
256+
public boolean schema_field_update(String entity_type, String field_name, Map properties) throws XmlRpcException {
257+
if (properties == null)
258+
properties = new HashMap();
259+
List fields = new ArrayList();
260+
261+
HashMap rf = new HashMap();
262+
for ( Iterator iter = properties.entrySet().iterator(); iter.hasNext(); ) {
263+
Entry e = (Entry)iter.next();
264+
rf = new HashMap();
265+
String key = (String)e.getKey();
266+
rf.put("property_name", key);
267+
rf.put("value", e.getValue());
268+
fields.add(rf);
269+
}
270+
HashMap req = new HashMap();
271+
req.put("type", entity_type);
272+
req.put("field_name", field_name);
273+
req.put("properties", fields);
274+
Object[] params = new Object[] { this.auth, req };
275+
Map response = (Map)this.client.execute("schema_field_update", params);
276+
Boolean didDelete = (Boolean)response.get("results");
277+
return (didDelete.equals(Boolean.TRUE));
278+
}
279+
280+
public boolean schema_field_delete(String entity_type, String field_name) throws XmlRpcException {
281+
HashMap req = new HashMap();
282+
req.put("type", entity_type);
283+
req.put("field_name", field_name);
284+
Object[] params = new Object[] { this.auth, req };
285+
Map response = (Map)this.client.execute("schema_field_delete", params);
286+
Boolean didDelete = (Boolean)response.get("results");
287+
return (didDelete.equals(Boolean.TRUE));
288+
}
289+
198290
class MyTypeFactory extends TypeFactoryImpl {
199291
public MyTypeFactory(XmlRpcController pController) {
200292
super(pController);
@@ -210,4 +302,5 @@ public TypeParser getParser(XmlRpcStreamConfig pConfig,
210302
}
211303
}
212304
}
305+
213306
}

0 commit comments

Comments
 (0)