]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0947ae0734db8219aa9eb5410dc4786e47681a3f
[tmp/jakarta-migration.git] /
1 /**\r
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
5 \r
6  *  http://www.collectionspace.org\r
7  *  http://wiki.collectionspace.org\r
8 \r
9  *  Copyright 2009 University of California at Berkeley\r
10 \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
13 \r
14  *  You may obtain a copy of the ECL 2.0 License at\r
15 \r
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt\r
17 \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
23  */\r
24 package org.collectionspace.services.nuxeo.client.java;\r
25 \r
26 import java.io.InputStream;\r
27 import java.util.HashMap;\r
28 import java.util.List;\r
29 import java.util.Map;\r
30 import java.util.Set;\r
31 import java.util.Map.Entry;\r
32 \r
33 import javax.ws.rs.core.MediaType;\r
34 \r
35 import org.collectionspace.services.common.context.MultipartServiceContext;\r
36 import org.collectionspace.services.common.document.DocumentUtils;\r
37 import org.collectionspace.services.common.document.DocumentWrapper;\r
38 import org.collectionspace.services.common.service.ObjectPartType;\r
39 import org.jboss.resteasy.plugins.providers.multipart.InputPart;\r
40 import org.nuxeo.ecm.core.api.DocumentModel;\r
41 import org.nuxeo.ecm.core.api.DocumentModelList;\r
42 import org.slf4j.Logger;\r
43 import org.slf4j.LoggerFactory;\r
44 import org.w3c.dom.Document;\r
45 \r
46 /**\r
47  * RemoteDocumentModelHandler\r
48  *\r
49  * @author pschmitz\r
50  * $LastChangedRevision: $\r
51  * $LastChangedDate: $\r
52  * @param <T> The {DocumentType}Common class\r
53  * @param <TL> The {DocumentType}CommonList class\r
54  */\r
55 public abstract class RemoteSubItemDocumentModelHandlerImpl<T, TL> extends\r
56                 RemoteDocumentModelHandlerImpl<T, TL> {\r
57 \r
58     private final Logger logger = LoggerFactory.getLogger(RemoteSubItemDocumentModelHandlerImpl.class);\r
59     private final String SI_LABEL = "subitem";\r
60     // We must align this to the schema:\r
61     //   <xs:element name="owner" type="xs:string" />\r
62         //   <xs:element name="isPrimary" type="xs:boolean"/>\r
63         //   <xs:element name="order" type="xs:unsignedInt"/>\r
64     private final String[] fields = {"owner", "isPrimary", "order"};\r
65 \r
66     /**\r
67      * Override fillPart to handle the Subitem XML part into the Subitem document model\r
68      * @param part to fill\r
69      * @param docModel for the given object\r
70      * @param partMeta metadata for the object to fill\r
71      * @throws Exception\r
72      */\r
73     protected void fillPart(InputPart part, DocumentModel docModel, ObjectPartType partMeta)\r
74             throws Exception {\r
75         InputStream payload = part.getBody(InputStream.class, null);\r
76 \r
77         //check if this is an xml part\r
78         // TODO - we could configure the parts that have subitem content, \r
79         // and then check that here, so skip other parts.\r
80         if(part.getMediaType().equals(MediaType.APPLICATION_XML_TYPE)){\r
81             if(payload != null){\r
82                 Document document = DocumentUtils.parseDocument(payload);\r
83                 //TODO: callback to handler if registered to validate the\r
84                 //document\r
85                 Map<String, Object> objectProps = DocumentUtils.parseProperties(document);\r
86                 // Now pull out the subitem props and set them into the Subitem schema\r
87                 Map<String, Object> subitemProps = null;\r
88                 for(String key:fields){\r
89                         // Fetch and remove as we go, so can safely set remaining values below\r
90                         String value = (String)(objectProps.remove(key));\r
91                         if(value!=null) {\r
92                         if(subitemProps == null) {\r
93                                 subitemProps = new HashMap<String, Object>();\r
94                         }\r
95                         subitemProps.put(key, value);\r
96                         }\r
97                 }\r
98                 if(subitemProps != null) {\r
99                         docModel.setProperties(SI_LABEL, subitemProps);\r
100                 }\r
101                 // Set all remaining values on the common part.\r
102                 docModel.setProperties(partMeta.getLabel(), objectProps);\r
103             }\r
104         }\r
105     }\r
106     \r
107 \r
108 }\r