]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
977d31b31329849736683c63fadc5d0f444cfa75
[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.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import java.util.Set;
32 import javax.ws.rs.core.MediaType;
33 import org.collectionspace.services.common.repository.DocumentWrapper;
34 import org.collectionspace.services.common.repository.AbstractDocumentHandler;
35 import org.collectionspace.services.common.repository.BadRequestException;
36 import org.collectionspace.services.common.repository.DocumentUtils;
37 import org.collectionspace.services.common.service.ObjectPartType;
38 import org.collectionspace.services.nuxeo.client.*;
39 import org.jboss.resteasy.plugins.providers.multipart.InputPart;
40 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
41 import org.nuxeo.ecm.core.api.DocumentModel;
42 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.w3c.dom.Document;
46
47 /**
48  * DocumentModelHandler is a base abstract Nuxeo document handler
49  * using Nuxeo Java Remote APIs for CollectionSpace services
50  *
51  * $LastChangedRevision: $
52  * $LastChangedDate: $
53  */
54 public abstract class DocumentModelHandler<T, TL>
55         extends AbstractDocumentHandler<T, TL> {
56
57     private final Logger logger = LoggerFactory.getLogger(DocumentModelHandler.class);
58     private RepositoryInstance repositorySession;
59     //key=schema, value=documentpart
60
61     /**
62      * getRepositorySession returns Nuxeo Repository Session
63      * @return
64      */
65     public RepositoryInstance getRepositorySession() {
66         return repositorySession;
67     }
68
69     /**
70      * setRepositorySession sets repository session
71      * @param repoSession
72      */
73     public void setRepositorySession(RepositoryInstance repoSession) {
74         this.repositorySession = repoSession;
75     }
76
77     @Override
78     public void handleCreate(DocumentWrapper wrapDoc) throws Exception {
79         fillAllParts(wrapDoc);
80     }
81
82     @Override
83     public void handleUpdate(DocumentWrapper wrapDoc) throws Exception {
84         fillAllParts(wrapDoc);
85     }
86
87     @Override
88     public void handleGet(DocumentWrapper wrapDoc) throws Exception {
89         extractAllParts(wrapDoc);
90     }
91
92     @Override
93     public void handleGetAll(DocumentWrapper wrapDoc) throws Exception {
94         setCommonPartList(extractCommonPartList(wrapDoc));
95     }
96
97     @Override
98     public void completeUpdate(DocumentWrapper wrapDoc) throws Exception {
99         DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject();
100         //return at least those document part(s) that were received
101         Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
102         List<InputPart> inputParts = getServiceContext().getInput().getParts();
103         for(InputPart part : inputParts){
104             String partLabel = part.getHeaders().getFirst("label");
105             ObjectPartType partMeta = partsMetaMap.get(partLabel);
106             extractObject(docModel, partLabel, partMeta);
107         }
108     }
109
110     @Override
111     public void extractAllParts(DocumentWrapper wrapDoc) throws Exception {
112
113         DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject();
114         String[] schemas = docModel.getDeclaredSchemas();
115         Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
116         for(String schema : schemas){
117             ObjectPartType partMeta = partsMetaMap.get(schema);
118             if(partMeta == null){
119                 continue; //unknown part, ignore
120             }
121             extractObject(docModel, schema, partMeta);
122         }
123     }
124
125     @Override
126     public abstract T extractCommonPart(DocumentWrapper wrapDoc) throws Exception;
127
128     @Override
129     public void fillAllParts(DocumentWrapper wrapDoc) throws Exception {
130
131         //TODO filling extension parts should be dynamic
132         //Nuxeo APIs lack to support stream/byte[] input, get/setting properties is
133         //not an ideal way of populating objects.
134         DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject();
135         MultipartInput input = getServiceContext().getInput();
136         if(input.getParts().isEmpty()){
137             String msg = "No payload found!";
138             logger.error(msg + "Ctx=" + getServiceContext().toString());
139             throw new BadRequestException(msg);
140         }
141
142         Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
143
144         //iterate over parts received and fill those parts
145         List<InputPart> inputParts = input.getParts();
146         for(InputPart part : inputParts){
147
148             String partLabel = part.getHeaders().getFirst("label");
149             //skip if the part is not in metadata
150             if(!partsMetaMap.containsKey(partLabel)){
151                 continue;
152             }
153             InputStream payload = part.getBody(InputStream.class, null);
154
155             //check if this is an xml part
156             if(part.getMediaType().equals(MediaType.APPLICATION_XML_TYPE)){
157                 if(payload != null){
158                     Document document = DocumentUtils.parseDocument(payload);
159                     //TODO: callback to handler if registered to validate the
160                     //document
161                     Map<String, Object> objectProps = DocumentUtils.parseProperties(document);
162                     docModel.setProperties(partLabel, objectProps);
163                 }
164             }
165         }//rof
166
167     }
168
169     @Override
170     public abstract void fillCommonPart(T obj, DocumentWrapper wrapDoc) throws Exception;
171
172     @Override
173     public abstract TL extractCommonPartList(DocumentWrapper wrapDoc) throws Exception;
174
175     @Override
176     public abstract T getCommonPart();
177
178     @Override
179     public abstract void setCommonPart(T obj);
180
181     @Override
182     public abstract TL getCommonPartList();
183
184     @Override
185     public abstract void setCommonPartList(TL obj);
186
187     /* (non-Javadoc)
188      * @see org.collectionspace.services.common.repository.DocumentHandler#getDocumentType()
189      */
190     @Override
191     public abstract String getDocumentType();
192
193     /**
194      * extractObject extracts an XML object from given DocumentModel
195      * @param docModel
196      * @param schema of the object to extract
197      * @param partMeta metadata for the object to extract
198      * @throws Exception
199      */
200     protected void extractObject(DocumentModel docModel, String schema, ObjectPartType partMeta)
201             throws Exception {
202         MediaType mt = MediaType.valueOf(partMeta.getContent().getContentType());
203         if(mt.equals(MediaType.APPLICATION_XML_TYPE)){
204             Map<String, Object> objectProps = docModel.getProperties(schema);
205             //unqualify properties before sending the doc over the wire (to save bandwidh)
206             //FIXME: is there a better way to avoid duplication of a collection?
207             Map<String, Object> unQObjectProperties = new HashMap<String, Object>();
208             Set<Entry<String, Object>> qualifiedEntries = objectProps.entrySet();
209             for(Entry<String, Object> entry : qualifiedEntries){
210                 String unqProp = getUnQProperty(entry.getKey());
211                 unQObjectProperties.put(unqProp, entry.getValue());
212             }
213             Document doc = DocumentUtils.buildDocument(partMeta, schema, unQObjectProperties);
214             if(logger.isDebugEnabled()){
215                 DocumentUtils.writeDocument(doc, System.out);
216             }
217             getServiceContext().addOutputPart(schema, doc, partMeta.getContent().getContentType());
218         } //TODO: handle other media types
219     }
220
221 }