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