2 * This document is a part of the source code and related artifacts
\r
3 * for CollectionSpace, an open source collections management system
\r
4 * for museums and related institutions:
\r
6 * http://www.collectionspace.org
\r
7 * http://wiki.collectionspace.org
\r
9 * Copyright (c)) 2009 Regents of the University of California
\r
11 * Licensed under the Educational Community License (ECL), Version 2.0.
\r
12 * You may not use this file except in compliance with this License.
\r
14 * You may obtain a copy of the ECL 2.0 License at
\r
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
17 * Unless required by applicable law or agreed to in writing, software
\r
18 * distributed under the License is distributed on an "AS IS" BASIS,
\r
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
20 * See the License for the specific language governing permissions and
\r
21 * limitations under the License.
\r
24 package org.collectionspace.services.organization.client.sample;
\r
26 import java.util.ArrayList;
\r
27 import java.util.Arrays;
\r
28 import java.util.HashMap;
\r
29 import java.util.List;
\r
30 import java.util.Map;
\r
32 import javax.ws.rs.core.MediaType;
\r
33 import javax.ws.rs.core.MultivaluedMap;
\r
34 import javax.ws.rs.core.Response;
\r
36 import org.apache.log4j.BasicConfigurator;
\r
37 import org.collectionspace.services.OrganizationJAXBSchema;
\r
38 import org.collectionspace.services.client.OrgAuthorityClient;
\r
39 import org.collectionspace.services.client.OrgAuthorityClientUtils;
\r
40 import org.collectionspace.services.client.PayloadInputPart;
\r
41 import org.collectionspace.services.client.PoxPayloadIn;
\r
42 import org.collectionspace.services.client.PoxPayloadOut;
\r
43 import org.collectionspace.services.client.test.ServiceRequestType;
\r
44 import org.collectionspace.services.organization.OrgauthoritiesCommon;
\r
45 import org.collectionspace.services.organization.OrgauthoritiesCommonList;
\r
46 import org.collectionspace.services.organization.OrganizationsCommon;
\r
47 import org.collectionspace.services.organization.OrganizationsCommonList;
\r
48 import org.jboss.resteasy.client.ClientResponse;
\r
49 import org.jboss.resteasy.plugins.providers.multipart.InputPart;
\r
50 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
\r
51 import org.slf4j.Logger;
\r
52 import org.slf4j.LoggerFactory;
\r
55 * OrgAuthority Sample, carries out tests against a
\r
56 * deployed and running OrgAuthority Service.
\r
58 * $LastChangedRevision: 1055 $
\r
59 * $LastChangedDate: 2009-12-09 12:25:15 -0800 (Wed, 09 Dec 2009) $
\r
61 public class Sample {
\r
62 private static final Logger logger =
\r
63 LoggerFactory.getLogger(Sample.class);
\r
65 // Instance variables specific to this test.
\r
66 private OrgAuthorityClient client = new OrgAuthorityClient();
\r
67 final String SERVICE_PATH_COMPONENT = "organizations";
\r
68 final String ITEM_SERVICE_PATH_COMPONENT = "items";
\r
71 // ---------------------------------------------------------------
\r
73 // ---------------------------------------------------------------
\r
74 protected String createOrgAuthRefName(String orgAuthorityName) {
\r
75 return "urn:cspace:org.collectionspace.demo:orgauthority:name("
\r
76 +orgAuthorityName+")";
\r
79 protected String createOrganizationRefName(
\r
80 String orgAuthRefName, String orgName) {
\r
81 return orgAuthRefName+":organization:name("+orgName+")";
\r
86 public void createOrgAuthority(String orgAuthName, List<Map<String,String>> orgInfos ) {
\r
88 // Expected status code: 201 Created
\r
89 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
90 // Type of service request being tested
\r
91 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
93 logger.info("Import: Create orgAuthority: \"" + orgAuthName +"\"");
\r
94 String baseOrgAuthRefName = createOrgAuthRefName(orgAuthName);
\r
95 String fullOrgAuthRefName = baseOrgAuthRefName+"'"+orgAuthName+"'";
\r
96 PoxPayloadOut multipart =
\r
97 OrgAuthorityClientUtils.createOrgAuthorityInstance(
\r
98 orgAuthName, fullOrgAuthRefName,
\r
99 client.getCommonPartName());
\r
100 ClientResponse<Response> res = client.create(multipart);
\r
102 int statusCode = res.getStatus();
\r
104 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
105 throw new RuntimeException("Could not create enumeration: \""+orgAuthName
\r
106 +"\" "+ OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
108 if(statusCode != EXPECTED_STATUS_CODE) {
\r
109 throw new RuntimeException("Unexpected Status when creating enumeration: \""
\r
110 +orgAuthName +"\", Status:"+ statusCode);
\r
113 // Store the ID returned from this create operation
\r
114 // for additional tests below.
\r
115 String newOrgAuthId = OrgAuthorityClientUtils.extractId(res);
\r
116 logger.info("Import: Created orgAuthority: \"" + orgAuthName +"\" ID:"
\r
119 // Add items to the orgAuthority
\r
120 for(Map<String,String> orgInfo : orgInfos){
\r
121 createItemInOrgAuth(newOrgAuthId, baseOrgAuthRefName, orgInfo);
\r
126 private String createItemInOrgAuth(String vcsid,
\r
127 String orgAuthorityRefName, Map<String,String> orgInfo) {
\r
128 // Expected status code: 201 Created
\r
129 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
130 // Type of service request being tested
\r
131 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
132 String shortName = orgInfo.get(OrganizationJAXBSchema.SHORT_NAME);
\r
133 String refName = createOrganizationRefName(
\r
134 orgAuthorityRefName, shortName)+"'"+shortName+"'";
\r
137 logger.info("Import: Create Item: \""+shortName+
\r
138 "\" in orgAuthority: \"" + orgAuthorityRefName +"\"");
\r
139 PoxPayloadOut multipart =
\r
140 OrgAuthorityClientUtils.createOrganizationInstance(refName, orgInfo, client.getItemCommonPartName() );
\r
142 ClientResponse<Response> res = client.createItem(vcsid, multipart);
\r
144 int statusCode = res.getStatus();
\r
146 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
147 throw new RuntimeException("Could not create Item: \""+shortName
\r
148 +"\" in orgAuthority: \"" + orgAuthorityRefName
\r
149 +"\" "+ OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
151 if(statusCode != EXPECTED_STATUS_CODE) {
\r
152 throw new RuntimeException("Unexpected Status when creating Item: \""+shortName
\r
153 +"\" in orgAuthority: \"" + orgAuthorityRefName +
\r
154 "\", Status:"+ statusCode);
\r
157 return OrgAuthorityClientUtils.extractId(res);
\r
161 // ---------------------------------------------------------------
\r
163 // ---------------------------------------------------------------
\r
165 private OrgauthoritiesCommonList readOrgAuthorities() {
\r
167 // Expected status code: 200 OK
\r
168 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
169 // Type of service request being tested
\r
170 ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;
\r
172 // Submit the request to the service and store the response.
\r
173 ClientResponse<OrgauthoritiesCommonList> res = client.readList();
\r
174 OrgauthoritiesCommonList list = res.getEntity();
\r
176 int statusCode = res.getStatus();
\r
177 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
178 throw new RuntimeException("Could not read list of orgAuthorities: "
\r
179 + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
181 if(statusCode != EXPECTED_STATUS_CODE) {
\r
182 throw new RuntimeException("Unexpected Status when reading " +
\r
183 "list of orgAuthorities, Status:"+ statusCode);
\r
189 private List<String> readOrgAuthorityIds(OrgauthoritiesCommonList list) {
\r
191 List<String> ids = new ArrayList<String>();
\r
192 List<OrgauthoritiesCommonList.OrgauthorityListItem> orgAuthorities =
\r
193 list.getOrgauthorityListItem();
\r
194 for (OrgauthoritiesCommonList.OrgauthorityListItem orgAuthority : orgAuthorities) {
\r
195 ids.add(orgAuthority.getCsid());
\r
200 private OrgauthoritiesCommon readOrgAuthority(String orgAuthId) {
\r
202 // Expected status code: 200 OK
\r
203 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
204 // Type of service request being tested
\r
205 ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;
\r
207 // Submit the request to the service and store the response.
\r
208 OrgauthoritiesCommon orgAuthority = null;
\r
210 ClientResponse<String> res = client.read(orgAuthId);
\r
211 int statusCode = res.getStatus();
\r
212 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
213 throw new RuntimeException("Could not read orgAuthority"
\r
214 + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
216 if(statusCode != EXPECTED_STATUS_CODE) {
\r
217 throw new RuntimeException("Unexpected Status when reading " +
\r
218 "orgAuthority, Status:"+ statusCode);
\r
220 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
\r
221 PayloadInputPart orgAuthorityPart = input.getPart(client.getCommonPartName());
\r
222 orgAuthority = (OrgauthoritiesCommon) orgAuthorityPart.getBody();
\r
223 } catch (Exception e) {
\r
224 throw new RuntimeException("Could not read orgAuthority: ", e);
\r
227 return orgAuthority;
\r
230 private OrganizationsCommonList readItemsInOrgAuth(String orgAuthId) {
\r
232 // Expected status code: 200 OK
\r
233 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
234 // Type of service request being tested
\r
235 ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;
\r
237 // Submit the request to the service and store the response.
\r
239 //was: ClientResponse<OrganizationsCommonList> res = client.readItemList(orgAuthId);
\r
240 //new API: readItemList(String inAuthority, String partialTerm, String keywords)
\r
241 ClientResponse<OrganizationsCommonList> res = client.readItemList(orgAuthId, "", "");//TODO: .New call, most certainly wrong. Just trying to get this to compile. Laramie20100728
\r
243 OrganizationsCommonList list = res.getEntity();
\r
245 int statusCode = res.getStatus();
\r
247 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
248 throw new RuntimeException("Could not read items in orgAuthority: "
\r
249 + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
251 if(statusCode != EXPECTED_STATUS_CODE) {
\r
252 throw new RuntimeException("Unexpected Status when reading " +
\r
253 "items in orgAuthority, Status:"+ statusCode);
\r
259 private List<String> readOrganizationIds(OrganizationsCommonList list) {
\r
261 List<String> ids = new ArrayList<String>();
\r
262 List<OrganizationsCommonList.OrganizationListItem> items =
\r
263 list.getOrganizationListItem();
\r
264 for (OrganizationsCommonList.OrganizationListItem item : items) {
\r
265 ids.add(item.getCsid());
\r
270 // ---------------------------------------------------------------
\r
272 // ---------------------------------------------------------------
\r
274 private void deleteOrgAuthority(String vcsid) {
\r
275 // Expected status code: 200 OK
\r
276 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
277 // Type of service request being tested
\r
278 ServiceRequestType REQUEST_TYPE = ServiceRequestType.DELETE;
\r
280 ClientResponse<Response> res = client.delete(vcsid);
\r
281 int statusCode = res.getStatus();
\r
283 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
284 throw new RuntimeException("Could not delete orgAuthority: "
\r
285 + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
287 if(statusCode != EXPECTED_STATUS_CODE) {
\r
288 throw new RuntimeException("Unexpected Status when deleting " +
\r
289 "orgAuthority, Status:"+ statusCode);
\r
293 private void deleteAllOrgAuthorities() {
\r
294 List<String> ids = readOrgAuthorityIds(readOrgAuthorities());
\r
295 for (String id : ids) {
\r
296 deleteOrgAuthority(id);
\r
300 private void deleteOrganization(String vcsid, String itemcsid) {
\r
301 // Expected status code: 200 OK
\r
302 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
303 // Type of service request being tested
\r
304 ServiceRequestType REQUEST_TYPE = ServiceRequestType.DELETE;
\r
306 ClientResponse<Response> res = client.deleteItem(vcsid, itemcsid);
\r
307 int statusCode = res.getStatus();
\r
309 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
310 throw new RuntimeException("Could not delete orgAuthority item: "
\r
311 + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
313 if(statusCode != EXPECTED_STATUS_CODE) {
\r
314 throw new RuntimeException("Unexpected Status when deleting " +
\r
315 "orgAuthority item, Status:"+ statusCode);
\r
319 private void deleteAllItemsForOrgAuth(String orgAuthId) {
\r
320 List<String> itemIds = readOrganizationIds(readItemsInOrgAuth(orgAuthId));
\r
321 for (String itemId : itemIds) {
\r
322 deleteOrganization(orgAuthId, itemId);
\r
326 // ---------------------------------------------------------------
\r
327 // Utility methods used by tests above
\r
328 // ---------------------------------------------------------------
\r
331 // Retrieve individual fields of orgAuthority records.
\r
333 private String displayAllOrgAuthorities(OrgauthoritiesCommonList list) {
\r
334 StringBuffer sb = new StringBuffer();
\r
335 List<OrgauthoritiesCommonList.OrgauthorityListItem> orgAuthorities =
\r
336 list.getOrgauthorityListItem();
\r
338 for (OrgauthoritiesCommonList.OrgauthorityListItem orgAuthority : orgAuthorities) {
\r
339 sb.append("orgAuthority [" + i + "]" + "\n");
\r
340 sb.append(displayOrgAuthorityDetails(orgAuthority));
\r
343 return sb.toString();
\r
346 private String displayOrgAuthorityDetails(
\r
347 OrgauthoritiesCommonList.OrgauthorityListItem orgAuthority) {
\r
348 StringBuffer sb = new StringBuffer();
\r
349 sb.append("displayName=" + orgAuthority.getDisplayName() + "\n");
\r
350 sb.append("vocabType=" + orgAuthority.getVocabType() + "\n");
\r
351 // sb.append("csid=" + orgAuthority.getCsid() + "\n");
\r
352 sb.append("URI=" + orgAuthority.getUri() + "\n");
\r
353 return sb.toString();
\r
356 // Retrieve individual fields of organization records.
\r
358 private String displayAllOrganizations(OrganizationsCommonList list) {
\r
359 StringBuffer sb = new StringBuffer();
\r
360 List<OrganizationsCommonList.OrganizationListItem> items =
\r
361 list.getOrganizationListItem();
\r
363 for (OrganizationsCommonList.OrganizationListItem item : items) {
\r
364 sb.append("organization [" + i + "]" + "\n");
\r
365 sb.append(displayOrganizationDetails(item));
\r
368 return sb.toString();
\r
371 private String displayOrganizationDetails(
\r
372 OrganizationsCommonList.OrganizationListItem item) {
\r
373 StringBuffer sb = new StringBuffer();
\r
374 sb.append("csid=" + item.getCsid() + "\n");
\r
375 sb.append("displayName=" + item.getDisplayName() + "\n");
\r
376 // sb.append("URI=" + item.getUri() + "\n");
\r
377 return sb.toString();
\r
380 // private Object extractPart(PoxPayloadIn input, String label,
\r
381 // Class clazz) throws Exception {
\r
382 // Object obj = null;
\r
383 // for(PayloadInputPart part : input.getParts()){
\r
384 // String partLabel = part.getHeaders().getFirst("label");
\r
385 // if(label.equalsIgnoreCase(partLabel)){
\r
386 // String partStr = part.getBodyAsString();
\r
387 // if(logger.isDebugEnabled()){
\r
388 // logger.debug("extracted part str=\n" + partStr);
\r
390 // obj = part.getBody(clazz, null);
\r
391 // if(logger.isDebugEnabled()){
\r
392 // logger.debug("extracted part obj=\n", obj, clazz);
\r
400 public static void main(String[] args) {
\r
402 // Configure logging.
\r
403 BasicConfigurator.configure();
\r
405 logger.info("OrgAuthority Sample starting...");
\r
407 Sample sample = new Sample();
\r
408 OrgauthoritiesCommonList orgAuthorities;
\r
409 List<String> orgAuthIds;
\r
410 String details = "";
\r
412 // Optionally delete all orgAuthorities and organizations.
\r
414 boolean ENABLE_DELETE_ALL = false;
\r
415 if (ENABLE_DELETE_ALL) {
\r
417 logger.info("Deleting all organizations and orgAuthorities ...");
\r
419 // For each orgAuthority ...
\r
420 orgAuthorities = sample.readOrgAuthorities();
\r
421 orgAuthIds = sample.readOrgAuthorityIds(orgAuthorities);
\r
422 for (String orgAuthId : orgAuthIds) {
\r
423 logger.info("Deleting all organizations for orgAuthority ...");
\r
424 sample.deleteAllItemsForOrgAuth(orgAuthId);
\r
425 logger.info("Deleting orgAuthority ...");
\r
426 sample.deleteOrgAuthority(orgAuthId);
\r
429 logger.info("Reading orgAuthorities after deletion ...");
\r
430 orgAuthorities = sample.readOrgAuthorities();
\r
431 details = sample.displayAllOrgAuthorities(orgAuthorities);
\r
432 logger.info(details);
\r
434 logger.info("Reading items in each orgAuthority after deletion ...");
\r
435 orgAuthIds = sample.readOrgAuthorityIds(orgAuthorities);
\r
436 for (String orgAuthId : orgAuthIds) {
\r
437 OrganizationsCommonList items = sample.readItemsInOrgAuth(orgAuthId);
\r
438 details = sample.displayAllOrganizations(items);
\r
439 logger.info(details);
\r
444 // Create new authorities, each populated with organizations.
\r
445 Map<String, String> mmiOrgMap = new HashMap<String,String>();
\r
446 mmiOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "MMI");
\r
447 mmiOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "Museum of the Moving Image");
\r
448 //mmiOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, "Megan Forbes");
\r
449 mmiOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "1984");
\r
450 mmiOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "Astoria, NY");
\r
451 Map<String, String> pahmaOrgMap = new HashMap<String,String>();
\r
452 pahmaOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "PAHMA");
\r
453 pahmaOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "Phoebe A. Hearst Museum of Anthropology");
\r
454 pahmaOrgMap.put(OrganizationJAXBSchema.NAME_ADDITIONS, "University of California, Berkeley");
\r
455 //pahmaOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, "Michael Black");
\r
456 pahmaOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "1901");
\r
457 pahmaOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "Berkeley, CA");
\r
458 Map<String, String> savoyOrgMap = new HashMap<String,String>();
\r
459 savoyOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Savoy Theatre");
\r
460 savoyOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "1900");
\r
461 savoyOrgMap.put(OrganizationJAXBSchema.DISSOLUTION_DATE, "1952");
\r
462 savoyOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "New York, NY");
\r
463 List<Map<String, String>> orgMaps =
\r
464 Arrays.asList(mmiOrgMap, pahmaOrgMap, savoyOrgMap );
\r
466 sample.createOrgAuthority("Sample Org Authority", orgMaps);
\r
468 logger.info("OrgAuthority Sample complete.");
\r
470 logger.info("Reading orgAuthorities and items ...");
\r
471 // Get a list of orgAuthorities.
\r
472 orgAuthorities = sample.readOrgAuthorities();
\r
473 // For each orgAuthority ...
\r
474 for (OrgauthoritiesCommonList.OrgauthorityListItem
\r
475 orgAuthority : orgAuthorities.getOrgauthorityListItem()) {
\r
476 // Get its display name.
\r
477 logger.info(orgAuthority.getDisplayName());
\r
478 // Get a list of the organizations in this orgAuthority.
\r
479 OrganizationsCommonList items =
\r
480 sample.readItemsInOrgAuth(orgAuthority.getCsid());
\r
481 // For each organization ...
\r
482 for (OrganizationsCommonList.OrganizationListItem
\r
483 item : items.getOrganizationListItem()) {
\r
484 // Get its display name.
\r
485 logger.info(" " + item.getDisplayName());
\r
489 // Sample alternate methods of reading all orgAuthorities and
\r
490 // organizations separately.
\r
491 boolean RUN_ADDITIONAL_SAMPLES = false;
\r
492 if (RUN_ADDITIONAL_SAMPLES) {
\r
494 logger.info("Reading all orgAuthorities ...");
\r
495 details = sample.displayAllOrgAuthorities(orgAuthorities);
\r
496 logger.info(details);
\r
498 logger.info("Reading all organizations ...");
\r
499 orgAuthIds = sample.readOrgAuthorityIds(orgAuthorities);
\r
500 for (String orgAuthId : orgAuthIds) {
\r
501 OrganizationsCommonList items = sample.readItemsInOrgAuth(orgAuthId);
\r
502 details = sample.displayAllOrganizations(items);
\r
503 logger.info(details);
\r