]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-610: Added delete methods to Vocabulary service sample client code.
authorAron Roberts <aron@socrates.berkeley.edu>
Wed, 25 Nov 2009 04:14:01 +0000 (04:14 +0000)
committerAron Roberts <aron@socrates.berkeley.edu>
Wed, 25 Nov 2009 04:14:01 +0000 (04:14 +0000)
services/vocabulary/sample/sample/src/main/java/org/collectionspace/services/vocabulary/client/sample/Sample.java

index 0ea5fea498fe01264ede3f985226e9e859190c96..b93872ec1bcd07edd6a26cce8bdd3e7a450491dd 100644 (file)
@@ -36,12 +36,12 @@ import org.collectionspace.services.client.VocabularyClient;
 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.MultipartInput;\r
 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
+import org.jboss.resteasy.plugins.providers.multipart.InputPart;\r
 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
@@ -63,7 +63,9 @@ public class Sample {
     final String ITEM_SERVICE_PATH_COMPONENT = "items";\r
 \r
 \r
+    // ---------------------------------------------------------------\r
     // Create\r
+    // ---------------------------------------------------------------\r
 \r
     public void createEnumeration(String vocabName, List<String> enumValues ) {\r
 \r
@@ -126,7 +128,10 @@ public class Sample {
        return extractId(res);\r
     }\r
 \r
+\r
+   // ---------------------------------------------------------------\r
    // Read\r
+   // ---------------------------------------------------------------\r
 \r
    private VocabulariesCommonList readVocabularies() {\r
 \r
@@ -150,22 +155,49 @@ public class Sample {
        }\r
 \r
         return list;\r
+   }\r
 \r
-    }\r
-/*\r
-    private List<String> readVocabularyIds() {\r
-\r
-        VocabulariesCommonList items = readVocabularies();\r
+    private List<String> readVocabularyIds(VocabulariesCommonList list) {\r
 \r
-        List<String> ids;\r
-        for (VocabulariesCommonList.VocabularyListItem item : items) {\r
-            ids.add(item.getCsid());\r
+        List<String> ids = new ArrayList<String>();\r
+        List<VocabulariesCommonList.VocabularyListItem> vocabularies =\r
+            list.getVocabularyListItem();\r
+        for (VocabulariesCommonList.VocabularyListItem vocabulary : vocabularies) {\r
+            ids.add(vocabulary.getCsid());\r
         }\r
+        return ids;\r
+   }\r
+    \r
+   private VocabulariesCommon readVocabulary(String vocabId) {\r
 \r
-        return list;\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
+        VocabulariesCommon vocabulary = null;\r
+        try {\r
+            ClientResponse<MultipartInput> res = client.read(vocabId);\r
+            int statusCode = res.getStatus();\r
+            if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
+                throw new RuntimeException("Could not read vocabulary"\r
+                    + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+            }\r
+            if(statusCode != EXPECTED_STATUS_CODE) {\r
+                throw new RuntimeException("Unexpected Status when reading " +\r
+                    "vocabulary, Status:"+ statusCode);\r
+            }\r
+            MultipartInput input = (MultipartInput) res.getEntity();\r
+            vocabulary = (VocabulariesCommon) extractPart(input,\r
+                    client.getCommonPartName(), VocabulariesCommon.class);\r
+        } catch (Exception e) {\r
+            throw new RuntimeException("Could not read vocabulary: ", e);\r
+        }\r
 \r
+        return vocabulary;\r
     }\r
-*/\r
+\r
     private VocabularyitemsCommonList readItemsInVocab(String vocabId) {\r
 \r
         // Expected status code: 200 OK\r
@@ -190,7 +222,73 @@ public class Sample {
        }\r
 \r
         return list;\r
+    }\r
+\r
+    private List<String> readVocabularyItemIds(VocabularyitemsCommonList list) {\r
+\r
+        List<String> ids = new ArrayList<String>();\r
+        List<VocabularyitemsCommonList.VocabularyitemListItem> items =\r
+            list.getVocabularyitemListItem();\r
+        for (VocabularyitemsCommonList.VocabularyitemListItem item : items) {\r
+            ids.add(item.getCsid());\r
+        }\r
+        return ids;\r
+   }\r
+\r
+    // ---------------------------------------------------------------\r
+    // Delete\r
+    // ---------------------------------------------------------------\r
+\r
+    private void deleteVocabulary(String vcsid) {\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.DELETE;\r
+\r
+        ClientResponse<Response> res = client.delete(vcsid);\r
+        int statusCode = res.getStatus();\r
+\r
+       if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
+               throw new RuntimeException("Could not delete vocabulary: "\r
+                + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+       }\r
+       if(statusCode != EXPECTED_STATUS_CODE) {\r
+               throw new RuntimeException("Unexpected Status when deleting " +\r
+                "vocabulary, Status:"+ statusCode);\r
+       }\r
+    }\r
+\r
+    private void deleteAllVocabularies() {\r
+        List<String> ids = readVocabularyIds(readVocabularies());\r
+        for (String id : ids) {\r
+            deleteVocabulary(id);\r
+        }\r
+    }\r
 \r
+        private void deleteVocabularyItem(String vcsid, String itemcsid) {\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.DELETE;\r
+\r
+        ClientResponse<Response> res = client.deleteItem(vcsid, itemcsid);\r
+        int statusCode = res.getStatus();\r
+\r
+       if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
+               throw new RuntimeException("Could not delete vocabulary item: "\r
+                + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+       }\r
+       if(statusCode != EXPECTED_STATUS_CODE) {\r
+               throw new RuntimeException("Unexpected Status when deleting " +\r
+                "vocabulary item, Status:"+ statusCode);\r
+       }\r
+    }\r
+\r
+    private void deleteAllItemsForVocab(String vocabId) {\r
+        List<String> itemIds = readVocabularyItemIds(readItemsInVocab(vocabId));\r
+        for (String itemId : itemIds) {\r
+            deleteVocabularyItem(vocabId, itemId);\r
+        }\r
     }\r
 \r
     // ---------------------------------------------------------------\r
@@ -234,36 +332,37 @@ public class Sample {
 \r
     // Retrieve individual fields of vocabulary records.\r
 \r
-    private String displayVocabularyListDetails(VocabulariesCommonList list) {\r
+    private String displayAllVocabularies(VocabulariesCommonList list) {\r
         StringBuffer sb = new StringBuffer();\r
-            List<VocabulariesCommonList.VocabularyListItem> items =\r
+            List<VocabulariesCommonList.VocabularyListItem> vocabularies =\r
                     list.getVocabularyListItem();\r
             int i = 0;\r
-        for (VocabulariesCommonList.VocabularyListItem item : items) {\r
+        for (VocabulariesCommonList.VocabularyListItem vocabulary : vocabularies) {\r
             sb.append("vocabulary [" + i + "]" + "\n");\r
-            sb.append(displayVocabularyDetails(item));\r
+            sb.append(displayVocabularyDetails(vocabulary));\r
             i++;\r
         }\r
         return sb.toString();\r
     }\r
 \r
     private String displayVocabularyDetails(\r
-        VocabulariesCommonList.VocabularyListItem item) {\r
+        VocabulariesCommonList.VocabularyListItem vocabulary) {\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
+            sb.append("displayName=" + vocabulary.getDisplayName() + "\n");\r
+            sb.append("vocabType=" + vocabulary.getVocabType() + "\n");\r
+            // sb.append("csid=" + vocabulary.getCsid() + "\n");\r
+            sb.append("URI=" + vocabulary.getUri() + "\n");\r
         return sb.toString();\r
     }\r
 \r
     // Retrieve individual fields of vocabulary item records.\r
 \r
-    private String displayVocabularyItemListDetails(VocabularyitemsCommonList list) {\r
+    private String displayAllVocabularyItems(VocabularyitemsCommonList list) {\r
         StringBuffer sb = new StringBuffer();\r
-        List<VocabularyitemListItem> items =\r
+        List<VocabularyitemsCommonList.VocabularyitemListItem> items =\r
                 list.getVocabularyitemListItem();\r
         int i = 0;\r
-        for (VocabularyitemListItem item : items) {\r
+        for (VocabularyitemsCommonList.VocabularyitemListItem item : items) {\r
             sb.append("vocabulary item [" + i + "]" + "\n");\r
             sb.append(displayVocabularyItemDetails(item));\r
             i++;\r
@@ -276,10 +375,30 @@ public class Sample {
             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
+            // sb.append("URI=" + item.getUri() + "\n");\r
         return sb.toString();\r
     }\r
 \r
+    private Object extractPart(MultipartInput input, String label,\r
+        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
+                if(logger.isDebugEnabled()){\r
+                    logger.debug("extracted part str=\n" + partStr);\r
+                }\r
+                obj = part.getBody(clazz, null);\r
+                if(logger.isDebugEnabled()){\r
+                    logger.debug("extracted part obj=\n", obj, clazz);\r
+                }\r
+                break;\r
+            }\r
+        }\r
+        return obj;\r
+    }\r
+\r
     /**\r
      * Returns an error message indicating that the status code returned by a\r
      * specific call to a service does not fall within a set of valid status\r
@@ -320,8 +439,45 @@ public class Sample {
                BasicConfigurator.configure();\r
                logger.info("VocabularyBaseImport starting...");\r
 \r
-\r
                Sample vbi = new Sample();\r
+\r
+        logger.info("Deleting all vocabulary items and vocabularies ...");\r
+\r
+        // For each vocabulary ...\r
+\r
+        VocabulariesCommonList vocabularies = vbi.readVocabularies();\r
+        List<String> vocabIds = vbi.readVocabularyIds(vocabularies);\r
+        for (String vocabId : vocabIds) {\r
+            // Delete vocabulary items for this vocabulary.\r
+            logger.info("Deleting vocabulary items ...");\r
+            vbi.deleteAllItemsForVocab(vocabId);\r
+            // Delete vocabularies.\r
+            logger.info("Deleting vocabulary ...");\r
+            vbi.deleteVocabulary(vocabId);\r
+        }\r
+\r
+        // Read vocabularies after deletion.\r
+        logger.info("Reading vocabularies after deletion ...");\r
+\r
+        String details = "";\r
+\r
+        vocabularies = vbi.readVocabularies();\r
+        details = vbi.displayAllVocabularies(vocabularies);\r
+        logger.info(details);\r
+\r
+        logger.info("Reading items in each vocabulary after deletion ...");\r
+\r
+        // Read items in each vocabulary.\r
+        vocabIds = vbi.readVocabularyIds(vocabularies);\r
+        for (String vocabId : vocabIds) {\r
+            VocabularyitemsCommonList items = vbi.readItemsInVocab(vocabId);\r
+            details = vbi.displayAllVocabularyItems(items);\r
+            logger.info(details);\r
+        }\r
+\r
+\r
+        // Create new vocabularies, each populated with vocabulary items.\r
+\r
                final String acquisitionMethodsVocabName = "Acquisition Methods";\r
                final String entryMethodsVocabName = "Entry Methods";\r
                final String entryReasonsVocabName = "Entry Reasons";\r
@@ -348,17 +504,22 @@ public class Sample {
 \r
         logger.info("Reading newly-created vocabularies ...");\r
 \r
-        // Read vocabularies.\r
-        VocabulariesCommonList vocabularies = vbi.readVocabularies();\r
-        String details = vbi.displayVocabularyListDetails(vocabularies);\r
+        // Read vocabularies after import.\r
+        vocabularies = vbi.readVocabularies();\r
+        details = vbi.displayAllVocabularies(vocabularies);\r
         logger.info(details);\r
 \r
-        // Read items in each vocabulary.\r
-//        readItemsInVocab(newVocabId);\r
+        logger.info("Reading items in each vocabulary ...");\r
 \r
-        // Delete vocabulary items.\r
+        // Read items in each vocabulary after import.\r
+        vocabIds = vbi.readVocabularyIds(vocabularies);\r
+        for (String vocabId : vocabIds) {\r
+            VocabularyitemsCommonList items = vbi.readItemsInVocab(vocabId);\r
+            details = vbi.displayAllVocabularyItems(items);\r
+            logger.info(details);\r
+        }\r
+        \r
 \r
-        // Delete vocabularies.\r
        }\r
 \r
 }\r