]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
00df2640aba6993511f159721314f370c871373e
[tmp/jakarta-migration.git] /
1 /**\r
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
5  *\r
6  * http://www.collectionspace.org\r
7  * http://wiki.collectionspace.org\r
8  *\r
9  * Copyright (c)) 2009 Regents of the University of California\r
10  *\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
13  *\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
16  *\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
22  */\r
23 \r
24 package org.collectionspace.services.person.client.sample;\r
25 \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
31 \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
35 \r
36 import org.apache.log4j.BasicConfigurator;\r
37 import org.collectionspace.services.PersonJAXBSchema;\r
38 import org.collectionspace.services.client.PersonAuthorityClient;\r
39 import org.collectionspace.services.client.PersonAuthorityClientUtils;\r
40 import org.collectionspace.services.client.test.ServiceRequestType;\r
41 import org.collectionspace.services.person.PersonauthoritiesCommon;\r
42 import org.collectionspace.services.person.PersonauthoritiesCommonList;\r
43 import org.collectionspace.services.person.PersonsCommon;\r
44 import org.collectionspace.services.person.PersonsCommonList;\r
45 import org.jboss.resteasy.client.ClientResponse;\r
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;\r
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
48 import org.jboss.resteasy.plugins.providers.multipart.InputPart;\r
49 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
50 import org.slf4j.Logger;\r
51 import org.slf4j.LoggerFactory;\r
52 \r
53 /**\r
54  * PersonAuthority Sample, carries out tests against a\r
55  * deployed and running PersonAuthority Service.\r
56  *\r
57  * $LastChangedRevision: 1055 $\r
58  * $LastChangedDate: 2009-12-09 12:25:15 -0800 (Wed, 09 Dec 2009) $\r
59  */\r
60 public class Sample {\r
61     private static final Logger logger =\r
62         LoggerFactory.getLogger(Sample.class);\r
63 \r
64     // Instance variables specific to this test.\r
65     private PersonAuthorityClient client = new PersonAuthorityClient();\r
66     final String SERVICE_PATH_COMPONENT = "persons";\r
67     final String ITEM_SERVICE_PATH_COMPONENT = "items";\r
68 \r
69 \r
70     // ---------------------------------------------------------------\r
71     // Create\r
72     // ---------------------------------------------------------------\r
73 \r
74     public void createPersonAuthority(String personAuthorityName, \r
75                 List<Map<String, String>> personMaps ) {\r
76 \r
77         // Expected status code: 201 Created\r
78         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
79         // Type of service request being tested\r
80         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
81 \r
82         logger.info("Import: Create personAuthority: \"" + personAuthorityName +"\"");\r
83         String basePersonRefName = PersonAuthorityClientUtils.createPersonAuthRefName(personAuthorityName);\r
84         String fullPersonRefName = basePersonRefName+"'"+personAuthorityName+"'";\r
85         MultipartOutput multipart = \r
86                 PersonAuthorityClientUtils.createPersonAuthorityInstance(\r
87                                 personAuthorityName, fullPersonRefName, client.getCommonPartName() );\r
88         ClientResponse<Response> res = client.create(multipart);\r
89 \r
90         int statusCode = res.getStatus();\r
91 \r
92         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
93                 throw new RuntimeException("Could not create enumeration: \""+personAuthorityName\r
94                                 +"\" "+ PersonAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
95         }\r
96         if(statusCode != EXPECTED_STATUS_CODE) {\r
97                 throw new RuntimeException("Unexpected Status when creating enumeration: \""\r
98                                 +personAuthorityName +"\", Status:"+ statusCode);\r
99         }\r
100 \r
101         // Store the ID returned from this create operation\r
102         // for additional tests below.\r
103         String newPersonAuthId = PersonAuthorityClientUtils.extractId(res);\r
104         logger.info("Import: Created personAuthority: \"" + personAuthorityName +"\" ID:"\r
105                                 +newPersonAuthId );\r
106         \r
107         // Add items to the personAuthority\r
108         for(Map<String,String> personMap : personMaps){\r
109                 createItemInAuthority(newPersonAuthId, basePersonRefName, personMap);\r
110         }\r
111         \r
112     }\r
113     \r
114     private String createItemInAuthority(String vcsid, \r
115                 String personAuthorityRefName, Map<String,String> personMap) {\r
116         // Expected status code: 201 Created\r
117         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
118         // Type of service request being tested\r
119         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
120         String foreName = personMap.get(PersonJAXBSchema.FORE_NAME);\r
121         String middleName = personMap.get(PersonJAXBSchema.MIDDLE_NAME);\r
122         String surName = personMap.get(PersonJAXBSchema.SUR_NAME);\r
123         String birthDate = personMap.get(PersonJAXBSchema.BIRTH_DATE);\r
124         String deathDate = personMap.get(PersonJAXBSchema.DEATH_DATE);\r
125         StringBuilder builtName = new StringBuilder();\r
126         if(foreName!=null)\r
127                 builtName.append(foreName);\r
128         if(middleName!=null)\r
129                 builtName.append(middleName);\r
130         if(surName!=null)\r
131                 builtName.append(surName);\r
132         if(birthDate!=null)\r
133                 builtName.append(birthDate);\r
134                 builtName.append("-");\r
135         if(deathDate!=null)\r
136                 builtName.append(deathDate);\r
137         String refName = PersonAuthorityClientUtils.createPersonRefName(personAuthorityRefName, builtName.toString());\r
138         logger.info("Import: Create Item: \""+refName+"\" in personAuthority: \"" + personAuthorityRefName +"\"");\r
139 \r
140         if(logger.isDebugEnabled()){\r
141                 logger.debug("Import: Create Item: \""+builtName.toString()\r
142                                 +"\" in personAuthorityulary: \"" + personAuthorityRefName +"\"");\r
143         }\r
144         MultipartOutput multipart = createPersonInstance( vcsid, refName,\r
145                         personMap );\r
146         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
147 \r
148         int statusCode = res.getStatus();\r
149 \r
150         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
151                 throw new RuntimeException("Could not create Item: \""+refName\r
152                                 +"\" in personAuthority: \"" + personAuthorityRefName\r
153                                 +"\" "+ PersonAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
154         }\r
155         if(statusCode != EXPECTED_STATUS_CODE) {\r
156                 throw new RuntimeException("Unexpected Status when creating Item: \""+refName\r
157                                 +"\" in personAuthority: \"" + personAuthorityRefName +"\", Status:"+ statusCode);\r
158         }\r
159 \r
160         return PersonAuthorityClientUtils.extractId(res);\r
161     }\r
162 \r
163 \r
164    // ---------------------------------------------------------------\r
165    // Read\r
166    // ---------------------------------------------------------------\r
167 \r
168    private PersonauthoritiesCommonList readPersonAuthorities() {\r
169 \r
170         // Expected status code: 200 OK\r
171         int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();\r
172         // Type of service request being tested\r
173         ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;\r
174 \r
175         // Submit the request to the service and store the response.\r
176         ClientResponse<PersonauthoritiesCommonList> res = client.readList();\r
177         PersonauthoritiesCommonList list = res.getEntity();\r
178 \r
179         int statusCode = res.getStatus();\r
180         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
181                 throw new RuntimeException("Could not read list of personAuthorities: "\r
182                 + PersonAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
183         }\r
184         if(statusCode != EXPECTED_STATUS_CODE) {\r
185                 throw new RuntimeException("Unexpected Status when reading " +\r
186                 "list of personAuthorities, Status:"+ statusCode);\r
187         }\r
188 \r
189         return list;\r
190    }\r
191 \r
192     private List<String> readPersonAuthorityIds(PersonauthoritiesCommonList list) {\r
193 \r
194         List<String> ids = new ArrayList<String>();\r
195         List<PersonauthoritiesCommonList.PersonauthorityListItem> personAuthorities =\r
196             list.getPersonauthorityListItem();\r
197         for (PersonauthoritiesCommonList.PersonauthorityListItem personAuthority : personAuthorities) {\r
198             ids.add(personAuthority.getCsid());\r
199         }\r
200         return ids;\r
201    }\r
202     \r
203    private PersonauthoritiesCommon readPersonAuthority(String personAuthId) {\r
204 \r
205         // Expected status code: 200 OK\r
206         int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();\r
207         // Type of service request being tested\r
208         ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;\r
209 \r
210         // Submit the request to the service and store the response.\r
211         PersonauthoritiesCommon personAuthority = null;\r
212         try {\r
213             ClientResponse<MultipartInput> res = client.read(personAuthId);\r
214             int statusCode = res.getStatus();\r
215             if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
216                 throw new RuntimeException("Could not read personAuthority"\r
217                     + PersonAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
218             }\r
219             if(statusCode != EXPECTED_STATUS_CODE) {\r
220                 throw new RuntimeException("Unexpected Status when reading " +\r
221                     "personAuthority, Status:"+ statusCode);\r
222             }\r
223             MultipartInput input = (MultipartInput) res.getEntity();\r
224             personAuthority = (PersonauthoritiesCommon) extractPart(input,\r
225                     client.getCommonPartName(), PersonauthoritiesCommon.class);\r
226         } catch (Exception e) {\r
227             throw new RuntimeException("Could not read personAuthority: ", e);\r
228         }\r
229 \r
230         return personAuthority;\r
231     }\r
232 \r
233     private PersonsCommonList readItemsInPersonAuth(String personAuthId) {\r
234 \r
235         // Expected status code: 200 OK\r
236         int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();\r
237         // Type of service request being tested\r
238         ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;\r
239 \r
240         // Submit the request to the service and store the response.\r
241         ClientResponse<PersonsCommonList> res =\r
242                 client.readItemList(personAuthId);\r
243         PersonsCommonList list = res.getEntity();\r
244 \r
245         int statusCode = res.getStatus();\r
246 \r
247         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
248                 throw new RuntimeException("Could not read items in personAuthority: "\r
249                 + PersonAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
250         }\r
251         if(statusCode != EXPECTED_STATUS_CODE) {\r
252                 throw new RuntimeException("Unexpected Status when reading " +\r
253                 "items in personAuthority, Status:"+ statusCode);\r
254         }\r
255 \r
256         return list;\r
257     }\r
258 \r
259     private List<String> readPersonIds(PersonsCommonList list) {\r
260 \r
261         List<String> ids = new ArrayList<String>();\r
262         List<PersonsCommonList.PersonListItem> items =\r
263             list.getPersonListItem();\r
264         for (PersonsCommonList.PersonListItem item : items) {\r
265             ids.add(item.getCsid());\r
266         }\r
267         return ids;\r
268    }\r
269 \r
270     // ---------------------------------------------------------------\r
271     // Delete\r
272     // ---------------------------------------------------------------\r
273 \r
274     private void deletePersonAuthority(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
279 \r
280         ClientResponse<Response> res = client.delete(vcsid);\r
281         int statusCode = res.getStatus();\r
282 \r
283         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
284                 throw new RuntimeException("Could not delete personAuthority: "\r
285                 + PersonAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
286         }\r
287         if(statusCode != EXPECTED_STATUS_CODE) {\r
288                 throw new RuntimeException("Unexpected Status when deleting " +\r
289                 "personAuthority, Status:"+ statusCode);\r
290         }\r
291     }\r
292 \r
293     private void deleteAllPersonAuthorities() {\r
294         List<String> ids = readPersonAuthorityIds(readPersonAuthorities());\r
295         for (String id : ids) {\r
296             deletePersonAuthority(id);\r
297         }\r
298     }\r
299 \r
300         private void deletePerson(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
305 \r
306         ClientResponse<Response> res = client.deleteItem(vcsid, itemcsid);\r
307         int statusCode = res.getStatus();\r
308 \r
309         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
310                 throw new RuntimeException("Could not delete personAuthority item: "\r
311                 + PersonAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
312         }\r
313         if(statusCode != EXPECTED_STATUS_CODE) {\r
314                 throw new RuntimeException("Unexpected Status when deleting " +\r
315                 "personAuthority item, Status:"+ statusCode);\r
316         }\r
317     }\r
318 \r
319     private void deleteAllItemsForPersonAuth(String personAuthId) {\r
320         List<String> itemIds = readPersonIds(readItemsInPersonAuth(personAuthId));\r
321         for (String itemId : itemIds) {\r
322             deletePerson(personAuthId, itemId);\r
323         }\r
324     }\r
325 \r
326     // ---------------------------------------------------------------\r
327     // Utility methods used by tests above\r
328     // ---------------------------------------------------------------\r
329 \r
330     /*\r
331     private MultipartOutput createPersonAuthorityInstance(\r
332                 String displayName, String refName ) {\r
333         PersonauthoritiesCommon personAuthority = new PersonauthoritiesCommon();\r
334         personAuthority.setDisplayName(displayName);\r
335         personAuthority.setRefName(refName);\r
336         personAuthority.setVocabType("PersonAuthority");\r
337         MultipartOutput multipart = new MultipartOutput();\r
338         OutputPart commonPart = multipart.addPart(personAuthority, MediaType.APPLICATION_XML_TYPE);\r
339         commonPart.getHeaders().add("label", client.getCommonPartName());\r
340 \r
341         if(logger.isDebugEnabled()){\r
342                 logger.debug("to be created, personAuthority common ",\r
343                                         personAuthority, PersonauthoritiesCommon.class);\r
344         }\r
345 \r
346         return multipart;\r
347     }\r
348     */\r
349 \r
350     private MultipartOutput createPersonInstance(String inAuthority, \r
351                 String refName, Map<String, String> personInfo ) {\r
352             PersonsCommon person = new PersonsCommon();\r
353             person.setInAuthority(inAuthority);\r
354                 person.setRefName(refName);\r
355                 String value = null;\r
356             if((value = (String)personInfo.get(PersonJAXBSchema.FORE_NAME))!=null)\r
357                 person.setForeName(value);\r
358             if((value = (String)personInfo.get(PersonJAXBSchema.MIDDLE_NAME))!=null)\r
359                 person.setMiddleName(value);\r
360             if((value = (String)personInfo.get(PersonJAXBSchema.SUR_NAME))!=null)\r
361                 person.setSurName(value);\r
362             if((value = (String)personInfo.get(PersonJAXBSchema.INITIALS))!=null)\r
363                 person.setInitials(value);\r
364             if((value = (String)personInfo.get(PersonJAXBSchema.SALUTATIONS))!=null)\r
365                 person.setSalutation(value);\r
366             if((value = (String)personInfo.get(PersonJAXBSchema.TITLE))!=null)\r
367                 person.setTitle(value);\r
368             if((value = (String)personInfo.get(PersonJAXBSchema.NAME_ADDITIONS))!=null)\r
369                 person.setNameAdditions(value);\r
370             if((value = (String)personInfo.get(PersonJAXBSchema.BIRTH_DATE))!=null)\r
371                 person.setBirthDate(value);\r
372             if((value = (String)personInfo.get(PersonJAXBSchema.DEATH_DATE))!=null)\r
373                 person.setDeathDate(value);\r
374             if((value = (String)personInfo.get(PersonJAXBSchema.BIRTH_PLACE))!=null)\r
375                 person.setBirthPlace(value);\r
376             if((value = (String)personInfo.get(PersonJAXBSchema.DEATH_PLACE))!=null)\r
377                 person.setDeathPlace(value);\r
378             if((value = (String)personInfo.get(PersonJAXBSchema.GROUP))!=null)\r
379                 person.setGroup(value);\r
380             if((value = (String)personInfo.get(PersonJAXBSchema.NATIONALITY))!=null)\r
381                 person.setNationality(value);\r
382             if((value = (String)personInfo.get(PersonJAXBSchema.GENDER))!=null)\r
383                 person.setGender(value);\r
384             if((value = (String)personInfo.get(PersonJAXBSchema.OCCUPATION))!=null)\r
385                 person.setOccupation(value);\r
386             if((value = (String)personInfo.get(PersonJAXBSchema.SCHOOL_OR_STYLE))!=null)\r
387                 person.setSchoolOrStyle(value);\r
388             if((value = (String)personInfo.get(PersonJAXBSchema.BIO_NOTE))!=null)\r
389                 person.setBioNote(value);\r
390             if((value = (String)personInfo.get(PersonJAXBSchema.NAME_NOTE))!=null)\r
391                 person.setNameNote(value);\r
392             MultipartOutput multipart = new MultipartOutput();\r
393             OutputPart commonPart = multipart.addPart(person,\r
394                 MediaType.APPLICATION_XML_TYPE);\r
395             commonPart.getHeaders().add("label", client.getItemCommonPartName());\r
396 \r
397             if(logger.isDebugEnabled()){\r
398                 logger.debug("to be created, person common"+person);\r
399             }\r
400 \r
401             return multipart;\r
402         }\r
403 \r
404     // Retrieve individual fields of personAuthority records.\r
405 \r
406     private String displayAllPersonAuthorities(PersonauthoritiesCommonList list) {\r
407         StringBuffer sb = new StringBuffer();\r
408             List<PersonauthoritiesCommonList.PersonauthorityListItem> personAuthorities =\r
409                     list.getPersonauthorityListItem();\r
410             int i = 0;\r
411         for (PersonauthoritiesCommonList.PersonauthorityListItem personAuthority : personAuthorities) {\r
412             sb.append("personAuthority [" + i + "]" + "\n");\r
413             sb.append(displayPersonAuthorityDetails(personAuthority));\r
414             i++;\r
415         }\r
416         return sb.toString();\r
417     }\r
418 \r
419     private String displayPersonAuthorityDetails(\r
420         PersonauthoritiesCommonList.PersonauthorityListItem personAuthority) {\r
421             StringBuffer sb = new StringBuffer();\r
422             sb.append("displayName=" + personAuthority.getDisplayName() + "\n");\r
423             sb.append("vocabType=" + personAuthority.getVocabType() + "\n");\r
424             // sb.append("csid=" + personAuthority.getCsid() + "\n");\r
425             sb.append("URI=" + personAuthority.getUri() + "\n");\r
426         return sb.toString();\r
427     }\r
428 \r
429     // Retrieve individual fields of person records.\r
430 \r
431     private String displayAllPersons(PersonsCommonList list) {\r
432         StringBuffer sb = new StringBuffer();\r
433         List<PersonsCommonList.PersonListItem> items =\r
434                 list.getPersonListItem();\r
435         int i = 0;\r
436         for (PersonsCommonList.PersonListItem item : items) {\r
437             sb.append("person [" + i + "]" + "\n");\r
438             sb.append(displayPersonDetails(item));\r
439             i++;\r
440         }\r
441         return sb.toString();\r
442     }\r
443 \r
444     private String displayPersonDetails(\r
445         PersonsCommonList.PersonListItem item) {\r
446             StringBuffer sb = new StringBuffer();\r
447             sb.append("csid=" + item.getCsid() + "\n");\r
448             sb.append("displayName=" + item.getDisplayName() + "\n");\r
449             // sb.append("URI=" + item.getUri() + "\n");\r
450         return sb.toString();\r
451     }\r
452 \r
453     private Object extractPart(MultipartInput input, String label,\r
454         Class clazz) throws Exception {\r
455         Object obj = null;\r
456         for(InputPart part : input.getParts()){\r
457             String partLabel = part.getHeaders().getFirst("label");\r
458             if(label.equalsIgnoreCase(partLabel)){\r
459                 String partStr = part.getBodyAsString();\r
460                 if(logger.isDebugEnabled()){\r
461                     logger.debug("extracted part str=\n" + partStr);\r
462                 }\r
463                 obj = part.getBody(clazz, null);\r
464                 if(logger.isDebugEnabled()){\r
465                     logger.debug("extracted part obj=\n", obj, clazz);\r
466                 }\r
467                 break;\r
468             }\r
469         }\r
470         return obj;\r
471     }\r
472 \r
473     /**\r
474      * Returns an error message indicating that the status code returned by a\r
475      * specific call to a service does not fall within a set of valid status\r
476      * codes for that service.\r
477      *\r
478      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
479      *\r
480      * @param statusCode  The invalid status code that was returned in the response,\r
481      *                    from submitting that type of request to the service.\r
482      *\r
483      * @return An error message.\r
484      */\r
485     /*\r
486     protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {\r
487         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +\r
488                 requestType.validStatusCodesAsString();\r
489     }\r
490 \r
491     protected String extractId(ClientResponse<Response> res) {\r
492         MultivaluedMap<String, Object> mvm = res.getMetadata();\r
493         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);\r
494         if(logger.isDebugEnabled()){\r
495                 logger.info("extractId:uri=" + uri);\r
496         }\r
497         String[] segments = uri.split("/");\r
498         String id = segments[segments.length - 1];\r
499         if(logger.isDebugEnabled()){\r
500                 logger.debug("id=" + id);\r
501         }\r
502         return id;\r
503     }\r
504     \r
505     protected String createPersonAuthRefName(String personAuthorityName) {\r
506         return "urn:cspace:org.collectionspace.demo:personauthority:name("\r
507                         +personAuthorityName+")";\r
508     }\r
509 \r
510     protected String createPersonRefName(\r
511                                                 String personAuthRefName, String personName) {\r
512         return personAuthRefName+":person:name("+personName+")";\r
513     }\r
514     */\r
515 \r
516         public static void main(String[] args) {\r
517 \r
518         // Configure logging.\r
519                 BasicConfigurator.configure();\r
520 \r
521         logger.info("PersonAuthority Sample starting...");\r
522 \r
523                 Sample sample = new Sample();\r
524         PersonauthoritiesCommonList personAuthorities;\r
525         List<String> personAuthIds;\r
526         String details = "";\r
527 \r
528         // Optionally delete all personAuthorities and persons.\r
529 \r
530         boolean ENABLE_DELETE_ALL = false;\r
531         if (ENABLE_DELETE_ALL) {\r
532 \r
533             logger.info("Deleting all persons and personAuthorities ...");\r
534 \r
535             // For each personAuthority ...\r
536             personAuthorities = sample.readPersonAuthorities();\r
537             personAuthIds = sample.readPersonAuthorityIds(personAuthorities);\r
538             for (String personAuthId : personAuthIds) {\r
539                 logger.info("Deleting all persons for personAuthority ...");\r
540                 sample.deleteAllItemsForPersonAuth(personAuthId);\r
541                 logger.info("Deleting personAuthority ...");\r
542                 sample.deletePersonAuthority(personAuthId);\r
543             }\r
544 \r
545             logger.info("Reading personAuthorities after deletion ...");\r
546             personAuthorities = sample.readPersonAuthorities();\r
547             details = sample.displayAllPersonAuthorities(personAuthorities);\r
548             logger.info(details);\r
549 \r
550             logger.info("Reading items in each personAuthority after deletion ...");\r
551             personAuthIds = sample.readPersonAuthorityIds(personAuthorities);\r
552             for (String personAuthId : personAuthIds) {\r
553                 PersonsCommonList items = sample.readItemsInPersonAuth(personAuthId);\r
554                 details = sample.displayAllPersons(items);\r
555                 logger.info(details);\r
556             }\r
557 \r
558         }\r
559 \r
560         // Create new authorities, each populated with persons.\r
561 \r
562         Map<String, String> johnWayneMap = new HashMap<String,String>();\r
563         johnWayneMap.put(PersonJAXBSchema.FORE_NAME, "John");\r
564         johnWayneMap.put(PersonJAXBSchema.SUR_NAME, "Wayne");\r
565         johnWayneMap.put(PersonJAXBSchema.GENDER, "male");\r
566         Map<String, String> patrickSchmitzMap = new HashMap<String,String>();\r
567         patrickSchmitzMap.put(PersonJAXBSchema.FORE_NAME, "Patrick");\r
568         patrickSchmitzMap.put(PersonJAXBSchema.SUR_NAME, "Schmitz");\r
569         patrickSchmitzMap.put(PersonJAXBSchema.GENDER, "male");\r
570         Map<String, String> janeDoeMap = new HashMap<String,String>();\r
571         janeDoeMap.put(PersonJAXBSchema.FORE_NAME, "Jane");\r
572         janeDoeMap.put(PersonJAXBSchema.SUR_NAME, "Doe");\r
573         janeDoeMap.put(PersonJAXBSchema.GENDER, "female");\r
574         List<Map<String, String>> personsMaps = \r
575                 Arrays.asList(johnWayneMap, patrickSchmitzMap, janeDoeMap );\r
576         \r
577         sample.createPersonAuthority("Sample Person Auth", personsMaps);\r
578 \r
579                 logger.info("PersonAuthority Sample complete.");\r
580 \r
581         logger.info("Reading personAuthorities and items ...");\r
582         // Get a list of personAuthorities.\r
583         personAuthorities = sample.readPersonAuthorities();\r
584         // For each personAuthority ...\r
585         for (PersonauthoritiesCommonList.PersonauthorityListItem\r
586             personAuthority : personAuthorities.getPersonauthorityListItem()) {\r
587             // Get its display name.\r
588             logger.info(personAuthority.getDisplayName());\r
589             // Get a list of the persons in this personAuthority.\r
590             PersonsCommonList items =\r
591                 sample.readItemsInPersonAuth(personAuthority.getCsid());\r
592             // For each person ...\r
593             for (PersonsCommonList.PersonListItem\r
594                 item : items.getPersonListItem()) {\r
595                 // Get its short name.\r
596                 logger.info(" " + item.getDisplayName());\r
597             }\r
598         }\r
599 \r
600         // Sample alternate methods of reading all personAuthorities and\r
601         // persons separately.\r
602         boolean RUN_ADDITIONAL_SAMPLES = false;\r
603         if (RUN_ADDITIONAL_SAMPLES) {\r
604 \r
605             logger.info("Reading all personAuthorities ...");\r
606             details = sample.displayAllPersonAuthorities(personAuthorities);\r
607             logger.info(details);\r
608 \r
609             logger.info("Reading all persons ...");\r
610             personAuthIds = sample.readPersonAuthorityIds(personAuthorities);\r
611             for (String personAuthId : personAuthIds) {\r
612                 PersonsCommonList items = sample.readItemsInPersonAuth(personAuthId);\r
613                 details = sample.displayAllPersons(items);\r
614                 logger.info(details);\r
615             }\r
616 \r
617         }\r
618 \r
619         }\r
620 \r
621 }\r