]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-610: Added read methods to Vocabulary service sample client code.
authorAron Roberts <aron@socrates.berkeley.edu>
Wed, 25 Nov 2009 01:16:43 +0000 (01:16 +0000)
committerAron Roberts <aron@socrates.berkeley.edu>
Wed, 25 Nov 2009 01:16:43 +0000 (01:16 +0000)
services/vocabulary/sample/sample/src/main/java/org/collectionspace/services/vocabulary/client/sample/Sample.java
services/vocabulary/sample/sample/src/main/resources/collectionspace-client.properties
services/vocabulary/sample/sample/src/main/resources/log4j.xml [new file with mode: 0644]

index ca54c60785d5d66a693ddce3fe1c64e89e473fcf..0ea5fea498fe01264ede3f985226e9e859190c96 100644 (file)
@@ -35,7 +35,11 @@ import org.apache.log4j.BasicConfigurator;
 import org.collectionspace.services.client.VocabularyClient;\r
 import org.collectionspace.services.client.test.ServiceRequestType;\r
 import org.collectionspace.services.vocabulary.VocabulariesCommon;\r
+import org.collectionspace.services.vocabulary.VocabulariesCommonList;\r
+import org.collectionspace.services.vocabulary.VocabulariesCommonList.VocabularyListItem;\r
 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;\r
+import org.collectionspace.services.vocabulary.VocabularyitemsCommonList;\r
+import org.collectionspace.services.vocabulary.VocabularyitemsCommonList.VocabularyitemListItem;\r
 import org.jboss.resteasy.client.ClientResponse;\r
 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
@@ -58,6 +62,9 @@ public class Sample {
     final String SERVICE_PATH_COMPONENT = "vocabularies";\r
     final String ITEM_SERVICE_PATH_COMPONENT = "items";\r
 \r
+\r
+    // Create\r
+\r
     public void createEnumeration(String vocabName, List<String> enumValues ) {\r
 \r
        // Expected status code: 201 Created\r
@@ -65,9 +72,7 @@ public class Sample {
        // Type of service request being tested\r
        ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
 \r
-       if(logger.isDebugEnabled()){\r
-               logger.debug("Import: Create vocabulary: \"" + vocabName +"\"");\r
-       }\r
+       logger.info("Import: Create vocabulary: \"" + vocabName +"\"");\r
        MultipartOutput multipart = createVocabularyInstance(vocabName, \r
                        createRefName(vocabName), "enum");\r
        ClientResponse<Response> res = client.create(multipart);\r
@@ -86,13 +91,14 @@ public class Sample {
        // Store the ID returned from this create operation\r
        // for additional tests below.\r
        String newVocabId = extractId(res);\r
-       if(logger.isDebugEnabled()){\r
-               logger.debug("Import: Created vocabulary: \"" + vocabName +"\" ID:"\r
+        logger.info("Import: Created vocabulary: \"" + vocabName +"\" ID:"\r
                                +newVocabId );\r
-       }\r
+        \r
+        // Add items to the vocabulary\r
        for(String itemName : enumValues){\r
                createItemInVocab(newVocabId, vocabName, itemName, createRefName(itemName));\r
        }\r
+        \r
     }\r
     \r
     private String createItemInVocab(String vcsid, String vocabName, String itemName, String refName) {\r
@@ -101,9 +107,7 @@ public class Sample {
        // Type of service request being tested\r
        ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
 \r
-       if(logger.isDebugEnabled()){\r
-               logger.debug("Import: Create Item: \""+itemName+"\" in vocabulary: \"" + vocabName +"\"");\r
-       }\r
+       logger.info("Import: Create Item: \""+itemName+"\" in vocabulary: \"" + vocabName +"\"");\r
        MultipartOutput multipart = createVocabularyItemInstance(vcsid, itemName, refName);\r
        ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
 \r
@@ -122,6 +126,73 @@ public class Sample {
        return extractId(res);\r
     }\r
 \r
+   // Read\r
+\r
+   private VocabulariesCommonList readVocabularies() {\r
+\r
+        // Expected status code: 200 OK\r
+       int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();\r
+       // Type of service request being tested\r
+       ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;\r
+\r
+        // Submit the request to the service and store the response.\r
+        ClientResponse<VocabulariesCommonList> res = client.readList();\r
+        VocabulariesCommonList list = res.getEntity();\r
+\r
+        int statusCode = res.getStatus();\r
+       if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
+               throw new RuntimeException("Could not read list of vocabularies: "\r
+                + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+       }\r
+       if(statusCode != EXPECTED_STATUS_CODE) {\r
+               throw new RuntimeException("Unexpected Status when reading " +\r
+                "list of vocabularies, Status:"+ statusCode);\r
+       }\r
+\r
+        return list;\r
+\r
+    }\r
+/*\r
+    private List<String> readVocabularyIds() {\r
+\r
+        VocabulariesCommonList items = readVocabularies();\r
+\r
+        List<String> ids;\r
+        for (VocabulariesCommonList.VocabularyListItem item : items) {\r
+            ids.add(item.getCsid());\r
+        }\r
+\r
+        return list;\r
+\r
+    }\r
+*/\r
+    private VocabularyitemsCommonList readItemsInVocab(String vocabId) {\r
+\r
+        // Expected status code: 200 OK\r
+       int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();\r
+       // Type of service request being tested\r
+       ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;\r
+\r
+        // Submit the request to the service and store the response.\r
+        ClientResponse<VocabularyitemsCommonList> res =\r
+                client.readItemList(vocabId);\r
+        VocabularyitemsCommonList list = res.getEntity();\r
+\r
+        int statusCode = res.getStatus();\r
+\r
+       if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
+               throw new RuntimeException("Could not read items in vocabulary: "\r
+                + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+       }\r
+       if(statusCode != EXPECTED_STATUS_CODE) {\r
+               throw new RuntimeException("Unexpected Status when reading " +\r
+                "items in vocabulary, Status:"+ statusCode);\r
+       }\r
+\r
+        return list;\r
+\r
+    }\r
+\r
     // ---------------------------------------------------------------\r
     // Utility methods used by tests above\r
     // ---------------------------------------------------------------\r
@@ -137,7 +208,7 @@ public class Sample {
         commonPart.getHeaders().add("label", client.getCommonPartName());\r
 \r
         if(logger.isDebugEnabled()){\r
-               logger.debug("to be created, vocabulary common ", \r
+               logger.debug("to be created, vocabulary common ",\r
                                        vocabulary, VocabulariesCommon.class);\r
         }\r
 \r
@@ -161,6 +232,53 @@ public class Sample {
         return multipart;\r
     }\r
 \r
+    // Retrieve individual fields of vocabulary records.\r
+\r
+    private String displayVocabularyListDetails(VocabulariesCommonList list) {\r
+        StringBuffer sb = new StringBuffer();\r
+            List<VocabulariesCommonList.VocabularyListItem> items =\r
+                    list.getVocabularyListItem();\r
+            int i = 0;\r
+        for (VocabulariesCommonList.VocabularyListItem item : items) {\r
+            sb.append("vocabulary [" + i + "]" + "\n");\r
+            sb.append(displayVocabularyDetails(item));\r
+            i++;\r
+        }\r
+        return sb.toString();\r
+    }\r
+\r
+    private String displayVocabularyDetails(\r
+        VocabulariesCommonList.VocabularyListItem item) {\r
+            StringBuffer sb = new StringBuffer();\r
+            sb.append("csid=" + item.getCsid() + "\n");\r
+            sb.append("displayName=" + item.getDisplayName() + "\n");\r
+            sb.append("URI=" + item.getUri() + "\n");\r
+        return sb.toString();\r
+    }\r
+\r
+    // Retrieve individual fields of vocabulary item records.\r
+\r
+    private String displayVocabularyItemListDetails(VocabularyitemsCommonList list) {\r
+        StringBuffer sb = new StringBuffer();\r
+        List<VocabularyitemListItem> items =\r
+                list.getVocabularyitemListItem();\r
+        int i = 0;\r
+        for (VocabularyitemListItem item : items) {\r
+            sb.append("vocabulary item [" + i + "]" + "\n");\r
+            sb.append(displayVocabularyItemDetails(item));\r
+            i++;\r
+        }\r
+        return sb.toString();\r
+    }\r
+\r
+    private String displayVocabularyItemDetails(\r
+        VocabularyitemsCommonList.VocabularyitemListItem item) {\r
+            StringBuffer sb = new StringBuffer();\r
+            sb.append("csid=" + item.getCsid() + "\n");\r
+            sb.append("displayName=" + item.getDisplayName() + "\n");\r
+            sb.append("URI=" + item.getUri() + "\n");\r
+        return sb.toString();\r
+    }\r
 \r
     /**\r
      * Returns an error message indicating that the status code returned by a\r
@@ -183,7 +301,7 @@ public class Sample {
         MultivaluedMap<String, Object> mvm = res.getMetadata();\r
         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);\r
         if(logger.isDebugEnabled()){\r
-               logger.debug("extractId:uri=" + uri);\r
+               logger.info("extractId:uri=" + uri);\r
         }\r
         String[] segments = uri.split("/");\r
         String id = segments[segments.length - 1];\r
@@ -202,6 +320,7 @@ public class Sample {
                BasicConfigurator.configure();\r
                logger.info("VocabularyBaseImport starting...");\r
 \r
+\r
                Sample vbi = new Sample();\r
                final String acquisitionMethodsVocabName = "Acquisition Methods";\r
                final String entryMethodsVocabName = "Entry Methods";\r
@@ -226,5 +345,20 @@ public class Sample {
                vbi.createEnumeration(responsibleDeptsVocabName, respDeptNamesEnumValues);\r
 \r
                logger.info("VocabularyBaseImport complete.");\r
+\r
+        logger.info("Reading newly-created vocabularies ...");\r
+\r
+        // Read vocabularies.\r
+        VocabulariesCommonList vocabularies = vbi.readVocabularies();\r
+        String details = vbi.displayVocabularyListDetails(vocabularies);\r
+        logger.info(details);\r
+\r
+        // Read items in each vocabulary.\r
+//        readItemsInVocab(newVocabId);\r
+\r
+        // Delete vocabulary items.\r
+\r
+        // Delete vocabularies.\r
        }\r
+\r
 }\r
index 429b9f6fe2fb8a87a0dab86aabfdc482b881a740..e4f17a8e45699db99f448c4af39a96110e10dea4 100644 (file)
@@ -1,5 +1,5 @@
 #url of the collectionspace server\r
-cspace.url=http://localhost:8180/cspace-services/\r
+cspace.url=http://173.45.237.99:8180/cspace-services/\r
 cspace.ssl=false\r
 cspace.auth=false\r
 cspace.user=test\r
diff --git a/services/vocabulary/sample/sample/src/main/resources/log4j.xml b/services/vocabulary/sample/sample/src/main/resources/log4j.xml
new file mode 100644 (file)
index 0000000..e5241bc
--- /dev/null
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
+
+       <appender name="console" class="org.apache.log4j.ConsoleAppender">
+               <param name="Target" value="System.out" />
+               <layout class="org.apache.log4j.TTCCLayout">
+                       <param name="DateFormat" value="ISO8601" />
+               </layout>
+       </appender>
+
+       <logger name="org.apache.commons.httpclient" additivity="false">
+               <level value="warn" />
+               <appender-ref ref="console" />
+       </logger>
+
+       <logger name="httpclient.wire" additivity="false">
+               <level value="info" />
+               <appender-ref ref="console" />
+       </logger>
+
+<!--
+       <logger name="org.collectionspace.services.vocabulary.client.sample.Sample" additivity="false">
+               <level value="debug" />
+               <appender-ref ref="console" />
+       </logger>
+-->
+
+       <root>
+               <priority value="debug" />
+               <appender-ref ref="console" />
+       </root>
+
+</log4j:configuration>
+
+
+
+