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.config.tenant.TenantBindingType;
66 import org.collectionspace.services.config.tenant.RepositoryDomainType;
69 // CSPACE-5036 - How to make CMISQL queries from Nuxeo
71 import org.apache.chemistry.opencmis.commons.enums.CmisVersion;
72 import org.apache.chemistry.opencmis.commons.server.CallContext;
73 import org.apache.chemistry.opencmis.server.impl.CallContextImpl;
74 import org.apache.chemistry.opencmis.server.shared.ThresholdOutputStreamFactory;
75 import org.nuxeo.common.utils.IdUtils;
76 import org.nuxeo.ecm.core.api.ClientException;
77 import org.nuxeo.ecm.core.api.DocumentModel;
78 import org.nuxeo.ecm.core.api.DocumentModelList;
79 import org.nuxeo.ecm.core.api.IterableQueryResult;
80 import org.nuxeo.ecm.core.api.VersioningOption;
81 import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
82 import org.nuxeo.ecm.core.api.DocumentRef;
83 import org.nuxeo.ecm.core.api.IdRef;
84 import org.nuxeo.ecm.core.api.PathRef;
85 import org.nuxeo.runtime.transaction.TransactionRuntimeException;
86 import org.nuxeo.ecm.core.opencmis.bindings.NuxeoCmisServiceFactory;
87 import org.nuxeo.ecm.core.opencmis.impl.server.NuxeoCmisService;
88 import org.slf4j.Logger;
89 import org.slf4j.LoggerFactory;
92 * RepositoryClientImpl is used to perform CRUD operations on documents in Nuxeo
93 * repository using Remote Java APIs. It uses
95 * @see DocumentHandler as IOHandler with the client.
97 * $LastChangedRevision: $ $LastChangedDate: $
99 public class RepositoryClientImpl implements RepositoryClient<PoxPayloadIn, PoxPayloadOut> {
104 private final Logger logger = LoggerFactory.getLogger(RepositoryClientImpl.class);
105 // private final Logger profilerLogger = LoggerFactory.getLogger("remperf");
106 // private String foo = Profiler.createLogger();
107 public static final String NUXEO_CORE_TYPE_DOMAIN = "Domain";
108 public static final String NUXEO_CORE_TYPE_WORKSPACEROOT = "WorkspaceRoot";
109 // FIXME: Get this value from an existing constant, if available
110 public static final String BACKSLASH = "\\";
111 public static final String USER_SUPPLIED_WILDCARD = "*";
112 public static final String USER_SUPPLIED_WILDCARD_REGEX = BACKSLASH + USER_SUPPLIED_WILDCARD;
113 public static final String USER_SUPPLIED_ANCHOR_CHAR = "^";
114 public static final String USER_SUPPLIED_ANCHOR_CHAR_REGEX = BACKSLASH + USER_SUPPLIED_ANCHOR_CHAR;
115 public static final String ENDING_ANCHOR_CHAR = "$";
116 public static final String ENDING_ANCHOR_CHAR_REGEX = BACKSLASH + ENDING_ANCHOR_CHAR;
120 * Instantiates a new repository java client impl.
122 public RepositoryClientImpl() {
126 public void assertWorkflowState(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
127 DocumentModel docModel) throws DocumentNotFoundException, ClientException {
128 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
129 if (queryParams != null) {
131 // Look for the workflow "delete" query param and see if we need to assert that the
132 // docModel is in a non-deleted workflow state.
134 String currentState = docModel.getCurrentLifeCycleState();
135 String includeDeletedStr = queryParams.getFirst(WorkflowClient.WORKFLOW_QUERY_NONDELETED);
136 boolean includeDeleted = includeDeletedStr == null ? true : Boolean.parseBoolean(includeDeletedStr);
137 if (includeDeleted == false) {
139 // We don't wanted soft-deleted object, so throw an exception if this one is soft-deleted.
141 if (currentState.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
142 String msg = "The GET assertion that docModel not be in 'deleted' workflow state failed.";
144 throw new DocumentNotFoundException(msg);
151 * create document in the Nuxeo repository
153 * @param ctx service context under which this method is invoked
154 * @param handler should be used by the caller to provide and transform the
156 * @return id in repository of the newly created document
157 * @throws BadRequestException
158 * @throws TransactionException
159 * @throws DocumentException
162 public String create(ServiceContext ctx,
163 DocumentHandler handler) throws BadRequestException,
164 TransactionException, DocumentException {
166 String docType = NuxeoUtils.getTenantQualifiedDocType(ctx); //ctx.getDocumentType();
167 if (docType == null) {
168 throw new IllegalArgumentException(
169 "RepositoryJavaClient.create: docType is missing");
172 if (handler == null) {
173 throw new IllegalArgumentException(
174 "RepositoryJavaClient.create: handler is missing");
176 String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
177 if (nuxeoWspaceId == null) {
178 throw new DocumentNotFoundException(
179 "Unable to find workspace for service " + ctx.getServiceName()
180 + " check if the workspace exists in the Nuxeo repository");
183 CoreSessionInterface repoSession = null;
185 handler.prepare(Action.CREATE);
186 repoSession = getRepositorySession(ctx);
187 DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);
188 DocumentModel wspaceDoc = repoSession.getDocument(nuxeoWspace);
189 String wspacePath = wspaceDoc.getPathAsString();
190 //give our own ID so PathRef could be constructed later on
191 String id = IdUtils.generateId(UUID.randomUUID().toString());
192 // create document model
193 DocumentModel doc = repoSession.createDocumentModel(wspacePath, id, docType);
194 /* Check for a versioned document, and check In and Out before we proceed.
195 * This does not work as we do not have the uid schema on our docs.
196 if(((DocumentModelHandler) handler).supportsVersioning()) {
197 doc.setProperty("uid","major_version",1);
198 doc.setProperty("uid","minor_version",0);
201 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
202 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
203 handler.handle(Action.CREATE, wrapDoc);
204 // create document with documentmodel
205 doc = repoSession.createDocument(doc);
207 // TODO for sub-docs need to call into the handler to let it deal with subitems. Pass in the id,
208 // and assume the handler has the state it needs (doc fragments).
209 handler.complete(Action.CREATE, wrapDoc);
211 } catch (BadRequestException bre) {
213 } catch (Exception e) {
214 throw new NuxeoDocumentException(e);
216 if (repoSession != null) {
217 releaseRepositorySession(ctx, repoSession);
225 public boolean reindex(DocumentHandler handler, String indexid) throws DocumentNotFoundException, DocumentException
227 return reindex(handler, null, indexid);
231 public boolean reindex(DocumentHandler handler, String csid, String indexid) throws DocumentNotFoundException, DocumentException
233 boolean result = true;
234 CoreSessionInterface repoSession = null;
235 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = handler.getServiceContext();
238 String queryString = handler.getDocumentsToIndexQuery(indexid, csid);
239 repoSession = getRepositorySession(ctx);
240 CSReindexFulltextRoot indexer = new CSReindexFulltextRoot(repoSession);
241 indexer.reindexFulltext(0, 0, queryString);
243 // Set repository session to handle the document
245 } catch (Exception e) {
246 if (logger.isDebugEnabled()) {
247 logger.debug("Caught exception ", e);
249 throw new NuxeoDocumentException(e);
251 if (repoSession != null) {
252 releaseRepositorySession(ctx, repoSession);
260 * get document from the Nuxeo repository
262 * @param ctx service context under which this method is invoked
263 * @param id of the document to retrieve
264 * @param handler should be used by the caller to provide and transform the
266 * @throws DocumentNotFoundException if the document cannot be found in the
268 * @throws TransactionException
269 * @throws DocumentException
272 public void get(ServiceContext ctx, String id, DocumentHandler handler)
273 throws DocumentNotFoundException, TransactionException, DocumentException {
275 if (handler == null) {
276 throw new IllegalArgumentException(
277 "RepositoryJavaClient.get: handler is missing");
280 CoreSessionInterface repoSession = null;
282 handler.prepare(Action.GET);
283 repoSession = getRepositorySession(ctx);
284 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
285 DocumentModel docModel = null;
287 docModel = repoSession.getDocument(docRef);
288 assertWorkflowState(ctx, docModel);
289 } catch (ClientException ce) {
290 String msg = logException(ce, "Could not find document with CSID=" + id);
291 throw new DocumentNotFoundException(msg, ce);
294 // Set repository session to handle the document
296 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
297 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(docModel);
298 handler.handle(Action.GET, wrapDoc);
299 handler.complete(Action.GET, wrapDoc);
300 } catch (IllegalArgumentException iae) {
302 } catch (DocumentException de) {
304 } catch (Exception e) {
305 if (logger.isDebugEnabled()) {
306 logger.debug("Caught exception ", e);
308 throw new NuxeoDocumentException(e);
310 if (repoSession != null) {
311 releaseRepositorySession(ctx, repoSession);
317 * get a document from the Nuxeo repository, using the docFilter params.
319 * @param ctx service context under which this method is invoked
320 * @param handler should be used by the caller to provide and transform the
321 * document. Handler must have a docFilter set to return a single item.
322 * @throws DocumentNotFoundException if the document cannot be found in the
324 * @throws TransactionException
325 * @throws DocumentException
328 public void get(ServiceContext ctx, DocumentHandler handler)
329 throws DocumentNotFoundException, TransactionException, DocumentException {
330 QueryContext queryContext = new QueryContext(ctx, handler);
331 CoreSessionInterface repoSession = null;
334 handler.prepare(Action.GET);
335 repoSession = getRepositorySession(ctx);
337 DocumentModelList docList = null;
338 // force limit to 1, and ignore totalSize
339 String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
340 docList = repoSession.query(query, null, 1, 0, false);
341 if (docList.size() != 1) {
342 throw new DocumentNotFoundException("No document found matching filter params: " + query);
344 DocumentModel doc = docList.get(0);
346 if (logger.isDebugEnabled()) {
347 logger.debug("Executed NXQL query: " + query);
350 //set reposession to handle the document
351 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
352 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
353 handler.handle(Action.GET, wrapDoc);
354 handler.complete(Action.GET, wrapDoc);
355 } catch (IllegalArgumentException iae) {
357 } catch (DocumentException de) {
359 } catch (Exception e) {
360 if (logger.isDebugEnabled()) {
361 logger.debug("Caught exception ", e);
363 throw new NuxeoDocumentException(e);
365 if (repoSession != null) {
366 releaseRepositorySession(ctx, repoSession);
371 public DocumentWrapper<DocumentModel> getDoc(
372 CoreSessionInterface repoSession,
373 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
374 String csid) throws DocumentNotFoundException, DocumentException {
375 DocumentWrapper<DocumentModel> wrapDoc = null;
378 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
379 DocumentModel doc = null;
381 doc = repoSession.getDocument(docRef);
382 } catch (ClientException ce) {
383 String msg = logException(ce, "Could not find document with CSID=" + csid);
384 throw new DocumentNotFoundException(msg, ce);
386 wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
387 } catch (IllegalArgumentException iae) {
389 } catch (DocumentException de) {
397 * Get wrapped documentModel from the Nuxeo repository. The search is
398 * restricted to the workspace of the current context.
400 * @param ctx service context under which this method is invoked
401 * @param csid of the document to retrieve
402 * @throws DocumentNotFoundException
403 * @throws TransactionException
404 * @throws DocumentException
405 * @return a wrapped documentModel
408 public DocumentWrapper<DocumentModel> getDoc(
409 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
410 String csid) throws DocumentNotFoundException, TransactionException, DocumentException {
411 CoreSessionInterface repoSession = null;
412 DocumentWrapper<DocumentModel> wrapDoc = null;
415 // Open a new repository session
416 repoSession = getRepositorySession(ctx);
417 wrapDoc = getDoc(repoSession, ctx, csid);
418 } catch (IllegalArgumentException iae) {
420 } catch (DocumentException de) {
422 } catch (Exception e) {
423 if (logger.isDebugEnabled()) {
424 logger.debug("Caught exception ", e);
426 throw new NuxeoDocumentException(e);
428 if (repoSession != null) {
429 releaseRepositorySession(ctx, repoSession);
433 if (logger.isWarnEnabled() == true) {
434 logger.warn("Returned DocumentModel instance was created with a repository session that is now closed.");
439 public DocumentWrapper<DocumentModel> findDoc(
440 CoreSessionInterface repoSession,
441 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
443 throws DocumentNotFoundException, DocumentException {
444 DocumentWrapper<DocumentModel> wrapDoc = null;
447 QueryContext queryContext = new QueryContext(ctx, whereClause);
448 DocumentModelList docList = null;
449 // force limit to 1, and ignore totalSize
450 String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
451 docList = repoSession.query(query,
456 if (docList.size() != 1) {
457 if (logger.isDebugEnabled()) {
458 logger.debug("findDoc: Query found: " + docList.size() + " items.");
459 logger.debug(" Query: " + query);
461 throw new DocumentNotFoundException("No document found matching filter params: " + query);
463 DocumentModel doc = docList.get(0);
464 wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
465 } catch (IllegalArgumentException iae) {
467 } catch (DocumentException de) {
469 } catch (Exception e) {
470 if (logger.isDebugEnabled()) {
471 logger.debug("Caught exception ", e);
473 throw new NuxeoDocumentException(e);
480 * find wrapped documentModel from the Nuxeo repository
482 * @param ctx service context under which this method is invoked
483 * @param whereClause where NXQL where clause to get the document
484 * @throws DocumentNotFoundException
485 * @throws TransactionException
486 * @throws DocumentException
487 * @return a wrapped documentModel retrieved by the repository query
490 public DocumentWrapper<DocumentModel> findDoc(
491 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
493 throws DocumentNotFoundException, TransactionException, DocumentException {
494 CoreSessionInterface repoSession = null;
495 DocumentWrapper<DocumentModel> wrapDoc = null;
498 repoSession = getRepositorySession(ctx);
499 wrapDoc = findDoc(repoSession, ctx, whereClause);
500 } catch (DocumentNotFoundException dnfe) {
502 } catch (DocumentException de) {
504 } catch (Exception e) {
505 if (repoSession == null) {
506 throw new NuxeoDocumentException("Unable to create a Nuxeo repository session.", e);
508 throw new NuxeoDocumentException("Unexpected Nuxeo exception.", e);
511 if (repoSession != null) {
512 releaseRepositorySession(ctx, repoSession);
516 if (logger.isWarnEnabled() == true) {
517 logger.warn("Returned DocumentModel instance was created with a repository session that is now closed.");
524 * find doc and return CSID from the Nuxeo repository
527 * @param ctx service context under which this method is invoked
528 * @param whereClause where NXQL where clause to get the document
529 * @throws DocumentNotFoundException
530 * @throws TransactionException
531 * @throws DocumentException
532 * @return the CollectionSpace ID (CSID) of the requested document
535 public String findDocCSID(CoreSessionInterface repoSession,
536 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String whereClause)
537 throws DocumentNotFoundException, TransactionException, DocumentException {
539 boolean releaseSession = false;
541 if (repoSession == null) {
542 repoSession = this.getRepositorySession(ctx);
543 releaseSession = true;
545 DocumentWrapper<DocumentModel> wrapDoc = findDoc(repoSession, ctx, whereClause);
546 DocumentModel docModel = wrapDoc.getWrappedObject();
547 csid = NuxeoUtils.getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
548 } catch (DocumentNotFoundException dnfe) {
550 } catch (IllegalArgumentException iae) {
552 } catch (DocumentException de) {
554 } catch (Exception e) {
555 if (logger.isDebugEnabled()) {
556 logger.debug("Caught exception ", e);
558 throw new NuxeoDocumentException(e);
560 if (releaseSession && (repoSession != null)) {
561 this.releaseRepositorySession(ctx, repoSession);
567 public DocumentWrapper<DocumentModelList> findDocs(
568 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
569 CoreSessionInterface repoSession,
570 List<String> docTypes,
572 String orderByClause,
575 boolean computeTotal)
576 throws DocumentNotFoundException, DocumentException {
577 DocumentWrapper<DocumentModelList> wrapDoc = null;
580 if (docTypes == null || docTypes.size() < 1) {
581 throw new DocumentNotFoundException(
582 "The findDocs() method must specify at least one DocumentType.");
584 DocumentModelList docList = null;
585 QueryContext queryContext = new QueryContext(ctx, whereClause, orderByClause);
586 String query = NuxeoUtils.buildNXQLQuery(docTypes, queryContext);
587 if (logger.isDebugEnabled()) {
588 logger.debug("findDocs() NXQL: " + query);
590 docList = repoSession.query(query, null, pageSize, pageSize * pageNum, computeTotal);
591 wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
592 } catch (IllegalArgumentException iae) {
594 } catch (Exception e) {
595 if (logger.isDebugEnabled()) {
596 logger.debug("Caught exception ", e);
598 throw new NuxeoDocumentException(e);
604 protected static String buildInListForDocTypes(List<String> docTypes) {
605 StringBuilder sb = new StringBuilder();
607 boolean first = true;
608 for (String docType : docTypes) {
619 return sb.toString();
622 public DocumentWrapper<DocumentModelList> findDocs(
623 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
624 DocumentHandler handler,
625 CoreSessionInterface repoSession,
626 List<String> docTypes)
627 throws DocumentNotFoundException, DocumentException {
628 DocumentWrapper<DocumentModelList> wrapDoc = null;
630 DocumentFilter filter = handler.getDocumentFilter();
631 String oldOrderBy = filter.getOrderByClause();
632 if (isClauseEmpty(oldOrderBy) == true) {
633 filter.setOrderByClause(DocumentFilter.ORDER_BY_LAST_UPDATED);
635 QueryContext queryContext = new QueryContext(ctx, handler);
638 if (docTypes == null || docTypes.size() < 1) {
639 throw new DocumentNotFoundException(
640 "The findDocs() method must specify at least one DocumentType.");
642 DocumentModelList docList = null;
643 if (handler.isCMISQuery() == true) {
644 String inList = buildInListForDocTypes(docTypes);
645 ctx.getQueryParams().add(IQueryManager.SEARCH_RELATED_MATCH_OBJ_DOCTYPES, inList);
646 docList = getFilteredCMIS(repoSession, ctx, handler, queryContext);
648 String query = NuxeoUtils.buildNXQLQuery(docTypes, queryContext);
649 if (logger.isDebugEnabled()) {
650 logger.debug("findDocs() NXQL: " + query);
652 docList = repoSession.query(query, null, filter.getPageSize(), filter.getOffset(), true);
654 wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
655 } catch (IllegalArgumentException iae) {
657 } catch (Exception e) {
658 if (logger.isDebugEnabled()) {
659 logger.debug("Caught exception ", e);
661 throw new NuxeoDocumentException(e);
668 * Find a list of documentModels from the Nuxeo repository
670 * @param docTypes a list of DocType names to match
671 * @param whereClause where the clause to qualify on
672 * @throws DocumentNotFoundException
673 * @throws TransactionException
674 * @throws DocumentException
675 * @return a list of documentModels
678 public DocumentWrapper<DocumentModelList> findDocs(
679 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
680 List<String> docTypes,
682 int pageSize, int pageNum, boolean computeTotal)
683 throws DocumentNotFoundException, TransactionException, DocumentException {
684 CoreSessionInterface repoSession = null;
685 DocumentWrapper<DocumentModelList> wrapDoc = null;
688 repoSession = getRepositorySession(ctx);
689 wrapDoc = findDocs(ctx, repoSession, docTypes, whereClause, null,
690 pageSize, pageNum, computeTotal);
691 } catch (IllegalArgumentException iae) {
693 } catch (Exception e) {
694 if (logger.isDebugEnabled()) {
695 logger.debug("Caught exception ", e);
697 throw new NuxeoDocumentException(e);
699 if (repoSession != null) {
700 releaseRepositorySession(ctx, repoSession);
704 if (logger.isWarnEnabled() == true) {
705 logger.warn("Returned DocumentModelList instance was created with a repository session that is now closed.");
712 * @see org.collectionspace.services.common.storage.StorageClient#get(org.collectionspace.services.common.context.ServiceContext, java.util.List, org.collectionspace.services.common.document.DocumentHandler)
715 public void get(ServiceContext ctx, List<String> csidList, DocumentHandler handler)
716 throws DocumentNotFoundException, TransactionException, DocumentException {
717 if (handler == null) {
718 throw new IllegalArgumentException(
719 "RepositoryJavaClient.getAll: handler is missing");
722 CoreSessionInterface repoSession = null;
724 handler.prepare(Action.GET_ALL);
725 repoSession = getRepositorySession(ctx);
726 DocumentModelList docModelList = new DocumentModelListImpl();
727 //FIXME: Should be using NuxeoUtils.createPathRef for security reasons
728 for (String csid : csidList) {
729 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
730 DocumentModel docModel = repoSession.getDocument(docRef);
731 docModelList.add(docModel);
734 //set reposession to handle the document
735 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
736 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docModelList);
737 handler.handle(Action.GET_ALL, wrapDoc);
738 handler.complete(Action.GET_ALL, wrapDoc);
739 } catch (DocumentException de) {
741 } catch (Exception e) {
742 if (logger.isDebugEnabled()) {
743 logger.debug("Caught exception ", e);
745 throw new NuxeoDocumentException(e);
747 if (repoSession != null) {
748 releaseRepositorySession(ctx, repoSession);
754 * getAll get all documents for an entity entity service from the Nuxeo
757 * @param ctx service context under which this method is invoked
758 * @param handler should be used by the caller to provide and transform the
760 * @throws DocumentNotFoundException
761 * @throws TransactionException
762 * @throws DocumentException
765 public void getAll(ServiceContext ctx, DocumentHandler handler)
766 throws DocumentNotFoundException, TransactionException, DocumentException {
767 if (handler == null) {
768 throw new IllegalArgumentException(
769 "RepositoryJavaClient.getAll: handler is missing");
771 String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
772 if (nuxeoWspaceId == null) {
773 throw new DocumentNotFoundException(
774 "Unable to find workspace for service "
775 + ctx.getServiceName()
776 + " check if the workspace exists in the Nuxeo repository.");
779 CoreSessionInterface repoSession = null;
781 handler.prepare(Action.GET_ALL);
782 repoSession = getRepositorySession(ctx);
783 DocumentRef wsDocRef = new IdRef(nuxeoWspaceId);
784 DocumentModelList docList = repoSession.getChildren(wsDocRef);
785 //set reposession to handle the document
786 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
787 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
788 handler.handle(Action.GET_ALL, wrapDoc);
789 handler.complete(Action.GET_ALL, wrapDoc);
790 } catch (DocumentException de) {
792 } catch (Exception e) {
793 if (logger.isDebugEnabled()) {
794 logger.debug("Caught exception ", e);
796 throw new NuxeoDocumentException(e);
798 if (repoSession != null) {
799 releaseRepositorySession(ctx, repoSession);
804 private boolean isClauseEmpty(String theString) {
805 boolean result = true;
806 if (theString != null && !theString.isEmpty()) {
812 public DocumentWrapper<DocumentModel> getDocFromCsid(
813 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
814 CoreSessionInterface repoSession,
817 DocumentWrapper<DocumentModel> result = null;
819 result = new DocumentWrapperImpl<DocumentModel>(NuxeoUtils.getDocFromCsid(ctx, repoSession, csid));
825 * A method to find a CollectionSpace document (of any type) given just a service context and
826 * its CSID. A search across *all* service workspaces (within a given tenant context) is performed to find
829 * This query searches Nuxeo's Hierarchy table where our CSIDs are stored in the "name" column.
832 public DocumentWrapper<DocumentModel> getDocFromCsid(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
835 DocumentWrapper<DocumentModel> result = null;
836 CoreSessionInterface repoSession = null;
838 repoSession = getRepositorySession(ctx);
839 result = getDocFromCsid(ctx, repoSession, csid);
841 if (repoSession != null) {
842 releaseRepositorySession(ctx, repoSession);
846 if (logger.isWarnEnabled() == true) {
847 logger.warn("Returned DocumentModel instance was created with a repository session that is now closed.");
854 * Returns a URI value for a document in the Nuxeo repository
856 * @param wrappedDoc a wrapped documentModel
857 * @throws ClientException
858 * @return a document URI
861 public String getDocURI(DocumentWrapper<DocumentModel> wrappedDoc) throws ClientException {
862 DocumentModel docModel = wrappedDoc.getWrappedObject();
863 String uri = (String) docModel.getProperty(CollectionSpaceClient.COLLECTIONSPACE_CORE_SCHEMA,
864 CollectionSpaceClient.COLLECTIONSPACE_CORE_URI);
869 * See CSPACE-5036 - How to make CMISQL queries from Nuxeo
871 private IterableQueryResult makeCMISQLQuery(CoreSessionInterface repoSession, String query, QueryContext queryContext) throws DocumentException {
872 IterableQueryResult result = null;
873 /** Threshold over which temporary files are not kept in memory. */
874 final int THRESHOLD = 1024 * 1024;
877 logger.debug(String.format("Performing a CMIS query on Nuxeo repository named %s",
878 repoSession.getRepositoryName()));
880 ThresholdOutputStreamFactory streamFactory = ThresholdOutputStreamFactory.newInstance(
881 null, THRESHOLD, -1, false);
882 CallContextImpl callContext = new CallContextImpl(
883 CallContext.BINDING_LOCAL,
884 CmisVersion.CMIS_1_1,
885 repoSession.getRepositoryName(),
886 null, // ServletContext
887 null, // HttpServletRequest
888 null, // HttpServletResponse
889 new NuxeoCmisServiceFactory(),
891 callContext.put(CallContext.USERNAME, repoSession.getPrincipal().getName());
893 NuxeoCmisService cmisService = new NuxeoCmisService(repoSession.getCoreSession());
894 result = repoSession.queryAndFetch(query, "CMISQL", cmisService);
895 } catch (ClientException e) {
896 // TODO Auto-generated catch block
897 logger.error("Encounter trouble making the following CMIS query: " + query, e);
898 throw new NuxeoDocumentException(e);
905 * getFiltered get all documents for an entity service from the Document
906 * repository, given filter parameters specified by the handler.
908 * @param ctx service context under which this method is invoked
909 * @param handler should be used by the caller to provide and transform the
911 * @throws DocumentNotFoundException if workspace not found
912 * @throws TransactionException
913 * @throws DocumentException
916 public void getFiltered(ServiceContext ctx, DocumentHandler handler)
917 throws DocumentNotFoundException, TransactionException, DocumentException {
919 DocumentFilter filter = handler.getDocumentFilter();
920 String oldOrderBy = filter.getOrderByClause();
921 if (isClauseEmpty(oldOrderBy) == true) {
922 filter.setOrderByClause(DocumentFilter.ORDER_BY_LAST_UPDATED);
924 QueryContext queryContext = new QueryContext(ctx, handler);
926 CoreSessionInterface repoSession = null;
928 handler.prepare(Action.GET_ALL);
929 repoSession = getRepositorySession(ctx); //Keeps a refcount here for the repository session so you need to release this when finished
931 DocumentModelList docList = null;
933 if (handler.isJDBCQuery() == true) {
934 docList = getFilteredJDBC(repoSession, ctx, handler);
936 } else if (handler.isCMISQuery() == true) {
937 docList = getFilteredCMIS(repoSession, ctx, handler, queryContext); //FIXME: REM - Need to deal with paging info in CMIS query
940 String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
941 if (logger.isDebugEnabled()) {
942 logger.debug("Executing NXQL query: " + query.toString());
944 Profiler profiler = new Profiler(this, 2);
945 profiler.log("Executing NXQL query: " + query.toString());
947 // If we have a page size and/or offset, then reflect those values
948 // when constructing the query, and also pass 'true' to get totalSize
949 // in the returned DocumentModelList.
950 if ((queryContext.getDocFilter().getOffset() > 0) || (queryContext.getDocFilter().getPageSize() > 0)) {
951 docList = repoSession.query(query, null,
952 queryContext.getDocFilter().getPageSize(), queryContext.getDocFilter().getOffset(), true);
954 docList = repoSession.query(query);
959 //set repoSession to handle the document
960 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
961 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
962 handler.handle(Action.GET_ALL, wrapDoc);
963 handler.complete(Action.GET_ALL, wrapDoc);
964 } catch (DocumentException de) {
966 } catch (Exception e) {
967 if (logger.isDebugEnabled()) {
968 logger.debug("Caught exception ", e); // REM - 1/17/2014: Check for org.nuxeo.ecm.core.api.ClientException and re-attempt
970 throw new NuxeoDocumentException(e);
972 if (repoSession != null) {
973 releaseRepositorySession(ctx, repoSession);
979 * Perform a database query, via JDBC and SQL, to retrieve matching records
980 * based on filter criteria.
982 * Although this method currently has a general-purpose name, it is
983 * currently dedicated to a specific task: that of improving performance
984 * for partial term matching queries on authority items / terms, via
985 * the use of a hand-tuned SQL query, rather than via the generated SQL
986 * produced by Nuxeo from an NXQL query. (See CSPACE-6361 for a task
987 * to generalize this method.)
989 * @param repoSession a repository session.
990 * @param ctx the service context.
991 * @param handler a relevant document handler.
992 * @return a list of document models matching the search criteria.
995 private DocumentModelList getFilteredJDBC(CoreSessionInterface repoSession, ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
996 DocumentHandler handler) throws Exception {
997 DocumentModelList result = new DocumentModelListImpl();
999 // FIXME: Get all of the following values from appropriate external constants.
1001 // At present, the two constants below are duplicated in both RepositoryClientImpl
1002 // and in AuthorityItemDocumentModelHandler.
1003 final String TERM_GROUP_LIST_NAME = "TERM_GROUP_LIST_NAME";
1004 final String TERM_GROUP_TABLE_NAME_PARAM = "TERM_GROUP_TABLE_NAME";
1005 final String IN_AUTHORITY_PARAM = "IN_AUTHORITY";
1006 // Get this from a constant in AuthorityResource or equivalent
1007 final String PARENT_WILDCARD = "_ALL_";
1009 // Build two SQL statements, to be executed within a single transaction:
1010 // the first statement to control join order, and the second statement
1011 // representing the actual 'get filtered' query
1013 // Build the join control statement
1015 // Per http://www.postgresql.org/docs/9.2/static/runtime-config-query.html#GUC-JOIN-COLLAPSE-LIMIT
1016 // "Setting [this value] to 1 prevents any reordering of explicit JOINs.
1017 // Thus, the explicit join order specified in the query will be the
1018 // actual order in which the relations are joined."
1019 // See CSPACE-5945 for further discussion of why this setting is needed.
1021 // Adding this statement is commented out here for now. It significantly
1022 // improved query performance for authority item / term queries where
1023 // large numbers of rows were retrieved, but appears to have resulted
1024 // in consistently slower-than-desired query performance where zero or
1025 // very few records were retrieved. See notes on CSPACE-5945. - ADR 2013-04-09
1026 // String joinControlSql = "SET LOCAL join_collapse_limit TO 1;";
1028 // Build the query statement
1030 // Start with the default query
1031 String selectStatement =
1032 "SELECT DISTINCT commonschema.id"
1033 + " FROM " + handler.getServiceContext().getCommonPartLabel() + " commonschema";
1035 String joinClauses =
1037 + " ON misc.id = commonschema.id"
1038 + " INNER JOIN hierarchy hierarchy_termgroup"
1039 + " ON hierarchy_termgroup.parentid = misc.id"
1040 + " INNER JOIN " + handler.getJDBCQueryParams().get(TERM_GROUP_TABLE_NAME_PARAM) + " termgroup"
1041 + " ON termgroup.id = hierarchy_termgroup.id ";
1044 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
1045 // Value for replaceable parameter 1 in the query
1046 String partialTerm = queryParams.getFirst(IQueryManager.SEARCH_TYPE_PARTIALTERM);
1047 // If the value of the partial term query parameter is blank ('pt='),
1048 // return all records, subject to restriction by any limit clause
1049 if (Tools.isBlank(partialTerm)) {
1052 // Otherwise, return records that match the supplied partial term
1054 " WHERE (termgroup.termdisplayname ILIKE ?)";
1057 // At present, results are ordered in code, below, rather than in SQL,
1058 // and the orderByClause below is thus intentionally blank.
1060 // To implement the orderByClause below in SQL; e.g. via
1061 // 'ORDER BY termgroup.termdisplayname', the relevant column
1062 // must be returned by the SELECT statement.
1063 String orderByClause = "";
1066 TenantBindingConfigReaderImpl tReader =
1067 ServiceMain.getInstance().getTenantBindingConfigReader();
1068 TenantBindingType tenantBinding = tReader.getTenantBinding(ctx.getTenantId());
1069 String maxListItemsLimit = TenantBindingUtils.getPropertyValue(tenantBinding,
1070 IQueryManager.MAX_LIST_ITEMS_RETURNED_LIMIT_ON_JDBC_QUERIES);
1072 " LIMIT " + getMaxItemsLimitOnJdbcQueries(maxListItemsLimit); // implicit int-to-String conversion
1074 // After building the individual parts of the query, set the values
1075 // of replaceable parameters that will be inserted into that query
1076 // and optionally add restrictions
1078 List<String> params = new ArrayList<>();
1080 if (Tools.notBlank(whereClause)) {
1082 // Read tenant bindings configuration to determine whether
1083 // to automatically insert leading, as well as trailing, wildcards
1084 // into the term matching string.
1085 String usesStartingWildcard = TenantBindingUtils.getPropertyValue(tenantBinding,
1086 IQueryManager.TENANT_USES_STARTING_WILDCARD_FOR_PARTIAL_TERM);
1087 // Handle user-provided leading wildcard characters, in the
1088 // configuration where a leading wildcard is not automatically inserted.
1089 // (The user-provided wildcard must be in the first, or "starting"
1090 // character position in the partial term value.)
1091 if (Tools.notBlank(usesStartingWildcard)) {
1092 if (usesStartingWildcard.equalsIgnoreCase(Boolean.FALSE.toString())) {
1093 partialTerm = handleProvidedStartingWildcard(partialTerm);
1094 // Otherwise, in the configuration where a leading wildcard
1095 // is usually automatically inserted, handle the cases where
1096 // a user has entered an anchor character in the first position
1097 // in the starting term value. In those cases, strip that
1098 // anchor character and don't add a leading wildcard
1100 if (partialTerm.startsWith(USER_SUPPLIED_ANCHOR_CHAR)) {
1101 partialTerm = partialTerm.substring(1, partialTerm.length());
1102 // Otherwise, automatically add a leading wildcard
1104 partialTerm = JDBCTools.SQL_WILDCARD + partialTerm;
1108 // Add SQL wildcards in the midst of the partial term match search
1109 // expression, whever user-supplied wildcards appear, except in the
1110 // first or last character positions of the search expression.
1111 partialTerm = subtituteWildcardsInPartialTerm(partialTerm);
1113 // If a designated 'anchor character' is present as the last character
1114 // in the search expression, strip that character and don't add
1115 // a trailing wildcard
1116 int lastCharPos = partialTerm.length() - 1;
1117 if (partialTerm.endsWith(USER_SUPPLIED_ANCHOR_CHAR) && lastCharPos > 0) {
1118 partialTerm = partialTerm.substring(0, lastCharPos);
1120 // Otherwise, automatically add a trailing wildcard
1121 partialTerm = partialTerm + JDBCTools.SQL_WILDCARD;
1123 params.add(partialTerm);
1126 // Optionally add restrictions to the default query, based on variables
1127 // in the current request
1129 // Restrict the query to filter out deleted records, if requested
1130 String includeDeleted = queryParams.getFirst(WorkflowClient.WORKFLOW_QUERY_NONDELETED);
1131 if (includeDeleted != null && includeDeleted.equalsIgnoreCase(Boolean.FALSE.toString())) {
1132 whereClause = whereClause
1133 + " AND (misc.lifecyclestate <> '" + WorkflowClient.WORKFLOWSTATE_DELETED + "')";
1136 // If a particular authority is specified, restrict the query further
1137 // to return only records within that authority
1138 String inAuthorityValue = (String) handler.getJDBCQueryParams().get(IN_AUTHORITY_PARAM);
1139 if (Tools.notBlank(inAuthorityValue)) {
1140 // Handle the '_ALL_' case for inAuthority
1141 if (inAuthorityValue.equals(PARENT_WILDCARD)) {
1142 // Add nothing to the query here if it should match within all authorities
1144 whereClause = whereClause
1145 + " AND (commonschema.inauthority = ?)";
1146 params.add(inAuthorityValue); // Value for replaceable parameter 2 in the query
1150 // Restrict the query further to return only records pertaining to
1151 // the current tenant, unless:
1152 // * Data for this service, in this tenant, is stored in its own,
1153 // separate repository, rather than being intermingled with other
1154 // tenants' data in the default repository; or
1155 // * Restriction by tenant ID in JDBC queries has been disabled,
1156 // via configuration for this tenant,
1157 if (restrictJDBCQueryByTenantID(tenantBinding, ctx)) {
1158 joinClauses = joinClauses
1159 + " INNER JOIN collectionspace_core core"
1160 + " ON core.id = hierarchy_termgroup.parentid";
1161 whereClause = whereClause
1162 + " AND (core.tenantid = ?)";
1163 params.add(ctx.getTenantId()); // Value for replaceable parameter 3 in the query
1166 // Piece together the SQL query from its parts
1167 String querySql = selectStatement + joinClauses + whereClause + orderByClause + limitClause;
1169 // Note: PostgreSQL 9.2 introduced a change that may improve performance
1170 // of certain queries using JDBC PreparedStatements. See comments on
1171 // CSPACE-5943 for details.
1173 // See a comment above for the reason that the joinControl SQL statement,
1174 // along with its corresponding prepared statement builder, is commented out for now.
1175 // PreparedStatementBuilder joinControlBuilder = new PreparedStatementBuilder(joinControlSql);
1176 PreparedStatementSimpleBuilder queryBuilder = new PreparedStatementSimpleBuilder(querySql, params);
1177 List<PreparedStatementBuilder> builders = new ArrayList<>();
1178 // builders.add(joinControlBuilder);
1179 builders.add(queryBuilder);
1180 String dataSourceName = JDBCTools.NUXEO_DATASOURCE_NAME;
1181 String repositoryName = ctx.getRepositoryName();
1182 final Boolean EXECUTE_WITHIN_TRANSACTION = true;
1183 Set<String> docIds = new HashSet<>();
1185 String cspaceInstanceId = ServiceMain.getInstance().getCspaceInstanceId();
1186 List<CachedRowSet> resultsList = JDBCTools.executePreparedQueries(builders,
1187 dataSourceName, repositoryName, cspaceInstanceId, EXECUTE_WITHIN_TRANSACTION);
1189 // At least one set of results is expected, from the second prepared
1190 // statement to be executed.
1191 // If fewer results are returned, return an empty list of document models
1192 if (resultsList == null || resultsList.size() < 1) {
1193 return result; // return an empty list of document models
1195 // The join control query (if enabled - it is currently commented
1196 // out as per comments above) will not return results, so query results
1197 // will be the first set of results (rowSet) returned in the list
1198 CachedRowSet queryResults = resultsList.get(0);
1200 // If the result from executing the query is null or contains zero rows,
1201 // return an empty list of document models
1202 if (queryResults == null) {
1203 return result; // return an empty list of document models
1205 queryResults.last();
1206 if (queryResults.getRow() == 0) {
1207 return result; // return an empty list of document models
1210 // Otherwise, get the document IDs from the results of the query
1212 queryResults.beforeFirst();
1213 while (queryResults.next()) {
1214 id = queryResults.getString(1);
1215 if (Tools.notBlank(id)) {
1219 } catch (SQLException sqle) {
1220 logger.warn("Could not obtain document IDs via SQL query '" + querySql + "': " + sqle.getMessage());
1221 return result; // return an empty list of document models
1224 // Get a list of document models, using the list of IDs obtained from the query
1226 // FIXME: Check whether we have a 'get document models from list of CSIDs'
1227 // utility method like this, and if not, add this to the appropriate
1229 DocumentModel docModel;
1230 for (String docId : docIds) {
1231 docModel = NuxeoUtils.getDocumentModel(repoSession, docId);
1232 if (docModel == null) {
1233 logger.warn("Could not obtain document model for document with ID " + docId);
1235 result.add(docModel);
1239 // Order the results
1240 final String COMMON_PART_SCHEMA = handler.getServiceContext().getCommonPartLabel();
1241 final String DISPLAY_NAME_XPATH =
1242 "//" + handler.getJDBCQueryParams().get(TERM_GROUP_LIST_NAME) + "/[0]/termDisplayName";
1243 Collections.sort(result, new Comparator<DocumentModel>() {
1245 public int compare(DocumentModel doc1, DocumentModel doc2) {
1246 String termDisplayName1 = null;
1247 String termDisplayName2 = null;
1249 termDisplayName1 = (String) NuxeoUtils.getXPathValue(doc1, COMMON_PART_SCHEMA, DISPLAY_NAME_XPATH);
1250 termDisplayName2 = (String) NuxeoUtils.getXPathValue(doc2, COMMON_PART_SCHEMA, DISPLAY_NAME_XPATH);
1251 } catch (NuxeoDocumentException e) {
1252 throw new RuntimeException(e); // We need to throw a RuntimeException because the compare() method of the Comparator interface does not support throwing an Exception
1254 return termDisplayName1.compareToIgnoreCase(termDisplayName2);
1262 private DocumentModelList getFilteredCMIS(CoreSessionInterface repoSession,
1263 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, DocumentHandler handler, QueryContext queryContext)
1264 throws DocumentNotFoundException, DocumentException {
1266 DocumentModelList result = new DocumentModelListImpl();
1268 String query = handler.getCMISQuery(queryContext);
1270 DocumentFilter docFilter = handler.getDocumentFilter();
1271 int pageSize = docFilter.getPageSize();
1272 int offset = docFilter.getOffset();
1273 if (logger.isDebugEnabled()) {
1274 logger.debug("Executing CMIS query: " + query.toString()
1275 + "with pageSize: " + pageSize + " at offset: " + offset);
1278 // If we have limit and/or offset, then pass true to get totalSize
1279 // in returned DocumentModelList.
1280 Profiler profiler = new Profiler(this, 2);
1281 profiler.log("Executing CMIS query: " + query.toString());
1284 IterableQueryResult queryResult = makeCMISQLQuery(repoSession, query, queryContext);
1286 int totalSize = (int) queryResult.size();
1287 ((DocumentModelListImpl) result).setTotalSize(totalSize);
1288 // Skip the rows before our offset
1290 queryResult.skipTo(offset);
1293 for (Map<String, Serializable> row : queryResult) {
1294 if (logger.isTraceEnabled()) {
1295 logger.trace(" Hierarchy Table ID is:" + row.get(IQueryManager.CMIS_TARGET_NUXEO_ID)
1296 + " nuxeo:pathSegment is: " + row.get(IQueryManager.CMIS_TARGET_NAME));
1298 String nuxeoId = (String) row.get(IQueryManager.CMIS_TARGET_NUXEO_ID);
1299 DocumentModel docModel = NuxeoUtils.getDocumentModel(repoSession, nuxeoId);
1300 result.add(docModel);
1302 if (nRows >= pageSize && pageSize != 0) { // A page size of zero means that they want all of them
1303 logger.debug("Got page full of items - quitting");
1308 queryResult.close();
1313 } catch (Exception e) {
1314 if (logger.isDebugEnabled()) {
1315 logger.debug("Caught exception ", e);
1317 throw new NuxeoDocumentException(e);
1321 // Since we're not supporting paging yet for CMIS queries, we need to perform
1322 // a workaround for the paging information we return in our list of results
1325 if (result != null) {
1326 docFilter.setStartPage(0);
1327 if (totalSize > docFilter.getPageSize()) {
1328 docFilter.setPageSize(totalSize);
1329 ((DocumentModelListImpl)result).setTotalSize(totalSize);
1337 private String logException(Exception e, String msg) {
1338 String result = null;
1340 String exceptionMessage = e.getMessage();
1341 exceptionMessage = exceptionMessage != null ? exceptionMessage : "<No details provided>";
1342 result = msg = msg + ". Caught exception:" + exceptionMessage;
1344 if (logger.isTraceEnabled() == true) {
1345 logger.error(msg, e);
1354 * update given document in the Nuxeo repository
1356 * @param ctx service context under which this method is invoked
1357 * @param csid of the document
1358 * @param handler should be used by the caller to provide and transform the
1360 * @throws BadRequestException
1361 * @throws DocumentNotFoundException
1362 * @throws TransactionException if the transaction times out or otherwise
1363 * cannot be successfully completed
1364 * @throws DocumentException
1367 public void update(ServiceContext ctx, String csid, DocumentHandler handler)
1368 throws BadRequestException, DocumentNotFoundException, TransactionException,
1370 if (handler == null) {
1371 throw new IllegalArgumentException(
1372 "RepositoryJavaClient.update: document handler is missing.");
1375 CoreSessionInterface repoSession = null;
1377 handler.prepare(Action.UPDATE);
1378 repoSession = getRepositorySession(ctx);
1379 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
1380 DocumentModel doc = null;
1382 doc = repoSession.getDocument(docRef);
1383 } catch (ClientException ce) {
1384 String msg = logException(ce, "Could not find document to update with CSID=" + csid);
1385 throw new DocumentNotFoundException(msg, ce);
1387 // Check for a versioned document, and check In and Out before we proceed.
1388 if (((DocumentModelHandler) handler).supportsVersioning()) {
1389 /* Once we advance to 5.5 or later, we can add this.
1390 * See also https://jira.nuxeo.com/browse/NXP-8506
1391 if(!doc.isVersionable()) {
1392 throw new NuxeoDocumentException("Configuration for: "
1393 +handler.getServiceContextPath()+" supports versioning, but Nuxeo config does not!");
1396 /* Force a version number - Not working. Apparently we need to configure the uid schema??
1397 if(doc.getProperty("uid","major_version") == null) {
1398 doc.setProperty("uid","major_version",1);
1400 if(doc.getProperty("uid","minor_version") == null) {
1401 doc.setProperty("uid","minor_version",0);
1404 doc.checkIn(VersioningOption.MINOR, null);
1409 // Set reposession to handle the document
1411 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
1412 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
1413 handler.handle(Action.UPDATE, wrapDoc);
1414 repoSession.saveDocument(doc);
1416 handler.complete(Action.UPDATE, wrapDoc);
1417 } catch (BadRequestException bre) {
1419 } catch (DocumentException de) {
1421 } catch (CSWebApplicationException wae) {
1423 } catch (Exception e) {
1424 throw new NuxeoDocumentException(e);
1426 if (repoSession != null) {
1427 releaseRepositorySession(ctx, repoSession);
1433 * Save a documentModel to the Nuxeo repository.
1435 * @param ctx service context under which this method is invoked
1436 * @param repoSession
1437 * @param docModel the document to save
1438 * @param fSaveSession if TRUE, will call CoreSessionInterface.save() to save
1439 * accumulated changes.
1440 * @throws ClientException
1441 * @throws DocumentException
1443 public void saveDocWithoutHandlerProcessing(
1444 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
1445 CoreSessionInterface repoSession,
1446 DocumentModel docModel,
1447 boolean fSaveSession)
1448 throws ClientException, DocumentException {
1451 repoSession.saveDocument(docModel);
1455 } catch (ClientException ce) {
1457 } catch (Exception e) {
1458 if (logger.isDebugEnabled()) {
1459 logger.debug("Caught exception ", e);
1461 throw new NuxeoDocumentException(e);
1466 * Save a list of documentModels to the Nuxeo repository.
1468 * @param ctx service context under which this method is invoked
1469 * @param repoSession a repository session
1470 * @param docModelList a list of document models
1471 * @param fSaveSession if TRUE, will call CoreSessionInterface.save() to save
1472 * accumulated changes.
1473 * @throws ClientException
1474 * @throws DocumentException
1476 public void saveDocListWithoutHandlerProcessing(
1477 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
1478 CoreSessionInterface repoSession,
1479 DocumentModelList docList,
1480 boolean fSaveSession)
1481 throws ClientException, DocumentException {
1483 DocumentModel[] docModelArray = new DocumentModel[docList.size()];
1484 repoSession.saveDocuments(docList.toArray(docModelArray));
1488 } catch (ClientException ce) {
1490 } catch (Exception e) {
1491 logger.error("Caught exception ", e);
1492 throw new NuxeoDocumentException(e);
1497 public void deleteWithWhereClause(@SuppressWarnings("rawtypes") ServiceContext ctx, String whereClause,
1498 @SuppressWarnings("rawtypes") DocumentHandler handler) throws
1499 DocumentNotFoundException, DocumentException {
1501 throw new IllegalArgumentException(
1502 "delete(ctx, specifier): ctx is missing");
1504 if (logger.isDebugEnabled()) {
1505 logger.debug("Deleting document with whereClause=" + whereClause);
1508 DocumentWrapper<DocumentModel> foundDocWrapper = this.findDoc(ctx, whereClause);
1509 if (foundDocWrapper != null) {
1510 DocumentModel docModel = foundDocWrapper.getWrappedObject();
1511 String csid = docModel.getName();
1512 this.delete(ctx, csid, handler);
1517 * delete a document from the Nuxeo repository
1519 * @param ctx service context under which this method is invoked
1520 * @param id of the document
1521 * @throws DocumentException
1524 public void delete(ServiceContext ctx, String id, DocumentHandler handler) throws DocumentNotFoundException,
1525 DocumentException, TransactionException {
1527 throw new IllegalArgumentException(
1528 "delete(ctx, ix, handler): ctx is missing");
1530 if (handler == null) {
1531 throw new IllegalArgumentException(
1532 "delete(ctx, ix, handler): handler is missing");
1534 if (logger.isDebugEnabled()) {
1535 logger.debug("Deleting document with CSID=" + id);
1537 CoreSessionInterface repoSession = null;
1539 handler.prepare(Action.DELETE);
1540 repoSession = getRepositorySession(ctx);
1541 DocumentWrapper<DocumentModel> wrapDoc = null;
1543 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
1544 wrapDoc = new DocumentWrapperImpl<DocumentModel>(repoSession.getDocument(docRef));
1545 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
1546 handler.handle(Action.DELETE, wrapDoc);
1547 repoSession.removeDocument(docRef);
1548 } catch (ClientException ce) {
1549 String msg = logException(ce, "Could not find document to delete with CSID=" + id);
1550 throw new DocumentNotFoundException(msg, ce);
1553 handler.complete(Action.DELETE, wrapDoc);
1554 } catch (DocumentException de) {
1556 } catch (Exception e) {
1557 if (logger.isDebugEnabled()) {
1558 logger.debug("Caught exception ", e);
1560 throw new NuxeoDocumentException(e);
1562 if (repoSession != null) {
1563 releaseRepositorySession(ctx, repoSession);
1569 * @see org.collectionspace.services.common.storage.StorageClient#delete(org.collectionspace.services.common.context.ServiceContext, java.lang.String, org.collectionspace.services.common.document.DocumentHandler)
1573 public void delete(@SuppressWarnings("rawtypes") ServiceContext ctx, String id)
1574 throws DocumentNotFoundException, DocumentException {
1575 throw new UnsupportedOperationException();
1576 // Use the other delete instead
1580 public Hashtable<String, String> retrieveWorkspaceIds(RepositoryDomainType repoDomain) throws Exception {
1581 return NuxeoConnectorEmbedded.getInstance().retrieveWorkspaceIds(repoDomain);
1585 public String createDomain(RepositoryDomainType repositoryDomain) throws Exception {
1586 CoreSessionInterface repoSession = null;
1587 String domainId = null;
1590 // Open a connection to the domain's repo/db
1592 String repoName = repositoryDomain.getRepositoryName();
1593 repoSession = getRepositorySession(repoName); // domainName=storageName=repoName=databaseName
1595 // First create the top-level domain directory
1597 String domainName = repositoryDomain.getStorageName();
1598 DocumentRef parentDocRef = new PathRef("/");
1599 DocumentModel parentDoc = repoSession.getDocument(parentDocRef);
1600 DocumentModel domainDoc = repoSession.createDocumentModel(parentDoc.getPathAsString(),
1601 domainName, NUXEO_CORE_TYPE_DOMAIN);
1602 domainDoc.setPropertyValue("dc:title", domainName);
1603 domainDoc.setPropertyValue("dc:description", "A CollectionSpace domain "
1605 domainDoc = repoSession.createDocument(domainDoc);
1606 domainId = domainDoc.getId();
1609 // Next, create a "Workspaces" root directory to contain the workspace folders for the individual service documents
1611 DocumentModel workspacesRoot = repoSession.createDocumentModel(domainDoc.getPathAsString(),
1612 NuxeoUtils.Workspaces, NUXEO_CORE_TYPE_WORKSPACEROOT);
1613 workspacesRoot.setPropertyValue("dc:title", NuxeoUtils.Workspaces);
1614 workspacesRoot.setPropertyValue("dc:description", "A CollectionSpace workspaces directory for "
1615 + domainDoc.getPathAsString());
1616 workspacesRoot = repoSession.createDocument(workspacesRoot);
1617 String workspacesRootId = workspacesRoot.getId();
1620 if (logger.isDebugEnabled()) {
1621 logger.debug("Created tenant domain name=" + domainName
1622 + " id=" + domainId + " "
1623 + NuxeoUtils.Workspaces + " id=" + workspacesRootId);
1624 logger.debug("Path to Domain: " + domainDoc.getPathAsString());
1625 logger.debug("Path to Workspaces root: " + workspacesRoot.getPathAsString());
1627 } catch (Exception e) {
1628 if (logger.isDebugEnabled()) {
1629 logger.debug("Could not create tenant domain name=" + repositoryDomain.getStorageName() + " caught exception ", e);
1633 if (repoSession != null) {
1634 releaseRepositorySession(null, repoSession);
1642 public String getDomainId(RepositoryDomainType repositoryDomain) throws Exception {
1643 String domainId = null;
1644 CoreSessionInterface repoSession = null;
1646 String repoName = repositoryDomain.getRepositoryName();
1647 String domainStorageName = repositoryDomain.getStorageName();
1648 if (domainStorageName != null && !domainStorageName.isEmpty()) {
1650 repoSession = getRepositorySession(repoName);
1651 DocumentRef docRef = new PathRef("/" + domainStorageName);
1652 DocumentModel domain = repoSession.getDocument(docRef);
1653 domainId = domain.getId();
1654 } catch (Exception e) {
1655 if (logger.isTraceEnabled()) {
1656 logger.trace("Caught exception ", e); // The document doesn't exist, this let's us know we need to create it
1658 //there is no way to identify if document does not exist due to
1659 //lack of typed exception for getDocument method
1662 if (repoSession != null) {
1663 releaseRepositorySession(null, repoSession);
1672 * Returns the workspaces root directory for a given domain.
1674 private DocumentModel getWorkspacesRoot(CoreSessionInterface repoSession,
1675 String domainName) throws Exception {
1676 DocumentModel result = null;
1678 String domainPath = "/" + domainName;
1679 DocumentRef parentDocRef = new PathRef(domainPath);
1680 DocumentModelList domainChildrenList = repoSession.getChildren(
1682 Iterator<DocumentModel> witer = domainChildrenList.iterator();
1683 while (witer.hasNext()) {
1684 DocumentModel childNode = witer.next();
1685 if (NuxeoUtils.Workspaces.equalsIgnoreCase(childNode.getName())) {
1687 logger.trace("Found workspaces directory at: " + result.getPathAsString());
1692 if (result == null) {
1693 throw new ClientException("Could not find workspace root directory in: "
1701 * @see org.collectionspace.services.common.repository.RepositoryClient#createWorkspace(java.lang.String, java.lang.String)
1704 public String createWorkspace(RepositoryDomainType repositoryDomain, String workspaceName) throws Exception {
1705 CoreSessionInterface repoSession = null;
1706 String workspaceId = null;
1708 String repoName = repositoryDomain.getRepositoryName();
1709 repoSession = getRepositorySession(repoName);
1711 String domainStorageName = repositoryDomain.getStorageName();
1712 DocumentModel parentDoc = getWorkspacesRoot(repoSession, domainStorageName);
1713 if (logger.isTraceEnabled()) {
1714 for (String facet : parentDoc.getFacets()) {
1715 logger.trace("Facet: " + facet);
1719 DocumentModel doc = repoSession.createDocumentModel(parentDoc.getPathAsString(),
1720 workspaceName, NuxeoUtils.WORKSPACE_DOCUMENT_TYPE);
1721 doc.setPropertyValue("dc:title", workspaceName);
1722 doc.setPropertyValue("dc:description", "A CollectionSpace workspace for "
1724 doc = repoSession.createDocument(doc);
1725 workspaceId = doc.getId();
1727 if (logger.isDebugEnabled()) {
1728 logger.debug("Created workspace name=" + workspaceName
1729 + " id=" + workspaceId);
1731 } catch (Exception e) {
1732 if (logger.isDebugEnabled()) {
1733 logger.debug("createWorkspace caught exception ", e);
1737 if (repoSession != null) {
1738 releaseRepositorySession(null, repoSession);
1745 * @see org.collectionspace.services.common.repository.RepositoryClient#getWorkspaceId(java.lang.String, java.lang.String)
1749 public String getWorkspaceId(String tenantDomain, String workspaceName) throws Exception {
1750 String workspaceId = null;
1752 CoreSessionInterface repoSession = null;
1754 repoSession = getRepositorySession((ServiceContext<PoxPayloadIn, PoxPayloadOut>) null);
1755 DocumentRef docRef = new PathRef(
1757 + "/" + NuxeoUtils.Workspaces
1758 + "/" + workspaceName);
1759 DocumentModel workspace = repoSession.getDocument(docRef);
1760 workspaceId = workspace.getId();
1761 } catch (DocumentException de) {
1763 } catch (Exception e) {
1764 if (logger.isDebugEnabled()) {
1765 logger.debug("Caught exception ", e);
1767 throw new NuxeoDocumentException(e);
1769 if (repoSession != null) {
1770 releaseRepositorySession(null, repoSession);
1777 public CoreSessionInterface getRepositorySession(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {
1778 return getRepositorySession(ctx, ctx.getRepositoryName(), ctx.getTimeoutSecs());
1781 public CoreSessionInterface getRepositorySession(String repoName) throws Exception {
1782 return getRepositorySession(null, repoName, ServiceContext.DEFAULT_TX_TIMEOUT);
1786 * Gets the repository session. - Package access only. If the 'ctx' param is
1787 * null then the repo name must be non-mull and vice-versa
1789 * @return the repository session
1790 * @throws Exception the exception
1792 public CoreSessionInterface getRepositorySession(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
1794 int timeoutSeconds) throws Exception {
1795 CoreSessionInterface repoSession = null;
1797 Profiler profiler = new Profiler("getRepositorySession():", 2);
1800 // To get a connection to the Nuxeo repo, we need either a valid ServiceContext instance or a repository name
1803 repoName = ctx.getRepositoryName(); // Notice we are overriding the passed in 'repoName' since we have a valid service context passed in to us
1804 repoSession = (CoreSessionInterface) ctx.getCurrentRepositorySession(); // Look to see if one exists in the context before creating one
1805 } else if (repoName == null || repoName.trim().isEmpty()) {
1806 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.");
1807 logger.error(errMsg);
1808 throw new Exception(errMsg);
1811 // 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
1812 // just the repo name
1814 if (repoSession == null) {
1815 NuxeoClientEmbedded client = NuxeoConnectorEmbedded.getInstance().getClient();
1816 repoSession = client.openRepository(repoName, timeoutSeconds);
1818 if (logger.isDebugEnabled() == true) {
1819 logger.warn("Reusing the current context's repository session.");
1824 if (logger.isTraceEnabled()) {
1825 logger.trace("Testing call to getRepository() repository root: " + repoSession.getRootDocument());
1827 } catch (Throwable e) {
1828 logger.trace("Test call to Nuxeo's getRepository() repository root failed", e);
1834 ctx.setCurrentRepositorySession(repoSession); // For reusing, save the repository session in the current service context
1841 * Release repository session. - Package access only.
1843 * @param repoSession the repo session
1845 public void releaseRepositorySession(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, CoreSessionInterface repoSession) throws TransactionException {
1847 NuxeoClientEmbedded client = NuxeoConnectorEmbedded.getInstance().getClient();
1850 ctx.clearCurrentRepositorySession(); //clear the current context of the now closed repo session
1851 if (ctx.getCurrentRepositorySession() == null) {
1852 client.releaseRepository(repoSession); //release the repo session if the service context's ref count is zeo.
1855 client.releaseRepository(repoSession); //repo session was acquired without a service context
1857 } catch (TransactionRuntimeException tre) {
1858 String causeMsg = null;
1859 Throwable cause = tre.getCause();
1860 if (cause != null) {
1861 causeMsg = cause.getMessage();
1864 TransactionException te; // a CollectionSpace specific tx exception
1865 if (causeMsg != null) {
1866 te = new TransactionException(causeMsg, tre);
1868 te = new TransactionException(tre);
1871 logger.error(te.getMessage(), tre); // Log the standard transaction exception message, plus an exception-specific stack trace
1873 } catch (Exception e) {
1874 logger.error("Could not close the repository session.", e);
1875 // no need to throw this service specific exception
1880 public void doWorkflowTransition(ServiceContext ctx, String id,
1881 DocumentHandler handler, TransitionDef transitionDef)
1882 throws BadRequestException, DocumentNotFoundException,
1884 // 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
1887 private String handleProvidedStartingWildcard(String partialTerm) {
1888 if (Tools.notBlank(partialTerm)) {
1889 if (partialTerm.substring(0, 1).equals(USER_SUPPLIED_WILDCARD)) {
1890 StringBuffer buffer = new StringBuffer(partialTerm);
1891 buffer.setCharAt(0, JDBCTools.SQL_WILDCARD.charAt(0));
1892 partialTerm = buffer.toString();
1899 * Replaces user-supplied wildcards with SQL wildcards, in a partial term
1900 * matching search expression.
1902 * The scope of this replacement excludes the beginning character
1903 * in that search expression, as that character is treated specially.
1905 * @param partialTerm
1906 * @return the partial term, with any user-supplied wildcards replaced
1909 private String subtituteWildcardsInPartialTerm(String partialTerm) {
1910 if (Tools.isBlank(partialTerm)) {
1913 if (! partialTerm.contains(USER_SUPPLIED_WILDCARD)) {
1916 int len = partialTerm.length();
1917 // Partial term search expressions of 2 or fewer characters
1918 // currently aren't amenable to the use of wildcards
1920 logger.warn("Partial term match search expression of just 1-2 characters in length contains a user-supplied wildcard: " + partialTerm);
1921 logger.warn("Will handle that character as a literal value, rather than as a wildcard ...");
1924 return partialTerm.substring(0, 1) // first char
1925 + partialTerm.substring(1, len).replaceAll(USER_SUPPLIED_WILDCARD_REGEX, JDBCTools.SQL_WILDCARD);
1929 private int getMaxItemsLimitOnJdbcQueries(String maxListItemsLimit) {
1930 final int DEFAULT_ITEMS_LIMIT = 40;
1931 if (maxListItemsLimit == null) {
1932 return DEFAULT_ITEMS_LIMIT;
1936 itemsLimit = Integer.parseInt(maxListItemsLimit);
1937 if (itemsLimit < 1) {
1938 logger.warn("Value of configuration setting "
1939 + IQueryManager.MAX_LIST_ITEMS_RETURNED_LIMIT_ON_JDBC_QUERIES
1940 + " must be a positive integer; invalid current value is " + maxListItemsLimit);
1941 logger.warn("Reverting to default value of " + DEFAULT_ITEMS_LIMIT);
1942 itemsLimit = DEFAULT_ITEMS_LIMIT;
1944 } catch (NumberFormatException nfe) {
1945 logger.warn("Value of configuration setting "
1946 + IQueryManager.MAX_LIST_ITEMS_RETURNED_LIMIT_ON_JDBC_QUERIES
1947 + " must be a positive integer; invalid current value is " + maxListItemsLimit);
1948 logger.warn("Reverting to default value of " + DEFAULT_ITEMS_LIMIT);
1949 itemsLimit = DEFAULT_ITEMS_LIMIT;
1955 * Identifies whether a restriction on tenant ID - to return only records
1956 * pertaining to the current tenant - is required in a JDBC query.
1958 * @param tenantBinding a tenant binding configuration.
1959 * @param ctx a service context.
1960 * @return true if a restriction on tenant ID is required in the query;
1961 * false if a restriction is not required.
1963 private boolean restrictJDBCQueryByTenantID(TenantBindingType tenantBinding, ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) {
1964 boolean restrict = true;
1965 // If data for the current service, in the current tenant, is isolated
1966 // within its own separate, per-tenant repository, as contrasted with
1967 // being intermingled with other tenants' data in the default repository,
1968 // no restriction on Tenant ID is required in the query.
1969 String repositoryDomainName = ConfigUtils.getRepositoryName(tenantBinding, ctx.getRepositoryDomainName());
1970 if (!(repositoryDomainName.equals(ConfigUtils.DEFAULT_NUXEO_REPOSITORY_NAME))) {
1973 // If a configuration setting for this tenant identifies that JDBC
1974 // queries should not be restricted by tenant ID (perhaps because
1975 // there is always expected to be only one tenant's data present in
1976 // the system), no restriction on Tenant ID is required in the query.
1977 String queriesRestrictedByTenantId = TenantBindingUtils.getPropertyValue(tenantBinding,
1978 IQueryManager.JDBC_QUERIES_ARE_TENANT_ID_RESTRICTED);
1979 if (Tools.notBlank(queriesRestrictedByTenantId) &&
1980 queriesRestrictedByTenantId.equalsIgnoreCase(Boolean.FALSE.toString())) {