+++ /dev/null
-/**
- * This document is a part of the source code and related artifacts
- * for CollectionSpace, an open source collections management system
- * for museums and related institutions:
- *
- * http://www.collectionspace.org
- * http://wiki.collectionspace.org
- *
- * Copyright (c)) 2009 Regents of the University of California
- *
- * Licensed under the Educational Community License (ECL), Version 2.0.
- * You may not use this file except in compliance with this License.
- *
- * You may obtain a copy of the ECL 2.0 License at
- * https://source.collectionspace.org/collection-space/LICENSE.txt
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.collectionspace.services.person.importer;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.core.Response;
-
-import org.apache.log4j.BasicConfigurator;
-import org.collectionspace.services.PersonJAXBSchema;
-import org.collectionspace.services.client.PersonAuthorityClient;
-import org.collectionspace.services.client.test.ServiceRequestType;
-import org.collectionspace.services.person.PersonauthoritiesCommon;
-import org.collectionspace.services.person.PersonsCommon;
-import org.jboss.resteasy.client.ClientResponse;
-import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
-import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * PersonAuthorityServiceTest, carries out tests against a
- * deployed and running PersonAuthority Service.
- *
- * $LastChangedRevision: 753 $
- * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
- */
-public class PersonAuthorityBaseImport {
- private static final Logger logger =
- LoggerFactory.getLogger(PersonAuthorityBaseImport.class);
-
- // Instance variables specific to this test.
- private PersonAuthorityClient client = new PersonAuthorityClient();
- final String SERVICE_PATH_COMPONENT = "personauthorities";
- final String ITEM_SERVICE_PATH_COMPONENT = "items";
-
- public void createPersonAuthority(String personAuthorityName,
- List<Map<String, String>> personMaps ) {
-
- // Expected status code: 201 Created
- int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
- // Type of service request being tested
- ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
-
- if(logger.isDebugEnabled()){
- logger.debug("Import: Create personAuthority: \"" + personAuthorityName +"\"");
- }
- String basePersonRefName = createPersonAuthRefName(personAuthorityName);
- String fullPersonRefName = basePersonRefName+"'"+personAuthorityName+"'";
- MultipartOutput multipart = createPersonAuthorityInstance(personAuthorityName,
- fullPersonRefName);
- ClientResponse<Response> res = client.create(multipart);
-
- int statusCode = res.getStatus();
-
- if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
- throw new RuntimeException("Could not create enumeration: \""+personAuthorityName
- +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
- }
- if(statusCode != EXPECTED_STATUS_CODE) {
- throw new RuntimeException("Unexpected Status when creating enumeration: \""
- +personAuthorityName +"\", Status:"+ statusCode);
- }
-
- // Store the ID returned from this create operation
- // for additional tests below.
- String newPersonAuthorityId = extractId(res);
- if(logger.isDebugEnabled()){
- logger.debug("Import: Created personAuthorityulary: \"" + personAuthorityName +"\" ID:"
- +newPersonAuthorityId );
- }
- for(Map<String,String> personMap : personMaps){
- createItemInAuthority(newPersonAuthorityId, basePersonRefName, personMap);
- }
- }
-
- private String createItemInAuthority(String vcsid,
- String personAuthorityRefName, Map<String,String> personMap) {
- // Expected status code: 201 Created
- int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
- // Type of service request being tested
- ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
- String foreName = personMap.get(PersonJAXBSchema.FORE_NAME);
- String middleName = personMap.get(PersonJAXBSchema.MIDDLE_NAME);
- String surName = personMap.get(PersonJAXBSchema.SUR_NAME);
- String birthDate = personMap.get(PersonJAXBSchema.BIRTH_DATE);
- String deathDate = personMap.get(PersonJAXBSchema.DEATH_DATE);
- StringBuilder builtName = new StringBuilder();
- if(foreName!=null)
- builtName.append(foreName);
- if(middleName!=null)
- builtName.append(middleName);
- if(surName!=null)
- builtName.append(surName);
- if(birthDate!=null)
- builtName.append(birthDate);
- builtName.append("-");
- if(deathDate!=null)
- builtName.append(deathDate);
- String refName = createPersonRefName(personAuthorityRefName, builtName.toString());
-
- if(logger.isDebugEnabled()){
- logger.debug("Import: Create Item: \""+builtName.toString()
- +"\" in personAuthorityulary: \"" + personAuthorityRefName +"\"");
- }
- MultipartOutput multipart = createPersonInstance( vcsid, refName,
- personMap );
- ClientResponse<Response> res = client.createItem(vcsid, multipart);
-
- int statusCode = res.getStatus();
-
- if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
- throw new RuntimeException("Could not create Item: \""+refName
- +"\" in personAuthority: \"" + personAuthorityRefName
- +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
- }
- if(statusCode != EXPECTED_STATUS_CODE) {
- throw new RuntimeException("Unexpected Status when creating Item: \""+refName
- +"\" in personAuthority: \"" + personAuthorityRefName +"\", Status:"+ statusCode);
- }
-
- return extractId(res);
- }
-
- // ---------------------------------------------------------------
- // Utility methods used by methods above
- // ---------------------------------------------------------------
-
- private MultipartOutput createPersonAuthorityInstance(
- String displayName, String refName ) {
- PersonauthoritiesCommon personAuthority = new PersonauthoritiesCommon();
- personAuthority.setDisplayName(displayName);
- personAuthority.setRefName(refName);
- personAuthority.setVocabType("PersonAuthority");
- MultipartOutput multipart = new MultipartOutput();
- OutputPart commonPart = multipart.addPart(personAuthority, MediaType.APPLICATION_XML_TYPE);
- commonPart.getHeaders().add("label", client.getCommonPartName());
-
- if(logger.isDebugEnabled()){
- logger.debug("to be created, personAuthority common ",
- personAuthority, PersonauthoritiesCommon.class);
- }
-
- return multipart;
- }
-
- private MultipartOutput createPersonInstance(String inAuthority,
- String refName, Map<String, String> personInfo ) {
- /*
- String foreName, String middleName, String lastName,
- String initials, String salutations, String title, String nameAdditions,
- String birthDate, String deathDate, String birthPlace, String deathPlace,
- String group, String nationality, String gender, String occupation,
- String schoolOrStyle, String bioNote, String nameNote ) {
- */
- PersonsCommon person = new PersonsCommon();
- person.setInAuthority(inAuthority);
- person.setRefName(refName);
- String value = null;
- if((value = (String)personInfo.get(PersonJAXBSchema.FORE_NAME))!=null)
- person.setForeName(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.MIDDLE_NAME))!=null)
- person.setMiddleName(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.SUR_NAME))!=null)
- person.setSurName(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.INITIALS))!=null)
- person.setInitials(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.SALUTATIONS))!=null)
- person.setSalutation(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.TITLE))!=null)
- person.setTitle(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.NAME_ADDITIONS))!=null)
- person.setNameAdditions(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.BIRTH_DATE))!=null)
- person.setBirthDate(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.DEATH_DATE))!=null)
- person.setDeathDate(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.BIRTH_PLACE))!=null)
- person.setBirthPlace(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.DEATH_PLACE))!=null)
- person.setDeathPlace(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.GROUP))!=null)
- person.setGroup(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.NATIONALITY))!=null)
- person.setNationality(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.GENDER))!=null)
- person.setGender(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.OCCUPATION))!=null)
- person.setOccupation(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.SCHOOL_OR_STYLE))!=null)
- person.setSchoolOrStyle(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.BIO_NOTE))!=null)
- person.setBioNote(value);
- if((value = (String)personInfo.get(PersonJAXBSchema.NAME_NOTE))!=null)
- person.setNameNote(value);
- MultipartOutput multipart = new MultipartOutput();
- OutputPart commonPart = multipart.addPart(person,
- MediaType.APPLICATION_XML_TYPE);
- commonPart.getHeaders().add("label", client.getItemCommonPartName());
-
- if(logger.isDebugEnabled()){
- logger.debug("to be created, person common"+person);
- }
-
- return multipart;
- }
-
-
- /**
- * Returns an error message indicating that the status code returned by a
- * specific call to a service does not fall within a set of valid status
- * codes for that service.
- *
- * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
- *
- * @param statusCode The invalid status code that was returned in the response,
- * from submitting that type of request to the service.
- *
- * @return An error message.
- */
- protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
- return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
- requestType.validStatusCodesAsString();
- }
-
- protected String extractId(ClientResponse<Response> res) {
- MultivaluedMap<String, Object> mvm = res.getMetadata();
- String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
- if(logger.isDebugEnabled()){
- logger.debug("extractId:uri=" + uri);
- }
- String[] segments = uri.split("/");
- String id = segments[segments.length - 1];
- if(logger.isDebugEnabled()){
- logger.debug("id=" + id);
- }
- return id;
- }
-
- protected String createPersonAuthRefName(String personAuthorityName) {
- return "urn:cspace:org.collectionspace.demo:personauthority:name("
- +personAuthorityName+")";
- }
-
- protected String createPersonRefName(
- String personAuthRefName, String personName) {
- return personAuthRefName+":person:name("+personName+")";
- }
-
- public static void main(String[] args) {
-
- BasicConfigurator.configure();
- logger.info("PersonAuthorityBaseImport starting...");
-
- PersonAuthorityBaseImport pabi = new PersonAuthorityBaseImport();
- final String demoPersonAuthorityName = "Demo Person Authority";
-
- /* Strings are:
- shortName, longName, nameAdditions, contactName,
- foundingDate, dissolutionDate, foundingPlace, function, description
- */
- Map<String, String> johnWayneMap = new HashMap<String,String>();
- johnWayneMap.put(PersonJAXBSchema.FORE_NAME, "John");
- johnWayneMap.put(PersonJAXBSchema.SUR_NAME, "Wayne");
- johnWayneMap.put(PersonJAXBSchema.GENDER, "male");
- johnWayneMap.put(PersonJAXBSchema.BIRTH_DATE, "May 26, 1907");
- johnWayneMap.put(PersonJAXBSchema.BIRTH_PLACE, "Winterset, Iowa");
- johnWayneMap.put(PersonJAXBSchema.DEATH_DATE, "June 11, 1979");
- johnWayneMap.put(PersonJAXBSchema.BIO_NOTE, "born Marion Robert Morrison and better" +
- "known by his stage name John Wayne, was an American film actor, director " +
- "and producer. He epitomized rugged masculinity and has become an enduring " +
- "American icon. He is famous for his distinctive voice, walk and height. " +
- "He was also known for his conservative political views and his support in " +
- "the 1950s for anti-communist positions.");
- Map<String, String> patrickSchmitzMap = new HashMap<String,String>();
- patrickSchmitzMap.put(PersonJAXBSchema.FORE_NAME, "Patrick");
- patrickSchmitzMap.put(PersonJAXBSchema.SUR_NAME, "Schmitz");
- patrickSchmitzMap.put(PersonJAXBSchema.GENDER, "male");
- patrickSchmitzMap.put(PersonJAXBSchema.BIRTH_DATE, "7/15/1960");
- patrickSchmitzMap.put(PersonJAXBSchema.BIRTH_PLACE, "MI");
- Map<String, String> janeDoeMap = new HashMap<String,String>();
- janeDoeMap.put(PersonJAXBSchema.FORE_NAME, "Jane");
- janeDoeMap.put(PersonJAXBSchema.SUR_NAME, "Doe");
- janeDoeMap.put(PersonJAXBSchema.GENDER, "female");
- janeDoeMap.put(PersonJAXBSchema.BIRTH_DATE, "7/04/1901");
- janeDoeMap.put(PersonJAXBSchema.DEATH_DATE, "4/01/1999");
- janeDoeMap.put(PersonJAXBSchema.BIRTH_PLACE, "Anytown, USA");
- janeDoeMap.put(PersonJAXBSchema.BIRTH_PLACE, "Nowheresville, USA");
- List<Map<String, String>> personsMaps =
- Arrays.asList(johnWayneMap, patrickSchmitzMap, janeDoeMap );
-
- pabi.createPersonAuthority(demoPersonAuthorityName, personsMaps);
-
- logger.info("PersonAuthorityBaseImport complete.");
- }
-}
+++ /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.person.client.sample;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\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.PersonJAXBSchema;\r
-import org.collectionspace.services.client.PersonAuthorityClient;\r
-import org.collectionspace.services.client.test.ServiceRequestType;\r
-import org.collectionspace.services.person.PersonauthoritiesCommon;\r
-import org.collectionspace.services.person.PersonauthoritiesCommonList;\r
-import org.collectionspace.services.person.PersonsCommon;\r
-import org.collectionspace.services.person.PersonsCommonList;\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
-\r
-/**\r
- * PersonAuthority Sample, carries out tests against a\r
- * deployed and running PersonAuthority Service.\r
- *\r
- * $LastChangedRevision: 1055 $\r
- * $LastChangedDate: 2009-12-09 12:25:15 -0800 (Wed, 09 Dec 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 PersonAuthorityClient client = new PersonAuthorityClient();\r
- final String SERVICE_PATH_COMPONENT = "persons";\r
- final String ITEM_SERVICE_PATH_COMPONENT = "items";\r
-\r
-\r
- // ---------------------------------------------------------------\r
- // Create\r
- // ---------------------------------------------------------------\r
-\r
- public void createPersonAuthority(String personAuthorityName, \r
- List<Map<String, String>> personMaps ) {\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
- logger.info("Import: Create personAuthority: \"" + personAuthorityName +"\"");\r
- String basePersonRefName = createPersonAuthRefName(personAuthorityName);\r
- String fullPersonRefName = basePersonRefName+"'"+personAuthorityName+"'";\r
- MultipartOutput multipart = createPersonAuthorityInstance(personAuthorityName, \r
- fullPersonRefName );\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: \""+personAuthorityName\r
- +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
- }\r
- if(statusCode != EXPECTED_STATUS_CODE) {\r
- throw new RuntimeException("Unexpected Status when creating enumeration: \""\r
- +personAuthorityName +"\", Status:"+ statusCode);\r
- }\r
-\r
- // Store the ID returned from this create operation\r
- // for additional tests below.\r
- String newPersonAuthId = extractId(res);\r
- logger.info("Import: Created personAuthority: \"" + personAuthorityName +"\" ID:"\r
- +newPersonAuthId );\r
- \r
- // Add items to the personAuthority\r
- for(Map<String,String> personMap : personMaps){\r
- createItemInAuthority(newPersonAuthId, basePersonRefName, personMap);\r
- }\r
- \r
- }\r
- \r
- private String createItemInAuthority(String vcsid, \r
- String personAuthorityRefName, Map<String,String> personMap) {\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
- String foreName = personMap.get(PersonJAXBSchema.FORE_NAME);\r
- String middleName = personMap.get(PersonJAXBSchema.MIDDLE_NAME);\r
- String surName = personMap.get(PersonJAXBSchema.SUR_NAME);\r
- String birthDate = personMap.get(PersonJAXBSchema.BIRTH_DATE);\r
- String deathDate = personMap.get(PersonJAXBSchema.DEATH_DATE);\r
- StringBuilder builtName = new StringBuilder();\r
- if(foreName!=null)\r
- builtName.append(foreName);\r
- if(middleName!=null)\r
- builtName.append(middleName);\r
- if(surName!=null)\r
- builtName.append(surName);\r
- if(birthDate!=null)\r
- builtName.append(birthDate);\r
- builtName.append("-");\r
- if(deathDate!=null)\r
- builtName.append(deathDate);\r
- String refName = createPersonRefName(personAuthorityRefName, builtName.toString());\r
- logger.info("Import: Create Item: \""+refName+"\" in personAuthority: \"" + personAuthorityRefName +"\"");\r
-\r
- if(logger.isDebugEnabled()){\r
- logger.debug("Import: Create Item: \""+builtName.toString()\r
- +"\" in personAuthorityulary: \"" + personAuthorityRefName +"\"");\r
- }\r
- MultipartOutput multipart = createPersonInstance( vcsid, refName,\r
- personMap );\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: \""+refName\r
- +"\" in personAuthority: \"" + personAuthorityRefName\r
- +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
- }\r
- if(statusCode != EXPECTED_STATUS_CODE) {\r
- throw new RuntimeException("Unexpected Status when creating Item: \""+refName\r
- +"\" in personAuthority: \"" + personAuthorityRefName +"\", Status:"+ statusCode);\r
- }\r
-\r
- return extractId(res);\r
- }\r
-\r
-\r
- // ---------------------------------------------------------------\r
- // Read\r
- // ---------------------------------------------------------------\r
-\r
- private PersonauthoritiesCommonList readPersonAuthorities() {\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<PersonauthoritiesCommonList> res = client.readList();\r
- PersonauthoritiesCommonList list = res.getEntity();\r
-\r
- int statusCode = res.getStatus();\r
- if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
- throw new RuntimeException("Could not read list of personAuthorities: "\r
- + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
- }\r
- if(statusCode != EXPECTED_STATUS_CODE) {\r
- throw new RuntimeException("Unexpected Status when reading " +\r
- "list of personAuthorities, Status:"+ statusCode);\r
- }\r
-\r
- return list;\r
- }\r
-\r
- private List<String> readPersonAuthorityIds(PersonauthoritiesCommonList list) {\r
-\r
- List<String> ids = new ArrayList<String>();\r
- List<PersonauthoritiesCommonList.PersonauthorityListItem> personAuthorities =\r
- list.getPersonauthorityListItem();\r
- for (PersonauthoritiesCommonList.PersonauthorityListItem personAuthority : personAuthorities) {\r
- ids.add(personAuthority.getCsid());\r
- }\r
- return ids;\r
- }\r
- \r
- private PersonauthoritiesCommon readPersonAuthority(String personAuthId) {\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
- PersonauthoritiesCommon personAuthority = null;\r
- try {\r
- ClientResponse<MultipartInput> res = client.read(personAuthId);\r
- int statusCode = res.getStatus();\r
- if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
- throw new RuntimeException("Could not read personAuthority"\r
- + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
- }\r
- if(statusCode != EXPECTED_STATUS_CODE) {\r
- throw new RuntimeException("Unexpected Status when reading " +\r
- "personAuthority, Status:"+ statusCode);\r
- }\r
- MultipartInput input = (MultipartInput) res.getEntity();\r
- personAuthority = (PersonauthoritiesCommon) extractPart(input,\r
- client.getCommonPartName(), PersonauthoritiesCommon.class);\r
- } catch (Exception e) {\r
- throw new RuntimeException("Could not read personAuthority: ", e);\r
- }\r
-\r
- return personAuthority;\r
- }\r
-\r
- private PersonsCommonList readItemsInPersonAuth(String personAuthId) {\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<PersonsCommonList> res =\r
- client.readItemList(personAuthId);\r
- PersonsCommonList 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 personAuthority: "\r
- + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
- }\r
- if(statusCode != EXPECTED_STATUS_CODE) {\r
- throw new RuntimeException("Unexpected Status when reading " +\r
- "items in personAuthority, Status:"+ statusCode);\r
- }\r
-\r
- return list;\r
- }\r
-\r
- private List<String> readPersonIds(PersonsCommonList list) {\r
-\r
- List<String> ids = new ArrayList<String>();\r
- List<PersonsCommonList.PersonListItem> items =\r
- list.getPersonListItem();\r
- for (PersonsCommonList.PersonListItem item : items) {\r
- ids.add(item.getCsid());\r
- }\r
- return ids;\r
- }\r
-\r
- // ---------------------------------------------------------------\r
- // Delete\r
- // ---------------------------------------------------------------\r
-\r
- private void deletePersonAuthority(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 personAuthority: "\r
- + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
- }\r
- if(statusCode != EXPECTED_STATUS_CODE) {\r
- throw new RuntimeException("Unexpected Status when deleting " +\r
- "personAuthority, Status:"+ statusCode);\r
- }\r
- }\r
-\r
- private void deleteAllPersonAuthorities() {\r
- List<String> ids = readPersonAuthorityIds(readPersonAuthorities());\r
- for (String id : ids) {\r
- deletePersonAuthority(id);\r
- }\r
- }\r
-\r
- private void deletePerson(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 personAuthority item: "\r
- + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
- }\r
- if(statusCode != EXPECTED_STATUS_CODE) {\r
- throw new RuntimeException("Unexpected Status when deleting " +\r
- "personAuthority item, Status:"+ statusCode);\r
- }\r
- }\r
-\r
- private void deleteAllItemsForPersonAuth(String personAuthId) {\r
- List<String> itemIds = readPersonIds(readItemsInPersonAuth(personAuthId));\r
- for (String itemId : itemIds) {\r
- deletePerson(personAuthId, itemId);\r
- }\r
- }\r
-\r
- // ---------------------------------------------------------------\r
- // Utility methods used by tests above\r
- // ---------------------------------------------------------------\r
-\r
- private MultipartOutput createPersonAuthorityInstance(\r
- String displayName, String refName ) {\r
- PersonauthoritiesCommon personAuthority = new PersonauthoritiesCommon();\r
- personAuthority.setDisplayName(displayName);\r
- personAuthority.setRefName(refName);\r
- personAuthority.setVocabType("PersonAuthority");\r
- MultipartOutput multipart = new MultipartOutput();\r
- OutputPart commonPart = multipart.addPart(personAuthority, MediaType.APPLICATION_XML_TYPE);\r
- commonPart.getHeaders().add("label", client.getCommonPartName());\r
-\r
- if(logger.isDebugEnabled()){\r
- logger.debug("to be created, personAuthority common ",\r
- personAuthority, PersonauthoritiesCommon.class);\r
- }\r
-\r
- return multipart;\r
- }\r
-\r
- private MultipartOutput createPersonInstance(String inAuthority, \r
- String refName, Map<String, String> personInfo ) {\r
- /*\r
- String foreName, String middleName, String lastName, \r
- String initials, String salutations, String title, String nameAdditions, \r
- String birthDate, String deathDate, String birthPlace, String deathPlace, \r
- String group, String nationality, String gender, String occupation, \r
- String schoolOrStyle, String bioNote, String nameNote ) {\r
- */\r
- PersonsCommon person = new PersonsCommon();\r
- person.setInAuthority(inAuthority);\r
- person.setRefName(refName);\r
- String value = null;\r
- if((value = (String)personInfo.get(PersonJAXBSchema.FORE_NAME))!=null)\r
- person.setForeName(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.MIDDLE_NAME))!=null)\r
- person.setMiddleName(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.SUR_NAME))!=null)\r
- person.setSurName(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.INITIALS))!=null)\r
- person.setInitials(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.SALUTATIONS))!=null)\r
- person.setSalutation(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.TITLE))!=null)\r
- person.setTitle(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.NAME_ADDITIONS))!=null)\r
- person.setNameAdditions(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.BIRTH_DATE))!=null)\r
- person.setBirthDate(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.DEATH_DATE))!=null)\r
- person.setDeathDate(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.BIRTH_PLACE))!=null)\r
- person.setBirthPlace(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.DEATH_PLACE))!=null)\r
- person.setDeathPlace(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.GROUP))!=null)\r
- person.setGroup(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.NATIONALITY))!=null)\r
- person.setNationality(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.GENDER))!=null)\r
- person.setGender(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.OCCUPATION))!=null)\r
- person.setOccupation(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.SCHOOL_OR_STYLE))!=null)\r
- person.setSchoolOrStyle(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.BIO_NOTE))!=null)\r
- person.setBioNote(value);\r
- if((value = (String)personInfo.get(PersonJAXBSchema.NAME_NOTE))!=null)\r
- person.setNameNote(value);\r
- MultipartOutput multipart = new MultipartOutput();\r
- OutputPart commonPart = multipart.addPart(person,\r
- MediaType.APPLICATION_XML_TYPE);\r
- commonPart.getHeaders().add("label", client.getItemCommonPartName());\r
-\r
- if(logger.isDebugEnabled()){\r
- logger.debug("to be created, person common"+person);\r
- }\r
-\r
- return multipart;\r
- }\r
-\r
- // Retrieve individual fields of personAuthority records.\r
-\r
- private String displayAllPersonAuthorities(PersonauthoritiesCommonList list) {\r
- StringBuffer sb = new StringBuffer();\r
- List<PersonauthoritiesCommonList.PersonauthorityListItem> personAuthorities =\r
- list.getPersonauthorityListItem();\r
- int i = 0;\r
- for (PersonauthoritiesCommonList.PersonauthorityListItem personAuthority : personAuthorities) {\r
- sb.append("personAuthority [" + i + "]" + "\n");\r
- sb.append(displayPersonAuthorityDetails(personAuthority));\r
- i++;\r
- }\r
- return sb.toString();\r
- }\r
-\r
- private String displayPersonAuthorityDetails(\r
- PersonauthoritiesCommonList.PersonauthorityListItem personAuthority) {\r
- StringBuffer sb = new StringBuffer();\r
- sb.append("displayName=" + personAuthority.getDisplayName() + "\n");\r
- sb.append("vocabType=" + personAuthority.getVocabType() + "\n");\r
- // sb.append("csid=" + personAuthority.getCsid() + "\n");\r
- sb.append("URI=" + personAuthority.getUri() + "\n");\r
- return sb.toString();\r
- }\r
-\r
- // Retrieve individual fields of person records.\r
-\r
- private String displayAllPersons(PersonsCommonList list) {\r
- StringBuffer sb = new StringBuffer();\r
- List<PersonsCommonList.PersonListItem> items =\r
- list.getPersonListItem();\r
- int i = 0;\r
- for (PersonsCommonList.PersonListItem item : items) {\r
- sb.append("person [" + i + "]" + "\n");\r
- sb.append(displayPersonDetails(item));\r
- i++;\r
- }\r
- return sb.toString();\r
- }\r
-\r
- private String displayPersonDetails(\r
- PersonsCommonList.PersonListItem 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
- 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
- * 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.info("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 createPersonAuthRefName(String personAuthorityName) {\r
- return "urn:cspace:org.collectionspace.demo:personauthority:name("\r
- +personAuthorityName+")";\r
- }\r
-\r
- protected String createPersonRefName(\r
- String personAuthRefName, String personName) {\r
- return personAuthRefName+":person:name("+personName+")";\r
- }\r
-\r
- public static void main(String[] args) {\r
-\r
- // Configure logging.\r
- BasicConfigurator.configure();\r
-\r
- logger.info("PersonAuthority Sample starting...");\r
-\r
- Sample sample = new Sample();\r
- PersonauthoritiesCommonList personAuthorities;\r
- List<String> personAuthIds;\r
- String details = "";\r
-\r
- // Optionally delete all personAuthorities and persons.\r
-\r
- boolean ENABLE_DELETE_ALL = false;\r
- if (ENABLE_DELETE_ALL) {\r
-\r
- logger.info("Deleting all persons and personAuthorities ...");\r
-\r
- // For each personAuthority ...\r
- personAuthorities = sample.readPersonAuthorities();\r
- personAuthIds = sample.readPersonAuthorityIds(personAuthorities);\r
- for (String personAuthId : personAuthIds) {\r
- logger.info("Deleting all persons for personAuthority ...");\r
- sample.deleteAllItemsForPersonAuth(personAuthId);\r
- logger.info("Deleting personAuthority ...");\r
- sample.deletePersonAuthority(personAuthId);\r
- }\r
-\r
- logger.info("Reading personAuthorities after deletion ...");\r
- personAuthorities = sample.readPersonAuthorities();\r
- details = sample.displayAllPersonAuthorities(personAuthorities);\r
- logger.info(details);\r
-\r
- logger.info("Reading items in each personAuthority after deletion ...");\r
- personAuthIds = sample.readPersonAuthorityIds(personAuthorities);\r
- for (String personAuthId : personAuthIds) {\r
- PersonsCommonList items = sample.readItemsInPersonAuth(personAuthId);\r
- details = sample.displayAllPersons(items);\r
- logger.info(details);\r
- }\r
-\r
- }\r
-\r
- // Create new authorities, each populated with persons.\r
-\r
- Map<String, String> johnWayneMap = new HashMap<String,String>();\r
- johnWayneMap.put(PersonJAXBSchema.FORE_NAME, "John");\r
- johnWayneMap.put(PersonJAXBSchema.SUR_NAME, "Wayne");\r
- johnWayneMap.put(PersonJAXBSchema.GENDER, "male");\r
- Map<String, String> patrickSchmitzMap = new HashMap<String,String>();\r
- patrickSchmitzMap.put(PersonJAXBSchema.FORE_NAME, "Patrick");\r
- patrickSchmitzMap.put(PersonJAXBSchema.SUR_NAME, "Schmitz");\r
- patrickSchmitzMap.put(PersonJAXBSchema.GENDER, "male");\r
- Map<String, String> janeDoeMap = new HashMap<String,String>();\r
- janeDoeMap.put(PersonJAXBSchema.FORE_NAME, "Jane");\r
- janeDoeMap.put(PersonJAXBSchema.SUR_NAME, "Doe");\r
- janeDoeMap.put(PersonJAXBSchema.GENDER, "female");\r
- List<Map<String, String>> personsMaps = \r
- Arrays.asList(johnWayneMap, patrickSchmitzMap, janeDoeMap );\r
- \r
- sample.createPersonAuthority("Sample Person Auth", personsMaps);\r
-\r
- logger.info("PersonAuthority Sample complete.");\r
-\r
- logger.info("Reading personAuthorities and items ...");\r
- // Get a list of personAuthorities.\r
- personAuthorities = sample.readPersonAuthorities();\r
- // For each personAuthority ...\r
- for (PersonauthoritiesCommonList.PersonauthorityListItem\r
- personAuthority : personAuthorities.getPersonauthorityListItem()) {\r
- // Get its display name.\r
- logger.info(personAuthority.getDisplayName());\r
- // Get a list of the persons in this personAuthority.\r
- PersonsCommonList items =\r
- sample.readItemsInPersonAuth(personAuthority.getCsid());\r
- // For each person ...\r
- for (PersonsCommonList.PersonListItem\r
- item : items.getPersonListItem()) {\r
- // Get its short name.\r
- logger.info(" " + item.getDisplayName());\r
- }\r
- }\r
-\r
- // Sample alternate methods of reading all personAuthorities and\r
- // persons separately.\r
- boolean RUN_ADDITIONAL_SAMPLES = false;\r
- if (RUN_ADDITIONAL_SAMPLES) {\r
-\r
- logger.info("Reading all personAuthorities ...");\r
- details = sample.displayAllPersonAuthorities(personAuthorities);\r
- logger.info(details);\r
-\r
- logger.info("Reading all persons ...");\r
- personAuthIds = sample.readPersonAuthorityIds(personAuthorities);\r
- for (String personAuthId : personAuthIds) {\r
- PersonsCommonList items = sample.readItemsInPersonAuth(personAuthId);\r
- details = sample.displayAllPersons(items);\r
- logger.info(details);\r
- }\r
-\r
- }\r
-\r
- }\r
-\r
-}\r