From 7333fb4a585e742b76de11ef64a284cf9676e74f Mon Sep 17 00:00:00 2001 From: Patrick Schmitz Date: Tue, 25 May 2010 03:27:49 +0000 Subject: [PATCH] CSPACE-1895 Working on the repeating sections of Location, and general support for this. --- ...RemoteSubItemDocumentModelHandlerImpl.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteSubItemDocumentModelHandlerImpl.java diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteSubItemDocumentModelHandlerImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteSubItemDocumentModelHandlerImpl.java new file mode 100644 index 000000000..0947ae073 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteSubItemDocumentModelHandlerImpl.java @@ -0,0 +1,108 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.collectionspace.services.nuxeo.client.java; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Map.Entry; + +import javax.ws.rs.core.MediaType; + +import org.collectionspace.services.common.context.MultipartServiceContext; +import org.collectionspace.services.common.document.DocumentUtils; +import org.collectionspace.services.common.document.DocumentWrapper; +import org.collectionspace.services.common.service.ObjectPartType; +import org.jboss.resteasy.plugins.providers.multipart.InputPart; +import org.nuxeo.ecm.core.api.DocumentModel; +import org.nuxeo.ecm.core.api.DocumentModelList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; + +/** + * RemoteDocumentModelHandler + * + * @author pschmitz + * $LastChangedRevision: $ + * $LastChangedDate: $ + * @param The {DocumentType}Common class + * @param The {DocumentType}CommonList class + */ +public abstract class RemoteSubItemDocumentModelHandlerImpl extends + RemoteDocumentModelHandlerImpl { + + private final Logger logger = LoggerFactory.getLogger(RemoteSubItemDocumentModelHandlerImpl.class); + private final String SI_LABEL = "subitem"; + // We must align this to the schema: + // + // + // + private final String[] fields = {"owner", "isPrimary", "order"}; + + /** + * Override fillPart to handle the Subitem XML part into the Subitem document model + * @param part to fill + * @param docModel for the given object + * @param partMeta metadata for the object to fill + * @throws Exception + */ + protected void fillPart(InputPart part, DocumentModel docModel, ObjectPartType partMeta) + throws Exception { + InputStream payload = part.getBody(InputStream.class, null); + + //check if this is an xml part + // TODO - we could configure the parts that have subitem content, + // and then check that here, so skip other parts. + if(part.getMediaType().equals(MediaType.APPLICATION_XML_TYPE)){ + if(payload != null){ + Document document = DocumentUtils.parseDocument(payload); + //TODO: callback to handler if registered to validate the + //document + Map objectProps = DocumentUtils.parseProperties(document); + // Now pull out the subitem props and set them into the Subitem schema + Map subitemProps = null; + for(String key:fields){ + // Fetch and remove as we go, so can safely set remaining values below + String value = (String)(objectProps.remove(key)); + if(value!=null) { + if(subitemProps == null) { + subitemProps = new HashMap(); + } + subitemProps.put(key, value); + } + } + if(subitemProps != null) { + docModel.setProperties(SI_LABEL, subitemProps); + } + // Set all remaining values on the common part. + docModel.setProperties(partMeta.getLabel(), objectProps); + } + } + } + + +} -- 2.47.3