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