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.util.HashMap;
\r
28 import java.util.Map;
\r
30 import org.collectionspace.services.common.context.ServiceContext;
\r
31 import org.collectionspace.services.common.document.DocumentUtils;
\r
32 import org.collectionspace.services.common.service.ObjectPartType;
\r
34 import org.jboss.resteasy.plugins.providers.multipart.InputPart;
\r
35 import javax.ws.rs.core.MediaType;
\r
36 import org.nuxeo.ecm.core.api.DocumentModel;
\r
37 import org.slf4j.Logger;
\r
38 import org.slf4j.LoggerFactory;
\r
39 import org.w3c.dom.Document;
\r
42 * RemoteDocumentModelHandler
\r
45 * $LastChangedRevision: $
\r
46 * $LastChangedDate: $
\r
47 * @param <T> The {DocumentType}Common class
\r
48 * @param <TL> The {DocumentType}CommonList class
\r
50 public abstract class RemoteSubItemDocumentModelHandlerImpl<T, TL> extends
\r
51 RemoteDocumentModelHandlerImpl<T, TL> {
\r
53 private final Logger logger = LoggerFactory.getLogger(RemoteSubItemDocumentModelHandlerImpl.class);
\r
54 private final String SI_LABEL = "subitem";
\r
55 // We must align this to the schema:
\r
56 // <xs:element name="owner" type="xs:string" />
\r
57 // <xs:element name="isPrimary" type="xs:boolean"/>
\r
58 // <xs:element name="order" type="xs:unsignedInt"/>
\r
59 private final String[] fields = {"owner", "isPrimary", "order"};
\r
62 * Override fillPart to handle the Subitem XML part into the Subitem document model
\r
63 * @param part to fill
\r
64 * @param docModel for the given object
\r
65 * @param partMeta metadata for the object to fill
\r
69 protected void fillPart(InputPart part, DocumentModel docModel,
\r
70 ObjectPartType partMeta, Action action, ServiceContext ctx)
\r
72 InputStream payload = part.getBody(InputStream.class, null);
\r
74 //check if this is an xml part
\r
75 // TODO - we could configure the parts that have subitem content,
\r
76 // and then check that here, so skip other parts.
\r
77 if(part.getMediaType().equals(MediaType.APPLICATION_XML_TYPE)){
\r
78 if(payload != null){
\r
79 Document document = DocumentUtils.parseDocument(payload, partMeta,
\r
80 false /*don't validate*/);
\r
81 //TODO: callback to handler if registered to validate the
\r
83 Map<String, Object> objectProps = DocumentUtils.parseProperties(document.getFirstChild());
\r
84 // Now pull out the subitem props and set them into the Subitem schema
\r
85 Map<String, Object> subitemProps = null;
\r
86 for(String key:fields){
\r
87 // Fetch and remove as we go, so can safely set remaining values below
\r
88 String value = (String)(objectProps.remove(key));
\r
90 if(subitemProps == null) {
\r
91 subitemProps = new HashMap<String, Object>();
\r
93 subitemProps.put(key, value);
\r
96 if(subitemProps != null) {
\r
97 docModel.setProperties(SI_LABEL, subitemProps);
\r
99 // Set all remaining values on the common part.
\r
100 docModel.setProperties(partMeta.getLabel(), objectProps);
\r
106 * extractPart extracts an XML object from given DocumentModel
\r
107 * This overridden form checks for schemas that extend subitem, and merges
\r
108 * in the subitem properties for that part.
\r
110 * @param schema of the object to extract
\r
111 * @param partMeta metadata for the object to extract
\r
112 * @throws Exception
\r
115 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
\r
117 Map<String, Object> map = extractPart( docModel, schema, partMeta, null );
\r
118 if(schemaHasSubItem(schema)) {
\r
119 extractPart(docModel, SI_LABEL, partMeta, map);
\r
124 // TODO HACK - should make this info be configured in the part metadata.
\r
125 public abstract boolean schemaHasSubItem(String schema);
\r