]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a35db35b5a1cf74808820cbe174c0e70c36586d1
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.nuxeo.client.java;
25
26 import java.io.InputStream;
27 import java.io.ByteArrayInputStream;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import org.collectionspace.services.client.PayloadInputPart;
32 import org.collectionspace.services.client.PoxPayloadIn;
33 import org.collectionspace.services.client.PoxPayloadOut;
34 import org.collectionspace.services.common.context.ServiceContext;
35 import org.collectionspace.services.common.document.DocumentUtils;
36 import org.collectionspace.services.config.service.ObjectPartType;
37
38 import org.jboss.resteasy.plugins.providers.multipart.InputPart;
39 import javax.ws.rs.core.MediaType;
40 import org.nuxeo.ecm.core.api.DocumentModel;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.w3c.dom.Document;
44
45 /**
46  * RemoteDocumentModelHandler
47  *
48  * @author pschmitz
49  * $LastChangedRevision: $
50  * $LastChangedDate: $
51  * @param <T> The {DocumentType}Common class
52  * @param <TL> The {DocumentType}CommonList class
53  */
54 public abstract class RemoteSubItemDocumentModelHandlerImpl<T, TL> extends
55                 RemoteDocumentModelHandlerImpl<T, TL> {
56
57     private final Logger logger = LoggerFactory.getLogger(RemoteSubItemDocumentModelHandlerImpl.class);
58     private final String SI_LABEL = "subitem";
59     // We must align this to the schema:
60     //   <xs:element name="owner" type="xs:string" />
61         //   <xs:element name="isPrimary" type="xs:boolean"/>
62         //   <xs:element name="order" type="xs:unsignedInt"/>
63     private final String[] fields = {"owner", "isPrimary", "order"};
64
65     /**
66      * Override fillPart to handle the Subitem XML part into the Subitem document model
67      * @param part to fill
68      * @param docModel for the given object
69      * @param partMeta metadata for the object to fill
70      * @throws Exception
71      */
72         @Override
73     protected void fillPart(PayloadInputPart part, DocumentModel docModel, 
74                                                 ObjectPartType partMeta, Action action, ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx)
75             throws Exception {
76                 ByteArrayInputStream bas = new ByteArrayInputStream(part.getElementBody().asXML().getBytes());
77         InputStream payload = bas;//part.getBody(/*InputStream.class, null*/);
78
79         //check if this is an xml part
80         // TODO - we could configure the parts that have subitem content, 
81         // and then check that here, so skip other parts.
82         if(part.getMediaType().equals(MediaType.APPLICATION_XML_TYPE)){
83             if(payload != null){
84                 Document document = DocumentUtils.parseDocument(payload, partMeta,
85                                 false /*don't validate*/);
86                 //TODO: callback to handler if registered to validate the
87                 //document
88                 Map<String, Object> objectProps = DocumentUtils.parseProperties(document.getFirstChild());
89                 // Now pull out the subitem props and set them into the Subitem schema
90                 Map<String, Object> subitemProps = null;
91                 for(String key:fields){
92                         // Fetch and remove as we go, so can safely set remaining values below
93                         String value = (String)(objectProps.remove(key));
94                         if(value!=null) {
95                         if(subitemProps == null) {
96                                 subitemProps = new HashMap<String, Object>();
97                         }
98                         subitemProps.put(key, value);
99                         }
100                 }
101                 if(subitemProps != null) {
102                         docModel.setProperties(SI_LABEL, subitemProps);
103                 }
104                 // Set all remaining values on the common part.
105                 docModel.setProperties(partMeta.getLabel(), objectProps);
106             }
107         }
108     }
109     
110     /**
111      * extractPart extracts an XML object from given DocumentModel
112      * This overridden form checks for schemas that extend subitem, and merges
113      * in the subitem properties for that part.
114      * @param docModel
115      * @param schema of the object to extract
116      * @param partMeta metadata for the object to extract
117      * @throws Exception
118      */
119         @Override
120     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
121             throws Exception {
122         Map<String, Object> map = extractPart( docModel, schema, partMeta, null ); 
123                 if(schemaHasSubItem(schema)) {
124                         extractPart(docModel, SI_LABEL, partMeta, map);
125                 }
126         return map;
127     }
128         
129         // TODO HACK - should make this info be configured in the part metadata.
130         public abstract boolean schemaHasSubItem(String schema);
131
132 }