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