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