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 java.util.UUID;
21 import java.util.List;
23 import org.collectionspace.services.common.ServiceMain;
24 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
25 import org.collectionspace.services.common.context.ServiceContext;
26 import org.collectionspace.services.common.document.BadRequestException;
27 import org.collectionspace.services.common.document.DocumentException;
28 import org.collectionspace.services.common.document.DocumentFilter;
29 import org.collectionspace.services.common.document.DocumentHandler;
30 import org.collectionspace.services.common.document.DocumentNotFoundException;
31 import org.collectionspace.services.common.repository.RepositoryClient;
32 import org.collectionspace.services.common.document.DocumentHandler.Action;
33 import org.collectionspace.services.common.document.DocumentWrapper;
34 import org.collectionspace.services.common.document.DocumentWrapperImpl;
35 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
36 import org.nuxeo.common.utils.IdUtils;
37 import org.nuxeo.ecm.core.api.ClientException;
38 import org.nuxeo.ecm.core.api.DocumentModel;
39 import org.nuxeo.ecm.core.api.DocumentModelList;
40 import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
41 import org.nuxeo.ecm.core.api.DocumentRef;
42 import org.nuxeo.ecm.core.api.IdRef;
43 import org.nuxeo.ecm.core.api.PathRef;
44 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
45 import org.nuxeo.ecm.core.client.NuxeoClient;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * RepositoryJavaClient is used to perform CRUD operations on documents in Nuxeo
51 * repository using Remote Java APIs. It uses @see DocumentHandler as IOHandler
54 * $LastChangedRevision: $ $LastChangedDate: $
56 public class RepositoryJavaClientImpl implements RepositoryClient {
59 * The Class QueryContext.
61 private class QueryContext {
66 /** The doc filter. */
67 DocumentFilter docFilter;
69 /** The where clause. */
79 * Instantiates a new query context.
82 * @throws DocumentNotFoundException the document not found exception
83 * @throws DocumentException the document exception
85 QueryContext(ServiceContext ctx) throws DocumentNotFoundException, DocumentException {
86 docType = ctx.getDocumentType();
87 if (docType == null) {
88 throw new DocumentNotFoundException(
89 "Unable to find DocumentType for service " + ctx.getServiceName());
91 domain = ctx.getRepositoryDomainName();
93 throw new DocumentNotFoundException(
94 "Unable to find Domain for service " + ctx.getServiceName());
96 tenantId = ctx.getTenantId();
97 if (tenantId == null) {
98 throw new IllegalArgumentException(
99 "Service context has no Tenant ID specified.");
104 // * Instantiates a new query context.
106 // * @param whereClause the where clause
107 // * @param theDomain the the domain
108 // * @param theTenantId the the tenant id
110 // QueryContext(String theWhereClause, String theDomain, String theTenantId) {
111 // whereClause = theWhereClause;
112 // domain = theDomain;
113 // tenantId = theTenantId;
117 * Instantiates a new query context.
120 * @param theWhereClause the the where clause
121 * @throws DocumentNotFoundException the document not found exception
122 * @throws DocumentException the document exception
124 QueryContext(ServiceContext ctx, String theWhereClause) throws DocumentNotFoundException, DocumentException {
126 whereClause = theWhereClause;
130 * Instantiates a new query context.
133 * @param handler the handler
134 * @throws DocumentNotFoundException the document not found exception
135 * @throws DocumentException the document exception
137 QueryContext(ServiceContext ctx, DocumentHandler handler) throws DocumentNotFoundException, DocumentException {
139 if (handler == null) {
140 throw new IllegalArgumentException(
141 "Document handler is missing.");
143 docFilter = handler.getDocumentFilter();
144 if (docFilter == null) {
145 throw new IllegalArgumentException(
146 "Document handler has no Filter specified.");
148 whereClause = docFilter.getWhereClause();
153 private final Logger logger = LoggerFactory.getLogger(RepositoryJavaClientImpl.class);
156 * Instantiates a new repository java client impl.
158 public RepositoryJavaClientImpl() {
163 * create document in the Nuxeo repository
165 * @param ctx service context under which this method is invoked
167 * of the document created
169 * should be used by the caller to provide and transform the
171 * @return id in repository of the newly created document
172 * @throws DocumentException
175 public String create(ServiceContext ctx,
176 DocumentHandler handler) throws BadRequestException,
179 if (ctx.getDocumentType() == null) {
180 throw new IllegalArgumentException(
181 "RepositoryJavaClient.create: docType is missing");
183 if (handler == null) {
184 throw new IllegalArgumentException(
185 "RepositoryJavaClient.create: handler is missing");
187 String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
188 if (nuxeoWspaceId == null) {
189 throw new DocumentNotFoundException(
190 "Unable to find workspace for service " + ctx.getServiceName()
191 + " check if the workspace exists in the Nuxeo repository");
193 RepositoryInstance repoSession = null;
195 handler.prepare(Action.CREATE);
196 repoSession = getRepositorySession();
197 DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);
198 DocumentModel wspaceDoc = repoSession.getDocument(nuxeoWspace);
199 String wspacePath = wspaceDoc.getPathAsString();
200 //give our own ID so PathRef could be constructed later on
201 String id = IdUtils.generateId(UUID.randomUUID().toString());
202 // create document model
203 DocumentModel doc = repoSession.createDocumentModel(wspacePath, id,
204 ctx.getDocumentType());
205 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
206 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
207 handler.handle(Action.CREATE, wrapDoc);
208 // create document with documentmodel
209 doc = repoSession.createDocument(doc);
211 handler.complete(Action.CREATE, wrapDoc);
213 } catch (BadRequestException bre) {
215 } catch (Exception e) {
216 if (logger.isDebugEnabled()) {
217 logger.debug("Caught exception ", e);
219 throw new DocumentException(e);
221 if (repoSession != null) {
222 releaseRepositorySession(repoSession);
229 * get document from the Nuxeo repository
230 * @param ctx service context under which this method is invoked
232 * of the document to retrieve
234 * should be used by the caller to provide and transform the
236 * @throws DocumentException
239 public void get(ServiceContext ctx, String id, DocumentHandler handler)
240 throws DocumentNotFoundException, DocumentException {
242 if (handler == null) {
243 throw new IllegalArgumentException(
244 "RepositoryJavaClient.get: handler is missing");
246 RepositoryInstance repoSession = null;
249 handler.prepare(Action.GET);
250 repoSession = getRepositorySession();
251 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
252 DocumentModel doc = null;
254 doc = repoSession.getDocument(docRef);
255 } catch (ClientException ce) {
256 String msg = "could not find document with id=" + id;
257 logger.error(msg, ce);
258 throw new DocumentNotFoundException(msg, ce);
260 //set reposession to handle the document
261 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
262 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
263 handler.handle(Action.GET, wrapDoc);
264 handler.complete(Action.GET, wrapDoc);
265 } catch (IllegalArgumentException iae) {
267 } catch (DocumentException de) {
269 } catch (Exception e) {
270 if (logger.isDebugEnabled()) {
271 logger.debug("Caught exception ", e);
273 throw new DocumentException(e);
275 if (repoSession != null) {
276 releaseRepositorySession(repoSession);
282 * get document from the Nuxeo repository, using the docFilter params.
283 * @param ctx service context under which this method is invoked
285 * should be used by the caller to provide and transform the
286 * document. Handler must have a docFilter set to return a single item.
287 * @throws DocumentException
290 public void get(ServiceContext ctx, DocumentHandler handler)
291 throws DocumentNotFoundException, DocumentException {
292 QueryContext queryContext = new QueryContext(ctx, handler);
293 RepositoryInstance repoSession = null;
296 handler.prepare(Action.GET);
297 repoSession = getRepositorySession();
299 DocumentModelList docList = null;
300 // force limit to 1, and ignore totalSize
301 String query = buildNXQLQuery(queryContext);
302 docList = repoSession.query(query, null, 1, 0, false);
303 if (docList.size() != 1) {
304 throw new DocumentNotFoundException("No document found matching filter params.");
306 DocumentModel doc = docList.get(0);
308 if (logger.isDebugEnabled()) {
309 logger.debug("Executed NXQL query: " + query);
312 //set reposession to handle the document
313 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
314 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
315 handler.handle(Action.GET, wrapDoc);
316 handler.complete(Action.GET, wrapDoc);
317 } catch (IllegalArgumentException iae) {
319 } catch (DocumentException de) {
321 } catch (Exception e) {
322 if (logger.isDebugEnabled()) {
323 logger.debug("Caught exception ", e);
325 throw new DocumentException(e);
327 if (repoSession != null) {
328 releaseRepositorySession(repoSession);
334 * get wrapped documentModel from the Nuxeo repository
335 * @param ctx service context under which this method is invoked
337 * of the document to retrieve
338 * @throws DocumentException
341 public DocumentWrapper<DocumentModel> getDoc(
342 ServiceContext ctx, String id)
343 throws DocumentNotFoundException, DocumentException {
344 RepositoryInstance repoSession = null;
345 DocumentWrapper<DocumentModel> wrapDoc = null;
348 repoSession = getRepositorySession();
349 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
350 DocumentModel doc = null;
352 doc = repoSession.getDocument(docRef);
353 } catch (ClientException ce) {
354 String msg = "could not find document with id=" + id;
355 logger.error(msg, ce);
356 throw new DocumentNotFoundException(msg, ce);
358 wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
359 } catch (IllegalArgumentException iae) {
361 } catch (DocumentException de) {
363 } catch (Exception e) {
364 if (logger.isDebugEnabled()) {
365 logger.debug("Caught exception ", e);
367 throw new DocumentException(e);
369 if (repoSession != null) {
370 releaseRepositorySession(repoSession);
382 String getDomain(ServiceContext ctx) {
383 String result = null;
384 TenantBindingConfigReaderImpl tReader =
385 ServiceMain.getInstance().getTenantBindingConfigReader();
387 tReader.getTenantBinding(ctx.getTenantId()).getRepositoryDomain();
392 * find wrapped documentModel from the Nuxeo repository
393 * @param ctx service context under which this method is invoked
394 * @param where NXQL where clause to get the document
395 * @throws DocumentException
398 public DocumentWrapper<DocumentModel> findDoc(
399 ServiceContext ctx, String whereClause)
400 throws DocumentNotFoundException, DocumentException {
401 RepositoryInstance repoSession = null;
402 DocumentWrapper<DocumentModel> wrapDoc = null;
405 QueryContext queryContext = new QueryContext(ctx, whereClause);
406 repoSession = getRepositorySession();
407 DocumentModelList docList = null;
408 // force limit to 1, and ignore totalSize
409 String query = buildNXQLQuery(queryContext);
410 docList = repoSession.query(query,
415 if (docList.size() != 1) {
416 if (logger.isDebugEnabled()) {
417 logger.debug("findDoc: Query found: " + docList.size() + " items.");
418 logger.debug(" Query: " + query);
420 throw new DocumentNotFoundException("No document found matching filter params.");
422 DocumentModel doc = docList.get(0);
423 wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
424 } catch (IllegalArgumentException iae) {
426 } catch (DocumentException de) {
428 } catch (Exception e) {
429 if (logger.isDebugEnabled()) {
430 logger.debug("Caught exception ", e);
432 throw new DocumentException(e);
434 if (repoSession != null) {
435 releaseRepositorySession(repoSession);
442 * find doc and return CSID from the Nuxeo repository
443 * @param ctx service context under which this method is invoked
444 * @param where NXQL where clause to get the document
445 * @throws DocumentException
447 public String findDocCSID(
448 ServiceContext ctx, String whereClause)
449 throws DocumentNotFoundException, DocumentException {
452 DocumentWrapper<DocumentModel> wrapDoc = findDoc(ctx, whereClause);
453 DocumentModel docModel = wrapDoc.getWrappedObject();
454 csid = NuxeoUtils.extractId(docModel.getPathAsString());
455 } catch (DocumentNotFoundException dnfe) {
457 } catch (IllegalArgumentException iae) {
459 } catch (DocumentException de) {
461 } catch (Exception e) {
462 if (logger.isDebugEnabled()) {
463 logger.debug("Caught exception ", e);
465 throw new DocumentException(e);
471 * Find a list of documentModels from the Nuxeo repository
472 * @param docTypes a list of DocType names to match
473 * @param where the clause to qualify on
474 * @param domain the domain for the associated services
478 public DocumentWrapper<DocumentModelList> findDocs(
480 List<String> docTypes,
482 int pageSize, int pageNum, boolean computeTotal)
483 throws DocumentNotFoundException, DocumentException {
484 RepositoryInstance repoSession = null;
485 DocumentWrapper<DocumentModelList> wrapDoc = null;
488 if (docTypes == null || docTypes.size() < 1) {
489 throw new DocumentNotFoundException(
490 "findDocs must specify at least one DocumentType.");
492 repoSession = getRepositorySession();
493 DocumentModelList docList = null;
494 // force limit to 1, and ignore totalSize
495 QueryContext queryContext = new QueryContext(ctx, whereClause);
496 // QueryContext queryContext = new QueryContext(where, this.getDomain(ctx), ctx.getTenantId());
497 String query = buildNXQLQuery(docTypes, queryContext);
498 docList = repoSession.query(query, null, pageSize, pageNum, computeTotal);
499 wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
500 } catch (IllegalArgumentException iae) {
502 } catch (Exception e) {
503 if (logger.isDebugEnabled()) {
504 logger.debug("Caught exception ", e);
506 throw new DocumentException(e);
508 if (repoSession != null) {
509 releaseRepositorySession(repoSession);
516 * @see org.collectionspace.services.common.storage.StorageClient#get(org.collectionspace.services.common.context.ServiceContext, java.util.List, org.collectionspace.services.common.document.DocumentHandler)
519 public void get(ServiceContext ctx, List<String> csidList, DocumentHandler handler)
520 throws DocumentNotFoundException, DocumentException {
521 if (handler == null) {
522 throw new IllegalArgumentException(
523 "RepositoryJavaClient.getAll: handler is missing");
526 RepositoryInstance repoSession = null;
529 handler.prepare(Action.GET_ALL);
530 repoSession = getRepositorySession();
531 DocumentModelList docModelList = new DocumentModelListImpl();
532 //FIXME: Should be using NuxeoUtils.createPathRef for security reasons
533 for (String csid : csidList) {
534 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
535 DocumentModel docModel = repoSession.getDocument(docRef);
536 docModelList.add(docModel);
539 //set reposession to handle the document
540 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
541 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docModelList);
542 handler.handle(Action.GET_ALL, wrapDoc);
543 handler.complete(Action.GET_ALL, wrapDoc);
544 } catch (DocumentException de) {
546 } catch (Exception e) {
547 if (logger.isDebugEnabled()) {
548 logger.debug("Caught exception ", e);
550 throw new DocumentException(e);
552 if (repoSession != null) {
553 releaseRepositorySession(repoSession);
559 * getAll get all documents for an entity entity service from the Nuxeo
562 * @param ctx service context under which this method is invoked
564 * should be used by the caller to provide and transform the
566 * @throws DocumentException
569 public void getAll(ServiceContext ctx, DocumentHandler handler)
570 throws DocumentNotFoundException, DocumentException {
571 if (handler == null) {
572 throw new IllegalArgumentException(
573 "RepositoryJavaClient.getAll: handler is missing");
575 String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
576 if (nuxeoWspaceId == null) {
577 throw new DocumentNotFoundException(
578 "Unable to find workspace for service "
579 + ctx.getServiceName()
580 + " check if the workspace exists in the Nuxeo repository");
582 RepositoryInstance repoSession = null;
585 handler.prepare(Action.GET_ALL);
586 repoSession = getRepositorySession();
587 DocumentRef wsDocRef = new IdRef(nuxeoWspaceId);
588 DocumentModelList docList = repoSession.getChildren(wsDocRef);
589 //set reposession to handle the document
590 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
591 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
592 handler.handle(Action.GET_ALL, wrapDoc);
593 handler.complete(Action.GET_ALL, wrapDoc);
594 } catch (DocumentException de) {
596 } catch (Exception e) {
597 if (logger.isDebugEnabled()) {
598 logger.debug("Caught exception ", e);
600 throw new DocumentException(e);
602 if (repoSession != null) {
603 releaseRepositorySession(repoSession);
609 * getFiltered get all documents for an entity service from the Document repository,
610 * given filter parameters specified by the handler.
611 * @param ctx service context under which this method is invoked
612 * @param handler should be used by the caller to provide and transform the document
613 * @throws DocumentNotFoundException if workspace not found
614 * @throws DocumentException
616 public void getFiltered(ServiceContext ctx, DocumentHandler handler)
617 throws DocumentNotFoundException, DocumentException {
618 QueryContext queryContext = new QueryContext(ctx, handler);
620 RepositoryInstance repoSession = null;
622 handler.prepare(Action.GET_ALL);
623 repoSession = getRepositorySession();
624 DocumentModelList docList = null;
625 String query = buildNXQLQuery(queryContext);
627 // If we have limit and/or offset, then pass true to get totalSize
628 // in returned DocumentModelList.
629 if ((queryContext.docFilter.getOffset() > 0) || (queryContext.docFilter.getPageSize() > 0)) {
630 docList = repoSession.query(query, null,
631 queryContext.docFilter.getPageSize(), queryContext.docFilter.getOffset(), true);
633 docList = repoSession.query(query);
636 if (logger.isDebugEnabled()) {
637 logger.debug("Executed NXQL query: " + query.toString());
640 //set repoSession to handle the document
641 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
642 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
643 handler.handle(Action.GET_ALL, wrapDoc);
644 handler.complete(Action.GET_ALL, wrapDoc);
645 } catch (DocumentException de) {
647 } catch (Exception e) {
648 if (logger.isDebugEnabled()) {
649 logger.debug("Caught exception ", e);
651 throw new DocumentException(e);
653 if (repoSession != null) {
654 releaseRepositorySession(repoSession);
660 * update given document in the Nuxeo repository
662 * @param ctx service context under which this method is invoked
666 * should be used by the caller to provide and transform the
668 * @throws DocumentException
671 public void update(ServiceContext ctx, String id, DocumentHandler handler)
672 throws BadRequestException, DocumentNotFoundException,
674 if (handler == null) {
675 throw new IllegalArgumentException(
676 "RepositoryJavaClient.update: handler is missing");
678 RepositoryInstance repoSession = null;
680 handler.prepare(Action.UPDATE);
681 repoSession = getRepositorySession();
682 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
683 DocumentModel doc = null;
685 doc = repoSession.getDocument(docRef);
686 } catch (ClientException ce) {
687 String msg = "Could not find document to update with id=" + id;
688 logger.error(msg, ce);
689 throw new DocumentNotFoundException(msg, ce);
691 //set reposession to handle the document
692 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
693 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
694 handler.handle(Action.UPDATE, wrapDoc);
695 repoSession.saveDocument(doc);
697 handler.complete(Action.UPDATE, wrapDoc);
698 } catch (BadRequestException bre) {
700 } catch (DocumentException de) {
702 } catch (Exception e) {
703 if (logger.isDebugEnabled()) {
704 logger.debug("Caught exception ", e);
706 throw new DocumentException(e);
708 if (repoSession != null) {
709 releaseRepositorySession(repoSession);
715 * delete a document from the Nuxeo repository
716 * @param ctx service context under which this method is invoked
719 * @throws DocumentException
722 public void delete(ServiceContext ctx, String id) throws DocumentNotFoundException,
725 if (logger.isDebugEnabled()) {
726 logger.debug("deleting document with id=" + id);
728 RepositoryInstance repoSession = null;
730 repoSession = getRepositorySession();
731 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
733 repoSession.removeDocument(docRef);
734 } catch (ClientException ce) {
735 String msg = "could not find document to delete with id=" + id;
736 logger.error(msg, ce);
737 throw new DocumentNotFoundException(msg, ce);
740 } catch (DocumentException de) {
742 } catch (Exception e) {
743 if (logger.isDebugEnabled()) {
744 logger.debug("Caught exception ", e);
746 throw new DocumentException(e);
748 if (repoSession != null) {
749 releaseRepositorySession(repoSession);
755 * @see org.collectionspace.services.common.storage.StorageClient#delete(org.collectionspace.services.common.context.ServiceContext, java.lang.String, org.collectionspace.services.common.document.DocumentHandler)
758 public void delete(ServiceContext ctx, String id, DocumentHandler handler)
759 throws DocumentNotFoundException, DocumentException {
760 throw new UnsupportedOperationException();
764 * @see org.collectionspace.services.common.repository.RepositoryClient#createWorkspace(java.lang.String, java.lang.String)
767 public String createWorkspace(String tenantDomain, String workspaceName) throws Exception {
768 RepositoryInstance repoSession = null;
769 String workspaceId = null;
771 repoSession = getRepositorySession();
772 DocumentRef docRef = new PathRef(
774 + "/" + "workspaces");
775 DocumentModel parent = repoSession.getDocument(docRef);
776 DocumentModel doc = repoSession.createDocumentModel(parent.getPathAsString(),
777 workspaceName, "Workspace");
778 doc.setPropertyValue("dc:title", workspaceName);
779 doc.setPropertyValue("dc:description", "A CollectionSpace workspace for "
781 doc = repoSession.createDocument(doc);
782 workspaceId = doc.getId();
784 if (logger.isDebugEnabled()) {
785 logger.debug("created workspace name=" + workspaceName
786 + " id=" + workspaceId);
788 } catch (Exception e) {
789 if (logger.isDebugEnabled()) {
790 logger.debug("createWorkspace caught exception ", e);
794 if (repoSession != null) {
795 releaseRepositorySession(repoSession);
802 * @see org.collectionspace.services.common.repository.RepositoryClient#getWorkspaceId(java.lang.String, java.lang.String)
805 public String getWorkspaceId(String tenantDomain, String workspaceName) throws Exception {
806 String workspaceId = null;
807 RepositoryInstance repoSession = null;
809 repoSession = getRepositorySession();
810 DocumentRef docRef = new PathRef(
813 + "/" + workspaceName);
814 DocumentModel workspace = repoSession.getDocument(docRef);
815 workspaceId = workspace.getId();
816 } catch (DocumentException de) {
818 } catch (Exception e) {
819 if (logger.isDebugEnabled()) {
820 logger.debug("Caught exception ", e);
822 throw new DocumentException(e);
824 if (repoSession != null) {
825 releaseRepositorySession(repoSession);
834 * @param query the query
835 * @param where the where
836 * @param domain the domain
838 private final void appendNXQLWhere(StringBuilder query, QueryContext queryContext) {
839 // TODO This is a slow method for tenant-filter
840 // We should make this a property that is indexed.
841 query.append(" WHERE ecm:path STARTSWITH '/" + queryContext.domain + "'");
842 // query.append(" AND collectionspace_core:tenantId = " + tenantId);//FIXME: REM - Uncomment this and add a constant for the collectionspace_core schema and tenantId element
843 String whereClause = queryContext.whereClause;
844 if (whereClause != null && whereClause.length() > 0) {
845 // Due to an apparent bug/issue in how Nuxeo translates the NXQL query string
846 // into SQL, we need to parenthesize our 'where' clause
847 query.append(" AND " + "(" + whereClause + ")");
849 query.append(" AND ecm:isProxy = 0");
853 * Builds the nxql query.
855 * @param docType the doc type
856 * @param where the where
857 * @param domain the domain
858 * @param tenantId the tenant id
861 private final String buildNXQLQuery(QueryContext queryContext) {
862 StringBuilder query = new StringBuilder("SELECT * FROM ");
863 query.append(queryContext.docType);
864 appendNXQLWhere(query, queryContext);
865 return query.toString();
869 * Builds the nxql query.
871 * @param docTypes the doc types
872 * @param where the where
873 * @param domain the domain
876 private final String buildNXQLQuery(List<String> docTypes, QueryContext queryContext) {
877 StringBuilder query = new StringBuilder("SELECT * FROM ");
878 boolean fFirst = true;
879 for (String docType : docTypes) {
885 query.append(docType);
887 appendNXQLWhere(query, queryContext);
888 return query.toString();
892 * Gets the repository session.
894 * @return the repository session
895 * @throws Exception the exception
897 private RepositoryInstance getRepositorySession() throws Exception {
898 // FIXME: is it possible to reuse repository session?
899 // Authentication failures happen while trying to reuse the session
900 NuxeoClient client = NuxeoConnector.getInstance().getClient();
901 RepositoryInstance repoSession = client.openRepository();
902 if (logger.isDebugEnabled()) {
903 logger.debug("getRepository() repository root: " + repoSession.getRootDocument());
909 * Release repository session.
911 * @param repoSession the repo session
913 private void releaseRepositorySession(RepositoryInstance repoSession) {
915 NuxeoClient client = NuxeoConnector.getInstance().getClient();
917 client.releaseRepository(repoSession);
918 } catch (Exception e) {
919 logger.error("Could not close the repository session", e);
920 // no need to throw this service specific exception