]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4685928db4b2bfa7e6985ae74130d79cba8d0ab1
[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, ObjectPartType partMeta)\r
69             throws Exception {\r
70         InputStream payload = part.getBody(InputStream.class, null);\r
71 \r
72         //check if this is an xml part\r
73         // TODO - we could configure the parts that have subitem content, \r
74         // and then check that here, so skip other parts.\r
75         if(part.getMediaType().equals(MediaType.APPLICATION_XML_TYPE)){\r
76             if(payload != null){\r
77                 Document document = DocumentUtils.parseDocument(payload, partMeta);\r
78                 //TODO: callback to handler if registered to validate the\r
79                 //document\r
80                 Map<String, Object> objectProps = DocumentUtils.parseProperties(document.getFirstChild());\r
81                 // Now pull out the subitem props and set them into the Subitem schema\r
82                 Map<String, Object> subitemProps = null;\r
83                 for(String key:fields){\r
84                         // Fetch and remove as we go, so can safely set remaining values below\r
85                         String value = (String)(objectProps.remove(key));\r
86                         if(value!=null) {\r
87                         if(subitemProps == null) {\r
88                                 subitemProps = new HashMap<String, Object>();\r
89                         }\r
90                         subitemProps.put(key, value);\r
91                         }\r
92                 }\r
93                 if(subitemProps != null) {\r
94                         docModel.setProperties(SI_LABEL, subitemProps);\r
95                 }\r
96                 // Set all remaining values on the common part.\r
97                 docModel.setProperties(partMeta.getLabel(), objectProps);\r
98             }\r
99         }\r
100     }\r
101     \r
102     /**\r
103      * extractPart extracts an XML object from given DocumentModel\r
104      * This overridden form checks for schemas that extend subitem, and merges\r
105      * in the subitem properties for that part.\r
106      * @param docModel\r
107      * @param schema of the object to extract\r
108      * @param partMeta metadata for the object to extract\r
109      * @throws Exception\r
110      */\r
111         @Override\r
112     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)\r
113             throws Exception {\r
114         Map<String, Object> map = extractPart( docModel, schema, partMeta, null ); \r
115                 if(schemaHasSubItem(schema)) {\r
116                         extractPart(docModel, SI_LABEL, partMeta, map);\r
117                 }\r
118         return map;\r
119     }\r
120         \r
121         // TODO HACK - should make this info be configured in the part metadata.\r
122         public abstract boolean schemaHasSubItem(String schema);\r
123 \r
124 }\r