--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>\r
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">\r
+ <modelVersion>4.0.0</modelVersion>\r
+ <groupId>org.collectionspace.services</groupId>\r
+ <artifactId>org.collectionspace.services.vocabulary.client.sample</artifactId>\r
+ <packaging>jar</packaging>\r
+ <version>1.0</version>\r
+ <name>vocabulary.client.sample</name>\r
+ \r
+ <dependencies>\r
+ <dependency>\r
+ <groupId>org.collectionspace.services</groupId>\r
+ <artifactId>org.collectionspace.services.vocabulary.jaxb</artifactId>\r
+ <version>1.0</version>\r
+ </dependency>\r
+ <dependency>\r
+ <groupId>org.collectionspace.services</groupId>\r
+ <artifactId>org.collectionspace.services.client</artifactId>\r
+ <version>1.0</version>\r
+ </dependency>\r
+ <dependency>\r
+ <groupId>org.collectionspace.services</groupId>\r
+ <artifactId>org.collectionspace.services.vocabulary.client</artifactId>\r
+ <version>1.0</version>\r
+ </dependency>\r
+ </dependencies>\r
+ \r
+ <repositories>\r
+ <repository>\r
+ <id>libs-releases-local</id>\r
+ <name>libs-releases-local</name>\r
+ <url>http://source.collectionspace.org:8081/artifactory/libs-releases-local</url>\r
+ <releases>\r
+ <enabled>true</enabled>\r
+ </releases>\r
+ <snapshots>\r
+ <enabled>false</enabled>\r
+ </snapshots> \r
+ </repository>\r
+ \r
+ <repository>\r
+ <id>libs-snapshots-local</id>\r
+ <name>libs-snapshots-local</name>\r
+ <url>http://source.collectionspace.org:8081/artifactory/libs-snapshots-local</url>\r
+ <releases>\r
+ <enabled>false</enabled>\r
+ </releases>\r
+ <snapshots>\r
+ <enabled>true</enabled>\r
+ </snapshots> \r
+ </repository>\r
+ <repository>\r
+ <id>collectionspace-releases</id>\r
+ <name>collectionspace-releases</name>\r
+ <url>http://source.collectionspace.org:8081/artifactory/public-nuxeo</url> \r
+ <releases>\r
+ <enabled>true</enabled>\r
+ </releases>\r
+ <snapshots>\r
+ <enabled>false</enabled>\r
+ </snapshots> \r
+ </repository>\r
+ <repository>\r
+ <id>collectionspace-snapshots</id>\r
+ <name>collectionspace-snapshots</name>\r
+ <url>http://source.collectionspace.org:8081/artifactory/public-snapshot-nuxeo</url>\r
+ <releases>\r
+ <enabled>false</enabled>\r
+ </releases>\r
+ <snapshots>\r
+ <enabled>true</enabled>\r
+ </snapshots>\r
+ </repository>\r
+ </repositories> \r
+ \r
+ <build>\r
+ <finalName>vocabulary-client-sample</finalName>\r
+ <plugins>\r
+ <plugin>\r
+ <groupId>org.apache.maven.plugins</groupId>\r
+ <artifactId>maven-compiler-plugin</artifactId>\r
+ <configuration>\r
+ <source>1.6</source>\r
+ <target>1.6</target>\r
+ </configuration>\r
+ </plugin>\r
+ </plugins>\r
+ </build>\r
+</project>\r
+\r
--- /dev/null
+/**\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 (c)) 2009 Regents of the University of California\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
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.collectionspace.services.vocabulary.client.sample;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.List;\r
+\r
+import javax.ws.rs.core.MediaType;\r
+import javax.ws.rs.core.MultivaluedMap;\r
+import javax.ws.rs.core.Response;\r
+\r
+import org.apache.log4j.BasicConfigurator;\r
+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.VocabularyitemsCommon;\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
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+/**\r
+ * VocabularyServiceTest, carries out tests against a\r
+ * deployed and running Vocabulary Service.\r
+ *\r
+ * $LastChangedRevision: 753 $\r
+ * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $\r
+ */\r
+public class Sample {\r
+ private static final Logger logger =\r
+ LoggerFactory.getLogger(Sample.class);\r
+\r
+ // Instance variables specific to this test.\r
+ private VocabularyClient client = new VocabularyClient();\r
+ final String SERVICE_PATH_COMPONENT = "vocabularies";\r
+ final String ITEM_SERVICE_PATH_COMPONENT = "items";\r
+\r
+ public void createEnumeration(String vocabName, List<String> enumValues ) {\r
+\r
+ // Expected status code: 201 Created\r
+ int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\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
+ MultipartOutput multipart = createVocabularyInstance(vocabName, \r
+ createRefName(vocabName), "enum");\r
+ ClientResponse<Response> res = client.create(multipart);\r
+\r
+ int statusCode = res.getStatus();\r
+\r
+ if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
+ throw new RuntimeException("Could not create enumeration: \""+vocabName\r
+ +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+ }\r
+ if(statusCode != EXPECTED_STATUS_CODE) {\r
+ throw new RuntimeException("Unexpected Status when creating enumeration: \""\r
+ +vocabName +"\", Status:"+ statusCode);\r
+ }\r
+\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
+ +newVocabId );\r
+ }\r
+ for(String itemName : enumValues){\r
+ createItemInVocab(newVocabId, vocabName, itemName, createRefName(itemName));\r
+ }\r
+ }\r
+ \r
+ private String createItemInVocab(String vcsid, String vocabName, String itemName, String refName) {\r
+ // Expected status code: 201 Created\r
+ int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\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
+ MultipartOutput multipart = createVocabularyItemInstance(vcsid, itemName, refName);\r
+ ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
+\r
+ int statusCode = res.getStatus();\r
+\r
+ if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
+ throw new RuntimeException("Could not create Item: \""+itemName\r
+ +"\" in vocabulary: \"" + vocabName\r
+ +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+ }\r
+ if(statusCode != EXPECTED_STATUS_CODE) {\r
+ throw new RuntimeException("Unexpected Status when creating Item: \""+itemName\r
+ +"\" in vocabulary: \"" + vocabName +"\", Status:"+ statusCode);\r
+ }\r
+\r
+ return extractId(res);\r
+ }\r
+\r
+ // ---------------------------------------------------------------\r
+ // Utility methods used by tests above\r
+ // ---------------------------------------------------------------\r
+\r
+ private MultipartOutput createVocabularyInstance(\r
+ String displayName, String refName, String vocabType) {\r
+ VocabulariesCommon vocabulary = new VocabulariesCommon();\r
+ vocabulary.setDisplayName(displayName);\r
+ vocabulary.setRefName(refName);\r
+ vocabulary.setVocabType(vocabType);\r
+ MultipartOutput multipart = new MultipartOutput();\r
+ OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);\r
+ commonPart.getHeaders().add("label", client.getCommonPartName());\r
+\r
+ if(logger.isDebugEnabled()){\r
+ logger.debug("to be created, vocabulary common ", \r
+ vocabulary, VocabulariesCommon.class);\r
+ }\r
+\r
+ return multipart;\r
+ }\r
+\r
+ private MultipartOutput createVocabularyItemInstance(\r
+ String inVocabulary, String displayName, String refName) {\r
+ VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();\r
+ vocabularyItem.setInVocabulary(inVocabulary);\r
+ vocabularyItem.setDisplayName(displayName);\r
+ vocabularyItem.setRefName(refName);\r
+ MultipartOutput multipart = new MultipartOutput();\r
+ OutputPart commonPart = multipart.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);\r
+ commonPart.getHeaders().add("label", client.getItemCommonPartName());\r
+\r
+ if(logger.isDebugEnabled()){\r
+ logger.debug("to be created, vocabularyitem common ", vocabularyItem, VocabularyitemsCommon.class);\r
+ }\r
+\r
+ return multipart;\r
+ }\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
+ * codes for that service.\r
+ *\r
+ * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).\r
+ *\r
+ * @param statusCode The invalid status code that was returned in the response,\r
+ * from submitting that type of request to the service.\r
+ *\r
+ * @return An error message.\r
+ */\r
+ protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {\r
+ return "Status code '" + statusCode + "' in response is NOT within the expected set: " +\r
+ requestType.validStatusCodesAsString();\r
+ }\r
+\r
+ protected String extractId(ClientResponse<Response> res) {\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
+ }\r
+ String[] segments = uri.split("/");\r
+ String id = segments[segments.length - 1];\r
+ if(logger.isDebugEnabled()){\r
+ logger.debug("id=" + id);\r
+ }\r
+ return id;\r
+ }\r
+ \r
+ protected String createRefName(String displayName) {\r
+ return displayName.replaceAll("\\W", "");\r
+ }\r
+\r
+ public static void main(String[] args) {\r
+ \r
+ BasicConfigurator.configure();\r
+ logger.info("VocabularyBaseImport starting...");\r
+\r
+ Sample vbi = new Sample();\r
+ final String acquisitionMethodsVocabName = "Acquisition Methods";\r
+ final String entryMethodsVocabName = "Entry Methods";\r
+ final String entryReasonsVocabName = "Entry Reasons";\r
+ final String responsibleDeptsVocabName = "Responsible Departments";\r
+\r
+ List<String> acquisitionMethodsEnumValues = \r
+ Arrays.asList("Gift","Purchase","Exchange","Transfer","Treasure");\r
+ List<String> entryMethodsEnumValues = \r
+ Arrays.asList("In person","Post","Found on doorstep");\r
+ List<String> entryReasonsEnumValues = \r
+ Arrays.asList("Enquiry","Commission","Loan");\r
+ List<String> respDeptNamesEnumValues = \r
+ Arrays.asList("Antiquities","Architecture and Design","Decorative Arts",\r
+ "Ethnography","Herpetology","Media and Performance Art",\r
+ "Paintings and Sculpture","Paleobotany","Photographs",\r
+ "Prints and Drawings");\r
+\r
+ vbi.createEnumeration(acquisitionMethodsVocabName, acquisitionMethodsEnumValues);\r
+ vbi.createEnumeration(entryMethodsVocabName, entryMethodsEnumValues);\r
+ vbi.createEnumeration(entryReasonsVocabName, entryReasonsEnumValues);\r
+ vbi.createEnumeration(responsibleDeptsVocabName, respDeptNamesEnumValues);\r
+\r
+ logger.info("VocabularyBaseImport complete.");\r
+ }\r
+}\r