]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c35ce6e089681dcb7f5a42e451ac530eb96c2c31
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.common.vocabulary.nuxeo;
2
3 import java.util.List;
4
5 import org.collectionspace.services.client.ContactClient;
6 import org.collectionspace.services.client.PayloadInputPart;
7 import org.collectionspace.services.client.PoxPayloadIn;
8 import org.collectionspace.services.client.PoxPayloadOut;
9 import org.collectionspace.services.common.api.Tools;
10 import org.collectionspace.services.common.context.ServiceContext;
11 import org.collectionspace.services.common.document.DocumentException;
12 import org.collectionspace.services.common.document.DocumentWrapper;
13 import org.collectionspace.services.common.vocabulary.AuthorityResourceWithContacts;
14 import org.collectionspace.services.contact.ContactsCommon;
15 import org.collectionspace.services.jaxb.AbstractCommonList;
16 import org.collectionspace.services.jaxb.AbstractCommonList.ListItem;
17 import org.nuxeo.ecm.core.api.DocumentModel;
18
19 public abstract class AuthorityItemWithContactsDocumentModelHandler<AICommon> extends AuthorityItemDocumentModelHandler<AICommon> {
20
21     public AuthorityItemWithContactsDocumentModelHandler(String authorityCommonSchemaName,
22             String authorityItemCommonSchemaName) {
23         super(authorityCommonSchemaName, authorityItemCommonSchemaName);
24         // TODO Auto-generated constructor stub
25     }
26
27     @Override
28     public void completeCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
29         super.completeCreate(wrapDoc);
30         handleContactCreate(wrapDoc);
31     }
32     
33     @Override
34     public void completeUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
35         super.completeUpdate(wrapDoc);
36         handleContactUpdate(wrapDoc);
37     }
38
39     private void handleContactCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
40         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = getServiceContext();
41         DocumentModel docModel = wrapDoc.getWrappedObject();
42         String authorityCsid = this.getInAuthorityCsid();
43         String itemCsid = this.getCsid(docModel);
44         PoxPayloadIn input = ctx.getInput();
45         ContactClient contactClient = new ContactClient();
46         //
47         // There may be multiple contact payloads
48         //
49         List<PayloadInputPart> contactPartList = input.getParts(contactClient.getCommonPartName());
50         for (PayloadInputPart contactPart: contactPartList) {
51             createContact(authorityCsid, itemCsid, contactPart);                    
52         }
53     }
54
55     private void createContact(String authorityCsid, String itemCsid, PayloadInputPart contactPart) throws Exception {
56         String payloadTemplate = "<?xml version='1.0' encoding='UTF-8'?><document>%s</document>";
57         String xmlPayload = String.format(payloadTemplate, contactPart.asXML());
58         PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
59                 
60         AuthorityResourceWithContacts contactResource = (AuthorityResourceWithContacts) getServiceContext().getResource();
61         contactResource.createContact(getServiceContext(), authorityCsid, itemCsid, input, null);
62     }
63     
64     private boolean csidInList(String csid, AbstractCommonList existingContactsList) {
65         boolean result = false;
66         
67         List<ListItem> itemList = existingContactsList.getListItem();
68         for (ListItem item: itemList) {
69             if (getCsid(item).equalsIgnoreCase(csid)) {
70                 result = true;
71                 break;
72             }
73         }
74         
75         return result;
76     }
77     
78     /*
79      * Updates the contact list for an authority item.  *** warning *** Will not page through all existing contacts, so if the authority
80      * items has more than a page of contacts (page size is 40 by default), this behavior of this function is
81      * undefined.
82      */
83     private void handleContactUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
84         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = getServiceContext();
85         DocumentModel docModel = wrapDoc.getWrappedObject();
86         String authorityCsid = this.getInAuthorityCsid();
87         String itemCsid = this.getCsid(docModel);
88         PoxPayloadIn input = ctx.getInput();
89         ContactClient contactClient = new ContactClient();
90         
91         AuthorityResourceWithContacts contactResource = (AuthorityResourceWithContacts) ctx.getResource();
92         AbstractCommonList existingContactList = contactResource.getContactList(ctx, authorityCsid, itemCsid, null);
93         List<PayloadInputPart> contactPartList = input.getParts(contactClient.getCommonPartName());
94
95         //
96         // If there are no update payloads, then return
97         //
98         if (contactPartList.isEmpty()) {
99             return;
100         }
101         
102         //
103         // If there are no existing contacts and we're given an update payload then fail with
104         // an exception
105         //
106         if (existingContactList.getTotalItems() == 0 && !contactPartList.isEmpty()) {
107             throw new DocumentException("There was a request to update a contact that does not exist.");
108         }
109         
110         //
111         // If there are more update payloads than existing contacts then fail with an exception
112         //
113         if (existingContactList.getTotalItems() < contactPartList.size()) {
114             throw new DocumentException("There are more update payloads than existing contacts.");
115         }
116         
117         //
118         // If there is only one existing contact and only 1 update payload being sent then
119         // update the existing contact -assuming the CSID (if any) in the update payload matches
120         // the CSID of the existing contact
121         //
122         if (existingContactList.getTotalItems() == 1 && contactPartList.size() == 1) {
123             String existingContactCsid = this.getCsid(existingContactList.getListItem().get(0));
124             PayloadInputPart contactPart = contactPartList.get(0);
125             ContactsCommon contactUpdate = (ContactsCommon) contactPart.getBody();
126             
127             if (Tools.isEmpty(contactUpdate.getCsid()) == true) {
128                 //
129                 // Assume the incoming update payload refers to the one (and only) existing contact
130                 //
131                 contactResource.updateContact(ctx, authorityCsid, itemCsid, existingContactCsid, contactPart);
132                 return;
133             } else {
134                 if (contactUpdate.getCsid().equalsIgnoreCase(existingContactCsid)) {
135                     contactResource.updateContact(ctx, authorityCsid, itemCsid, existingContactCsid, contactPart);
136                     return;
137                 } else {
138                     throw new DocumentException("The CSID's of the contact being requested an update does not match the existing contact CSID.");
139                 }
140             }
141         }
142         
143         //
144         // If we've made it this far, it better mean that the number of update payloads is the same as the
145         // number of existing contacts.  If so, all the payloads need to have CSIDs that match a CSID of an existing
146         // contact
147         //
148         if (existingContactList.getTotalItems() == contactPartList.size()) {
149             for (PayloadInputPart contactPart: contactPartList) {
150                 ContactsCommon contact = (ContactsCommon) contactPart.getBody();
151                 if (Tools.isEmpty(contact.getCsid()) == false) {
152                     if (csidInList(contact.getCsid(), existingContactList)) {
153                         contactResource.updateContact(ctx, authorityCsid, itemCsid, contact.getCsid(), contactPart);
154                     }
155                 } else {
156                     throw new DocumentException("Contact payload for update did not contain a CSID.");
157                 }
158             }
159         } else {
160             throw new DocumentException("The number of update payloads does not match the number of existing contacts.");
161         }
162         
163
164     }    
165 }