2 * This document is a part of the source code and related artifacts for
3 * CollectionSpace, an open source collections management system for museums and
4 * related institutions:
6 * http://www.collectionspace.org http://wiki.collectionspace.org
8 * Copyright 2009 University of California at Berkeley
10 * Licensed under the Educational Community License (ECL), Version 2.0. You may
11 * not use this file except in compliance with this License.
13 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 package org.collectionspace.services.nuxeo.client.java;
19 import java.io.Serializable;
20 import java.sql.SQLException;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.HashSet;
25 import java.util.Hashtable;
26 import java.util.Iterator;
27 import java.util.List;
30 import java.util.UUID;
32 import javax.sql.rowset.CachedRowSet;
33 import javax.ws.rs.core.MultivaluedMap;
35 import org.collectionspace.services.lifecycle.TransitionDef;
36 import org.collectionspace.services.nuxeo.util.CSReindexFulltextRoot;
37 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
38 import org.collectionspace.services.client.CollectionSpaceClient;
39 import org.collectionspace.services.client.IQueryManager;
40 import org.collectionspace.services.client.PoxPayloadIn;
41 import org.collectionspace.services.client.PoxPayloadOut;
42 import org.collectionspace.services.client.Profiler;
43 import org.collectionspace.services.client.workflow.WorkflowClient;
44 import org.collectionspace.services.common.context.ServiceContext;
45 import org.collectionspace.services.common.query.QueryContext;
46 import org.collectionspace.services.common.repository.RepositoryClient;
47 import org.collectionspace.services.common.storage.JDBCTools;
48 import org.collectionspace.services.common.storage.PreparedStatementSimpleBuilder;
49 import org.collectionspace.services.common.document.BadRequestException;
50 import org.collectionspace.services.common.document.DocumentException;
51 import org.collectionspace.services.common.document.DocumentFilter;
52 import org.collectionspace.services.common.document.DocumentHandler;
53 import org.collectionspace.services.common.document.DocumentNotFoundException;
54 import org.collectionspace.services.common.document.DocumentHandler.Action;
55 import org.collectionspace.services.common.document.DocumentWrapper;
56 import org.collectionspace.services.common.document.DocumentWrapperImpl;
57 import org.collectionspace.services.common.document.TransactionException;
58 import org.collectionspace.services.common.CSWebApplicationException;
59 import org.collectionspace.services.common.ServiceMain;
60 import org.collectionspace.services.common.api.Tools;
61 import org.collectionspace.services.common.config.ConfigUtils;
62 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
63 import org.collectionspace.services.common.config.TenantBindingUtils;
64 import org.collectionspace.services.common.storage.PreparedStatementBuilder;
65 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.Specifier;
66 import org.collectionspace.services.config.tenant.TenantBindingType;
67 import org.collectionspace.services.config.tenant.RepositoryDomainType;
70 // CSPACE-5036 - How to make CMISQL queries from Nuxeo
72 import org.apache.chemistry.opencmis.commons.enums.CmisVersion;
73 import org.apache.chemistry.opencmis.commons.server.CallContext;
74 import org.apache.chemistry.opencmis.server.impl.CallContextImpl;
75 import org.apache.chemistry.opencmis.server.shared.ThresholdOutputStreamFactory;
76 import org.nuxeo.common.utils.IdUtils;
77 import org.nuxeo.ecm.core.api.ClientException;
78 import org.nuxeo.ecm.core.api.DocumentModel;
79 import org.nuxeo.ecm.core.api.DocumentModelList;
80 import org.nuxeo.ecm.core.api.IterableQueryResult;
81 import org.nuxeo.ecm.core.api.VersioningOption;
82 import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
83 import org.nuxeo.ecm.core.api.DocumentRef;
84 import org.nuxeo.ecm.core.api.IdRef;
85 import org.nuxeo.ecm.core.api.PathRef;
86 import org.nuxeo.runtime.transaction.TransactionRuntimeException;
87 import org.nuxeo.ecm.core.opencmis.bindings.NuxeoCmisServiceFactory;
88 import org.nuxeo.ecm.core.opencmis.impl.server.NuxeoCmisService;
89 import org.slf4j.Logger;
90 import org.slf4j.LoggerFactory;
93 * RepositoryClientImpl is used to perform CRUD operations on documents in Nuxeo
94 * repository using Remote Java APIs. It uses
96 * @see DocumentHandler as IOHandler with the client.
98 * $LastChangedRevision: $ $LastChangedDate: $
100 public class RepositoryClientImpl implements RepositoryClient<PoxPayloadIn, PoxPayloadOut> {
105 private final Logger logger = LoggerFactory.getLogger(RepositoryClientImpl.class);
106 // private final Logger profilerLogger = LoggerFactory.getLogger("remperf");
107 // private String foo = Profiler.createLogger();
108 public static final String NUXEO_CORE_TYPE_DOMAIN = "Domain";
109 public static final String NUXEO_CORE_TYPE_WORKSPACEROOT = "WorkspaceRoot";
110 // FIXME: Get this value from an existing constant, if available
111 public static final String BACKSLASH = "\\";
112 public static final String USER_SUPPLIED_WILDCARD = "*";
113 public static final String USER_SUPPLIED_WILDCARD_REGEX = BACKSLASH + USER_SUPPLIED_WILDCARD;
114 public static final String USER_SUPPLIED_ANCHOR_CHAR = "^";
115 public static final String USER_SUPPLIED_ANCHOR_CHAR_REGEX = BACKSLASH + USER_SUPPLIED_ANCHOR_CHAR;
116 public static final String ENDING_ANCHOR_CHAR = "$";
117 public static final String ENDING_ANCHOR_CHAR_REGEX = BACKSLASH + ENDING_ANCHOR_CHAR;
121 * Instantiates a new repository java client impl.
123 public RepositoryClientImpl() {
127 public void assertWorkflowState(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
128 DocumentModel docModel) throws DocumentNotFoundException, ClientException {
129 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
130 if (queryParams != null) {
132 // Look for the workflow "delete" query param and see if we need to assert that the
133 // docModel is in a non-deleted workflow state.
135 String currentState = docModel.getCurrentLifeCycleState();
136 String includeDeletedStr = queryParams.getFirst(WorkflowClient.WORKFLOW_QUERY_NONDELETED);
137 boolean includeDeleted = includeDeletedStr == null ? true : Boolean.parseBoolean(includeDeletedStr);
138 if (includeDeleted == false) {
140 // We don't wanted soft-deleted object, so throw an exception if this one is soft-deleted.
142 if (currentState.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
143 String msg = "The GET assertion that docModel not be in 'deleted' workflow state failed.";
145 throw new DocumentNotFoundException(msg);
152 * create document in the Nuxeo repository
154 * @param ctx service context under which this method is invoked
155 * @param handler should be used by the caller to provide and transform the
157 * @return id in repository of the newly created document
158 * @throws BadRequestException
159 * @throws TransactionException
160 * @throws DocumentException
163 public String create(ServiceContext ctx,
164 DocumentHandler handler) throws BadRequestException,
165 TransactionException, DocumentException {
167 String docType = NuxeoUtils.getTenantQualifiedDocType(ctx); //ctx.getDocumentType();
168 if (docType == null) {
169 throw new IllegalArgumentException(
170 "RepositoryJavaClient.create: docType is missing");
173 if (handler == null) {
174 throw new IllegalArgumentException(
175 "RepositoryJavaClient.create: handler is missing");
177 String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
178 if (nuxeoWspaceId == null) {
179 throw new DocumentNotFoundException(
180 "Unable to find workspace for service " + ctx.getServiceName()
181 + " check if the workspace exists in the Nuxeo repository");
184 CoreSessionInterface repoSession = null;
186 handler.prepare(Action.CREATE);
187 repoSession = getRepositorySession(ctx);
188 DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);
189 DocumentModel wspaceDoc = repoSession.getDocument(nuxeoWspace);
190 String wspacePath = wspaceDoc.getPathAsString();
191 //give our own ID so PathRef could be constructed later on
192 String id = IdUtils.generateId(UUID.randomUUID().toString());
193 // create document model
194 DocumentModel doc = repoSession.createDocumentModel(wspacePath, id, docType);
195 /* Check for a versioned document, and check In and Out before we proceed.
196 * This does not work as we do not have the uid schema on our docs.
197 if(((DocumentModelHandler) handler).supportsVersioning()) {
198 doc.setProperty("uid","major_version",1);
199 doc.setProperty("uid","minor_version",0);
202 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
203 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
204 handler.handle(Action.CREATE, wrapDoc);
205 // create document with documentmodel
206 doc = repoSession.createDocument(doc);
208 // TODO for sub-docs need to call into the handler to let it deal with subitems. Pass in the id,
209 // and assume the handler has the state it needs (doc fragments).
210 handler.complete(Action.CREATE, wrapDoc);
212 } catch (BadRequestException bre) {
214 } catch (Exception e) {
215 throw new NuxeoDocumentException(e);
217 if (repoSession != null) {
218 releaseRepositorySession(ctx, repoSession);
226 public boolean reindex(DocumentHandler handler, String indexid) throws DocumentNotFoundException, DocumentException
228 return reindex(handler, null, indexid);
232 public boolean reindex(DocumentHandler handler, String csid, String indexid) throws DocumentNotFoundException, DocumentException
234 boolean result = true;
235 CoreSessionInterface repoSession = null;
236 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = handler.getServiceContext();
239 String queryString = handler.getDocumentsToIndexQuery(indexid, csid);
240 repoSession = getRepositorySession(ctx);
241 CSReindexFulltextRoot indexer = new CSReindexFulltextRoot(repoSession);
242 indexer.reindexFulltext(0, 0, queryString);
244 // Set repository session to handle the document
246 } catch (Exception e) {
247 if (logger.isDebugEnabled()) {
248 logger.debug("Caught exception ", e);
250 throw new NuxeoDocumentException(e);
252 if (repoSession != null) {
253 releaseRepositorySession(ctx, repoSession);
261 public void synchronize(ServiceContext ctx, Specifier specifier, DocumentHandler handler)
262 throws DocumentNotFoundException, TransactionException, DocumentException {
264 if (handler == null) {
265 throw new IllegalArgumentException(
266 "RepositoryJavaClient.get: handler is missing");
269 CoreSessionInterface repoSession = null;
271 handler.prepare(Action.SYNC);
272 repoSession = getRepositorySession(ctx);
273 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
274 DocumentWrapper<Specifier> wrapDoc = new DocumentWrapperImpl<Specifier>(specifier);
275 handler.handle(Action.SYNC, wrapDoc);
276 handler.complete(Action.SYNC, wrapDoc);
277 } catch (IllegalArgumentException iae) {
279 } catch (DocumentException de) {
281 } catch (Exception e) {
282 if (logger.isDebugEnabled()) {
283 logger.debug("Caught exception ", e);
285 throw new NuxeoDocumentException(e);
287 if (repoSession != null) {
288 releaseRepositorySession(ctx, repoSession);
294 * get document from the Nuxeo repository
296 * @param ctx service context under which this method is invoked
297 * @param id of the document to retrieve
298 * @param handler should be used by the caller to provide and transform the
300 * @throws DocumentNotFoundException if the document cannot be found in the
302 * @throws TransactionException
303 * @throws DocumentException
306 public void get(ServiceContext ctx, String id, DocumentHandler handler)
307 throws DocumentNotFoundException, TransactionException, DocumentException {
309 if (handler == null) {
310 throw new IllegalArgumentException(
311 "RepositoryJavaClient.get: handler is missing");
314 CoreSessionInterface repoSession = null;
316 handler.prepare(Action.GET);
317 repoSession = getRepositorySession(ctx);
318 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
319 DocumentModel docModel = null;
321 docModel = repoSession.getDocument(docRef);
322 assertWorkflowState(ctx, docModel);
323 } catch (ClientException ce) {
324 String msg = logException(ce, "Could not find document with CSID=" + id);
325 throw new DocumentNotFoundException(msg, ce);
328 // Set repository session to handle the document
330 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
331 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(docModel);
332 handler.handle(Action.GET, wrapDoc);
333 handler.complete(Action.GET, wrapDoc);
334 } catch (IllegalArgumentException iae) {
336 } catch (DocumentException de) {
338 } catch (Exception e) {
339 if (logger.isDebugEnabled()) {
340 logger.debug("Caught exception ", e);
342 throw new NuxeoDocumentException(e);
344 if (repoSession != null) {
345 releaseRepositorySession(ctx, repoSession);
351 * get a document from the Nuxeo repository, using the docFilter params.
353 * @param ctx service context under which this method is invoked
354 * @param handler should be used by the caller to provide and transform the
355 * document. Handler must have a docFilter set to return a single item.
356 * @throws DocumentNotFoundException if the document cannot be found in the
358 * @throws TransactionException
359 * @throws DocumentException
362 public void get(ServiceContext ctx, DocumentHandler handler)
363 throws DocumentNotFoundException, TransactionException, DocumentException {
364 QueryContext queryContext = new QueryContext(ctx, handler);
365 CoreSessionInterface repoSession = null;
368 handler.prepare(Action.GET);
369 repoSession = getRepositorySession(ctx);
371 DocumentModelList docList = null;
372 // force limit to 1, and ignore totalSize
373 String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
374 docList = repoSession.query(query, null, 1, 0, false);
375 if (docList.size() != 1) {
376 throw new DocumentNotFoundException("No document found matching filter params: " + query);
378 DocumentModel doc = docList.get(0);
380 if (logger.isDebugEnabled()) {
381 logger.debug("Executed NXQL query: " + query);
384 //set reposession to handle the document
385 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
386 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
387 handler.handle(Action.GET, wrapDoc);
388 handler.complete(Action.GET, wrapDoc);
389 } catch (IllegalArgumentException iae) {
391 } catch (DocumentException de) {
393 } catch (Exception e) {
394 if (logger.isDebugEnabled()) {
395 logger.debug("Caught exception ", e);
397 throw new NuxeoDocumentException(e);
399 if (repoSession != null) {
400 releaseRepositorySession(ctx, repoSession);
405 public DocumentWrapper<DocumentModel> getDoc(
406 CoreSessionInterface repoSession,
407 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
408 String csid) throws DocumentNotFoundException, DocumentException {
409 DocumentWrapper<DocumentModel> wrapDoc = null;
412 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
413 DocumentModel doc = null;
415 doc = repoSession.getDocument(docRef);
416 } catch (ClientException ce) {
417 String msg = logException(ce, "Could not find document with CSID=" + csid);
418 throw new DocumentNotFoundException(msg, ce);
420 wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
421 } catch (IllegalArgumentException iae) {
423 } catch (DocumentException de) {
431 * Get wrapped documentModel from the Nuxeo repository. The search is
432 * restricted to the workspace of the current context.
434 * @param ctx service context under which this method is invoked
435 * @param csid of the document to retrieve
436 * @throws DocumentNotFoundException
437 * @throws TransactionException
438 * @throws DocumentException
439 * @return a wrapped documentModel
442 public DocumentWrapper<DocumentModel> getDoc(
443 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
444 String csid) throws DocumentNotFoundException, TransactionException, DocumentException {
445 CoreSessionInterface repoSession = null;
446 DocumentWrapper<DocumentModel> wrapDoc = null;
449 // Open a new repository session
450 repoSession = getRepositorySession(ctx);
451 wrapDoc = getDoc(repoSession, ctx, csid);
452 } catch (IllegalArgumentException iae) {
454 } catch (DocumentException de) {
456 } catch (Exception e) {
457 if (logger.isDebugEnabled()) {
458 logger.debug("Caught exception ", e);
460 throw new NuxeoDocumentException(e);
462 if (repoSession != null) {
463 releaseRepositorySession(ctx, repoSession);
467 if (logger.isWarnEnabled() == true) {
468 logger.warn("Returned DocumentModel instance was created with a repository session that is now closed.");
473 public DocumentWrapper<DocumentModel> findDoc(
474 CoreSessionInterface repoSession,
475 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
477 throws DocumentNotFoundException, DocumentException {
478 DocumentWrapper<DocumentModel> wrapDoc = null;
481 QueryContext queryContext = new QueryContext(ctx, whereClause);
482 DocumentModelList docList = null;
483 // force limit to 1, and ignore totalSize
484 String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
485 docList = repoSession.query(query,
490 if (docList.size() != 1) {
491 if (logger.isDebugEnabled()) {
492 logger.debug("findDoc: Query found: " + docList.size() + " items.");
493 logger.debug(" Query: " + query);
495 throw new DocumentNotFoundException("No document found matching filter params: " + query);
497 DocumentModel doc = docList.get(0);
498 wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
499 } catch (IllegalArgumentException iae) {
501 } catch (DocumentException de) {
503 } catch (Exception e) {
504 if (logger.isDebugEnabled()) {
505 logger.debug("Caught exception ", e);
507 throw new NuxeoDocumentException(e);
514 * find wrapped documentModel from the Nuxeo repository
516 * @param ctx service context under which this method is invoked
517 * @param whereClause where NXQL where clause to get the document
518 * @throws DocumentNotFoundException
519 * @throws TransactionException
520 * @throws DocumentException
521 * @return a wrapped documentModel retrieved by the repository query
524 public DocumentWrapper<DocumentModel> findDoc(
525 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
527 throws DocumentNotFoundException, TransactionException, DocumentException {
528 CoreSessionInterface repoSession = null;
529 DocumentWrapper<DocumentModel> wrapDoc = null;
532 repoSession = getRepositorySession(ctx);
533 wrapDoc = findDoc(repoSession, ctx, whereClause);
534 } catch (DocumentNotFoundException dnfe) {
536 } catch (DocumentException de) {
538 } catch (Exception e) {
539 if (repoSession == null) {
540 throw new NuxeoDocumentException("Unable to create a Nuxeo repository session.", e);
542 throw new NuxeoDocumentException("Unexpected Nuxeo exception.", e);
545 if (repoSession != null) {
546 releaseRepositorySession(ctx, repoSession);
550 if (logger.isWarnEnabled() == true) {
551 logger.warn("Returned DocumentModel instance was created with a repository session that is now closed.");
558 * find doc and return CSID from the Nuxeo repository
561 * @param ctx service context under which this method is invoked
562 * @param whereClause where NXQL where clause to get the document
563 * @throws DocumentNotFoundException
564 * @throws TransactionException
565 * @throws DocumentException
566 * @return the CollectionSpace ID (CSID) of the requested document
569 public String findDocCSID(CoreSessionInterface repoSession,
570 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String whereClause)
571 throws DocumentNotFoundException, TransactionException, DocumentException {
573 boolean releaseSession = false;
575 if (repoSession == null) {
576 repoSession = this.getRepositorySession(ctx);
577 releaseSession = true;
579 DocumentWrapper<DocumentModel> wrapDoc = findDoc(repoSession, ctx, whereClause);
580 DocumentModel docModel = wrapDoc.getWrappedObject();
581 csid = NuxeoUtils.getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
582 } catch (DocumentNotFoundException dnfe) {
584 } catch (IllegalArgumentException iae) {
586 } catch (DocumentException de) {
588 } catch (Exception e) {
589 if (logger.isDebugEnabled()) {
590 logger.debug("Caught exception ", e);
592 throw new NuxeoDocumentException(e);
594 if (releaseSession && (repoSession != null)) {
595 this.releaseRepositorySession(ctx, repoSession);
601 public DocumentWrapper<DocumentModelList> findDocs(
602 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
603 CoreSessionInterface repoSession,
604 List<String> docTypes,
606 String orderByClause,
609 boolean computeTotal)
610 throws DocumentNotFoundException, DocumentException {
611 DocumentWrapper<DocumentModelList> wrapDoc = null;
614 if (docTypes == null || docTypes.size() < 1) {
615 throw new DocumentNotFoundException(
616 "The findDocs() method must specify at least one DocumentType.");
618 DocumentModelList docList = null;
619 QueryContext queryContext = new QueryContext(ctx, whereClause, orderByClause);
620 String query = NuxeoUtils.buildNXQLQuery(docTypes, queryContext);
621 if (logger.isDebugEnabled()) {
622 logger.debug("findDocs() NXQL: " + query);
624 docList = repoSession.query(query, null, pageSize, pageSize * pageNum, computeTotal);
625 wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
626 } catch (IllegalArgumentException iae) {
628 } catch (Exception e) {
629 if (logger.isDebugEnabled()) {
630 logger.debug("Caught exception ", e);
632 throw new NuxeoDocumentException(e);
638 protected static String buildInListForDocTypes(List<String> docTypes) {
639 StringBuilder sb = new StringBuilder();
641 boolean first = true;
642 for (String docType : docTypes) {
653 return sb.toString();
656 public DocumentWrapper<DocumentModelList> findDocs(
657 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
658 DocumentHandler handler,
659 CoreSessionInterface repoSession,
660 List<String> docTypes)
661 throws DocumentNotFoundException, DocumentException {
662 DocumentWrapper<DocumentModelList> wrapDoc = null;
664 DocumentFilter filter = handler.getDocumentFilter();
665 String oldOrderBy = filter.getOrderByClause();
666 if (isClauseEmpty(oldOrderBy) == true) {
667 filter.setOrderByClause(DocumentFilter.ORDER_BY_LAST_UPDATED);
669 QueryContext queryContext = new QueryContext(ctx, handler);
672 if (docTypes == null || docTypes.size() < 1) {
673 throw new DocumentNotFoundException(
674 "The findDocs() method must specify at least one DocumentType.");
676 DocumentModelList docList = null;
677 if (handler.isCMISQuery() == true) {
678 String inList = buildInListForDocTypes(docTypes);
679 ctx.getQueryParams().add(IQueryManager.SEARCH_RELATED_MATCH_OBJ_DOCTYPES, inList);
680 docList = getFilteredCMIS(repoSession, ctx, handler, queryContext);
682 String query = NuxeoUtils.buildNXQLQuery(docTypes, queryContext);
683 if (logger.isDebugEnabled()) {
684 logger.debug("findDocs() NXQL: " + query);
686 docList = repoSession.query(query, null, filter.getPageSize(), filter.getOffset(), true);
688 wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
689 } catch (IllegalArgumentException iae) {
691 } catch (Exception e) {
692 if (logger.isDebugEnabled()) {
693 logger.debug("Caught exception ", e);
695 throw new NuxeoDocumentException(e);
702 * Find a list of documentModels from the Nuxeo repository
704 * @param docTypes a list of DocType names to match
705 * @param whereClause where the clause to qualify on
706 * @throws DocumentNotFoundException
707 * @throws TransactionException
708 * @throws DocumentException
709 * @return a list of documentModels
712 public DocumentWrapper<DocumentModelList> findDocs(
713 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
714 List<String> docTypes,
716 int pageSize, int pageNum, boolean computeTotal)
717 throws DocumentNotFoundException, TransactionException, DocumentException {
718 CoreSessionInterface repoSession = null;
719 DocumentWrapper<DocumentModelList> wrapDoc = null;
722 repoSession = getRepositorySession(ctx);
723 wrapDoc = findDocs(ctx, repoSession, docTypes, whereClause, null,
724 pageSize, pageNum, computeTotal);
725 } catch (IllegalArgumentException iae) {
727 } catch (Exception e) {
728 if (logger.isDebugEnabled()) {
729 logger.debug("Caught exception ", e);
731 throw new NuxeoDocumentException(e);
733 if (repoSession != null) {
734 releaseRepositorySession(ctx, repoSession);
738 if (logger.isWarnEnabled() == true) {
739 logger.warn("Returned DocumentModelList instance was created with a repository session that is now closed.");
746 * @see org.collectionspace.services.common.storage.StorageClient#get(org.collectionspace.services.common.context.ServiceContext, java.util.List, org.collectionspace.services.common.document.DocumentHandler)
749 public void get(ServiceContext ctx, List<String> csidList, DocumentHandler handler)
750 throws DocumentNotFoundException, TransactionException, DocumentException {
751 if (handler == null) {
752 throw new IllegalArgumentException(
753 "RepositoryJavaClient.getAll: handler is missing");
756 CoreSessionInterface repoSession = null;
758 handler.prepare(Action.GET_ALL);
759 repoSession = getRepositorySession(ctx);
760 DocumentModelList docModelList = new DocumentModelListImpl();
761 //FIXME: Should be using NuxeoUtils.createPathRef for security reasons
762 for (String csid : csidList) {
763 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
764 DocumentModel docModel = repoSession.getDocument(docRef);
765 docModelList.add(docModel);
768 //set reposession to handle the document
769 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
770 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docModelList);
771 handler.handle(Action.GET_ALL, wrapDoc);
772 handler.complete(Action.GET_ALL, wrapDoc);
773 } catch (DocumentException de) {
775 } catch (Exception e) {
776 if (logger.isDebugEnabled()) {
777 logger.debug("Caught exception ", e);
779 throw new NuxeoDocumentException(e);
781 if (repoSession != null) {
782 releaseRepositorySession(ctx, repoSession);
788 * getAll get all documents for an entity entity service from the Nuxeo
791 * @param ctx service context under which this method is invoked
792 * @param handler should be used by the caller to provide and transform the
794 * @throws DocumentNotFoundException
795 * @throws TransactionException
796 * @throws DocumentException
799 public void getAll(ServiceContext ctx, DocumentHandler handler)
800 throws DocumentNotFoundException, TransactionException, DocumentException {
801 if (handler == null) {
802 throw new IllegalArgumentException(
803 "RepositoryJavaClient.getAll: handler is missing");
805 String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
806 if (nuxeoWspaceId == null) {
807 throw new DocumentNotFoundException(
808 "Unable to find workspace for service "
809 + ctx.getServiceName()
810 + " check if the workspace exists in the Nuxeo repository.");
813 CoreSessionInterface repoSession = null;
815 handler.prepare(Action.GET_ALL);
816 repoSession = getRepositorySession(ctx);
817 DocumentRef wsDocRef = new IdRef(nuxeoWspaceId);
818 DocumentModelList docList = repoSession.getChildren(wsDocRef);
819 //set reposession to handle the document
820 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
821 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
822 handler.handle(Action.GET_ALL, wrapDoc);
823 handler.complete(Action.GET_ALL, wrapDoc);
824 } catch (DocumentException de) {
826 } catch (Exception e) {
827 if (logger.isDebugEnabled()) {
828 logger.debug("Caught exception ", e);
830 throw new NuxeoDocumentException(e);
832 if (repoSession != null) {
833 releaseRepositorySession(ctx, repoSession);
838 private boolean isClauseEmpty(String theString) {
839 boolean result = true;
840 if (theString != null && !theString.isEmpty()) {
846 public DocumentWrapper<DocumentModel> getDocFromCsid(
847 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
848 CoreSessionInterface repoSession,
851 DocumentWrapper<DocumentModel> result = null;
853 result = new DocumentWrapperImpl<DocumentModel>(NuxeoUtils.getDocFromCsid(ctx, repoSession, csid));
859 * A method to find a CollectionSpace document (of any type) given just a service context and
860 * its CSID. A search across *all* service workspaces (within a given tenant context) is performed to find
863 * This query searches Nuxeo's Hierarchy table where our CSIDs are stored in the "name" column.
866 public DocumentWrapper<DocumentModel> getDocFromCsid(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
869 DocumentWrapper<DocumentModel> result = null;
870 CoreSessionInterface repoSession = null;
872 repoSession = getRepositorySession(ctx);
873 result = getDocFromCsid(ctx, repoSession, csid);
875 if (repoSession != null) {
876 releaseRepositorySession(ctx, repoSession);
880 if (logger.isWarnEnabled() == true) {
881 logger.warn("Returned DocumentModel instance was created with a repository session that is now closed.");
888 * Returns a URI value for a document in the Nuxeo repository
890 * @param wrappedDoc a wrapped documentModel
891 * @throws ClientException
892 * @return a document URI
895 public String getDocURI(DocumentWrapper<DocumentModel> wrappedDoc) throws ClientException {
896 DocumentModel docModel = wrappedDoc.getWrappedObject();
897 String uri = (String) docModel.getProperty(CollectionSpaceClient.COLLECTIONSPACE_CORE_SCHEMA,
898 CollectionSpaceClient.COLLECTIONSPACE_CORE_URI);
903 * See CSPACE-5036 - How to make CMISQL queries from Nuxeo
905 private IterableQueryResult makeCMISQLQuery(CoreSessionInterface repoSession, String query, QueryContext queryContext) throws DocumentException {
906 IterableQueryResult result = null;
907 /** Threshold over which temporary files are not kept in memory. */
908 final int THRESHOLD = 1024 * 1024;
911 logger.debug(String.format("Performing a CMIS query on Nuxeo repository named %s",
912 repoSession.getRepositoryName()));
914 ThresholdOutputStreamFactory streamFactory = ThresholdOutputStreamFactory.newInstance(
915 null, THRESHOLD, -1, false);
916 CallContextImpl callContext = new CallContextImpl(
917 CallContext.BINDING_LOCAL,
918 CmisVersion.CMIS_1_1,
919 repoSession.getRepositoryName(),
920 null, // ServletContext
921 null, // HttpServletRequest
922 null, // HttpServletResponse
923 new NuxeoCmisServiceFactory(),
925 callContext.put(CallContext.USERNAME, repoSession.getPrincipal().getName());
927 NuxeoCmisService cmisService = new NuxeoCmisService(repoSession.getCoreSession());
928 result = repoSession.queryAndFetch(query, "CMISQL", cmisService);
929 } catch (ClientException e) {
930 // TODO Auto-generated catch block
931 logger.error("Encounter trouble making the following CMIS query: " + query, e);
932 throw new NuxeoDocumentException(e);
939 * getFiltered get all documents for an entity service from the Document
940 * repository, given filter parameters specified by the handler.
942 * @param ctx service context under which this method is invoked
943 * @param handler should be used by the caller to provide and transform the
945 * @throws DocumentNotFoundException if workspace not found
946 * @throws TransactionException
947 * @throws DocumentException
950 public void getFiltered(ServiceContext ctx, DocumentHandler handler)
951 throws DocumentNotFoundException, TransactionException, DocumentException {
953 DocumentFilter filter = handler.getDocumentFilter();
954 String oldOrderBy = filter.getOrderByClause();
955 if (isClauseEmpty(oldOrderBy) == true) {
956 filter.setOrderByClause(DocumentFilter.ORDER_BY_LAST_UPDATED);
958 QueryContext queryContext = new QueryContext(ctx, handler);
960 CoreSessionInterface repoSession = null;
962 handler.prepare(Action.GET_ALL);
963 repoSession = getRepositorySession(ctx); //Keeps a refcount here for the repository session so you need to release this when finished
965 DocumentModelList docList = null;
967 if (handler.isJDBCQuery() == true) {
968 docList = getFilteredJDBC(repoSession, ctx, handler);
970 } else if (handler.isCMISQuery() == true) {
971 docList = getFilteredCMIS(repoSession, ctx, handler, queryContext); //FIXME: REM - Need to deal with paging info in CMIS query
974 String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
975 if (logger.isDebugEnabled()) {
976 logger.debug("Executing NXQL query: " + query.toString());
978 Profiler profiler = new Profiler(this, 2);
979 profiler.log("Executing NXQL query: " + query.toString());
981 // If we have a page size and/or offset, then reflect those values
982 // when constructing the query, and also pass 'true' to get totalSize
983 // in the returned DocumentModelList.
984 if ((queryContext.getDocFilter().getOffset() > 0) || (queryContext.getDocFilter().getPageSize() > 0)) {
985 docList = repoSession.query(query, null,
986 queryContext.getDocFilter().getPageSize(), queryContext.getDocFilter().getOffset(), true);
988 docList = repoSession.query(query);
993 //set repoSession to handle the document
994 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
995 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
996 handler.handle(Action.GET_ALL, wrapDoc);
997 handler.complete(Action.GET_ALL, wrapDoc);
998 } catch (DocumentException de) {
1000 } catch (Exception e) {
1001 if (logger.isDebugEnabled()) {
1002 logger.debug("Caught exception ", e); // REM - 1/17/2014: Check for org.nuxeo.ecm.core.api.ClientException and re-attempt
1004 throw new NuxeoDocumentException(e);
1006 if (repoSession != null) {
1007 releaseRepositorySession(ctx, repoSession);
1013 * Perform a database query, via JDBC and SQL, to retrieve matching records
1014 * based on filter criteria.
1016 * Although this method currently has a general-purpose name, it is
1017 * currently dedicated to a specific task: that of improving performance
1018 * for partial term matching queries on authority items / terms, via
1019 * the use of a hand-tuned SQL query, rather than via the generated SQL
1020 * produced by Nuxeo from an NXQL query. (See CSPACE-6361 for a task
1021 * to generalize this method.)
1023 * @param repoSession a repository session.
1024 * @param ctx the service context.
1025 * @param handler a relevant document handler.
1026 * @return a list of document models matching the search criteria.
1029 private DocumentModelList getFilteredJDBC(CoreSessionInterface repoSession, ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
1030 DocumentHandler handler) throws Exception {
1031 DocumentModelList result = new DocumentModelListImpl();
1033 // FIXME: Get all of the following values from appropriate external constants.
1035 // At present, the two constants below are duplicated in both RepositoryClientImpl
1036 // and in AuthorityItemDocumentModelHandler.
1037 final String TERM_GROUP_LIST_NAME = "TERM_GROUP_LIST_NAME";
1038 final String TERM_GROUP_TABLE_NAME_PARAM = "TERM_GROUP_TABLE_NAME";
1039 final String IN_AUTHORITY_PARAM = "IN_AUTHORITY";
1040 // Get this from a constant in AuthorityResource or equivalent
1041 final String PARENT_WILDCARD = "_ALL_";
1043 // Build two SQL statements, to be executed within a single transaction:
1044 // the first statement to control join order, and the second statement
1045 // representing the actual 'get filtered' query
1047 // Build the join control statement
1049 // Per http://www.postgresql.org/docs/9.2/static/runtime-config-query.html#GUC-JOIN-COLLAPSE-LIMIT
1050 // "Setting [this value] to 1 prevents any reordering of explicit JOINs.
1051 // Thus, the explicit join order specified in the query will be the
1052 // actual order in which the relations are joined."
1053 // See CSPACE-5945 for further discussion of why this setting is needed.
1055 // Adding this statement is commented out here for now. It significantly
1056 // improved query performance for authority item / term queries where
1057 // large numbers of rows were retrieved, but appears to have resulted
1058 // in consistently slower-than-desired query performance where zero or
1059 // very few records were retrieved. See notes on CSPACE-5945. - ADR 2013-04-09
1060 // String joinControlSql = "SET LOCAL join_collapse_limit TO 1;";
1062 // Build the query statement
1064 // Start with the default query
1065 String selectStatement =
1066 "SELECT DISTINCT commonschema.id"
1067 + " FROM " + handler.getServiceContext().getCommonPartLabel() + " commonschema";
1069 String joinClauses =
1071 + " ON misc.id = commonschema.id"
1072 + " INNER JOIN hierarchy hierarchy_termgroup"
1073 + " ON hierarchy_termgroup.parentid = misc.id"
1074 + " INNER JOIN " + handler.getJDBCQueryParams().get(TERM_GROUP_TABLE_NAME_PARAM) + " termgroup"
1075 + " ON termgroup.id = hierarchy_termgroup.id ";
1078 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
1079 // Value for replaceable parameter 1 in the query
1080 String partialTerm = queryParams.getFirst(IQueryManager.SEARCH_TYPE_PARTIALTERM);
1081 // If the value of the partial term query parameter is blank ('pt='),
1082 // return all records, subject to restriction by any limit clause
1083 if (Tools.isBlank(partialTerm)) {
1086 // Otherwise, return records that match the supplied partial term
1088 " WHERE (termgroup.termdisplayname ILIKE ?)";
1091 // At present, results are ordered in code, below, rather than in SQL,
1092 // and the orderByClause below is thus intentionally blank.
1094 // To implement the orderByClause below in SQL; e.g. via
1095 // 'ORDER BY termgroup.termdisplayname', the relevant column
1096 // must be returned by the SELECT statement.
1097 String orderByClause = "";
1100 TenantBindingConfigReaderImpl tReader =
1101 ServiceMain.getInstance().getTenantBindingConfigReader();
1102 TenantBindingType tenantBinding = tReader.getTenantBinding(ctx.getTenantId());
1103 String maxListItemsLimit = TenantBindingUtils.getPropertyValue(tenantBinding,
1104 IQueryManager.MAX_LIST_ITEMS_RETURNED_LIMIT_ON_JDBC_QUERIES);
1106 " LIMIT " + getMaxItemsLimitOnJdbcQueries(maxListItemsLimit); // implicit int-to-String conversion
1108 // After building the individual parts of the query, set the values
1109 // of replaceable parameters that will be inserted into that query
1110 // and optionally add restrictions
1112 List<String> params = new ArrayList<>();
1114 if (Tools.notBlank(whereClause)) {
1116 // Read tenant bindings configuration to determine whether
1117 // to automatically insert leading, as well as trailing, wildcards
1118 // into the term matching string.
1119 String usesStartingWildcard = TenantBindingUtils.getPropertyValue(tenantBinding,
1120 IQueryManager.TENANT_USES_STARTING_WILDCARD_FOR_PARTIAL_TERM);
1121 // Handle user-provided leading wildcard characters, in the
1122 // configuration where a leading wildcard is not automatically inserted.
1123 // (The user-provided wildcard must be in the first, or "starting"
1124 // character position in the partial term value.)
1125 if (Tools.notBlank(usesStartingWildcard)) {
1126 if (usesStartingWildcard.equalsIgnoreCase(Boolean.FALSE.toString())) {
1127 partialTerm = handleProvidedStartingWildcard(partialTerm);
1128 // Otherwise, in the configuration where a leading wildcard
1129 // is usually automatically inserted, handle the cases where
1130 // a user has entered an anchor character in the first position
1131 // in the starting term value. In those cases, strip that
1132 // anchor character and don't add a leading wildcard
1134 if (partialTerm.startsWith(USER_SUPPLIED_ANCHOR_CHAR)) {
1135 partialTerm = partialTerm.substring(1, partialTerm.length());
1136 // Otherwise, automatically add a leading wildcard
1138 partialTerm = JDBCTools.SQL_WILDCARD + partialTerm;
1142 // Add SQL wildcards in the midst of the partial term match search
1143 // expression, whever user-supplied wildcards appear, except in the
1144 // first or last character positions of the search expression.
1145 partialTerm = subtituteWildcardsInPartialTerm(partialTerm);
1147 // If a designated 'anchor character' is present as the last character
1148 // in the search expression, strip that character and don't add
1149 // a trailing wildcard
1150 int lastCharPos = partialTerm.length() - 1;
1151 if (partialTerm.endsWith(USER_SUPPLIED_ANCHOR_CHAR) && lastCharPos > 0) {
1152 partialTerm = partialTerm.substring(0, lastCharPos);
1154 // Otherwise, automatically add a trailing wildcard
1155 partialTerm = partialTerm + JDBCTools.SQL_WILDCARD;
1157 params.add(partialTerm);
1160 // Optionally add restrictions to the default query, based on variables
1161 // in the current request
1163 // Restrict the query to filter out deleted records, if requested
1164 String includeDeleted = queryParams.getFirst(WorkflowClient.WORKFLOW_QUERY_NONDELETED);
1165 if (includeDeleted != null && includeDeleted.equalsIgnoreCase(Boolean.FALSE.toString())) {
1166 whereClause = whereClause
1167 + " AND (misc.lifecyclestate <> '" + WorkflowClient.WORKFLOWSTATE_DELETED + "')";
1170 // If a particular authority is specified, restrict the query further
1171 // to return only records within that authority
1172 String inAuthorityValue = (String) handler.getJDBCQueryParams().get(IN_AUTHORITY_PARAM);
1173 if (Tools.notBlank(inAuthorityValue)) {
1174 // Handle the '_ALL_' case for inAuthority
1175 if (inAuthorityValue.equals(PARENT_WILDCARD)) {
1176 // Add nothing to the query here if it should match within all authorities
1178 whereClause = whereClause
1179 + " AND (commonschema.inauthority = ?)";
1180 params.add(inAuthorityValue); // Value for replaceable parameter 2 in the query
1184 // Restrict the query further to return only records pertaining to
1185 // the current tenant, unless:
1186 // * Data for this service, in this tenant, is stored in its own,
1187 // separate repository, rather than being intermingled with other
1188 // tenants' data in the default repository; or
1189 // * Restriction by tenant ID in JDBC queries has been disabled,
1190 // via configuration for this tenant,
1191 if (restrictJDBCQueryByTenantID(tenantBinding, ctx)) {
1192 joinClauses = joinClauses
1193 + " INNER JOIN collectionspace_core core"
1194 + " ON core.id = hierarchy_termgroup.parentid";
1195 whereClause = whereClause
1196 + " AND (core.tenantid = ?)";
1197 params.add(ctx.getTenantId()); // Value for replaceable parameter 3 in the query
1200 // Piece together the SQL query from its parts
1201 String querySql = selectStatement + joinClauses + whereClause + orderByClause + limitClause;
1203 // Note: PostgreSQL 9.2 introduced a change that may improve performance
1204 // of certain queries using JDBC PreparedStatements. See comments on
1205 // CSPACE-5943 for details.
1207 // See a comment above for the reason that the joinControl SQL statement,
1208 // along with its corresponding prepared statement builder, is commented out for now.
1209 // PreparedStatementBuilder joinControlBuilder = new PreparedStatementBuilder(joinControlSql);
1210 PreparedStatementSimpleBuilder queryBuilder = new PreparedStatementSimpleBuilder(querySql, params);
1211 List<PreparedStatementBuilder> builders = new ArrayList<>();
1212 // builders.add(joinControlBuilder);
1213 builders.add(queryBuilder);
1214 String dataSourceName = JDBCTools.NUXEO_DATASOURCE_NAME;
1215 String repositoryName = ctx.getRepositoryName();
1216 final Boolean EXECUTE_WITHIN_TRANSACTION = true;
1217 Set<String> docIds = new HashSet<>();
1219 String cspaceInstanceId = ServiceMain.getInstance().getCspaceInstanceId();
1220 List<CachedRowSet> resultsList = JDBCTools.executePreparedQueries(builders,
1221 dataSourceName, repositoryName, cspaceInstanceId, EXECUTE_WITHIN_TRANSACTION);
1223 // At least one set of results is expected, from the second prepared
1224 // statement to be executed.
1225 // If fewer results are returned, return an empty list of document models
1226 if (resultsList == null || resultsList.size() < 1) {
1227 return result; // return an empty list of document models
1229 // The join control query (if enabled - it is currently commented
1230 // out as per comments above) will not return results, so query results
1231 // will be the first set of results (rowSet) returned in the list
1232 CachedRowSet queryResults = resultsList.get(0);
1234 // If the result from executing the query is null or contains zero rows,
1235 // return an empty list of document models
1236 if (queryResults == null) {
1237 return result; // return an empty list of document models
1239 queryResults.last();
1240 if (queryResults.getRow() == 0) {
1241 return result; // return an empty list of document models
1244 // Otherwise, get the document IDs from the results of the query
1246 queryResults.beforeFirst();
1247 while (queryResults.next()) {
1248 id = queryResults.getString(1);
1249 if (Tools.notBlank(id)) {
1253 } catch (SQLException sqle) {
1254 logger.warn("Could not obtain document IDs via SQL query '" + querySql + "': " + sqle.getMessage());
1255 return result; // return an empty list of document models
1258 // Get a list of document models, using the list of IDs obtained from the query
1260 // FIXME: Check whether we have a 'get document models from list of CSIDs'
1261 // utility method like this, and if not, add this to the appropriate
1263 DocumentModel docModel;
1264 for (String docId : docIds) {
1265 docModel = NuxeoUtils.getDocumentModel(repoSession, docId);
1266 if (docModel == null) {
1267 logger.warn("Could not obtain document model for document with ID " + docId);
1269 result.add(docModel);
1273 // Order the results
1274 final String COMMON_PART_SCHEMA = handler.getServiceContext().getCommonPartLabel();
1275 final String DISPLAY_NAME_XPATH =
1276 "//" + handler.getJDBCQueryParams().get(TERM_GROUP_LIST_NAME) + "/[0]/termDisplayName";
1277 Collections.sort(result, new Comparator<DocumentModel>() {
1279 public int compare(DocumentModel doc1, DocumentModel doc2) {
1280 String termDisplayName1 = null;
1281 String termDisplayName2 = null;
1283 termDisplayName1 = (String) NuxeoUtils.getXPathValue(doc1, COMMON_PART_SCHEMA, DISPLAY_NAME_XPATH);
1284 termDisplayName2 = (String) NuxeoUtils.getXPathValue(doc2, COMMON_PART_SCHEMA, DISPLAY_NAME_XPATH);
1285 } catch (NuxeoDocumentException e) {
1286 throw new RuntimeException(e); // We need to throw a RuntimeException because the compare() method of the Comparator interface does not support throwing an Exception
1288 return termDisplayName1.compareToIgnoreCase(termDisplayName2);
1296 private DocumentModelList getFilteredCMIS(CoreSessionInterface repoSession,
1297 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, DocumentHandler handler, QueryContext queryContext)
1298 throws DocumentNotFoundException, DocumentException {
1300 DocumentModelList result = new DocumentModelListImpl();
1302 String query = handler.getCMISQuery(queryContext);
1304 DocumentFilter docFilter = handler.getDocumentFilter();
1305 int pageSize = docFilter.getPageSize();
1306 int offset = docFilter.getOffset();
1307 if (logger.isDebugEnabled()) {
1308 logger.debug("Executing CMIS query: " + query.toString()
1309 + "with pageSize: " + pageSize + " at offset: " + offset);
1312 // If we have limit and/or offset, then pass true to get totalSize
1313 // in returned DocumentModelList.
1314 Profiler profiler = new Profiler(this, 2);
1315 profiler.log("Executing CMIS query: " + query.toString());
1318 IterableQueryResult queryResult = makeCMISQLQuery(repoSession, query, queryContext);
1320 int totalSize = (int) queryResult.size();
1321 ((DocumentModelListImpl) result).setTotalSize(totalSize);
1322 // Skip the rows before our offset
1324 queryResult.skipTo(offset);
1327 for (Map<String, Serializable> row : queryResult) {
1328 if (logger.isTraceEnabled()) {
1329 logger.trace(" Hierarchy Table ID is:" + row.get(IQueryManager.CMIS_TARGET_NUXEO_ID)
1330 + " nuxeo:pathSegment is: " + row.get(IQueryManager.CMIS_TARGET_NAME));
1332 String nuxeoId = (String) row.get(IQueryManager.CMIS_TARGET_NUXEO_ID);
1333 DocumentModel docModel = NuxeoUtils.getDocumentModel(repoSession, nuxeoId);
1334 result.add(docModel);
1336 if (nRows >= pageSize && pageSize != 0) { // A page size of zero means that they want all of them
1337 logger.debug("Got page full of items - quitting");
1342 queryResult.close();
1347 } catch (Exception e) {
1348 if (logger.isDebugEnabled()) {
1349 logger.debug("Caught exception ", e);
1351 throw new NuxeoDocumentException(e);
1355 // Since we're not supporting paging yet for CMIS queries, we need to perform
1356 // a workaround for the paging information we return in our list of results
1359 if (result != null) {
1360 docFilter.setStartPage(0);
1361 if (totalSize > docFilter.getPageSize()) {
1362 docFilter.setPageSize(totalSize);
1363 ((DocumentModelListImpl)result).setTotalSize(totalSize);
1371 private String logException(Exception e, String msg) {
1372 String result = null;
1374 String exceptionMessage = e.getMessage();
1375 exceptionMessage = exceptionMessage != null ? exceptionMessage : "<No details provided>";
1376 result = msg = msg + ". Caught exception:" + exceptionMessage;
1378 if (logger.isTraceEnabled() == true) {
1379 logger.error(msg, e);
1388 * update given document in the Nuxeo repository
1390 * @param ctx service context under which this method is invoked
1391 * @param csid of the document
1392 * @param handler should be used by the caller to provide and transform the
1394 * @throws BadRequestException
1395 * @throws DocumentNotFoundException
1396 * @throws TransactionException if the transaction times out or otherwise
1397 * cannot be successfully completed
1398 * @throws DocumentException
1401 public void update(ServiceContext ctx, String csid, DocumentHandler handler)
1402 throws BadRequestException, DocumentNotFoundException, TransactionException,
1404 if (handler == null) {
1405 throw new IllegalArgumentException(
1406 "RepositoryJavaClient.update: document handler is missing.");
1409 CoreSessionInterface repoSession = null;
1411 handler.prepare(Action.UPDATE);
1412 repoSession = getRepositorySession(ctx);
1413 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
1414 DocumentModel doc = null;
1416 doc = repoSession.getDocument(docRef);
1417 } catch (ClientException ce) {
1418 String msg = logException(ce, "Could not find document to update with CSID=" + csid);
1419 throw new DocumentNotFoundException(msg, ce);
1421 // Check for a versioned document, and check In and Out before we proceed.
1422 if (((DocumentModelHandler) handler).supportsVersioning()) {
1423 /* Once we advance to 5.5 or later, we can add this.
1424 * See also https://jira.nuxeo.com/browse/NXP-8506
1425 if(!doc.isVersionable()) {
1426 throw new NuxeoDocumentException("Configuration for: "
1427 +handler.getServiceContextPath()+" supports versioning, but Nuxeo config does not!");
1430 /* Force a version number - Not working. Apparently we need to configure the uid schema??
1431 if(doc.getProperty("uid","major_version") == null) {
1432 doc.setProperty("uid","major_version",1);
1434 if(doc.getProperty("uid","minor_version") == null) {
1435 doc.setProperty("uid","minor_version",0);
1438 doc.checkIn(VersioningOption.MINOR, null);
1443 // Set reposession to handle the document
1445 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
1446 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
1447 handler.handle(Action.UPDATE, wrapDoc);
1448 repoSession.saveDocument(doc);
1450 handler.complete(Action.UPDATE, wrapDoc);
1451 } catch (BadRequestException bre) {
1453 } catch (DocumentException de) {
1455 } catch (CSWebApplicationException wae) {
1457 } catch (Exception e) {
1458 throw new NuxeoDocumentException(e);
1460 if (repoSession != null) {
1461 releaseRepositorySession(ctx, repoSession);
1467 * Save a documentModel to the Nuxeo repository.
1469 * @param ctx service context under which this method is invoked
1470 * @param repoSession
1471 * @param docModel the document to save
1472 * @param fSaveSession if TRUE, will call CoreSessionInterface.save() to save
1473 * accumulated changes.
1474 * @throws ClientException
1475 * @throws DocumentException
1477 public void saveDocWithoutHandlerProcessing(
1478 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
1479 CoreSessionInterface repoSession,
1480 DocumentModel docModel,
1481 boolean fSaveSession)
1482 throws ClientException, DocumentException {
1485 repoSession.saveDocument(docModel);
1489 } catch (ClientException ce) {
1491 } catch (Exception e) {
1492 if (logger.isDebugEnabled()) {
1493 logger.debug("Caught exception ", e);
1495 throw new NuxeoDocumentException(e);
1500 * Save a list of documentModels to the Nuxeo repository.
1502 * @param ctx service context under which this method is invoked
1503 * @param repoSession a repository session
1504 * @param docModelList a list of document models
1505 * @param fSaveSession if TRUE, will call CoreSessionInterface.save() to save
1506 * accumulated changes.
1507 * @throws ClientException
1508 * @throws DocumentException
1510 public void saveDocListWithoutHandlerProcessing(
1511 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
1512 CoreSessionInterface repoSession,
1513 DocumentModelList docList,
1514 boolean fSaveSession)
1515 throws ClientException, DocumentException {
1517 DocumentModel[] docModelArray = new DocumentModel[docList.size()];
1518 repoSession.saveDocuments(docList.toArray(docModelArray));
1522 } catch (ClientException ce) {
1524 } catch (Exception e) {
1525 logger.error("Caught exception ", e);
1526 throw new NuxeoDocumentException(e);
1531 public void deleteWithWhereClause(@SuppressWarnings("rawtypes") ServiceContext ctx, String whereClause,
1532 @SuppressWarnings("rawtypes") DocumentHandler handler) throws
1533 DocumentNotFoundException, DocumentException {
1535 throw new IllegalArgumentException(
1536 "delete(ctx, specifier): ctx is missing");
1538 if (logger.isDebugEnabled()) {
1539 logger.debug("Deleting document with whereClause=" + whereClause);
1542 DocumentWrapper<DocumentModel> foundDocWrapper = this.findDoc(ctx, whereClause);
1543 if (foundDocWrapper != null) {
1544 DocumentModel docModel = foundDocWrapper.getWrappedObject();
1545 String csid = docModel.getName();
1546 this.delete(ctx, csid, handler);
1551 * delete a document from the Nuxeo repository
1553 * @param ctx service context under which this method is invoked
1554 * @param id of the document
1555 * @throws DocumentException
1558 public void delete(ServiceContext ctx, String id, DocumentHandler handler) throws DocumentNotFoundException,
1559 DocumentException, TransactionException {
1561 throw new IllegalArgumentException(
1562 "delete(ctx, ix, handler): ctx is missing");
1564 if (handler == null) {
1565 throw new IllegalArgumentException(
1566 "delete(ctx, ix, handler): handler is missing");
1568 if (logger.isDebugEnabled()) {
1569 logger.debug("Deleting document with CSID=" + id);
1571 CoreSessionInterface repoSession = null;
1573 handler.prepare(Action.DELETE);
1574 repoSession = getRepositorySession(ctx);
1575 DocumentWrapper<DocumentModel> wrapDoc = null;
1577 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
1578 wrapDoc = new DocumentWrapperImpl<DocumentModel>(repoSession.getDocument(docRef));
1579 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
1580 handler.handle(Action.DELETE, wrapDoc);
1581 repoSession.removeDocument(docRef);
1582 } catch (ClientException ce) {
1583 String msg = logException(ce, "Could not find document to delete with CSID=" + id);
1584 throw new DocumentNotFoundException(msg, ce);
1587 handler.complete(Action.DELETE, wrapDoc);
1588 } catch (DocumentException de) {
1590 } catch (Exception e) {
1591 if (logger.isDebugEnabled()) {
1592 logger.debug("Caught exception ", e);
1594 throw new NuxeoDocumentException(e);
1596 if (repoSession != null) {
1597 releaseRepositorySession(ctx, repoSession);
1603 * @see org.collectionspace.services.common.storage.StorageClient#delete(org.collectionspace.services.common.context.ServiceContext, java.lang.String, org.collectionspace.services.common.document.DocumentHandler)
1607 public void delete(@SuppressWarnings("rawtypes") ServiceContext ctx, String id)
1608 throws DocumentNotFoundException, DocumentException {
1609 throw new UnsupportedOperationException();
1610 // Use the other delete instead
1614 public Hashtable<String, String> retrieveWorkspaceIds(RepositoryDomainType repoDomain) throws Exception {
1615 return NuxeoConnectorEmbedded.getInstance().retrieveWorkspaceIds(repoDomain);
1619 public String createDomain(RepositoryDomainType repositoryDomain) throws Exception {
1620 CoreSessionInterface repoSession = null;
1621 String domainId = null;
1624 // Open a connection to the domain's repo/db
1626 String repoName = repositoryDomain.getRepositoryName();
1627 repoSession = getRepositorySession(repoName); // domainName=storageName=repoName=databaseName
1629 // First create the top-level domain directory
1631 String domainName = repositoryDomain.getStorageName();
1632 DocumentRef parentDocRef = new PathRef("/");
1633 DocumentModel parentDoc = repoSession.getDocument(parentDocRef);
1634 DocumentModel domainDoc = repoSession.createDocumentModel(parentDoc.getPathAsString(),
1635 domainName, NUXEO_CORE_TYPE_DOMAIN);
1636 domainDoc.setPropertyValue("dc:title", domainName);
1637 domainDoc.setPropertyValue("dc:description", "A CollectionSpace domain "
1639 domainDoc = repoSession.createDocument(domainDoc);
1640 domainId = domainDoc.getId();
1643 // Next, create a "Workspaces" root directory to contain the workspace folders for the individual service documents
1645 DocumentModel workspacesRoot = repoSession.createDocumentModel(domainDoc.getPathAsString(),
1646 NuxeoUtils.Workspaces, NUXEO_CORE_TYPE_WORKSPACEROOT);
1647 workspacesRoot.setPropertyValue("dc:title", NuxeoUtils.Workspaces);
1648 workspacesRoot.setPropertyValue("dc:description", "A CollectionSpace workspaces directory for "
1649 + domainDoc.getPathAsString());
1650 workspacesRoot = repoSession.createDocument(workspacesRoot);
1651 String workspacesRootId = workspacesRoot.getId();
1654 if (logger.isDebugEnabled()) {
1655 logger.debug("Created tenant domain name=" + domainName
1656 + " id=" + domainId + " "
1657 + NuxeoUtils.Workspaces + " id=" + workspacesRootId);
1658 logger.debug("Path to Domain: " + domainDoc.getPathAsString());
1659 logger.debug("Path to Workspaces root: " + workspacesRoot.getPathAsString());
1661 } catch (Exception e) {
1662 if (logger.isDebugEnabled()) {
1663 logger.debug("Could not create tenant domain name=" + repositoryDomain.getStorageName() + " caught exception ", e);
1667 if (repoSession != null) {
1668 releaseRepositorySession(null, repoSession);
1676 public String getDomainId(RepositoryDomainType repositoryDomain) throws Exception {
1677 String domainId = null;
1678 CoreSessionInterface repoSession = null;
1680 String repoName = repositoryDomain.getRepositoryName();
1681 String domainStorageName = repositoryDomain.getStorageName();
1682 if (domainStorageName != null && !domainStorageName.isEmpty()) {
1684 repoSession = getRepositorySession(repoName);
1685 DocumentRef docRef = new PathRef("/" + domainStorageName);
1686 DocumentModel domain = repoSession.getDocument(docRef);
1687 domainId = domain.getId();
1688 } catch (Exception e) {
1689 if (logger.isTraceEnabled()) {
1690 logger.trace("Caught exception ", e); // The document doesn't exist, this let's us know we need to create it
1692 //there is no way to identify if document does not exist due to
1693 //lack of typed exception for getDocument method
1696 if (repoSession != null) {
1697 releaseRepositorySession(null, repoSession);
1706 * Returns the workspaces root directory for a given domain.
1708 private DocumentModel getWorkspacesRoot(CoreSessionInterface repoSession,
1709 String domainName) throws Exception {
1710 DocumentModel result = null;
1712 String domainPath = "/" + domainName;
1713 DocumentRef parentDocRef = new PathRef(domainPath);
1714 DocumentModelList domainChildrenList = repoSession.getChildren(
1716 Iterator<DocumentModel> witer = domainChildrenList.iterator();
1717 while (witer.hasNext()) {
1718 DocumentModel childNode = witer.next();
1719 if (NuxeoUtils.Workspaces.equalsIgnoreCase(childNode.getName())) {
1721 logger.trace("Found workspaces directory at: " + result.getPathAsString());
1726 if (result == null) {
1727 throw new ClientException("Could not find workspace root directory in: "
1735 * @see org.collectionspace.services.common.repository.RepositoryClient#createWorkspace(java.lang.String, java.lang.String)
1738 public String createWorkspace(RepositoryDomainType repositoryDomain, String workspaceName) throws Exception {
1739 CoreSessionInterface repoSession = null;
1740 String workspaceId = null;
1742 String repoName = repositoryDomain.getRepositoryName();
1743 repoSession = getRepositorySession(repoName);
1745 String domainStorageName = repositoryDomain.getStorageName();
1746 DocumentModel parentDoc = getWorkspacesRoot(repoSession, domainStorageName);
1747 if (logger.isTraceEnabled()) {
1748 for (String facet : parentDoc.getFacets()) {
1749 logger.trace("Facet: " + facet);
1753 DocumentModel doc = repoSession.createDocumentModel(parentDoc.getPathAsString(),
1754 workspaceName, NuxeoUtils.WORKSPACE_DOCUMENT_TYPE);
1755 doc.setPropertyValue("dc:title", workspaceName);
1756 doc.setPropertyValue("dc:description", "A CollectionSpace workspace for "
1758 doc = repoSession.createDocument(doc);
1759 workspaceId = doc.getId();
1761 if (logger.isDebugEnabled()) {
1762 logger.debug("Created workspace name=" + workspaceName
1763 + " id=" + workspaceId);
1765 } catch (Exception e) {
1766 if (logger.isDebugEnabled()) {
1767 logger.debug("createWorkspace caught exception ", e);
1771 if (repoSession != null) {
1772 releaseRepositorySession(null, repoSession);
1779 * @see org.collectionspace.services.common.repository.RepositoryClient#getWorkspaceId(java.lang.String, java.lang.String)
1783 public String getWorkspaceId(String tenantDomain, String workspaceName) throws Exception {
1784 String workspaceId = null;
1786 CoreSessionInterface repoSession = null;
1788 repoSession = getRepositorySession((ServiceContext<PoxPayloadIn, PoxPayloadOut>) null);
1789 DocumentRef docRef = new PathRef(
1791 + "/" + NuxeoUtils.Workspaces
1792 + "/" + workspaceName);
1793 DocumentModel workspace = repoSession.getDocument(docRef);
1794 workspaceId = workspace.getId();
1795 } catch (DocumentException de) {
1797 } catch (Exception e) {
1798 if (logger.isDebugEnabled()) {
1799 logger.debug("Caught exception ", e);
1801 throw new NuxeoDocumentException(e);
1803 if (repoSession != null) {
1804 releaseRepositorySession(null, repoSession);
1811 public CoreSessionInterface getRepositorySession(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {
1812 return getRepositorySession(ctx, ctx.getRepositoryName(), ctx.getTimeoutSecs());
1815 public CoreSessionInterface getRepositorySession(String repoName) throws Exception {
1816 return getRepositorySession(null, repoName, ServiceContext.DEFAULT_TX_TIMEOUT);
1820 * Gets the repository session. - Package access only. If the 'ctx' param is
1821 * null then the repo name must be non-mull and vice-versa
1823 * @return the repository session
1824 * @throws Exception the exception
1826 public CoreSessionInterface getRepositorySession(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
1828 int timeoutSeconds) throws Exception {
1829 CoreSessionInterface repoSession = null;
1831 Profiler profiler = new Profiler("getRepositorySession():", 2);
1834 // To get a connection to the Nuxeo repo, we need either a valid ServiceContext instance or a repository name
1837 repoName = ctx.getRepositoryName(); // Notice we are overriding the passed in 'repoName' since we have a valid service context passed in to us
1838 repoSession = (CoreSessionInterface) ctx.getCurrentRepositorySession(); // Look to see if one exists in the context before creating one
1839 } else if (repoName == null || repoName.trim().isEmpty()) {
1840 String errMsg = String.format("We can't get a connection to the Nuxeo repo because the service context passed in was null and no repository name was passed in either.");
1841 logger.error(errMsg);
1842 throw new Exception(errMsg);
1845 // If we couldn't find a repoSession from the service context (or the context was null) then we need to create a new one using
1846 // just the repo name
1848 if (repoSession == null) {
1849 NuxeoClientEmbedded client = NuxeoConnectorEmbedded.getInstance().getClient();
1850 repoSession = client.openRepository(repoName, timeoutSeconds);
1852 if (logger.isDebugEnabled() == true) {
1853 logger.warn("Reusing the current context's repository session.");
1858 if (logger.isTraceEnabled()) {
1859 logger.trace("Testing call to getRepository() repository root: " + repoSession.getRootDocument());
1861 } catch (Throwable e) {
1862 logger.trace("Test call to Nuxeo's getRepository() repository root failed", e);
1868 ctx.setCurrentRepositorySession(repoSession); // For reusing, save the repository session in the current service context
1875 * Release repository session. - Package access only.
1877 * @param repoSession the repo session
1879 public void releaseRepositorySession(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, CoreSessionInterface repoSession) throws TransactionException {
1881 NuxeoClientEmbedded client = NuxeoConnectorEmbedded.getInstance().getClient();
1884 ctx.clearCurrentRepositorySession(); //clear the current context of the now closed repo session
1885 if (ctx.getCurrentRepositorySession() == null) {
1886 client.releaseRepository(repoSession); //release the repo session if the service context's ref count is zeo.
1889 client.releaseRepository(repoSession); //repo session was acquired without a service context
1891 } catch (TransactionRuntimeException tre) {
1892 String causeMsg = null;
1893 Throwable cause = tre.getCause();
1894 if (cause != null) {
1895 causeMsg = cause.getMessage();
1898 TransactionException te; // a CollectionSpace specific tx exception
1899 if (causeMsg != null) {
1900 te = new TransactionException(causeMsg, tre);
1902 te = new TransactionException(tre);
1905 logger.error(te.getMessage(), tre); // Log the standard transaction exception message, plus an exception-specific stack trace
1907 } catch (Exception e) {
1908 logger.error("Could not close the repository session.", e);
1909 // no need to throw this service specific exception
1914 public void doWorkflowTransition(ServiceContext ctx, String id,
1915 DocumentHandler handler, TransitionDef transitionDef)
1916 throws BadRequestException, DocumentNotFoundException,
1918 // This is a placeholder for when we change the StorageClient interface to treat workflow transitions as 1st class operations like 'get', 'create', 'update, 'delete', etc
1921 private String handleProvidedStartingWildcard(String partialTerm) {
1922 if (Tools.notBlank(partialTerm)) {
1923 if (partialTerm.substring(0, 1).equals(USER_SUPPLIED_WILDCARD)) {
1924 StringBuffer buffer = new StringBuffer(partialTerm);
1925 buffer.setCharAt(0, JDBCTools.SQL_WILDCARD.charAt(0));
1926 partialTerm = buffer.toString();
1933 * Replaces user-supplied wildcards with SQL wildcards, in a partial term
1934 * matching search expression.
1936 * The scope of this replacement excludes the beginning character
1937 * in that search expression, as that character is treated specially.
1939 * @param partialTerm
1940 * @return the partial term, with any user-supplied wildcards replaced
1943 private String subtituteWildcardsInPartialTerm(String partialTerm) {
1944 if (Tools.isBlank(partialTerm)) {
1947 if (! partialTerm.contains(USER_SUPPLIED_WILDCARD)) {
1950 int len = partialTerm.length();
1951 // Partial term search expressions of 2 or fewer characters
1952 // currently aren't amenable to the use of wildcards
1954 logger.warn("Partial term match search expression of just 1-2 characters in length contains a user-supplied wildcard: " + partialTerm);
1955 logger.warn("Will handle that character as a literal value, rather than as a wildcard ...");
1958 return partialTerm.substring(0, 1) // first char
1959 + partialTerm.substring(1, len).replaceAll(USER_SUPPLIED_WILDCARD_REGEX, JDBCTools.SQL_WILDCARD);
1963 private int getMaxItemsLimitOnJdbcQueries(String maxListItemsLimit) {
1964 final int DEFAULT_ITEMS_LIMIT = 40;
1965 if (maxListItemsLimit == null) {
1966 return DEFAULT_ITEMS_LIMIT;
1970 itemsLimit = Integer.parseInt(maxListItemsLimit);
1971 if (itemsLimit < 1) {
1972 logger.warn("Value of configuration setting "
1973 + IQueryManager.MAX_LIST_ITEMS_RETURNED_LIMIT_ON_JDBC_QUERIES
1974 + " must be a positive integer; invalid current value is " + maxListItemsLimit);
1975 logger.warn("Reverting to default value of " + DEFAULT_ITEMS_LIMIT);
1976 itemsLimit = DEFAULT_ITEMS_LIMIT;
1978 } catch (NumberFormatException nfe) {
1979 logger.warn("Value of configuration setting "
1980 + IQueryManager.MAX_LIST_ITEMS_RETURNED_LIMIT_ON_JDBC_QUERIES
1981 + " must be a positive integer; invalid current value is " + maxListItemsLimit);
1982 logger.warn("Reverting to default value of " + DEFAULT_ITEMS_LIMIT);
1983 itemsLimit = DEFAULT_ITEMS_LIMIT;
1989 * Identifies whether a restriction on tenant ID - to return only records
1990 * pertaining to the current tenant - is required in a JDBC query.
1992 * @param tenantBinding a tenant binding configuration.
1993 * @param ctx a service context.
1994 * @return true if a restriction on tenant ID is required in the query;
1995 * false if a restriction is not required.
1997 private boolean restrictJDBCQueryByTenantID(TenantBindingType tenantBinding, ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) {
1998 boolean restrict = true;
1999 // If data for the current service, in the current tenant, is isolated
2000 // within its own separate, per-tenant repository, as contrasted with
2001 // being intermingled with other tenants' data in the default repository,
2002 // no restriction on Tenant ID is required in the query.
2003 String repositoryDomainName = ConfigUtils.getRepositoryName(tenantBinding, ctx.getRepositoryDomainName());
2004 if (!(repositoryDomainName.equals(ConfigUtils.DEFAULT_NUXEO_REPOSITORY_NAME))) {
2007 // If a configuration setting for this tenant identifies that JDBC
2008 // queries should not be restricted by tenant ID (perhaps because
2009 // there is always expected to be only one tenant's data present in
2010 // the system), no restriction on Tenant ID is required in the query.
2011 String queriesRestrictedByTenantId = TenantBindingUtils.getPropertyValue(tenantBinding,
2012 IQueryManager.JDBC_QUERIES_ARE_TENANT_ID_RESTRICTED);
2013 if (Tools.notBlank(queriesRestrictedByTenantId) &&
2014 queriesRestrictedByTenantId.equalsIgnoreCase(Boolean.FALSE.toString())) {