1 package org.collectionspace.services.common.vocabulary.nuxeo;
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;
19 public abstract class AuthorityItemWithContactsDocumentModelHandler<AICommon> extends AuthorityItemDocumentModelHandler<AICommon> {
21 public AuthorityItemWithContactsDocumentModelHandler(String authorityCommonSchemaName,
22 String authorityItemCommonSchemaName) {
23 super(authorityCommonSchemaName, authorityItemCommonSchemaName);
24 // TODO Auto-generated constructor stub
28 public void completeCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
29 super.completeCreate(wrapDoc);
30 handleContactCreate(wrapDoc);
34 public void completeUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
35 super.completeUpdate(wrapDoc);
36 handleContactUpdate(wrapDoc);
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();
47 // There may be multiple contact payloads
49 List<PayloadInputPart> contactPartList = input.getParts(contactClient.getCommonPartName());
50 for (PayloadInputPart contactPart: contactPartList) {
51 createContact(authorityCsid, itemCsid, contactPart);
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);
60 AuthorityResourceWithContacts contactResource = (AuthorityResourceWithContacts) getServiceContext().getResource();
61 contactResource.createContact(getServiceContext(), authorityCsid, itemCsid, input, null);
64 private boolean csidInList(String csid, AbstractCommonList existingContactsList) {
65 boolean result = false;
67 List<ListItem> itemList = existingContactsList.getListItem();
68 for (ListItem item: itemList) {
69 if (getCsid(item).equalsIgnoreCase(csid)) {
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
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();
91 AuthorityResourceWithContacts contactResource = (AuthorityResourceWithContacts) ctx.getResource();
92 AbstractCommonList existingContactList = contactResource.getContactList(ctx, authorityCsid, itemCsid, null);
93 List<PayloadInputPart> contactPartList = input.getParts(contactClient.getCommonPartName());
96 // If there are no update payloads, then return
98 if (contactPartList.isEmpty()) {
103 // If there are no existing contacts and we're given an update payload then fail with
106 if (existingContactList.getTotalItems() == 0 && !contactPartList.isEmpty()) {
107 throw new DocumentException("There was a request to update a contact that does not exist.");
111 // If there are more update payloads than existing contacts then fail with an exception
113 if (existingContactList.getTotalItems() < contactPartList.size()) {
114 throw new DocumentException("There are more update payloads than existing contacts.");
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
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();
127 if (Tools.isEmpty(contactUpdate.getCsid()) == true) {
129 // Assume the incoming update payload refers to the one (and only) existing contact
131 contactResource.updateContact(ctx, authorityCsid, itemCsid, existingContactCsid, contactPart);
134 if (contactUpdate.getCsid().equalsIgnoreCase(existingContactCsid)) {
135 contactResource.updateContact(ctx, authorityCsid, itemCsid, existingContactCsid, contactPart);
138 throw new DocumentException("The CSID's of the contact being requested an update does not match the existing contact CSID.");
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
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);
156 throw new DocumentException("Contact payload for update did not contain a CSID.");
160 throw new DocumentException("The number of update payloads does not match the number of existing contacts.");