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