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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 package org.collectionspace.services.nuxeo.client.java;
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;
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.
43 * $LastChangedRevision: $
46 public class RepositoryJavaClient implements RepositoryClient {
48 private final Logger logger = LoggerFactory.getLogger(RepositoryJavaClient.class);
50 public RepositoryJavaClient() {
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
63 public String create(String serviceName, String docType, DocumentHandler handler) throws BadRequestException, DocumentException {
65 if(serviceName == null){
66 throw new IllegalArgumentException("RemoteRepositoryClient.create: serviceName is missing");
69 throw new IllegalArgumentException("RemoteRepositoryClient.create: docType is missing");
72 throw new IllegalArgumentException("RemoteRepositoryClient.create: handler is missing");
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");
81 RepositoryInstance repoSession = null;
83 handler.prepare(Action.CREATE);
84 repoSession = getRepositorySession();
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);
100 if(logger.isDebugEnabled()){
101 logger.debug("Caught exception ", e);
103 throw new DocumentException(e);
105 if(repoSession != null){
106 releaseRepositorySession(repoSession);
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
119 public void get(String id, DocumentHandler handler) throws DocumentNotFoundException, DocumentException {
122 throw new IllegalArgumentException("RemoteRepositoryClient.get: handler is missing");
124 RepositoryInstance repoSession = null;
127 handler.prepare(Action.GET);
128 repoSession = getRepositorySession();
129 DocumentRef docRef = new IdRef(id);
130 DocumentModel doc = null;
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);
138 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
139 DocumentModelWrapper wrapDoc = new DocumentModelWrapper(doc);
140 handler.handle(Action.GET, wrapDoc);
141 }catch(IllegalArgumentException iae) {
143 }catch(DocumentException de){
146 if(logger.isDebugEnabled()){
147 logger.debug("Caught exception ", e);
149 throw new DocumentException(e);
151 if(repoSession != null){
152 releaseRepositorySession(repoSession);
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
165 public void getAll(String serviceName, DocumentHandler handler) throws DocumentNotFoundException, DocumentException {
166 if(serviceName == null){
167 throw new IllegalArgumentException("RemoteRepositoryClient.getAll: serviceName is missing");
170 throw new IllegalArgumentException("RemoteRepositoryClient.getAll: handler is missing");
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");
179 RepositoryInstance repoSession = null;
182 handler.prepare(Action.GET_ALL);
183 repoSession = getRepositorySession();
184 DocumentRef wsDocRef = new IdRef(nuxeoWspaceId);
185 DocumentModelList docList = repoSession.getChildren(wsDocRef);
187 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
188 DocumentModelListWrapper wrapDoc = new DocumentModelListWrapper(docList);
189 handler.handle(Action.GET_ALL, wrapDoc);
191 }catch(DocumentException de){
194 if(logger.isDebugEnabled()){
195 logger.debug("Caught exception ", e);
197 throw new DocumentException(e);
199 if(repoSession != null){
200 releaseRepositorySession(repoSession);
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
212 public void update(String id, DocumentHandler handler) throws BadRequestException, DocumentNotFoundException, DocumentException {
214 throw new BadRequestException("RemoteRepositoryClient.update: id is missing");
217 throw new IllegalArgumentException("RemoteRepositoryClient.update: handler is missing");
219 RepositoryInstance repoSession = null;
221 handler.prepare(Action.UPDATE);
222 repoSession = getRepositorySession();
223 DocumentRef docRef = new IdRef(id);
224 DocumentModel doc = null;
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);
232 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
233 DocumentModelWrapper wrapDoc = new DocumentModelWrapper(doc);
234 handler.handle(Action.UPDATE, wrapDoc);
235 repoSession.saveDocument(doc);
237 }catch(DocumentException de){
240 if(logger.isDebugEnabled()){
241 logger.debug("Caught exception ", e);
243 throw new DocumentException(e);
245 if(repoSession != null){
246 releaseRepositorySession(repoSession);
252 * delete a document from the Nuxeo repository
253 * @param id of the document
254 * @throws DocumentException
257 public void delete(String id) throws DocumentNotFoundException, DocumentException {
259 if(logger.isDebugEnabled()){
260 logger.debug("deleting document with id=" + id);
262 RepositoryInstance repoSession = null;
264 repoSession = getRepositorySession();
265 DocumentRef docRef = new IdRef(id);
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);
274 }catch(DocumentException de){
277 if(logger.isDebugEnabled()){
278 logger.debug("Caught exception ", e);
280 throw new DocumentException(e);
282 if(repoSession != null){
283 releaseRepositorySession(repoSession);
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());
300 private void releaseRepositorySession(RepositoryInstance repoSession) {
302 NuxeoClient client = NuxeoConnector.getInstance().getClient();
304 client.releaseRepository(repoSession);
306 logger.error("Could not close the repository session", e);
307 //no need to throw this service specific exception