]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-87: Adding an example "update" call.
authorRichard Millet <richard.millet@berkeley.edu>
Tue, 13 Oct 2009 00:23:05 +0000 (00:23 +0000)
committerRichard Millet <richard.millet@berkeley.edu>
Tue, 13 Oct 2009 00:23:05 +0000 (00:23 +0000)
services/sdk/sample/src/main/java/org/collectionspace/services/sdk/sample/Sample.java

index 64475132795a046c39e54244f9f668f898515382..414592d9cf52463a1cbe4158aeee4da235d9747b 100644 (file)
@@ -1,3 +1,29 @@
+/**    \r
+ * Sample.java\r
+ *\r
+ * {Purpose of This Class}\r
+ *\r
+ * {Other Notes Relating to This Class (Optional)}\r
+ *\r
+ * $LastChangedBy: $\r
+ * $LastChangedRevision: $\r
+ * $LastChangedDate: $\r
+ *\r
+ * This document is a part of the source code and related artifacts\r
+ * for CollectionSpace, an open source collections management system\r
+ * for museums and related institutions:\r
+ *\r
+ * http://www.collectionspace.org\r
+ * http://wiki.collectionspace.org\r
+ *\r
+ * Copyright © 2009 {Contributing Institution}\r
+ *\r
+ * Licensed under the Educational Community License (ECL), Version 2.0.\r
+ * You may not use this file except in compliance with this License.\r
+ *\r
+ * You may obtain a copy of the ECL 2.0 License at\r
+ * https://source.collectionspace.org/collection-space/LICENSE.txt\r
+ */\r
 package org.collectionspace.services.sdk.sample;\r
 \r
 import java.io.IOException;\r
@@ -21,81 +47,178 @@ import org.collectionspace.services.client.CollectionObjectClient;
 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;\r
 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;\r
 \r
+/**\r
+ * The Class Sample.\r
+ */\r
 public class Sample {\r
 \r
+       /** The collection object client. */\r
        private static CollectionObjectClient collectionObjectClient = new CollectionObjectClient();\r
 \r
        /**\r
         * @param args\r
         */\r
        public static void main(String[] args) {\r
-               \r
-               System.out.println("Base URL is: " + collectionObjectClient.getBaseURL());\r
-               \r
+\r
+               System.out.println("Base URL is: "\r
+                               + collectionObjectClient.getBaseURL());\r
+\r
                String csid = createCollectionObject();\r
                System.out.println("Created a new collection object with CSID=" + csid);\r
-               \r
+\r
                CollectionobjectsCommon co = readCollectionObject(csid);\r
                System.out.println("Got a collection object with CSID=" + csid);\r
+               \r
+               int status = updateCollectionObject(csid);\r
+               System.out.println("Updated the collection object with CSID=" + csid);\r
        }\r
-       \r
+\r
+       /**\r
+        * Creates the collection object.\r
+        * \r
+        * @return the string\r
+        */\r
        static String createCollectionObject() {\r
                String result = null;\r
 \r
                CollectionobjectsCommon co = new CollectionobjectsCommon();\r
                co.setObjectName("Keiko CollectionobjectsCommon");\r
-       \r
-        MultipartOutput multipart = new MultipartOutput();\r
-        OutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);\r
-        commonPart.getHeaders().add("label", collectionObjectClient.getCommonPartName());\r
-               \r
-           ClientResponse<Response> response = collectionObjectClient.create(multipart);\r
-           Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());\r
-           result = extractId(response);\r
-           \r
-           return result;\r
+\r
+               MultipartOutput multipart = new MultipartOutput();\r
+               OutputPart commonPart = multipart.addPart(co,\r
+                               MediaType.APPLICATION_XML_TYPE);\r
+               commonPart.getHeaders().add("label",\r
+                               collectionObjectClient.getCommonPartName());\r
+\r
+               ClientResponse<Response> response = collectionObjectClient\r
+                               .create(multipart);\r
+               Assert.assertEquals(response.getStatus(), Response.Status.CREATED\r
+                               .getStatusCode());\r
+               result = extractId(response);\r
+\r
+               return result;\r
        }\r
-       \r
+\r
+       /**\r
+        * Read collection object.\r
+        * \r
+        * @param csid\r
+        *            the csid\r
+        * \r
+        * @return the collectionobjects common\r
+        */\r
        static CollectionobjectsCommon readCollectionObject(String csid) {\r
                CollectionobjectsCommon result = null;\r
-               \r
-        ClientResponse<MultipartInput> response = collectionObjectClient.read(csid);\r
-               Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());\r
-        try{\r
-            MultipartInput input = (MultipartInput) response.getEntity();\r
-            result = (CollectionobjectsCommon) extractPart(input,\r
-                       collectionObjectClient.getCommonPartName(), CollectionobjectsCommon.class);\r
-        } catch(Exception e){\r
-            throw new RuntimeException(e);\r
-        }\r
-               \r
+\r
+               ClientResponse<MultipartInput> response = collectionObjectClient\r
+                               .read(csid);\r
+               Assert.assertEquals(response.getStatus(), Response.Status.OK\r
+                               .getStatusCode());\r
+               try {\r
+                       MultipartInput input = (MultipartInput) response.getEntity();\r
+                       result = (CollectionobjectsCommon) extractPart(input,\r
+                                       collectionObjectClient.getCommonPartName(),\r
+                                       CollectionobjectsCommon.class);\r
+               } catch (Exception e) {\r
+                       throw new RuntimeException(e);\r
+               }\r
+\r
                return result;\r
-       }       \r
-//\r
-// Utility methods that belong somewhere in the SDK and NOT the sample.\r
-//     \r
+       }\r
+\r
+       /**\r
+        * Update collection object.\r
+        */\r
+       static int updateCollectionObject(String csid) {\r
+               ClientResponse<MultipartInput> response = collectionObjectClient\r
+                               .read(csid);\r
+               Assert.assertEquals(response.getStatus(), Response.Status.OK\r
+                               .getStatusCode());\r
+\r
+               MultipartInput input = (MultipartInput) response.getEntity();\r
+               CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(\r
+                               input, collectionObjectClient.getCommonPartName(),\r
+                               CollectionobjectsCommon.class);\r
+               Assert.assertNotNull(collectionObject);\r
+\r
+               // Update the content of this resource.\r
+               collectionObject.setObjectNumber("updated-"\r
+                               + collectionObject.getObjectNumber());\r
+               collectionObject.setObjectName("updated-"\r
+                               + collectionObject.getObjectName());\r
+\r
+               // Submit the request to the service and store the response.\r
+               MultipartOutput output = new MultipartOutput();\r
+               OutputPart commonPart = output.addPart(collectionObject,\r
+                               MediaType.APPLICATION_XML_TYPE);\r
+               commonPart.getHeaders().add("label",\r
+                               collectionObjectClient.getCommonPartName());\r
+\r
+               response = collectionObjectClient.update(csid, output);\r
+               int statusCode = response.getStatus();\r
+\r
+               return statusCode;\r
+       }\r
+\r
+       //\r
+       // Utility methods that belong somewhere in the SDK and NOT the sample.\r
+       //\r
+       \r
+       /**\r
+        * Extract id.\r
+        * \r
+        * @param res\r
+        *            the res\r
+        * \r
+        * @return the string\r
+        */\r
        static String extractId(ClientResponse<Response> res) {\r
                String result = null;\r
-               \r
-               MultivaluedMap mvm = res.getMetadata();\r
-               String uri = (String) ((ArrayList) mvm.get("Location")).get(0);\r
-               String[] segments = uri.split("/");\r
-               result = segments[segments.length - 1];\r
-               \r
+\r
+               try {\r
+                       MultivaluedMap mvm = res.getMetadata();\r
+                       String uri = (String) ((ArrayList) mvm.get("Location")).get(0);\r
+                       String[] segments = uri.split("/");\r
+                       result = segments[segments.length - 1];\r
+               } catch (Exception e) {\r
+                       e.printStackTrace();\r
+               }\r
+\r
                return result;\r
-       }       \r
-\r
-    static Object extractPart(MultipartInput input, String label, Class clazz) throws Exception {\r
-        Object obj = null;\r
-        for(InputPart part : input.getParts()){\r
-            String partLabel = part.getHeaders().getFirst("label");\r
-            if(label.equalsIgnoreCase(partLabel)){\r
-                String partStr = part.getBodyAsString();\r
-                obj = part.getBody(clazz, null);\r
-                break;\r
-            }\r
-        }\r
-        return obj;\r
-    }\r
-       \r
+       }\r
+\r
+       /**\r
+        * Extract part.\r
+        * \r
+        * @param input\r
+        *            the input\r
+        * @param label\r
+        *            the label\r
+        * @param clazz\r
+        *            the clazz\r
+        * \r
+        * @return the object\r
+        * \r
+        * @throws Exception\r
+        *             the exception\r
+        */\r
+       static Object extractPart(MultipartInput input, String label, Class clazz) {\r
+               Object obj = null;\r
+               \r
+               try {\r
+                       for (InputPart part : input.getParts()) {\r
+                               String partLabel = part.getHeaders().getFirst("label");\r
+                               if (label.equalsIgnoreCase(partLabel)) {\r
+                                       String partStr = part.getBodyAsString();\r
+                                       obj = part.getBody(clazz, null);\r
+                                       break;\r
+                               }\r
+                       }\r
+               } catch (Exception e) {\r
+                       e.printStackTrace();\r
+               }\r
+\r
+               return obj;\r
+       }\r
+\r
 }\r