2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.nuxeo.client.java;
26 import java.io.InputStream;
27 import java.io.ByteArrayInputStream;
28 import java.util.HashMap;
31 import org.collectionspace.services.client.PayloadInputPart;
32 import org.collectionspace.services.client.PoxPayloadIn;
33 import org.collectionspace.services.client.PoxPayloadOut;
34 import org.collectionspace.services.common.context.ServiceContext;
35 import org.collectionspace.services.common.document.DocumentUtils;
36 import org.collectionspace.services.config.service.ObjectPartType;
38 import org.jboss.resteasy.plugins.providers.multipart.InputPart;
39 import javax.ws.rs.core.MediaType;
40 import org.nuxeo.ecm.core.api.DocumentModel;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.w3c.dom.Document;
46 * RemoteDocumentModelHandler
49 * $LastChangedRevision: $
51 * @param <T> The {DocumentType}Common class
52 * @param <TL> The {DocumentType}CommonList class
54 public abstract class RemoteSubItemDocumentModelHandlerImpl<T, TL> extends
55 RemoteDocumentModelHandlerImpl<T, TL> {
57 private final Logger logger = LoggerFactory.getLogger(RemoteSubItemDocumentModelHandlerImpl.class);
58 private final String SI_LABEL = "subitem";
59 // We must align this to the schema:
60 // <xs:element name="owner" type="xs:string" />
61 // <xs:element name="isPrimary" type="xs:boolean"/>
62 // <xs:element name="order" type="xs:unsignedInt"/>
63 private final String[] fields = {"owner", "isPrimary", "order"};
66 * Override fillPart to handle the Subitem XML part into the Subitem document model
68 * @param docModel for the given object
69 * @param partMeta metadata for the object to fill
73 protected void fillPart(PayloadInputPart part, DocumentModel docModel,
74 ObjectPartType partMeta, Action action, ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx)
76 ByteArrayInputStream bas = new ByteArrayInputStream(part.getElementBody().asXML().getBytes());
77 InputStream payload = bas;//part.getBody(/*InputStream.class, null*/);
79 //check if this is an xml part
80 // TODO - we could configure the parts that have subitem content,
81 // and then check that here, so skip other parts.
82 if(part.getMediaType().equals(MediaType.APPLICATION_XML_TYPE)){
84 Document document = DocumentUtils.parseDocument(payload, partMeta,
85 false /*don't validate*/);
86 //TODO: callback to handler if registered to validate the
88 Map<String, Object> objectProps = DocumentUtils.parseProperties(document.getFirstChild());
89 // Now pull out the subitem props and set them into the Subitem schema
90 Map<String, Object> subitemProps = null;
91 for(String key:fields){
92 // Fetch and remove as we go, so can safely set remaining values below
93 String value = (String)(objectProps.remove(key));
95 if(subitemProps == null) {
96 subitemProps = new HashMap<String, Object>();
98 subitemProps.put(key, value);
101 if(subitemProps != null) {
102 docModel.setProperties(SI_LABEL, subitemProps);
104 // Set all remaining values on the common part.
105 docModel.setProperties(partMeta.getLabel(), objectProps);
111 * extractPart extracts an XML object from given DocumentModel
112 * This overridden form checks for schemas that extend subitem, and merges
113 * in the subitem properties for that part.
115 * @param schema of the object to extract
116 * @param partMeta metadata for the object to extract
120 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
122 Map<String, Object> map = extractPart( docModel, schema, partMeta, null );
123 if(schemaHasSubItem(schema)) {
124 extractPart(docModel, SI_LABEL, partMeta, map);
129 // TODO HACK - should make this info be configured in the part metadata.
130 public abstract boolean schemaHasSubItem(String schema);