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