]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3831c45658d57be452c3195c5bab294e7671b516
[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 package org.collectionspace.services.nuxeo.client.java;
19
20 import org.collectionspace.services.common.repository.RepositoryClient;
21 import org.collectionspace.services.common.ServiceMain;
22 import org.collectionspace.services.common.repository.BadRequestException;
23 import org.collectionspace.services.common.repository.DocumentNotFoundException;
24 import org.collectionspace.services.common.repository.DocumentHandler;
25 import org.collectionspace.services.common.repository.DocumentException;
26 import org.collectionspace.services.common.repository.DocumentHandler.Action;
27 import org.nuxeo.common.utils.IdUtils;
28 import org.nuxeo.ecm.core.api.ClientException;
29 import org.nuxeo.ecm.core.api.DocumentModel;
30 import org.nuxeo.ecm.core.api.DocumentModelList;
31 import org.nuxeo.ecm.core.api.DocumentRef;
32 import org.nuxeo.ecm.core.api.IdRef;
33 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
34 import org.nuxeo.ecm.core.client.NuxeoClient;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * RepositoryJavaClient is used to perform CRUD operations on documents
40  * in Nuxeo repository using Remote Java APIs. It uses @see DocumentHandler
41  * as IOHandler with the client.
42  *
43  * $LastChangedRevision: $
44  * $LastChangedDate: $
45  */
46 public class RepositoryJavaClient implements RepositoryClient {
47
48     private final Logger logger = LoggerFactory.getLogger(RepositoryJavaClient.class);
49
50     public RepositoryJavaClient() {
51     }
52
53     /**
54      * create document in the Nuxeo repository
55      * @param serviceName entity service for which document is created. this is used to find mapping
56      * to a Nuxeo workspace using service-config.xml
57      * @param docType of the document created
58      * @param handler should be used by the caller to provide and transform the document
59      * @return id in repository of the newly created document
60      * @throws DocumentException
61      */
62     @Override
63     public String create(String serviceName, String docType, DocumentHandler handler) throws BadRequestException, DocumentException {
64
65         if(serviceName == null){
66             throw new IllegalArgumentException("RemoteRepositoryClient.create: serviceName is missing");
67         }
68         if(docType == null){
69             throw new IllegalArgumentException("RemoteRepositoryClient.create: docType is missing");
70         }
71         if(handler == null){
72             throw new IllegalArgumentException("RemoteRepositoryClient.create: handler is missing");
73         }
74         ServiceMain smain = ServiceMain.getInstance();
75         String nuxeoWspaceId = smain.getWorkspaceId(serviceName);
76         if(nuxeoWspaceId == null){
77             throw new DocumentNotFoundException("Unable to find workspace for service " + serviceName +
78                     " check if the mapping exists in service-config.xml or " +
79                     " the the mapped workspace exists in the Nuxeo repository");
80         }
81         RepositoryInstance repoSession = null;
82         try{
83             handler.prepare(Action.CREATE);
84             repoSession = getRepositorySession();
85
86             DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);
87             DocumentModel wspaceDoc = repoSession.getDocument(nuxeoWspace);
88             String wspacePath = wspaceDoc.getPathAsString();
89             String id = IdUtils.generateId("New " + docType);
90             //create document model
91             DocumentModel doc = repoSession.createDocumentModel(wspacePath, id, docType);
92             ((DocumentModelHandler) handler).setRepositorySession(repoSession);
93             DocumentModelWrapper wrapDoc = new DocumentModelWrapper(doc);
94             handler.handle(Action.CREATE, wrapDoc);
95             //create document with documentmodel
96             doc = repoSession.createDocument(doc);
97             repoSession.save();
98             return doc.getId();
99         }catch(Exception e){
100             if(logger.isDebugEnabled()){
101                 logger.debug("Caught exception ", e);
102             }
103             throw new DocumentException(e);
104         }finally{
105             if(repoSession != null){
106                 releaseRepositorySession(repoSession);
107             }
108         }
109
110     }
111
112     /**
113      * get document from the Nuxeo repository
114      * @param id of the document to retrieve
115      * @param handler should be used by the caller to provide and transform the document
116      * @throws DocumentException
117      */
118     @Override
119     public void get(String id, DocumentHandler handler) throws DocumentNotFoundException, DocumentException {
120
121         if(handler == null){
122             throw new IllegalArgumentException("RemoteRepositoryClient.get: handler is missing");
123         }
124         RepositoryInstance repoSession = null;
125
126         try{
127             handler.prepare(Action.GET);
128             repoSession = getRepositorySession();
129             DocumentRef docRef = new IdRef(id);
130             DocumentModel doc = null;
131             try{
132                 doc = repoSession.getDocument(docRef);
133             }catch(ClientException ce){
134                 String msg = "could not find document with id=" + id;
135                 logger.error(msg, ce);
136                 throw new DocumentNotFoundException(msg, ce);
137             }
138             ((DocumentModelHandler) handler).setRepositorySession(repoSession);
139             DocumentModelWrapper wrapDoc = new DocumentModelWrapper(doc);
140             handler.handle(Action.GET, wrapDoc);
141                     }catch(IllegalArgumentException iae) {
142             throw iae;
143         }catch(DocumentException de){
144             throw de;
145         }catch(Exception e){
146             if(logger.isDebugEnabled()){
147                 logger.debug("Caught exception ", e);
148             }
149             throw new DocumentException(e);
150         }finally{
151             if(repoSession != null){
152                 releaseRepositorySession(repoSession);
153             }
154         }
155     }
156
157     /**
158      * getAll get all documents for an entity entity service from the Nuxeo repository
159      * @param serviceName entity service for which documents are retrieved. this is used to find mapping
160      * to a Nuxeo workspace using service-config.xml
161      * @param handler should be used by the caller to provide and transform the document
162      * @throws DocumentException
163      */
164     @Override
165     public void getAll(String serviceName, DocumentHandler handler) throws DocumentNotFoundException, DocumentException {
166         if(serviceName == null){
167             throw new IllegalArgumentException("RemoteRepositoryClient.getAll: serviceName is missing");
168         }
169         if(handler == null){
170             throw new IllegalArgumentException("RemoteRepositoryClient.getAll: handler is missing");
171         }
172         ServiceMain smain = ServiceMain.getInstance();
173         String nuxeoWspaceId = smain.getWorkspaceId(serviceName);
174         if(nuxeoWspaceId == null){
175             throw new DocumentNotFoundException("Unable to find workspace for service " + serviceName +
176                     " check if the mapping exists in service-config.xml or " +
177                     " the the mapped workspace exists in the Nuxeo repository");
178         }
179         RepositoryInstance repoSession = null;
180
181         try{
182             handler.prepare(Action.GET_ALL);
183             repoSession = getRepositorySession();
184             DocumentRef wsDocRef = new IdRef(nuxeoWspaceId);
185             DocumentModelList docList = repoSession.getChildren(wsDocRef);
186
187             ((DocumentModelHandler) handler).setRepositorySession(repoSession);
188             DocumentModelListWrapper wrapDoc = new DocumentModelListWrapper(docList);
189             handler.handle(Action.GET_ALL, wrapDoc);
190             
191         }catch(DocumentException de){
192             throw de;
193         }catch(Exception e){
194             if(logger.isDebugEnabled()){
195                 logger.debug("Caught exception ", e);
196             }
197             throw new DocumentException(e);
198         }finally{
199             if(repoSession != null){
200                 releaseRepositorySession(repoSession);
201             }
202         }
203     }
204
205     /**
206      * update given document in the Nuxeo repository
207      * @param id of the document
208      * @param handler should be used by the caller to provide and transform the document
209      * @throws DocumentException
210      */
211     @Override
212     public void update(String id, DocumentHandler handler) throws BadRequestException, DocumentNotFoundException, DocumentException {
213         if(id == null){
214             throw new BadRequestException("RemoteRepositoryClient.update: id is missing");
215         }
216         if(handler == null){
217             throw new IllegalArgumentException("RemoteRepositoryClient.update: handler is missing");
218         }
219         RepositoryInstance repoSession = null;
220         try{
221             handler.prepare(Action.UPDATE);
222             repoSession = getRepositorySession();
223             DocumentRef docRef = new IdRef(id);
224             DocumentModel doc = null;
225             try{
226                 doc = repoSession.getDocument(docRef);
227             }catch(ClientException ce){
228                 String msg = "Could not find document to update with id=" + id;
229                 logger.error(msg, ce);
230                 throw new DocumentNotFoundException(msg, ce);
231             }
232             ((DocumentModelHandler) handler).setRepositorySession(repoSession);
233             DocumentModelWrapper wrapDoc = new DocumentModelWrapper(doc);
234             handler.handle(Action.UPDATE, wrapDoc);
235             repoSession.saveDocument(doc);
236             repoSession.save();
237         }catch(DocumentException de){
238             throw de;
239         }catch(Exception e){
240             if(logger.isDebugEnabled()){
241                 logger.debug("Caught exception ", e);
242             }
243             throw new DocumentException(e);
244         }finally{
245             if(repoSession != null){
246                 releaseRepositorySession(repoSession);
247             }
248         }
249     }
250
251     /**
252      * delete a document from the Nuxeo repository
253      * @param id of the document
254      * @throws DocumentException
255      */
256     @Override
257     public void delete(String id) throws DocumentNotFoundException, DocumentException {
258
259         if(logger.isDebugEnabled()){
260             logger.debug("deleting document with id=" + id);
261         }
262         RepositoryInstance repoSession = null;
263         try{
264             repoSession = getRepositorySession();
265             DocumentRef docRef = new IdRef(id);
266             try{
267                 repoSession.removeDocument(docRef);
268             }catch(ClientException ce){
269                 String msg = "could not find document to delete with id=" + id;
270                 logger.error(msg, ce);
271                 throw new DocumentNotFoundException(msg, ce);
272             }
273             repoSession.save();
274         }catch(DocumentException de){
275             throw de;
276         }catch(Exception e){
277             if(logger.isDebugEnabled()){
278                 logger.debug("Caught exception ", e);
279             }
280             throw new DocumentException(e);
281         }finally{
282             if(repoSession != null){
283                 releaseRepositorySession(repoSession);
284             }
285         }
286     }
287
288     private RepositoryInstance getRepositorySession() throws Exception {
289         //FIXME: is it possible to reuse repository session?
290         //Authentication failures happen while trying to reuse the session
291         NuxeoClient client = NuxeoConnector.getInstance().getClient();
292         RepositoryInstance repoSession = client.openRepository();
293         if(logger.isDebugEnabled()){
294             logger.debug("getRepository() repository root: " +
295                     repoSession.getRootDocument());
296         }
297         return repoSession;
298     }
299
300     private void releaseRepositorySession(RepositoryInstance repoSession) {
301         try{
302             NuxeoClient client = NuxeoConnector.getInstance().getClient();
303             //release session
304             client.releaseRepository(repoSession);
305         }catch(Exception e){
306             logger.error("Could not close the repository session", e);
307             //no need to throw this service specific exception
308         }
309     }
310 }