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.test.ServiceRequestType;
\r
41 import org.collectionspace.services.organization.OrgauthoritiesCommon;
\r
42 import org.collectionspace.services.organization.OrgauthoritiesCommonList;
\r
43 import org.collectionspace.services.organization.OrganizationsCommon;
\r
44 import org.collectionspace.services.organization.OrganizationsCommonList;
\r
45 import org.jboss.resteasy.client.ClientResponse;
\r
46 import org.jboss.resteasy.plugins.providers.multipart.InputPart;
\r
47 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
\r
48 import org.slf4j.Logger;
\r
49 import org.slf4j.LoggerFactory;
\r
52 * OrgAuthority Sample, carries out tests against a
\r
53 * deployed and running OrgAuthority Service.
\r
55 * $LastChangedRevision: 1055 $
\r
56 * $LastChangedDate: 2009-12-09 12:25:15 -0800 (Wed, 09 Dec 2009) $
\r
58 public class Sample {
\r
59 private static final Logger logger =
\r
60 LoggerFactory.getLogger(Sample.class);
\r
62 // Instance variables specific to this test.
\r
63 private OrgAuthorityClient client = new OrgAuthorityClient();
\r
64 final String SERVICE_PATH_COMPONENT = "organizations";
\r
65 final String ITEM_SERVICE_PATH_COMPONENT = "items";
\r
68 // ---------------------------------------------------------------
\r
70 // ---------------------------------------------------------------
\r
71 protected String createOrgAuthRefName(String orgAuthorityName) {
\r
72 return "urn:cspace:org.collectionspace.demo:orgauthority:name("
\r
73 +orgAuthorityName+")";
\r
76 protected String createOrganizationRefName(
\r
77 String orgAuthRefName, String orgName) {
\r
78 return orgAuthRefName+":organization:name("+orgName+")";
\r
83 public void createOrgAuthority(String orgAuthName, List<Map<String,String>> orgInfos ) {
\r
85 // Expected status code: 201 Created
\r
86 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
87 // Type of service request being tested
\r
88 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
90 logger.info("Import: Create orgAuthority: \"" + orgAuthName +"\"");
\r
91 String baseOrgAuthRefName = createOrgAuthRefName(orgAuthName);
\r
92 String fullOrgAuthRefName = baseOrgAuthRefName+"'"+orgAuthName+"'";
\r
93 PoxPayloadOut multipart =
\r
94 OrgAuthorityClientUtils.createOrgAuthorityInstance(
\r
95 orgAuthName, fullOrgAuthRefName,
\r
96 client.getCommonPartName());
\r
97 ClientResponse<Response> res = client.create(multipart);
\r
99 int statusCode = res.getStatus();
\r
101 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
102 throw new RuntimeException("Could not create enumeration: \""+orgAuthName
\r
103 +"\" "+ OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
105 if(statusCode != EXPECTED_STATUS_CODE) {
\r
106 throw new RuntimeException("Unexpected Status when creating enumeration: \""
\r
107 +orgAuthName +"\", Status:"+ statusCode);
\r
110 // Store the ID returned from this create operation
\r
111 // for additional tests below.
\r
112 String newOrgAuthId = OrgAuthorityClientUtils.extractId(res);
\r
113 logger.info("Import: Created orgAuthority: \"" + orgAuthName +"\" ID:"
\r
116 // Add items to the orgAuthority
\r
117 for(Map<String,String> orgInfo : orgInfos){
\r
118 createItemInOrgAuth(newOrgAuthId, baseOrgAuthRefName, orgInfo);
\r
123 private String createItemInOrgAuth(String vcsid,
\r
124 String orgAuthorityRefName, Map<String,String> orgInfo) {
\r
125 // Expected status code: 201 Created
\r
126 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
127 // Type of service request being tested
\r
128 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
129 String shortName = orgInfo.get(OrganizationJAXBSchema.SHORT_NAME);
\r
130 String refName = createOrganizationRefName(
\r
131 orgAuthorityRefName, shortName)+"'"+shortName+"'";
\r
134 logger.info("Import: Create Item: \""+shortName+
\r
135 "\" in orgAuthority: \"" + orgAuthorityRefName +"\"");
\r
136 PoxPayloadOut multipart =
\r
137 OrgAuthorityClientUtils.createOrganizationInstance( vcsid,
\r
138 refName, orgInfo, client.getItemCommonPartName() );
\r
140 ClientResponse<Response> res = client.createItem(vcsid, multipart);
\r
142 int statusCode = res.getStatus();
\r
144 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
145 throw new RuntimeException("Could not create Item: \""+shortName
\r
146 +"\" in orgAuthority: \"" + orgAuthorityRefName
\r
147 +"\" "+ OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
149 if(statusCode != EXPECTED_STATUS_CODE) {
\r
150 throw new RuntimeException("Unexpected Status when creating Item: \""+shortName
\r
151 +"\" in orgAuthority: \"" + orgAuthorityRefName +
\r
152 "\", Status:"+ statusCode);
\r
155 return OrgAuthorityClientUtils.extractId(res);
\r
159 // ---------------------------------------------------------------
\r
161 // ---------------------------------------------------------------
\r
163 private OrgauthoritiesCommonList readOrgAuthorities() {
\r
165 // Expected status code: 200 OK
\r
166 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
167 // Type of service request being tested
\r
168 ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;
\r
170 // Submit the request to the service and store the response.
\r
171 ClientResponse<OrgauthoritiesCommonList> res = client.readList();
\r
172 OrgauthoritiesCommonList list = res.getEntity();
\r
174 int statusCode = res.getStatus();
\r
175 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
176 throw new RuntimeException("Could not read list of orgAuthorities: "
\r
177 + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
179 if(statusCode != EXPECTED_STATUS_CODE) {
\r
180 throw new RuntimeException("Unexpected Status when reading " +
\r
181 "list of orgAuthorities, Status:"+ statusCode);
\r
187 private List<String> readOrgAuthorityIds(OrgauthoritiesCommonList list) {
\r
189 List<String> ids = new ArrayList<String>();
\r
190 List<OrgauthoritiesCommonList.OrgauthorityListItem> orgAuthorities =
\r
191 list.getOrgauthorityListItem();
\r
192 for (OrgauthoritiesCommonList.OrgauthorityListItem orgAuthority : orgAuthorities) {
\r
193 ids.add(orgAuthority.getCsid());
\r
198 private OrgauthoritiesCommon readOrgAuthority(String orgAuthId) {
\r
200 // Expected status code: 200 OK
\r
201 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
202 // Type of service request being tested
\r
203 ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;
\r
205 // Submit the request to the service and store the response.
\r
206 OrgauthoritiesCommon orgAuthority = null;
\r
208 ClientResponse<PoxPayloadIn> res = client.read(orgAuthId);
\r
209 int statusCode = res.getStatus();
\r
210 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
211 throw new RuntimeException("Could not read orgAuthority"
\r
212 + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
214 if(statusCode != EXPECTED_STATUS_CODE) {
\r
215 throw new RuntimeException("Unexpected Status when reading " +
\r
216 "orgAuthority, Status:"+ statusCode);
\r
218 PoxPayloadIn input = (PoxPayloadIn) res.getEntity();
\r
219 orgAuthority = (OrgauthoritiesCommon) extractPart(input,
\r
220 client.getCommonPartName(), OrgauthoritiesCommon.class);
\r
221 } catch (Exception e) {
\r
222 throw new RuntimeException("Could not read orgAuthority: ", e);
\r
225 return orgAuthority;
\r
228 private OrganizationsCommonList readItemsInOrgAuth(String orgAuthId) {
\r
230 // Expected status code: 200 OK
\r
231 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
232 // Type of service request being tested
\r
233 ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;
\r
235 // Submit the request to the service and store the response.
\r
237 //was: ClientResponse<OrganizationsCommonList> res = client.readItemList(orgAuthId);
\r
238 //new API: readItemList(String inAuthority, String partialTerm, String keywords)
\r
239 ClientResponse<OrganizationsCommonList> res = client.readItemList(orgAuthId, "", "");//TODO: .New call, most certainly wrong. Just trying to get this to compile. Laramie20100728
\r
241 OrganizationsCommonList list = res.getEntity();
\r
243 int statusCode = res.getStatus();
\r
245 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
246 throw new RuntimeException("Could not read items in orgAuthority: "
\r
247 + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
249 if(statusCode != EXPECTED_STATUS_CODE) {
\r
250 throw new RuntimeException("Unexpected Status when reading " +
\r
251 "items in orgAuthority, Status:"+ statusCode);
\r
257 private List<String> readOrganizationIds(OrganizationsCommonList list) {
\r
259 List<String> ids = new ArrayList<String>();
\r
260 List<OrganizationsCommonList.OrganizationListItem> items =
\r
261 list.getOrganizationListItem();
\r
262 for (OrganizationsCommonList.OrganizationListItem item : items) {
\r
263 ids.add(item.getCsid());
\r
268 // ---------------------------------------------------------------
\r
270 // ---------------------------------------------------------------
\r
272 private void deleteOrgAuthority(String vcsid) {
\r
273 // Expected status code: 200 OK
\r
274 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
275 // Type of service request being tested
\r
276 ServiceRequestType REQUEST_TYPE = ServiceRequestType.DELETE;
\r
278 ClientResponse<Response> res = client.delete(vcsid);
\r
279 int statusCode = res.getStatus();
\r
281 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
282 throw new RuntimeException("Could not delete orgAuthority: "
\r
283 + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
285 if(statusCode != EXPECTED_STATUS_CODE) {
\r
286 throw new RuntimeException("Unexpected Status when deleting " +
\r
287 "orgAuthority, Status:"+ statusCode);
\r
291 private void deleteAllOrgAuthorities() {
\r
292 List<String> ids = readOrgAuthorityIds(readOrgAuthorities());
\r
293 for (String id : ids) {
\r
294 deleteOrgAuthority(id);
\r
298 private void deleteOrganization(String vcsid, String itemcsid) {
\r
299 // Expected status code: 200 OK
\r
300 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
301 // Type of service request being tested
\r
302 ServiceRequestType REQUEST_TYPE = ServiceRequestType.DELETE;
\r
304 ClientResponse<Response> res = client.deleteItem(vcsid, itemcsid);
\r
305 int statusCode = res.getStatus();
\r
307 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
308 throw new RuntimeException("Could not delete orgAuthority item: "
\r
309 + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
311 if(statusCode != EXPECTED_STATUS_CODE) {
\r
312 throw new RuntimeException("Unexpected Status when deleting " +
\r
313 "orgAuthority item, Status:"+ statusCode);
\r
317 private void deleteAllItemsForOrgAuth(String orgAuthId) {
\r
318 List<String> itemIds = readOrganizationIds(readItemsInOrgAuth(orgAuthId));
\r
319 for (String itemId : itemIds) {
\r
320 deleteOrganization(orgAuthId, itemId);
\r
324 // ---------------------------------------------------------------
\r
325 // Utility methods used by tests above
\r
326 // ---------------------------------------------------------------
\r
329 // Retrieve individual fields of orgAuthority records.
\r
331 private String displayAllOrgAuthorities(OrgauthoritiesCommonList list) {
\r
332 StringBuffer sb = new StringBuffer();
\r
333 List<OrgauthoritiesCommonList.OrgauthorityListItem> orgAuthorities =
\r
334 list.getOrgauthorityListItem();
\r
336 for (OrgauthoritiesCommonList.OrgauthorityListItem orgAuthority : orgAuthorities) {
\r
337 sb.append("orgAuthority [" + i + "]" + "\n");
\r
338 sb.append(displayOrgAuthorityDetails(orgAuthority));
\r
341 return sb.toString();
\r
344 private String displayOrgAuthorityDetails(
\r
345 OrgauthoritiesCommonList.OrgauthorityListItem orgAuthority) {
\r
346 StringBuffer sb = new StringBuffer();
\r
347 sb.append("displayName=" + orgAuthority.getDisplayName() + "\n");
\r
348 sb.append("vocabType=" + orgAuthority.getVocabType() + "\n");
\r
349 // sb.append("csid=" + orgAuthority.getCsid() + "\n");
\r
350 sb.append("URI=" + orgAuthority.getUri() + "\n");
\r
351 return sb.toString();
\r
354 // Retrieve individual fields of organization records.
\r
356 private String displayAllOrganizations(OrganizationsCommonList list) {
\r
357 StringBuffer sb = new StringBuffer();
\r
358 List<OrganizationsCommonList.OrganizationListItem> items =
\r
359 list.getOrganizationListItem();
\r
361 for (OrganizationsCommonList.OrganizationListItem item : items) {
\r
362 sb.append("organization [" + i + "]" + "\n");
\r
363 sb.append(displayOrganizationDetails(item));
\r
366 return sb.toString();
\r
369 private String displayOrganizationDetails(
\r
370 OrganizationsCommonList.OrganizationListItem item) {
\r
371 StringBuffer sb = new StringBuffer();
\r
372 sb.append("csid=" + item.getCsid() + "\n");
\r
373 sb.append("displayName=" + item.getDisplayName() + "\n");
\r
374 // sb.append("URI=" + item.getUri() + "\n");
\r
375 return sb.toString();
\r
378 private Object extractPart(PoxPayloadIn input, String label,
\r
379 Class clazz) throws Exception {
\r
381 for(InputPart part : input.getParts()){
\r
382 String partLabel = part.getHeaders().getFirst("label");
\r
383 if(label.equalsIgnoreCase(partLabel)){
\r
384 String partStr = part.getBodyAsString();
\r
385 if(logger.isDebugEnabled()){
\r
386 logger.debug("extracted part str=\n" + partStr);
\r
388 obj = part.getBody(clazz, null);
\r
389 if(logger.isDebugEnabled()){
\r
390 logger.debug("extracted part obj=\n", obj, clazz);
\r
398 public static void main(String[] args) {
\r
400 // Configure logging.
\r
401 BasicConfigurator.configure();
\r
403 logger.info("OrgAuthority Sample starting...");
\r
405 Sample sample = new Sample();
\r
406 OrgauthoritiesCommonList orgAuthorities;
\r
407 List<String> orgAuthIds;
\r
408 String details = "";
\r
410 // Optionally delete all orgAuthorities and organizations.
\r
412 boolean ENABLE_DELETE_ALL = false;
\r
413 if (ENABLE_DELETE_ALL) {
\r
415 logger.info("Deleting all organizations and orgAuthorities ...");
\r
417 // For each orgAuthority ...
\r
418 orgAuthorities = sample.readOrgAuthorities();
\r
419 orgAuthIds = sample.readOrgAuthorityIds(orgAuthorities);
\r
420 for (String orgAuthId : orgAuthIds) {
\r
421 logger.info("Deleting all organizations for orgAuthority ...");
\r
422 sample.deleteAllItemsForOrgAuth(orgAuthId);
\r
423 logger.info("Deleting orgAuthority ...");
\r
424 sample.deleteOrgAuthority(orgAuthId);
\r
427 logger.info("Reading orgAuthorities after deletion ...");
\r
428 orgAuthorities = sample.readOrgAuthorities();
\r
429 details = sample.displayAllOrgAuthorities(orgAuthorities);
\r
430 logger.info(details);
\r
432 logger.info("Reading items in each orgAuthority after deletion ...");
\r
433 orgAuthIds = sample.readOrgAuthorityIds(orgAuthorities);
\r
434 for (String orgAuthId : orgAuthIds) {
\r
435 OrganizationsCommonList items = sample.readItemsInOrgAuth(orgAuthId);
\r
436 details = sample.displayAllOrganizations(items);
\r
437 logger.info(details);
\r
442 // Create new authorities, each populated with organizations.
\r
443 Map<String, String> mmiOrgMap = new HashMap<String,String>();
\r
444 mmiOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "MMI");
\r
445 mmiOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "Museum of the Moving Image");
\r
446 //mmiOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, "Megan Forbes");
\r
447 mmiOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "1984");
\r
448 mmiOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "Astoria, NY");
\r
449 Map<String, String> pahmaOrgMap = new HashMap<String,String>();
\r
450 pahmaOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "PAHMA");
\r
451 pahmaOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "Phoebe A. Hearst Museum of Anthropology");
\r
452 pahmaOrgMap.put(OrganizationJAXBSchema.NAME_ADDITIONS, "University of California, Berkeley");
\r
453 //pahmaOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, "Michael Black");
\r
454 pahmaOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "1901");
\r
455 pahmaOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "Berkeley, CA");
\r
456 Map<String, String> savoyOrgMap = new HashMap<String,String>();
\r
457 savoyOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Savoy Theatre");
\r
458 savoyOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "1900");
\r
459 savoyOrgMap.put(OrganizationJAXBSchema.DISSOLUTION_DATE, "1952");
\r
460 savoyOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "New York, NY");
\r
461 List<Map<String, String>> orgMaps =
\r
462 Arrays.asList(mmiOrgMap, pahmaOrgMap, savoyOrgMap );
\r
464 sample.createOrgAuthority("Sample Org Authority", orgMaps);
\r
466 logger.info("OrgAuthority Sample complete.");
\r
468 logger.info("Reading orgAuthorities and items ...");
\r
469 // Get a list of orgAuthorities.
\r
470 orgAuthorities = sample.readOrgAuthorities();
\r
471 // For each orgAuthority ...
\r
472 for (OrgauthoritiesCommonList.OrgauthorityListItem
\r
473 orgAuthority : orgAuthorities.getOrgauthorityListItem()) {
\r
474 // Get its display name.
\r
475 logger.info(orgAuthority.getDisplayName());
\r
476 // Get a list of the organizations in this orgAuthority.
\r
477 OrganizationsCommonList items =
\r
478 sample.readItemsInOrgAuth(orgAuthority.getCsid());
\r
479 // For each organization ...
\r
480 for (OrganizationsCommonList.OrganizationListItem
\r
481 item : items.getOrganizationListItem()) {
\r
482 // Get its display name.
\r
483 logger.info(" " + item.getDisplayName());
\r
487 // Sample alternate methods of reading all orgAuthorities and
\r
488 // organizations separately.
\r
489 boolean RUN_ADDITIONAL_SAMPLES = false;
\r
490 if (RUN_ADDITIONAL_SAMPLES) {
\r
492 logger.info("Reading all orgAuthorities ...");
\r
493 details = sample.displayAllOrgAuthorities(orgAuthorities);
\r
494 logger.info(details);
\r
496 logger.info("Reading all organizations ...");
\r
497 orgAuthIds = sample.readOrgAuthorityIds(orgAuthorities);
\r
498 for (String orgAuthId : orgAuthIds) {
\r
499 OrganizationsCommonList items = sample.readItemsInOrgAuth(orgAuthId);
\r
500 details = sample.displayAllOrganizations(items);
\r
501 logger.info(details);
\r