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.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.document.DocumentHandler.Action;
32 import org.collectionspace.services.common.document.DocumentWrapper;
33 import org.collectionspace.services.common.document.DocumentWrapperImpl;
35 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
36 import org.collectionspace.services.common.repository.RepositoryClient;
37 import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler;
39 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
40 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
41 import org.nuxeo.common.utils.IdUtils;
42 import org.nuxeo.ecm.core.api.ClientException;
43 import org.nuxeo.ecm.core.api.DocumentModel;
44 import org.nuxeo.ecm.core.api.DocumentModelList;
45 import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
46 import org.nuxeo.ecm.core.api.DocumentRef;
47 import org.nuxeo.ecm.core.api.IdRef;
48 import org.nuxeo.ecm.core.api.PathRef;
49 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
50 import org.nuxeo.ecm.core.client.NuxeoClient;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * RepositoryJavaClient is used to perform CRUD operations on documents in Nuxeo
57 * repository using Remote Java APIs. It uses @see DocumentHandler as IOHandler
60 * $LastChangedRevision: $ $LastChangedDate: $
62 public class RepositoryJavaClientImpl implements RepositoryClient {
65 * The Class QueryContext.
67 private class QueryContext {
72 /** The doc filter. */
73 DocumentFilter docFilter;
75 /** The where clause. */
85 * Instantiates a new query context.
88 * @throws DocumentNotFoundException the document not found exception
89 * @throws DocumentException the document exception
91 QueryContext(ServiceContext<MultipartInput, MultipartOutput> ctx) throws DocumentNotFoundException, DocumentException {
92 docType = ctx.getDocumentType();
93 if (docType == null) {
94 throw new DocumentNotFoundException(
95 "Unable to find DocumentType for service " + ctx.getServiceName());
97 domain = ctx.getRepositoryDomainName();
99 throw new DocumentNotFoundException(
100 "Unable to find Domain for service " + ctx.getServiceName());
102 tenantId = ctx.getTenantId();
103 if (tenantId == null) {
104 throw new IllegalArgumentException(
105 "Service context has no Tenant ID specified.");
110 * Instantiates a new query context.
113 * @param theWhereClause the the where clause
114 * @throws DocumentNotFoundException the document not found exception
115 * @throws DocumentException the document exception
117 QueryContext(ServiceContext<MultipartInput, MultipartOutput> ctx,
118 String theWhereClause) throws DocumentNotFoundException, DocumentException {
120 whereClause = theWhereClause;
124 * Instantiates a new query context.
127 * @param handler the handler
128 * @throws DocumentNotFoundException the document not found exception
129 * @throws DocumentException the document exception
131 QueryContext(ServiceContext<MultipartInput, MultipartOutput> ctx,
132 DocumentHandler handler) throws DocumentNotFoundException, DocumentException {
134 if (handler == null) {
135 throw new IllegalArgumentException(
136 "Document handler is missing.");
138 docFilter = handler.getDocumentFilter();
139 if (docFilter == null) {
140 throw new IllegalArgumentException(
141 "Document handler has no Filter specified.");
143 whereClause = docFilter.getWhereClause();
148 private final Logger logger = LoggerFactory.getLogger(RepositoryJavaClientImpl.class);
151 * Instantiates a new repository java client impl.
153 public RepositoryJavaClientImpl() {
158 * Sets the collection space core values.
161 * @param documentModel the document model
162 * @throws ClientException the client exception
164 private void setCollectionSpaceCoreValues(ServiceContext<MultipartInput, MultipartOutput> ctx,
165 DocumentModel documentModel,
166 Action action) throws ClientException {
168 // Add the tenant ID value to the new entity
170 documentModel.setProperty(DocumentModelHandler.COLLECTIONSPACE_CORE_SCHEMA,
171 DocumentModelHandler.COLLECTIONSPACE_CORE_TENANTID,
175 //add creation date value
185 * create document in the Nuxeo repository
187 * @param ctx service context under which this method is invoked
189 * of the document created
191 * should be used by the caller to provide and transform the
193 * @return id in repository of the newly created document
194 * @throws DocumentException
197 public String create(ServiceContext ctx,
198 DocumentHandler handler) throws BadRequestException,
201 if (ctx.getDocumentType() == null) {
202 throw new IllegalArgumentException(
203 "RepositoryJavaClient.create: docType is missing");
205 if (handler == null) {
206 throw new IllegalArgumentException(
207 "RepositoryJavaClient.create: handler is missing");
209 String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
210 if (nuxeoWspaceId == null) {
211 throw new DocumentNotFoundException(
212 "Unable to find workspace for service " + ctx.getServiceName()
213 + " check if the workspace exists in the Nuxeo repository");
215 RepositoryInstance repoSession = null;
217 handler.prepare(Action.CREATE);
218 repoSession = getRepositorySession();
219 DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);
220 DocumentModel wspaceDoc = repoSession.getDocument(nuxeoWspace);
221 String wspacePath = wspaceDoc.getPathAsString();
222 //give our own ID so PathRef could be constructed later on
223 String id = IdUtils.generateId(UUID.randomUUID().toString());
224 // create document model
225 DocumentModel doc = repoSession.createDocumentModel(wspacePath, id,
226 ctx.getDocumentType());
227 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
228 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
229 handler.handle(Action.CREATE, wrapDoc);
230 // create document with documentmodel
231 setCollectionSpaceCoreValues(ctx, doc, Action.CREATE);
232 doc = repoSession.createDocument(doc);
234 handler.complete(Action.CREATE, wrapDoc);
236 } catch (BadRequestException bre) {
238 } catch (Exception e) {
239 if (logger.isDebugEnabled()) {
240 logger.debug("Caught exception ", e);
242 throw new DocumentException(e);
244 if (repoSession != null) {
245 releaseRepositorySession(repoSession);
252 * get document from the Nuxeo repository
253 * @param ctx service context under which this method is invoked
255 * of the document to retrieve
257 * should be used by the caller to provide and transform the
259 * @throws DocumentException
262 public void get(ServiceContext ctx, String id, DocumentHandler handler)
263 throws DocumentNotFoundException, DocumentException {
265 if (handler == null) {
266 throw new IllegalArgumentException(
267 "RepositoryJavaClient.get: handler is missing");
269 RepositoryInstance repoSession = null;
272 handler.prepare(Action.GET);
273 repoSession = getRepositorySession();
274 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
275 DocumentModel doc = null;
277 doc = repoSession.getDocument(docRef);
278 } catch (ClientException ce) {
279 String msg = "could not find document with id=" + id;
280 logger.error(msg, ce);
281 throw new DocumentNotFoundException(msg, ce);
283 //set reposession to handle the document
284 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
285 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
286 handler.handle(Action.GET, wrapDoc);
287 handler.complete(Action.GET, wrapDoc);
288 } catch (IllegalArgumentException iae) {
290 } catch (DocumentException de) {
292 } catch (Exception e) {
293 if (logger.isDebugEnabled()) {
294 logger.debug("Caught exception ", e);
296 throw new DocumentException(e);
298 if (repoSession != null) {
299 releaseRepositorySession(repoSession);
305 * get document from the Nuxeo repository, using the docFilter params.
306 * @param ctx service context under which this method is invoked
308 * should be used by the caller to provide and transform the
309 * document. Handler must have a docFilter set to return a single item.
310 * @throws DocumentException
313 public void get(ServiceContext ctx, DocumentHandler handler)
314 throws DocumentNotFoundException, DocumentException {
315 QueryContext queryContext = new QueryContext(ctx, handler);
316 RepositoryInstance repoSession = null;
319 handler.prepare(Action.GET);
320 repoSession = getRepositorySession();
322 DocumentModelList docList = null;
323 // force limit to 1, and ignore totalSize
324 String query = buildNXQLQuery(queryContext);
325 docList = repoSession.query(query, null, 1, 0, false);
326 if (docList.size() != 1) {
327 throw new DocumentNotFoundException("No document found matching filter params.");
329 DocumentModel doc = docList.get(0);
331 if (logger.isDebugEnabled()) {
332 logger.debug("Executed NXQL query: " + query);
335 //set reposession to handle the document
336 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
337 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
338 handler.handle(Action.GET, wrapDoc);
339 handler.complete(Action.GET, wrapDoc);
340 } catch (IllegalArgumentException iae) {
342 } catch (DocumentException de) {
344 } catch (Exception e) {
345 if (logger.isDebugEnabled()) {
346 logger.debug("Caught exception ", e);
348 throw new DocumentException(e);
350 if (repoSession != null) {
351 releaseRepositorySession(repoSession);
357 * get wrapped documentModel from the Nuxeo repository
358 * @param ctx service context under which this method is invoked
360 * of the document to retrieve
361 * @throws DocumentException
364 public DocumentWrapper<DocumentModel> getDoc(
365 ServiceContext ctx, String id)
366 throws DocumentNotFoundException, DocumentException {
367 RepositoryInstance repoSession = null;
368 DocumentWrapper<DocumentModel> wrapDoc = null;
371 repoSession = getRepositorySession();
372 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
373 DocumentModel doc = null;
375 doc = repoSession.getDocument(docRef);
376 } catch (ClientException ce) {
377 String msg = "could not find document with id=" + id;
378 logger.error(msg, ce);
379 throw new DocumentNotFoundException(msg, ce);
381 wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
382 } catch (IllegalArgumentException iae) {
384 } catch (DocumentException de) {
386 } catch (Exception e) {
387 if (logger.isDebugEnabled()) {
388 logger.debug("Caught exception ", e);
390 throw new DocumentException(e);
392 if (repoSession != null) {
393 releaseRepositorySession(repoSession);
400 * find wrapped documentModel from the Nuxeo repository
401 * @param ctx service context under which this method is invoked
402 * @param where NXQL where clause to get the document
403 * @throws DocumentException
406 public DocumentWrapper<DocumentModel> findDoc(
407 ServiceContext ctx, String whereClause)
408 throws DocumentNotFoundException, DocumentException {
409 RepositoryInstance repoSession = null;
410 DocumentWrapper<DocumentModel> wrapDoc = null;
413 QueryContext queryContext = new QueryContext(ctx, whereClause);
414 repoSession = getRepositorySession();
415 DocumentModelList docList = null;
416 // force limit to 1, and ignore totalSize
417 String query = buildNXQLQuery(queryContext);
418 docList = repoSession.query(query,
423 if (docList.size() != 1) {
424 if (logger.isDebugEnabled()) {
425 logger.debug("findDoc: Query found: " + docList.size() + " items.");
426 logger.debug(" Query: " + query);
428 throw new DocumentNotFoundException("No document found matching filter params.");
430 DocumentModel doc = docList.get(0);
431 wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
432 } catch (IllegalArgumentException iae) {
434 } catch (DocumentException de) {
436 } catch (Exception e) {
437 if (logger.isDebugEnabled()) {
438 logger.debug("Caught exception ", e);
440 throw new DocumentException(e);
442 if (repoSession != null) {
443 releaseRepositorySession(repoSession);
450 * find doc and return CSID from the Nuxeo repository
451 * @param ctx service context under which this method is invoked
452 * @param where NXQL where clause to get the document
453 * @throws DocumentException
456 public String findDocCSID(
457 ServiceContext ctx, String whereClause)
458 throws DocumentNotFoundException, DocumentException {
461 DocumentWrapper<DocumentModel> wrapDoc = findDoc(ctx, whereClause);
462 DocumentModel docModel = wrapDoc.getWrappedObject();
463 csid = NuxeoUtils.extractId(docModel.getPathAsString());
464 } catch (DocumentNotFoundException dnfe) {
466 } catch (IllegalArgumentException iae) {
468 } catch (DocumentException de) {
470 } catch (Exception e) {
471 if (logger.isDebugEnabled()) {
472 logger.debug("Caught exception ", e);
474 throw new DocumentException(e);
480 * Find a list of documentModels from the Nuxeo repository
481 * @param docTypes a list of DocType names to match
482 * @param where the clause to qualify on
483 * @param domain the domain for the associated services
487 public DocumentWrapper<DocumentModelList> findDocs(
489 List<String> docTypes,
491 int pageSize, int pageNum, boolean computeTotal)
492 throws DocumentNotFoundException, DocumentException {
493 RepositoryInstance repoSession = null;
494 DocumentWrapper<DocumentModelList> wrapDoc = null;
497 if (docTypes == null || docTypes.size() < 1) {
498 throw new DocumentNotFoundException(
499 "findDocs must specify at least one DocumentType.");
501 repoSession = getRepositorySession();
502 DocumentModelList docList = null;
503 // force limit to 1, and ignore totalSize
504 QueryContext queryContext = new QueryContext(ctx, whereClause);
505 String query = buildNXQLQuery(docTypes, queryContext);
506 docList = repoSession.query(query, null, pageSize, pageNum, computeTotal);
507 wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
508 } catch (IllegalArgumentException iae) {
510 } catch (Exception e) {
511 if (logger.isDebugEnabled()) {
512 logger.debug("Caught exception ", e);
514 throw new DocumentException(e);
516 if (repoSession != null) {
517 releaseRepositorySession(repoSession);
524 * @see org.collectionspace.services.common.storage.StorageClient#get(org.collectionspace.services.common.context.ServiceContext, java.util.List, org.collectionspace.services.common.document.DocumentHandler)
527 public void get(ServiceContext ctx, List<String> csidList, DocumentHandler handler)
528 throws DocumentNotFoundException, DocumentException {
529 if (handler == null) {
530 throw new IllegalArgumentException(
531 "RepositoryJavaClient.getAll: handler is missing");
534 RepositoryInstance repoSession = null;
537 handler.prepare(Action.GET_ALL);
538 repoSession = getRepositorySession();
539 DocumentModelList docModelList = new DocumentModelListImpl();
540 //FIXME: Should be using NuxeoUtils.createPathRef for security reasons
541 for (String csid : csidList) {
542 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
543 DocumentModel docModel = repoSession.getDocument(docRef);
544 docModelList.add(docModel);
547 //set reposession to handle the document
548 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
549 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docModelList);
550 handler.handle(Action.GET_ALL, wrapDoc);
551 handler.complete(Action.GET_ALL, wrapDoc);
552 } catch (DocumentException de) {
554 } catch (Exception e) {
555 if (logger.isDebugEnabled()) {
556 logger.debug("Caught exception ", e);
558 throw new DocumentException(e);
560 if (repoSession != null) {
561 releaseRepositorySession(repoSession);
567 * getAll get all documents for an entity entity service from the Nuxeo
570 * @param ctx service context under which this method is invoked
572 * should be used by the caller to provide and transform the
574 * @throws DocumentException
577 public void getAll(ServiceContext ctx, DocumentHandler handler)
578 throws DocumentNotFoundException, DocumentException {
579 if (handler == null) {
580 throw new IllegalArgumentException(
581 "RepositoryJavaClient.getAll: handler is missing");
583 String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
584 if (nuxeoWspaceId == null) {
585 throw new DocumentNotFoundException(
586 "Unable to find workspace for service "
587 + ctx.getServiceName()
588 + " check if the workspace exists in the Nuxeo repository");
590 RepositoryInstance repoSession = null;
593 handler.prepare(Action.GET_ALL);
594 repoSession = getRepositorySession();
595 DocumentRef wsDocRef = new IdRef(nuxeoWspaceId);
596 DocumentModelList docList = repoSession.getChildren(wsDocRef);
597 //set reposession to handle the document
598 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
599 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
600 handler.handle(Action.GET_ALL, wrapDoc);
601 handler.complete(Action.GET_ALL, wrapDoc);
602 } catch (DocumentException de) {
604 } catch (Exception e) {
605 if (logger.isDebugEnabled()) {
606 logger.debug("Caught exception ", e);
608 throw new DocumentException(e);
610 if (repoSession != null) {
611 releaseRepositorySession(repoSession);
617 * getFiltered get all documents for an entity service from the Document repository,
618 * given filter parameters specified by the handler.
619 * @param ctx service context under which this method is invoked
620 * @param handler should be used by the caller to provide and transform the document
621 * @throws DocumentNotFoundException if workspace not found
622 * @throws DocumentException
625 public void getFiltered(ServiceContext ctx, DocumentHandler handler)
626 throws DocumentNotFoundException, DocumentException {
627 QueryContext queryContext = new QueryContext(ctx, handler);
629 RepositoryInstance repoSession = null;
631 handler.prepare(Action.GET_ALL);
632 repoSession = getRepositorySession();
633 DocumentModelList docList = null;
634 String query = buildNXQLQuery(queryContext);
636 // If we have limit and/or offset, then pass true to get totalSize
637 // in returned DocumentModelList.
638 if ((queryContext.docFilter.getOffset() > 0) || (queryContext.docFilter.getPageSize() > 0)) {
639 docList = repoSession.query(query, null,
640 queryContext.docFilter.getPageSize(), queryContext.docFilter.getOffset(), true);
642 docList = repoSession.query(query);
645 if (logger.isDebugEnabled()) {
646 logger.debug("Executed NXQL query: " + query.toString());
649 //set repoSession to handle the document
650 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
651 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
652 handler.handle(Action.GET_ALL, wrapDoc);
653 handler.complete(Action.GET_ALL, wrapDoc);
654 } catch (DocumentException de) {
656 } catch (Exception e) {
657 if (logger.isDebugEnabled()) {
658 logger.debug("Caught exception ", e);
660 throw new DocumentException(e);
662 if (repoSession != null) {
663 releaseRepositorySession(repoSession);
669 * update given document in the Nuxeo repository
671 * @param ctx service context under which this method is invoked
675 * should be used by the caller to provide and transform the
677 * @throws DocumentException
680 public void update(ServiceContext ctx, String id, DocumentHandler handler)
681 throws BadRequestException, DocumentNotFoundException,
683 if (handler == null) {
684 throw new IllegalArgumentException(
685 "RepositoryJavaClient.update: handler is missing");
687 RepositoryInstance repoSession = null;
689 handler.prepare(Action.UPDATE);
690 repoSession = getRepositorySession();
691 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
692 DocumentModel doc = null;
694 doc = repoSession.getDocument(docRef);
695 } catch (ClientException ce) {
696 String msg = "Could not find document to update with id=" + id;
697 logger.error(msg, ce);
698 throw new DocumentNotFoundException(msg, ce);
700 //set reposession to handle the document
701 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
702 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
703 handler.handle(Action.UPDATE, wrapDoc);
704 setCollectionSpaceCoreValues(ctx, doc, Action.CREATE);
705 repoSession.saveDocument(doc);
707 handler.complete(Action.UPDATE, wrapDoc);
708 } catch (BadRequestException bre) {
710 } catch (DocumentException de) {
712 } catch (Exception e) {
713 if (logger.isDebugEnabled()) {
714 logger.debug("Caught exception ", e);
716 throw new DocumentException(e);
718 if (repoSession != null) {
719 releaseRepositorySession(repoSession);
725 * delete a document from the Nuxeo repository
726 * @param ctx service context under which this method is invoked
729 * @throws DocumentException
732 public void delete(ServiceContext ctx, String id) throws DocumentNotFoundException,
735 if (logger.isDebugEnabled()) {
736 logger.debug("deleting document with id=" + id);
738 RepositoryInstance repoSession = null;
740 repoSession = getRepositorySession();
741 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
743 repoSession.removeDocument(docRef);
744 } catch (ClientException ce) {
745 String msg = "could not find document to delete with id=" + id;
746 logger.error(msg, ce);
747 throw new DocumentNotFoundException(msg, ce);
750 } catch (DocumentException de) {
752 } catch (Exception e) {
753 if (logger.isDebugEnabled()) {
754 logger.debug("Caught exception ", e);
756 throw new DocumentException(e);
758 if (repoSession != null) {
759 releaseRepositorySession(repoSession);
765 * @see org.collectionspace.services.common.storage.StorageClient#delete(org.collectionspace.services.common.context.ServiceContext, java.lang.String, org.collectionspace.services.common.document.DocumentHandler)
768 public void delete(ServiceContext ctx, String id, DocumentHandler handler)
769 throws DocumentNotFoundException, DocumentException {
770 throw new UnsupportedOperationException();
774 * @see org.collectionspace.services.common.repository.RepositoryClient#createWorkspace(java.lang.String, java.lang.String)
777 public String createWorkspace(String tenantDomain, String workspaceName) throws Exception {
778 RepositoryInstance repoSession = null;
779 String workspaceId = null;
781 repoSession = getRepositorySession();
782 DocumentRef docRef = new PathRef(
784 + "/" + "workspaces");
785 DocumentModel parent = repoSession.getDocument(docRef);
786 DocumentModel doc = repoSession.createDocumentModel(parent.getPathAsString(),
787 workspaceName, "Workspace");
788 doc.setPropertyValue("dc:title", workspaceName);
789 doc.setPropertyValue("dc:description", "A CollectionSpace workspace for "
791 doc = repoSession.createDocument(doc);
792 workspaceId = doc.getId();
794 if (logger.isDebugEnabled()) {
795 logger.debug("created workspace name=" + workspaceName
796 + " id=" + workspaceId);
798 } catch (Exception e) {
799 if (logger.isDebugEnabled()) {
800 logger.debug("createWorkspace caught exception ", e);
804 if (repoSession != null) {
805 releaseRepositorySession(repoSession);
812 * @see org.collectionspace.services.common.repository.RepositoryClient#getWorkspaceId(java.lang.String, java.lang.String)
815 public String getWorkspaceId(String tenantDomain, String workspaceName) throws Exception {
816 String workspaceId = null;
817 RepositoryInstance repoSession = null;
819 repoSession = getRepositorySession();
820 DocumentRef docRef = new PathRef(
823 + "/" + workspaceName);
824 DocumentModel workspace = repoSession.getDocument(docRef);
825 workspaceId = workspace.getId();
826 } catch (DocumentException de) {
828 } catch (Exception e) {
829 if (logger.isDebugEnabled()) {
830 logger.debug("Caught exception ", e);
832 throw new DocumentException(e);
834 if (repoSession != null) {
835 releaseRepositorySession(repoSession);
844 * @param query the query
845 * @param where the where
846 * @param domain the domain
848 private final void appendNXQLWhere(StringBuilder query, QueryContext queryContext) {
850 // Restrict search to a specific Nuxeo domain
851 // TODO This is a slow method for tenant-filter
852 // We should make this a property that is indexed.
854 query.append(" WHERE ecm:path STARTSWITH '/" + queryContext.domain + "'");
857 // Restrict search to the current tenant ID. Is the domain path filter (above) still needed?
859 query.append("AND " + DocumentModelHandler.COLLECTIONSPACE_CORE_SCHEMA + ":" +
860 DocumentModelHandler.COLLECTIONSPACE_CORE_TENANTID +
861 " = " + queryContext.tenantId);
863 // Finally, append the incoming where clause
865 String whereClause = queryContext.whereClause;
866 if (whereClause != null && whereClause.length() > 0) {
867 // Due to an apparent bug/issue in how Nuxeo translates the NXQL query string
868 // into SQL, we need to parenthesize our 'where' clause
869 query.append(" AND " + "(" + whereClause + ")");
872 // Please lookup this use in Nuxeo support and document here
874 query.append(" AND ecm:isProxy = 0");
878 * Builds the nxql query.
880 * @param docType the doc type
881 * @param where the where
882 * @param domain the domain
883 * @param tenantId the tenant id
886 private final String buildNXQLQuery(QueryContext queryContext) {
887 StringBuilder query = new StringBuilder("SELECT * FROM ");
888 query.append(queryContext.docType);
889 appendNXQLWhere(query, queryContext);
890 return query.toString();
894 * Builds the nxql query.
896 * @param docTypes the doc types
897 * @param where the where
898 * @param domain the domain
901 private final String buildNXQLQuery(List<String> docTypes, QueryContext queryContext) {
902 StringBuilder query = new StringBuilder("SELECT * FROM ");
903 boolean fFirst = true;
904 for (String docType : docTypes) {
910 query.append(docType);
912 appendNXQLWhere(query, queryContext);
913 return query.toString();
917 * Gets the repository session.
919 * @return the repository session
920 * @throws Exception the exception
922 private RepositoryInstance getRepositorySession() throws Exception {
923 // FIXME: is it possible to reuse repository session?
924 // Authentication failures happen while trying to reuse the session
925 NuxeoClient client = NuxeoConnector.getInstance().getClient();
926 RepositoryInstance repoSession = client.openRepository();
927 if (logger.isDebugEnabled()) {
928 logger.debug("getRepository() repository root: " + repoSession.getRootDocument());
934 * Release repository session.
936 * @param repoSession the repo session
938 private void releaseRepositorySession(RepositoryInstance repoSession) {
940 NuxeoClient client = NuxeoConnector.getInstance().getClient();
942 client.releaseRepository(repoSession);
943 } catch (Exception e) {
944 logger.error("Could not close the repository session", e);
945 // no need to throw this service specific exception