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
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
// 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
// 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
// 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
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
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
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
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
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
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