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.NuxeoUtils;
37 import org.collectionspace.services.client.CollectionSpaceClient;
38 import org.collectionspace.services.client.IQueryManager;
39 import org.collectionspace.services.client.PoxPayloadIn;
40 import org.collectionspace.services.client.PoxPayloadOut;
41 import org.collectionspace.services.client.Profiler;
42 import org.collectionspace.services.client.workflow.WorkflowClient;
43 import org.collectionspace.services.common.context.ServiceContext;
44 import org.collectionspace.services.common.query.QueryContext;
45 import org.collectionspace.services.common.repository.RepositoryClient;
46 import org.collectionspace.services.common.storage.JDBCTools;
47 import org.collectionspace.services.common.storage.PreparedStatementSimpleBuilder;
48 import org.collectionspace.services.common.document.BadRequestException;
49 import org.collectionspace.services.common.document.DocumentException;
50 import org.collectionspace.services.common.document.DocumentFilter;
51 import org.collectionspace.services.common.document.DocumentHandler;
52 import org.collectionspace.services.common.document.DocumentNotFoundException;
53 import org.collectionspace.services.common.document.DocumentHandler.Action;
54 import org.collectionspace.services.common.document.DocumentWrapper;
55 import org.collectionspace.services.common.document.DocumentWrapperImpl;
56 import org.collectionspace.services.common.document.TransactionException;
57 import org.collectionspace.services.common.CSWebApplicationException;
58 import org.collectionspace.services.common.ServiceMain;
59 import org.collectionspace.services.common.api.Tools;
60 import org.collectionspace.services.common.config.ConfigUtils;
61 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
62 import org.collectionspace.services.common.config.TenantBindingUtils;
63 import org.collectionspace.services.common.storage.PreparedStatementBuilder;
64 import org.collectionspace.services.config.tenant.TenantBindingType;
65 import org.collectionspace.services.config.tenant.RepositoryDomainType;
68 // CSPACE-5036 - How to make CMISQL queries from Nuxeo
70 import org.apache.chemistry.opencmis.commons.enums.CmisVersion;
71 import org.apache.chemistry.opencmis.commons.server.CallContext;
72 import org.apache.chemistry.opencmis.server.impl.CallContextImpl;
73 import org.apache.chemistry.opencmis.server.shared.ThresholdOutputStreamFactory;
74 import org.nuxeo.common.utils.IdUtils;
75 import org.nuxeo.ecm.core.api.ClientException;
76 import org.nuxeo.ecm.core.api.DocumentModel;
77 import org.nuxeo.ecm.core.api.DocumentModelList;
78 import org.nuxeo.ecm.core.api.IterableQueryResult;
79 import org.nuxeo.ecm.core.api.VersioningOption;
80 import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
81 import org.nuxeo.ecm.core.api.DocumentRef;
82 import org.nuxeo.ecm.core.api.IdRef;
83 import org.nuxeo.ecm.core.api.PathRef;
84 import org.nuxeo.runtime.transaction.TransactionRuntimeException;
85 import org.nuxeo.ecm.core.opencmis.bindings.NuxeoCmisServiceFactory;
86 import org.nuxeo.ecm.core.opencmis.impl.server.NuxeoCmisService;
87 import org.slf4j.Logger;
88 import org.slf4j.LoggerFactory;
91 * RepositoryJavaClient is used to perform CRUD operations on documents in Nuxeo
92 * repository using Remote Java APIs. It uses
94 * @see DocumentHandler as IOHandler with the client.
96 * $LastChangedRevision: $ $LastChangedDate: $
98 public class RepositoryJavaClientImpl implements RepositoryClient<PoxPayloadIn, PoxPayloadOut> {
103 private final Logger logger = LoggerFactory.getLogger(RepositoryJavaClientImpl.class);
104 // private final Logger profilerLogger = LoggerFactory.getLogger("remperf");
105 // private String foo = Profiler.createLogger();
106 public static final String NUXEO_CORE_TYPE_DOMAIN = "Domain";
107 public static final String NUXEO_CORE_TYPE_WORKSPACEROOT = "WorkspaceRoot";
108 // FIXME: Get this value from an existing constant, if available
109 public static final String BACKSLASH = "\\";
110 public static final String USER_SUPPLIED_WILDCARD = "*";
111 public static final String USER_SUPPLIED_WILDCARD_REGEX = BACKSLASH + USER_SUPPLIED_WILDCARD;
112 public static final String USER_SUPPLIED_ANCHOR_CHAR = "^";
113 public static final String USER_SUPPLIED_ANCHOR_CHAR_REGEX = BACKSLASH + USER_SUPPLIED_ANCHOR_CHAR;
114 public static final String ENDING_ANCHOR_CHAR = "$";
115 public static final String ENDING_ANCHOR_CHAR_REGEX = BACKSLASH + ENDING_ANCHOR_CHAR;
119 * Instantiates a new repository java client impl.
121 public RepositoryJavaClientImpl() {
125 public void assertWorkflowState(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
126 DocumentModel docModel) throws DocumentNotFoundException, ClientException {
127 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
128 if (queryParams != null) {
130 // Look for the workflow "delete" query param and see if we need to assert that the
131 // docModel is in a non-deleted workflow state.
133 String currentState = docModel.getCurrentLifeCycleState();
134 String includeDeletedStr = queryParams.getFirst(WorkflowClient.WORKFLOW_QUERY_NONDELETED);
135 boolean includeDeleted = includeDeletedStr == null ? true : Boolean.parseBoolean(includeDeletedStr);
136 if (includeDeleted == false) {
138 // We don't wanted soft-deleted object, so throw an exception if this one is soft-deleted.
140 if (currentState.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
141 String msg = "The GET assertion that docModel not be in 'deleted' workflow state failed.";
143 throw new DocumentNotFoundException(msg);
150 * create document in the Nuxeo repository
152 * @param ctx service context under which this method is invoked
153 * @param handler should be used by the caller to provide and transform the
155 * @return id in repository of the newly created document
156 * @throws BadRequestException
157 * @throws TransactionException
158 * @throws DocumentException
161 public String create(ServiceContext ctx,
162 DocumentHandler handler) throws BadRequestException,
163 TransactionException, DocumentException {
165 String docType = NuxeoUtils.getTenantQualifiedDocType(ctx); //ctx.getDocumentType();
166 if (docType == null) {
167 throw new IllegalArgumentException(
168 "RepositoryJavaClient.create: docType is missing");
171 if (handler == null) {
172 throw new IllegalArgumentException(
173 "RepositoryJavaClient.create: handler is missing");
175 String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
176 if (nuxeoWspaceId == null) {
177 throw new DocumentNotFoundException(
178 "Unable to find workspace for service " + ctx.getServiceName()
179 + " check if the workspace exists in the Nuxeo repository");
182 CoreSessionInterface repoSession = null;
184 handler.prepare(Action.CREATE);
185 repoSession = getRepositorySession(ctx);
186 DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);
187 DocumentModel wspaceDoc = repoSession.getDocument(nuxeoWspace);
188 String wspacePath = wspaceDoc.getPathAsString();
189 //give our own ID so PathRef could be constructed later on
190 String id = IdUtils.generateId(UUID.randomUUID().toString());
191 // create document model
192 DocumentModel doc = repoSession.createDocumentModel(wspacePath, id, docType);
193 /* Check for a versioned document, and check In and Out before we proceed.
194 * This does not work as we do not have the uid schema on our docs.
195 if(((DocumentModelHandler) handler).supportsVersioning()) {
196 doc.setProperty("uid","major_version",1);
197 doc.setProperty("uid","minor_version",0);
200 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
201 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
202 handler.handle(Action.CREATE, wrapDoc);
203 // create document with documentmodel
204 doc = repoSession.createDocument(doc);
206 // TODO for sub-docs need to call into the handler to let it deal with subitems. Pass in the id,
207 // and assume the handler has the state it needs (doc fragments).
208 handler.complete(Action.CREATE, wrapDoc);
210 } catch (BadRequestException bre) {
212 } catch (Exception e) {
213 logger.error("Caught exception ", e);
214 throw new NuxeoDocumentException(e);
216 if (repoSession != null) {
217 releaseRepositorySession(ctx, repoSession);
224 * get document from the Nuxeo repository
226 * @param ctx service context under which this method is invoked
227 * @param id of the document to retrieve
228 * @param handler should be used by the caller to provide and transform the
230 * @throws DocumentNotFoundException if the document cannot be found in the
232 * @throws TransactionException
233 * @throws DocumentException
236 public void get(ServiceContext ctx, String id, DocumentHandler handler)
237 throws DocumentNotFoundException, TransactionException, DocumentException {
239 if (handler == null) {
240 throw new IllegalArgumentException(
241 "RepositoryJavaClient.get: handler is missing");
244 CoreSessionInterface repoSession = null;
246 handler.prepare(Action.GET);
247 repoSession = getRepositorySession(ctx);
248 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
249 DocumentModel docModel = null;
251 docModel = repoSession.getDocument(docRef);
252 assertWorkflowState(ctx, docModel);
253 } catch (ClientException ce) {
254 String msg = logException(ce, "Could not find document with CSID=" + id);
255 throw new DocumentNotFoundException(msg, ce);
258 // Set repository session to handle the document
260 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
261 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(docModel);
262 handler.handle(Action.GET, wrapDoc);
263 handler.complete(Action.GET, wrapDoc);
264 } catch (IllegalArgumentException iae) {
266 } catch (DocumentException de) {
268 } catch (Exception e) {
269 if (logger.isDebugEnabled()) {
270 logger.debug("Caught exception ", e);
272 throw new NuxeoDocumentException(e);
274 if (repoSession != null) {
275 releaseRepositorySession(ctx, repoSession);
281 * get a document from the Nuxeo repository, using the docFilter params.
283 * @param ctx service context under which this method is invoked
284 * @param handler should be used by the caller to provide and transform the
285 * document. Handler must have a docFilter set to return a single item.
286 * @throws DocumentNotFoundException if the document cannot be found in the
288 * @throws TransactionException
289 * @throws DocumentException
292 public void get(ServiceContext ctx, DocumentHandler handler)
293 throws DocumentNotFoundException, TransactionException, DocumentException {
294 QueryContext queryContext = new QueryContext(ctx, handler);
295 CoreSessionInterface repoSession = null;
298 handler.prepare(Action.GET);
299 repoSession = getRepositorySession(ctx);
301 DocumentModelList docList = null;
302 // force limit to 1, and ignore totalSize
303 String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
304 docList = repoSession.query(query, null, 1, 0, false);
305 if (docList.size() != 1) {
306 throw new DocumentNotFoundException("No document found matching filter params: " + query);
308 DocumentModel doc = docList.get(0);
310 if (logger.isDebugEnabled()) {
311 logger.debug("Executed NXQL query: " + query);
314 //set reposession to handle the document
315 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
316 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
317 handler.handle(Action.GET, wrapDoc);
318 handler.complete(Action.GET, wrapDoc);
319 } catch (IllegalArgumentException iae) {
321 } catch (DocumentException de) {
323 } catch (Exception e) {
324 if (logger.isDebugEnabled()) {
325 logger.debug("Caught exception ", e);
327 throw new NuxeoDocumentException(e);
329 if (repoSession != null) {
330 releaseRepositorySession(ctx, repoSession);
335 public DocumentWrapper<DocumentModel> getDoc(
336 CoreSessionInterface repoSession,
337 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
338 String csid) throws DocumentNotFoundException, DocumentException {
339 DocumentWrapper<DocumentModel> wrapDoc = null;
342 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
343 DocumentModel doc = null;
345 doc = repoSession.getDocument(docRef);
346 } catch (ClientException ce) {
347 String msg = logException(ce, "Could not find document with CSID=" + csid);
348 throw new DocumentNotFoundException(msg, ce);
350 wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
351 } catch (IllegalArgumentException iae) {
353 } catch (DocumentException de) {
361 * Get wrapped documentModel from the Nuxeo repository. The search is
362 * restricted to the workspace of the current context.
364 * @param ctx service context under which this method is invoked
365 * @param csid of the document to retrieve
366 * @throws DocumentNotFoundException
367 * @throws TransactionException
368 * @throws DocumentException
369 * @return a wrapped documentModel
372 public DocumentWrapper<DocumentModel> getDoc(
373 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
374 String csid) throws DocumentNotFoundException, TransactionException, DocumentException {
375 CoreSessionInterface repoSession = null;
376 DocumentWrapper<DocumentModel> wrapDoc = null;
379 // Open a new repository session
380 repoSession = getRepositorySession(ctx);
381 wrapDoc = getDoc(repoSession, ctx, csid);
382 } catch (IllegalArgumentException iae) {
384 } catch (DocumentException de) {
386 } catch (Exception e) {
387 if (logger.isDebugEnabled()) {
388 logger.debug("Caught exception ", e);
390 throw new NuxeoDocumentException(e);
392 if (repoSession != null) {
393 releaseRepositorySession(ctx, repoSession);
397 if (logger.isWarnEnabled() == true) {
398 logger.warn("Returned DocumentModel instance was created with a repository session that is now closed.");
403 public DocumentWrapper<DocumentModel> findDoc(
404 CoreSessionInterface repoSession,
405 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
407 throws DocumentNotFoundException, DocumentException {
408 DocumentWrapper<DocumentModel> wrapDoc = null;
411 QueryContext queryContext = new QueryContext(ctx, whereClause);
412 DocumentModelList docList = null;
413 // force limit to 1, and ignore totalSize
414 String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
415 docList = repoSession.query(query,
420 if (docList.size() != 1) {
421 if (logger.isDebugEnabled()) {
422 logger.debug("findDoc: Query found: " + docList.size() + " items.");
423 logger.debug(" Query: " + query);
425 throw new DocumentNotFoundException("No document found matching filter params: " + query);
427 DocumentModel doc = docList.get(0);
428 wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
429 } catch (IllegalArgumentException iae) {
431 } catch (DocumentException de) {
433 } catch (Exception e) {
434 if (logger.isDebugEnabled()) {
435 logger.debug("Caught exception ", e);
437 throw new NuxeoDocumentException(e);
444 * find wrapped documentModel from the Nuxeo repository
446 * @param ctx service context under which this method is invoked
447 * @param whereClause where NXQL where clause to get the document
448 * @throws DocumentNotFoundException
449 * @throws TransactionException
450 * @throws DocumentException
451 * @return a wrapped documentModel retrieved by the repository query
454 public DocumentWrapper<DocumentModel> findDoc(
455 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
457 throws DocumentNotFoundException, TransactionException, DocumentException {
458 CoreSessionInterface repoSession = null;
459 DocumentWrapper<DocumentModel> wrapDoc = null;
462 repoSession = getRepositorySession(ctx);
463 wrapDoc = findDoc(repoSession, ctx, whereClause);
464 } catch (DocumentNotFoundException dnfe) {
466 } catch (DocumentException de) {
468 } catch (Exception e) {
469 if (repoSession == null) {
470 throw new NuxeoDocumentException("Unable to create a Nuxeo repository session.", e);
472 throw new NuxeoDocumentException("Unexpected Nuxeo exception.", e);
475 if (repoSession != null) {
476 releaseRepositorySession(ctx, repoSession);
480 if (logger.isWarnEnabled() == true) {
481 logger.warn("Returned DocumentModel instance was created with a repository session that is now closed.");
488 * find doc and return CSID from the Nuxeo repository
491 * @param ctx service context under which this method is invoked
492 * @param whereClause where NXQL where clause to get the document
493 * @throws DocumentNotFoundException
494 * @throws TransactionException
495 * @throws DocumentException
496 * @return the CollectionSpace ID (CSID) of the requested document
499 public String findDocCSID(CoreSessionInterface repoSession,
500 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String whereClause)
501 throws DocumentNotFoundException, TransactionException, DocumentException {
503 boolean releaseSession = false;
505 if (repoSession == null) {
506 repoSession = this.getRepositorySession(ctx);
507 releaseSession = true;
509 DocumentWrapper<DocumentModel> wrapDoc = findDoc(repoSession, ctx, whereClause);
510 DocumentModel docModel = wrapDoc.getWrappedObject();
511 csid = NuxeoUtils.getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
512 } catch (DocumentNotFoundException dnfe) {
514 } catch (IllegalArgumentException iae) {
516 } catch (DocumentException de) {
518 } catch (Exception e) {
519 if (logger.isDebugEnabled()) {
520 logger.debug("Caught exception ", e);
522 throw new NuxeoDocumentException(e);
524 if (releaseSession && (repoSession != null)) {
525 this.releaseRepositorySession(ctx, repoSession);
531 public DocumentWrapper<DocumentModelList> findDocs(
532 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
533 CoreSessionInterface repoSession,
534 List<String> docTypes,
536 String orderByClause,
539 boolean computeTotal)
540 throws DocumentNotFoundException, DocumentException {
541 DocumentWrapper<DocumentModelList> wrapDoc = null;
544 if (docTypes == null || docTypes.size() < 1) {
545 throw new DocumentNotFoundException(
546 "The findDocs() method must specify at least one DocumentType.");
548 DocumentModelList docList = null;
549 QueryContext queryContext = new QueryContext(ctx, whereClause, orderByClause);
550 String query = NuxeoUtils.buildNXQLQuery(docTypes, queryContext);
551 if (logger.isDebugEnabled()) {
552 logger.debug("findDocs() NXQL: " + query);
554 docList = repoSession.query(query, null, pageSize, pageSize * pageNum, computeTotal);
555 wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
556 } catch (IllegalArgumentException iae) {
558 } catch (Exception e) {
559 if (logger.isDebugEnabled()) {
560 logger.debug("Caught exception ", e);
562 throw new NuxeoDocumentException(e);
568 protected static String buildInListForDocTypes(List<String> docTypes) {
569 StringBuilder sb = new StringBuilder();
571 boolean first = true;
572 for (String docType : docTypes) {
583 return sb.toString();
586 public DocumentWrapper<DocumentModelList> findDocs(
587 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
588 DocumentHandler handler,
589 CoreSessionInterface repoSession,
590 List<String> docTypes)
591 throws DocumentNotFoundException, DocumentException {
592 DocumentWrapper<DocumentModelList> wrapDoc = null;
594 DocumentFilter filter = handler.getDocumentFilter();
595 String oldOrderBy = filter.getOrderByClause();
596 if (isClauseEmpty(oldOrderBy) == true) {
597 filter.setOrderByClause(DocumentFilter.ORDER_BY_LAST_UPDATED);
599 QueryContext queryContext = new QueryContext(ctx, handler);
602 if (docTypes == null || docTypes.size() < 1) {
603 throw new DocumentNotFoundException(
604 "The findDocs() method must specify at least one DocumentType.");
606 DocumentModelList docList = null;
607 if (handler.isCMISQuery() == true) {
608 String inList = buildInListForDocTypes(docTypes);
609 ctx.getQueryParams().add(IQueryManager.SEARCH_RELATED_MATCH_OBJ_DOCTYPES, inList);
610 docList = getFilteredCMIS(repoSession, ctx, handler, queryContext);
612 String query = NuxeoUtils.buildNXQLQuery(docTypes, queryContext);
613 if (logger.isDebugEnabled()) {
614 logger.debug("findDocs() NXQL: " + query);
616 docList = repoSession.query(query, null, filter.getPageSize(), filter.getOffset(), true);
618 wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
619 } catch (IllegalArgumentException iae) {
621 } catch (Exception e) {
622 if (logger.isDebugEnabled()) {
623 logger.debug("Caught exception ", e);
625 throw new NuxeoDocumentException(e);
632 * Find a list of documentModels from the Nuxeo repository
634 * @param docTypes a list of DocType names to match
635 * @param whereClause where the clause to qualify on
636 * @throws DocumentNotFoundException
637 * @throws TransactionException
638 * @throws DocumentException
639 * @return a list of documentModels
642 public DocumentWrapper<DocumentModelList> findDocs(
643 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
644 List<String> docTypes,
646 int pageSize, int pageNum, boolean computeTotal)
647 throws DocumentNotFoundException, TransactionException, DocumentException {
648 CoreSessionInterface repoSession = null;
649 DocumentWrapper<DocumentModelList> wrapDoc = null;
652 repoSession = getRepositorySession(ctx);
653 wrapDoc = findDocs(ctx, repoSession, docTypes, whereClause, null,
654 pageSize, pageNum, computeTotal);
655 } catch (IllegalArgumentException iae) {
657 } catch (Exception e) {
658 if (logger.isDebugEnabled()) {
659 logger.debug("Caught exception ", e);
661 throw new NuxeoDocumentException(e);
663 if (repoSession != null) {
664 releaseRepositorySession(ctx, repoSession);
668 if (logger.isWarnEnabled() == true) {
669 logger.warn("Returned DocumentModelList instance was created with a repository session that is now closed.");
676 * @see org.collectionspace.services.common.storage.StorageClient#get(org.collectionspace.services.common.context.ServiceContext, java.util.List, org.collectionspace.services.common.document.DocumentHandler)
679 public void get(ServiceContext ctx, List<String> csidList, DocumentHandler handler)
680 throws DocumentNotFoundException, TransactionException, DocumentException {
681 if (handler == null) {
682 throw new IllegalArgumentException(
683 "RepositoryJavaClient.getAll: handler is missing");
686 CoreSessionInterface repoSession = null;
688 handler.prepare(Action.GET_ALL);
689 repoSession = getRepositorySession(ctx);
690 DocumentModelList docModelList = new DocumentModelListImpl();
691 //FIXME: Should be using NuxeoUtils.createPathRef for security reasons
692 for (String csid : csidList) {
693 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
694 DocumentModel docModel = repoSession.getDocument(docRef);
695 docModelList.add(docModel);
698 //set reposession to handle the document
699 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
700 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docModelList);
701 handler.handle(Action.GET_ALL, wrapDoc);
702 handler.complete(Action.GET_ALL, wrapDoc);
703 } catch (DocumentException de) {
705 } catch (Exception e) {
706 if (logger.isDebugEnabled()) {
707 logger.debug("Caught exception ", e);
709 throw new NuxeoDocumentException(e);
711 if (repoSession != null) {
712 releaseRepositorySession(ctx, repoSession);
718 * getAll get all documents for an entity entity service from the Nuxeo
721 * @param ctx service context under which this method is invoked
722 * @param handler should be used by the caller to provide and transform the
724 * @throws DocumentNotFoundException
725 * @throws TransactionException
726 * @throws DocumentException
729 public void getAll(ServiceContext ctx, DocumentHandler handler)
730 throws DocumentNotFoundException, TransactionException, DocumentException {
731 if (handler == null) {
732 throw new IllegalArgumentException(
733 "RepositoryJavaClient.getAll: handler is missing");
735 String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
736 if (nuxeoWspaceId == null) {
737 throw new DocumentNotFoundException(
738 "Unable to find workspace for service "
739 + ctx.getServiceName()
740 + " check if the workspace exists in the Nuxeo repository.");
743 CoreSessionInterface repoSession = null;
745 handler.prepare(Action.GET_ALL);
746 repoSession = getRepositorySession(ctx);
747 DocumentRef wsDocRef = new IdRef(nuxeoWspaceId);
748 DocumentModelList docList = repoSession.getChildren(wsDocRef);
749 //set reposession to handle the document
750 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
751 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
752 handler.handle(Action.GET_ALL, wrapDoc);
753 handler.complete(Action.GET_ALL, wrapDoc);
754 } catch (DocumentException de) {
756 } catch (Exception e) {
757 if (logger.isDebugEnabled()) {
758 logger.debug("Caught exception ", e);
760 throw new NuxeoDocumentException(e);
762 if (repoSession != null) {
763 releaseRepositorySession(ctx, repoSession);
768 private boolean isClauseEmpty(String theString) {
769 boolean result = true;
770 if (theString != null && !theString.isEmpty()) {
776 public DocumentWrapper<DocumentModel> getDocFromCsid(
777 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
778 CoreSessionInterface repoSession,
781 DocumentWrapper<DocumentModel> result = null;
783 result = new DocumentWrapperImpl<DocumentModel>(NuxeoUtils.getDocFromCsid(ctx, repoSession, csid));
789 * A method to find a CollectionSpace document (of any type) given just a service context and
790 * its CSID. A search across *all* service workspaces (within a given tenant context) is performed to find
793 * This query searches Nuxeo's Hierarchy table where our CSIDs are stored in the "name" column.
796 public DocumentWrapper<DocumentModel> getDocFromCsid(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
799 DocumentWrapper<DocumentModel> result = null;
800 CoreSessionInterface repoSession = null;
802 repoSession = getRepositorySession(ctx);
803 result = getDocFromCsid(ctx, repoSession, csid);
805 if (repoSession != null) {
806 releaseRepositorySession(ctx, repoSession);
810 if (logger.isWarnEnabled() == true) {
811 logger.warn("Returned DocumentModel instance was created with a repository session that is now closed.");
818 * Returns a URI value for a document in the Nuxeo repository
820 * @param wrappedDoc a wrapped documentModel
821 * @throws ClientException
822 * @return a document URI
825 public String getDocURI(DocumentWrapper<DocumentModel> wrappedDoc) throws ClientException {
826 DocumentModel docModel = wrappedDoc.getWrappedObject();
827 String uri = (String) docModel.getProperty(CollectionSpaceClient.COLLECTIONSPACE_CORE_SCHEMA,
828 CollectionSpaceClient.COLLECTIONSPACE_CORE_URI);
833 * See CSPACE-5036 - How to make CMISQL queries from Nuxeo
835 private IterableQueryResult makeCMISQLQuery(CoreSessionInterface repoSession, String query, QueryContext queryContext) throws DocumentException {
836 IterableQueryResult result = null;
837 /** Threshold over which temporary files are not kept in memory. */
838 final int THRESHOLD = 1024 * 1024;
841 logger.debug(String.format("Performing a CMIS query on Nuxeo repository named %s",
842 repoSession.getRepositoryName()));
844 ThresholdOutputStreamFactory streamFactory = ThresholdOutputStreamFactory.newInstance(
845 null, THRESHOLD, -1, false);
846 CallContextImpl callContext = new CallContextImpl(
847 CallContext.BINDING_LOCAL,
848 CmisVersion.CMIS_1_1,
849 repoSession.getRepositoryName(),
850 null, // ServletContext
851 null, // HttpServletRequest
852 null, // HttpServletResponse
853 new NuxeoCmisServiceFactory(),
855 callContext.put(CallContext.USERNAME, repoSession.getPrincipal().getName());
857 NuxeoCmisService cmisService = new NuxeoCmisService(repoSession.getCoreSession());
858 result = repoSession.queryAndFetch(query, "CMISQL", cmisService);
859 } catch (ClientException e) {
860 // TODO Auto-generated catch block
861 logger.error("Encounter trouble making the following CMIS query: " + query, e);
862 throw new NuxeoDocumentException(e);
869 * getFiltered get all documents for an entity service from the Document
870 * repository, given filter parameters specified by the handler.
872 * @param ctx service context under which this method is invoked
873 * @param handler should be used by the caller to provide and transform the
875 * @throws DocumentNotFoundException if workspace not found
876 * @throws TransactionException
877 * @throws DocumentException
880 public void getFiltered(ServiceContext ctx, DocumentHandler handler)
881 throws DocumentNotFoundException, TransactionException, DocumentException {
883 DocumentFilter filter = handler.getDocumentFilter();
884 String oldOrderBy = filter.getOrderByClause();
885 if (isClauseEmpty(oldOrderBy) == true) {
886 filter.setOrderByClause(DocumentFilter.ORDER_BY_LAST_UPDATED);
888 QueryContext queryContext = new QueryContext(ctx, handler);
890 CoreSessionInterface repoSession = null;
892 handler.prepare(Action.GET_ALL);
893 repoSession = getRepositorySession(ctx); //Keeps a refcount here for the repository session so you need to release this when finished
895 DocumentModelList docList = null;
897 if (handler.isJDBCQuery() == true) {
898 docList = getFilteredJDBC(repoSession, ctx, handler);
900 } else if (handler.isCMISQuery() == true) {
901 docList = getFilteredCMIS(repoSession, ctx, handler, queryContext); //FIXME: REM - Need to deal with paging info in CMIS query
904 String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
905 if (logger.isDebugEnabled()) {
906 logger.debug("Executing NXQL query: " + query.toString());
908 Profiler profiler = new Profiler(this, 2);
909 profiler.log("Executing NXQL query: " + query.toString());
911 // If we have a page size and/or offset, then reflect those values
912 // when constructing the query, and also pass 'true' to get totalSize
913 // in the returned DocumentModelList.
914 if ((queryContext.getDocFilter().getOffset() > 0) || (queryContext.getDocFilter().getPageSize() > 0)) {
915 docList = repoSession.query(query, null,
916 queryContext.getDocFilter().getPageSize(), queryContext.getDocFilter().getOffset(), true);
918 docList = repoSession.query(query);
923 //set repoSession to handle the document
924 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
925 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
926 handler.handle(Action.GET_ALL, wrapDoc);
927 handler.complete(Action.GET_ALL, wrapDoc);
928 } catch (DocumentException de) {
930 } catch (Exception e) {
931 if (logger.isDebugEnabled()) {
932 logger.debug("Caught exception ", e); // REM - 1/17/2014: Check for org.nuxeo.ecm.core.api.ClientException and re-attempt
934 throw new NuxeoDocumentException(e);
936 if (repoSession != null) {
937 releaseRepositorySession(ctx, repoSession);
943 * Perform a database query, via JDBC and SQL, to retrieve matching records
944 * based on filter criteria.
946 * Although this method currently has a general-purpose name, it is
947 * currently dedicated to a specific task: that of improving performance
948 * for partial term matching queries on authority items / terms, via
949 * the use of a hand-tuned SQL query, rather than via the generated SQL
950 * produced by Nuxeo from an NXQL query. (See CSPACE-6361 for a task
951 * to generalize this method.)
953 * @param repoSession a repository session.
954 * @param ctx the service context.
955 * @param handler a relevant document handler.
956 * @return a list of document models matching the search criteria.
959 private DocumentModelList getFilteredJDBC(CoreSessionInterface repoSession, ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
960 DocumentHandler handler) throws Exception {
961 DocumentModelList result = new DocumentModelListImpl();
963 // FIXME: Get all of the following values from appropriate external constants.
965 // At present, the two constants below are duplicated in both RepositoryJavaClientImpl
966 // and in AuthorityItemDocumentModelHandler.
967 final String TERM_GROUP_LIST_NAME = "TERM_GROUP_LIST_NAME";
968 final String TERM_GROUP_TABLE_NAME_PARAM = "TERM_GROUP_TABLE_NAME";
969 final String IN_AUTHORITY_PARAM = "IN_AUTHORITY";
970 // Get this from a constant in AuthorityResource or equivalent
971 final String PARENT_WILDCARD = "_ALL_";
973 // Build two SQL statements, to be executed within a single transaction:
974 // the first statement to control join order, and the second statement
975 // representing the actual 'get filtered' query
977 // Build the join control statement
979 // Per http://www.postgresql.org/docs/9.2/static/runtime-config-query.html#GUC-JOIN-COLLAPSE-LIMIT
980 // "Setting [this value] to 1 prevents any reordering of explicit JOINs.
981 // Thus, the explicit join order specified in the query will be the
982 // actual order in which the relations are joined."
983 // See CSPACE-5945 for further discussion of why this setting is needed.
985 // Adding this statement is commented out here for now. It significantly
986 // improved query performance for authority item / term queries where
987 // large numbers of rows were retrieved, but appears to have resulted
988 // in consistently slower-than-desired query performance where zero or
989 // very few records were retrieved. See notes on CSPACE-5945. - ADR 2013-04-09
990 // String joinControlSql = "SET LOCAL join_collapse_limit TO 1;";
992 // Build the query statement
994 // Start with the default query
995 String selectStatement =
996 "SELECT DISTINCT commonschema.id"
997 + " FROM " + handler.getServiceContext().getCommonPartLabel() + " commonschema";
1001 + " ON misc.id = commonschema.id"
1002 + " INNER JOIN hierarchy hierarchy_termgroup"
1003 + " ON hierarchy_termgroup.parentid = misc.id"
1004 + " INNER JOIN " + handler.getJDBCQueryParams().get(TERM_GROUP_TABLE_NAME_PARAM) + " termgroup"
1005 + " ON termgroup.id = hierarchy_termgroup.id ";
1008 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
1009 // Value for replaceable parameter 1 in the query
1010 String partialTerm = queryParams.getFirst(IQueryManager.SEARCH_TYPE_PARTIALTERM);
1011 // If the value of the partial term query parameter is blank ('pt='),
1012 // return all records, subject to restriction by any limit clause
1013 if (Tools.isBlank(partialTerm)) {
1016 // Otherwise, return records that match the supplied partial term
1018 " WHERE (termgroup.termdisplayname ILIKE ?)";
1021 // At present, results are ordered in code, below, rather than in SQL,
1022 // and the orderByClause below is thus intentionally blank.
1024 // To implement the orderByClause below in SQL; e.g. via
1025 // 'ORDER BY termgroup.termdisplayname', the relevant column
1026 // must be returned by the SELECT statement.
1027 String orderByClause = "";
1030 TenantBindingConfigReaderImpl tReader =
1031 ServiceMain.getInstance().getTenantBindingConfigReader();
1032 TenantBindingType tenantBinding = tReader.getTenantBinding(ctx.getTenantId());
1033 String maxListItemsLimit = TenantBindingUtils.getPropertyValue(tenantBinding,
1034 IQueryManager.MAX_LIST_ITEMS_RETURNED_LIMIT_ON_JDBC_QUERIES);
1036 " LIMIT " + getMaxItemsLimitOnJdbcQueries(maxListItemsLimit); // implicit int-to-String conversion
1038 // After building the individual parts of the query, set the values
1039 // of replaceable parameters that will be inserted into that query
1040 // and optionally add restrictions
1042 List<String> params = new ArrayList<>();
1044 if (Tools.notBlank(whereClause)) {
1046 // Read tenant bindings configuration to determine whether
1047 // to automatically insert leading, as well as trailing, wildcards
1048 // into the term matching string.
1049 String usesStartingWildcard = TenantBindingUtils.getPropertyValue(tenantBinding,
1050 IQueryManager.TENANT_USES_STARTING_WILDCARD_FOR_PARTIAL_TERM);
1051 // Handle user-provided leading wildcard characters, in the
1052 // configuration where a leading wildcard is not automatically inserted.
1053 // (The user-provided wildcard must be in the first, or "starting"
1054 // character position in the partial term value.)
1055 if (Tools.notBlank(usesStartingWildcard)) {
1056 if (usesStartingWildcard.equalsIgnoreCase(Boolean.FALSE.toString())) {
1057 partialTerm = handleProvidedStartingWildcard(partialTerm);
1058 // Otherwise, in the configuration where a leading wildcard
1059 // is usually automatically inserted, handle the cases where
1060 // a user has entered an anchor character in the first position
1061 // in the starting term value. In those cases, strip that
1062 // anchor character and don't add a leading wildcard
1064 if (partialTerm.startsWith(USER_SUPPLIED_ANCHOR_CHAR)) {
1065 partialTerm = partialTerm.substring(1, partialTerm.length());
1066 // Otherwise, automatically add a leading wildcard
1068 partialTerm = JDBCTools.SQL_WILDCARD + partialTerm;
1072 // Add SQL wildcards in the midst of the partial term match search
1073 // expression, whever user-supplied wildcards appear, except in the
1074 // first or last character positions of the search expression.
1075 partialTerm = subtituteWildcardsInPartialTerm(partialTerm);
1077 // If a designated 'anchor character' is present as the last character
1078 // in the search expression, strip that character and don't add
1079 // a trailing wildcard
1080 int lastCharPos = partialTerm.length() - 1;
1081 if (partialTerm.endsWith(USER_SUPPLIED_ANCHOR_CHAR) && lastCharPos > 0) {
1082 partialTerm = partialTerm.substring(0, lastCharPos);
1084 // Otherwise, automatically add a trailing wildcard
1085 partialTerm = partialTerm + JDBCTools.SQL_WILDCARD;
1087 params.add(partialTerm);
1090 // Optionally add restrictions to the default query, based on variables
1091 // in the current request
1093 // Restrict the query to filter out deleted records, if requested
1094 String includeDeleted = queryParams.getFirst(WorkflowClient.WORKFLOW_QUERY_NONDELETED);
1095 if (includeDeleted != null && includeDeleted.equalsIgnoreCase(Boolean.FALSE.toString())) {
1096 whereClause = whereClause
1097 + " AND (misc.lifecyclestate <> '" + WorkflowClient.WORKFLOWSTATE_DELETED + "')";
1100 // If a particular authority is specified, restrict the query further
1101 // to return only records within that authority
1102 String inAuthorityValue = (String) handler.getJDBCQueryParams().get(IN_AUTHORITY_PARAM);
1103 if (Tools.notBlank(inAuthorityValue)) {
1104 // Handle the '_ALL_' case for inAuthority
1105 if (inAuthorityValue.equals(PARENT_WILDCARD)) {
1106 // Add nothing to the query here if it should match within all authorities
1108 whereClause = whereClause
1109 + " AND (commonschema.inauthority = ?)";
1110 params.add(inAuthorityValue); // Value for replaceable parameter 2 in the query
1114 // Restrict the query further to return only records pertaining to
1115 // the current tenant, unless:
1116 // * Data for this service, in this tenant, is stored in its own,
1117 // separate repository, rather than being intermingled with other
1118 // tenants' data in the default repository; or
1119 // * Restriction by tenant ID in JDBC queries has been disabled,
1120 // via configuration for this tenant,
1121 if (restrictJDBCQueryByTenantID(tenantBinding, ctx)) {
1122 joinClauses = joinClauses
1123 + " INNER JOIN collectionspace_core core"
1124 + " ON core.id = hierarchy_termgroup.parentid";
1125 whereClause = whereClause
1126 + " AND (core.tenantid = ?)";
1127 params.add(ctx.getTenantId()); // Value for replaceable parameter 3 in the query
1130 // Piece together the SQL query from its parts
1131 String querySql = selectStatement + joinClauses + whereClause + orderByClause + limitClause;
1133 // Note: PostgreSQL 9.2 introduced a change that may improve performance
1134 // of certain queries using JDBC PreparedStatements. See comments on
1135 // CSPACE-5943 for details.
1137 // See a comment above for the reason that the joinControl SQL statement,
1138 // along with its corresponding prepared statement builder, is commented out for now.
1139 // PreparedStatementBuilder joinControlBuilder = new PreparedStatementBuilder(joinControlSql);
1140 PreparedStatementSimpleBuilder queryBuilder = new PreparedStatementSimpleBuilder(querySql, params);
1141 List<PreparedStatementBuilder> builders = new ArrayList<>();
1142 // builders.add(joinControlBuilder);
1143 builders.add(queryBuilder);
1144 String dataSourceName = JDBCTools.NUXEO_DATASOURCE_NAME;
1145 String repositoryName = ctx.getRepositoryName();
1146 final Boolean EXECUTE_WITHIN_TRANSACTION = true;
1147 Set<String> docIds = new HashSet<>();
1149 String cspaceInstanceId = ServiceMain.getInstance().getCspaceInstanceId();
1150 List<CachedRowSet> resultsList = JDBCTools.executePreparedQueries(builders,
1151 dataSourceName, repositoryName, cspaceInstanceId, EXECUTE_WITHIN_TRANSACTION);
1153 // At least one set of results is expected, from the second prepared
1154 // statement to be executed.
1155 // If fewer results are returned, return an empty list of document models
1156 if (resultsList == null || resultsList.size() < 1) {
1157 return result; // return an empty list of document models
1159 // The join control query (if enabled - it is currently commented
1160 // out as per comments above) will not return results, so query results
1161 // will be the first set of results (rowSet) returned in the list
1162 CachedRowSet queryResults = resultsList.get(0);
1164 // If the result from executing the query is null or contains zero rows,
1165 // return an empty list of document models
1166 if (queryResults == null) {
1167 return result; // return an empty list of document models
1169 queryResults.last();
1170 if (queryResults.getRow() == 0) {
1171 return result; // return an empty list of document models
1174 // Otherwise, get the document IDs from the results of the query
1176 queryResults.beforeFirst();
1177 while (queryResults.next()) {
1178 id = queryResults.getString(1);
1179 if (Tools.notBlank(id)) {
1183 } catch (SQLException sqle) {
1184 logger.warn("Could not obtain document IDs via SQL query '" + querySql + "': " + sqle.getMessage());
1185 return result; // return an empty list of document models
1188 // Get a list of document models, using the list of IDs obtained from the query
1190 // FIXME: Check whether we have a 'get document models from list of CSIDs'
1191 // utility method like this, and if not, add this to the appropriate
1193 DocumentModel docModel;
1194 for (String docId : docIds) {
1195 docModel = NuxeoUtils.getDocumentModel(repoSession, docId);
1196 if (docModel == null) {
1197 logger.warn("Could not obtain document model for document with ID " + docId);
1199 result.add(docModel);
1203 // Order the results
1204 final String COMMON_PART_SCHEMA = handler.getServiceContext().getCommonPartLabel();
1205 final String DISPLAY_NAME_XPATH =
1206 "//" + handler.getJDBCQueryParams().get(TERM_GROUP_LIST_NAME) + "/[0]/termDisplayName";
1207 Collections.sort(result, new Comparator<DocumentModel>() {
1209 public int compare(DocumentModel doc1, DocumentModel doc2) {
1210 String termDisplayName1 = null;
1211 String termDisplayName2 = null;
1213 termDisplayName1 = (String) NuxeoUtils.getXPathValue(doc1, COMMON_PART_SCHEMA, DISPLAY_NAME_XPATH);
1214 termDisplayName2 = (String) NuxeoUtils.getXPathValue(doc2, COMMON_PART_SCHEMA, DISPLAY_NAME_XPATH);
1215 } catch (NuxeoDocumentException e) {
1216 throw new RuntimeException(e); // We need to throw a RuntimeException because the compare() method of the Comparator interface does not support throwing an Exception
1218 return termDisplayName1.compareToIgnoreCase(termDisplayName2);
1226 private DocumentModelList getFilteredCMIS(CoreSessionInterface repoSession,
1227 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, DocumentHandler handler, QueryContext queryContext)
1228 throws DocumentNotFoundException, DocumentException {
1230 DocumentModelList result = new DocumentModelListImpl();
1232 String query = handler.getCMISQuery(queryContext);
1234 DocumentFilter docFilter = handler.getDocumentFilter();
1235 int pageSize = docFilter.getPageSize();
1236 int offset = docFilter.getOffset();
1237 if (logger.isDebugEnabled()) {
1238 logger.debug("Executing CMIS query: " + query.toString()
1239 + "with pageSize: " + pageSize + " at offset: " + offset);
1242 // If we have limit and/or offset, then pass true to get totalSize
1243 // in returned DocumentModelList.
1244 Profiler profiler = new Profiler(this, 2);
1245 profiler.log("Executing CMIS query: " + query.toString());
1248 IterableQueryResult queryResult = makeCMISQLQuery(repoSession, query, queryContext);
1250 int totalSize = (int) queryResult.size();
1251 ((DocumentModelListImpl) result).setTotalSize(totalSize);
1252 // Skip the rows before our offset
1254 queryResult.skipTo(offset);
1257 for (Map<String, Serializable> row : queryResult) {
1258 if (logger.isTraceEnabled()) {
1259 logger.trace(" Hierarchy Table ID is:" + row.get(IQueryManager.CMIS_TARGET_NUXEO_ID)
1260 + " nuxeo:pathSegment is: " + row.get(IQueryManager.CMIS_TARGET_NAME));
1262 String nuxeoId = (String) row.get(IQueryManager.CMIS_TARGET_NUXEO_ID);
1263 DocumentModel docModel = NuxeoUtils.getDocumentModel(repoSession, nuxeoId);
1264 result.add(docModel);
1266 if (nRows >= pageSize && pageSize != 0) { // A page size of zero means that they want all of them
1267 logger.debug("Got page full of items - quitting");
1272 queryResult.close();
1277 } catch (Exception e) {
1278 if (logger.isDebugEnabled()) {
1279 logger.debug("Caught exception ", e);
1281 throw new NuxeoDocumentException(e);
1285 // Since we're not supporting paging yet for CMIS queries, we need to perform
1286 // a workaround for the paging information we return in our list of results
1289 if (result != null) {
1290 docFilter.setStartPage(0);
1291 if (totalSize > docFilter.getPageSize()) {
1292 docFilter.setPageSize(totalSize);
1293 ((DocumentModelListImpl)result).setTotalSize(totalSize);
1301 private String logException(Exception e, String msg) {
1302 String result = null;
1304 String exceptionMessage = e.getMessage();
1305 exceptionMessage = exceptionMessage != null ? exceptionMessage : "<No details provided>";
1306 result = msg = msg + ". Caught exception:" + exceptionMessage;
1308 if (logger.isTraceEnabled() == true) {
1309 logger.error(msg, e);
1318 * update given document in the Nuxeo repository
1320 * @param ctx service context under which this method is invoked
1321 * @param csid of the document
1322 * @param handler should be used by the caller to provide and transform the
1324 * @throws BadRequestException
1325 * @throws DocumentNotFoundException
1326 * @throws TransactionException if the transaction times out or otherwise
1327 * cannot be successfully completed
1328 * @throws DocumentException
1331 public void update(ServiceContext ctx, String csid, DocumentHandler handler)
1332 throws BadRequestException, DocumentNotFoundException, TransactionException,
1334 if (handler == null) {
1335 throw new IllegalArgumentException(
1336 "RepositoryJavaClient.update: document handler is missing.");
1339 CoreSessionInterface repoSession = null;
1341 handler.prepare(Action.UPDATE);
1342 repoSession = getRepositorySession(ctx);
1343 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
1344 DocumentModel doc = null;
1346 doc = repoSession.getDocument(docRef);
1347 } catch (ClientException ce) {
1348 String msg = logException(ce, "Could not find document to update with CSID=" + csid);
1349 throw new DocumentNotFoundException(msg, ce);
1351 // Check for a versioned document, and check In and Out before we proceed.
1352 if (((DocumentModelHandler) handler).supportsVersioning()) {
1353 /* Once we advance to 5.5 or later, we can add this.
1354 * See also https://jira.nuxeo.com/browse/NXP-8506
1355 if(!doc.isVersionable()) {
1356 throw new NuxeoDocumentException("Configuration for: "
1357 +handler.getServiceContextPath()+" supports versioning, but Nuxeo config does not!");
1360 /* Force a version number - Not working. Apparently we need to configure the uid schema??
1361 if(doc.getProperty("uid","major_version") == null) {
1362 doc.setProperty("uid","major_version",1);
1364 if(doc.getProperty("uid","minor_version") == null) {
1365 doc.setProperty("uid","minor_version",0);
1368 doc.checkIn(VersioningOption.MINOR, null);
1373 // Set reposession to handle the document
1375 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
1376 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
1377 handler.handle(Action.UPDATE, wrapDoc);
1378 repoSession.saveDocument(doc);
1380 handler.complete(Action.UPDATE, wrapDoc);
1381 } catch (BadRequestException bre) {
1383 } catch (DocumentException de) {
1385 } catch (CSWebApplicationException wae) {
1387 } catch (Exception e) {
1388 if (logger.isDebugEnabled()) {
1389 logger.debug("Caught exception ", e);
1391 throw new NuxeoDocumentException(e);
1393 if (repoSession != null) {
1394 releaseRepositorySession(ctx, repoSession);
1400 * Save a documentModel to the Nuxeo repository.
1402 * @param ctx service context under which this method is invoked
1403 * @param repoSession
1404 * @param docModel the document to save
1405 * @param fSaveSession if TRUE, will call CoreSessionInterface.save() to save
1406 * accumulated changes.
1407 * @throws ClientException
1408 * @throws DocumentException
1410 public void saveDocWithoutHandlerProcessing(
1411 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
1412 CoreSessionInterface repoSession,
1413 DocumentModel docModel,
1414 boolean fSaveSession)
1415 throws ClientException, DocumentException {
1418 repoSession.saveDocument(docModel);
1422 } catch (ClientException ce) {
1424 } catch (Exception e) {
1425 if (logger.isDebugEnabled()) {
1426 logger.debug("Caught exception ", e);
1428 throw new NuxeoDocumentException(e);
1433 * Save a list of documentModels to the Nuxeo repository.
1435 * @param ctx service context under which this method is invoked
1436 * @param repoSession a repository session
1437 * @param docModelList a list of document models
1438 * @param fSaveSession if TRUE, will call CoreSessionInterface.save() to save
1439 * accumulated changes.
1440 * @throws ClientException
1441 * @throws DocumentException
1443 public void saveDocListWithoutHandlerProcessing(
1444 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
1445 CoreSessionInterface repoSession,
1446 DocumentModelList docList,
1447 boolean fSaveSession)
1448 throws ClientException, DocumentException {
1450 DocumentModel[] docModelArray = new DocumentModel[docList.size()];
1451 repoSession.saveDocuments(docList.toArray(docModelArray));
1455 } catch (ClientException ce) {
1457 } catch (Exception e) {
1458 logger.error("Caught exception ", e);
1459 throw new NuxeoDocumentException(e);
1464 public void deleteWithWhereClause(@SuppressWarnings("rawtypes") ServiceContext ctx, String whereClause,
1465 @SuppressWarnings("rawtypes") DocumentHandler handler) throws
1466 DocumentNotFoundException, DocumentException {
1468 throw new IllegalArgumentException(
1469 "delete(ctx, specifier): ctx is missing");
1471 if (logger.isDebugEnabled()) {
1472 logger.debug("Deleting document with whereClause=" + whereClause);
1475 DocumentWrapper<DocumentModel> foundDocWrapper = this.findDoc(ctx, whereClause);
1476 if (foundDocWrapper != null) {
1477 DocumentModel docModel = foundDocWrapper.getWrappedObject();
1478 String csid = docModel.getName();
1479 this.delete(ctx, csid, handler);
1484 * delete a document from the Nuxeo repository
1486 * @param ctx service context under which this method is invoked
1487 * @param id of the document
1488 * @throws DocumentException
1491 public void delete(ServiceContext ctx, String id, DocumentHandler handler) throws DocumentNotFoundException,
1492 DocumentException, TransactionException {
1494 throw new IllegalArgumentException(
1495 "delete(ctx, ix, handler): ctx is missing");
1497 if (handler == null) {
1498 throw new IllegalArgumentException(
1499 "delete(ctx, ix, handler): handler is missing");
1501 if (logger.isDebugEnabled()) {
1502 logger.debug("Deleting document with CSID=" + id);
1504 CoreSessionInterface repoSession = null;
1506 handler.prepare(Action.DELETE);
1507 repoSession = getRepositorySession(ctx);
1508 DocumentWrapper<DocumentModel> wrapDoc = null;
1510 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
1511 wrapDoc = new DocumentWrapperImpl<DocumentModel>(repoSession.getDocument(docRef));
1512 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
1513 handler.handle(Action.DELETE, wrapDoc);
1514 repoSession.removeDocument(docRef);
1515 } catch (ClientException ce) {
1516 String msg = logException(ce, "Could not find document to delete with CSID=" + id);
1517 throw new DocumentNotFoundException(msg, ce);
1520 handler.complete(Action.DELETE, wrapDoc);
1521 } catch (DocumentException de) {
1523 } catch (Exception e) {
1524 if (logger.isDebugEnabled()) {
1525 logger.debug("Caught exception ", e);
1527 throw new NuxeoDocumentException(e);
1529 if (repoSession != null) {
1530 releaseRepositorySession(ctx, repoSession);
1536 * @see org.collectionspace.services.common.storage.StorageClient#delete(org.collectionspace.services.common.context.ServiceContext, java.lang.String, org.collectionspace.services.common.document.DocumentHandler)
1540 public void delete(@SuppressWarnings("rawtypes") ServiceContext ctx, String id)
1541 throws DocumentNotFoundException, DocumentException {
1542 throw new UnsupportedOperationException();
1543 // Use the other delete instead
1547 public Hashtable<String, String> retrieveWorkspaceIds(RepositoryDomainType repoDomain) throws Exception {
1548 return NuxeoConnectorEmbedded.getInstance().retrieveWorkspaceIds(repoDomain);
1552 public String createDomain(RepositoryDomainType repositoryDomain) throws Exception {
1553 CoreSessionInterface repoSession = null;
1554 String domainId = null;
1557 // Open a connection to the domain's repo/db
1559 String repoName = repositoryDomain.getRepositoryName();
1560 repoSession = getRepositorySession(repoName); // domainName=storageName=repoName=databaseName
1562 // First create the top-level domain directory
1564 String domainName = repositoryDomain.getStorageName();
1565 DocumentRef parentDocRef = new PathRef("/");
1566 DocumentModel parentDoc = repoSession.getDocument(parentDocRef);
1567 DocumentModel domainDoc = repoSession.createDocumentModel(parentDoc.getPathAsString(),
1568 domainName, NUXEO_CORE_TYPE_DOMAIN);
1569 domainDoc.setPropertyValue("dc:title", domainName);
1570 domainDoc.setPropertyValue("dc:description", "A CollectionSpace domain "
1572 domainDoc = repoSession.createDocument(domainDoc);
1573 domainId = domainDoc.getId();
1576 // Next, create a "Workspaces" root directory to contain the workspace folders for the individual service documents
1578 DocumentModel workspacesRoot = repoSession.createDocumentModel(domainDoc.getPathAsString(),
1579 NuxeoUtils.Workspaces, NUXEO_CORE_TYPE_WORKSPACEROOT);
1580 workspacesRoot.setPropertyValue("dc:title", NuxeoUtils.Workspaces);
1581 workspacesRoot.setPropertyValue("dc:description", "A CollectionSpace workspaces directory for "
1582 + domainDoc.getPathAsString());
1583 workspacesRoot = repoSession.createDocument(workspacesRoot);
1584 String workspacesRootId = workspacesRoot.getId();
1587 if (logger.isDebugEnabled()) {
1588 logger.debug("Created tenant domain name=" + domainName
1589 + " id=" + domainId + " "
1590 + NuxeoUtils.Workspaces + " id=" + workspacesRootId);
1591 logger.debug("Path to Domain: " + domainDoc.getPathAsString());
1592 logger.debug("Path to Workspaces root: " + workspacesRoot.getPathAsString());
1594 } catch (Exception e) {
1595 if (logger.isDebugEnabled()) {
1596 logger.debug("Could not create tenant domain name=" + repositoryDomain.getStorageName() + " caught exception ", e);
1600 if (repoSession != null) {
1601 releaseRepositorySession(null, repoSession);
1609 public String getDomainId(RepositoryDomainType repositoryDomain) throws Exception {
1610 String domainId = null;
1611 CoreSessionInterface repoSession = null;
1613 String repoName = repositoryDomain.getRepositoryName();
1614 String domainStorageName = repositoryDomain.getStorageName();
1615 if (domainStorageName != null && !domainStorageName.isEmpty()) {
1617 repoSession = getRepositorySession(repoName);
1618 DocumentRef docRef = new PathRef("/" + domainStorageName);
1619 DocumentModel domain = repoSession.getDocument(docRef);
1620 domainId = domain.getId();
1621 } catch (Exception e) {
1622 if (logger.isTraceEnabled()) {
1623 logger.trace("Caught exception ", e); // The document doesn't exist, this let's us know we need to create it
1625 //there is no way to identify if document does not exist due to
1626 //lack of typed exception for getDocument method
1629 if (repoSession != null) {
1630 releaseRepositorySession(null, repoSession);
1639 * Returns the workspaces root directory for a given domain.
1641 private DocumentModel getWorkspacesRoot(CoreSessionInterface repoSession,
1642 String domainName) throws Exception {
1643 DocumentModel result = null;
1645 String domainPath = "/" + domainName;
1646 DocumentRef parentDocRef = new PathRef(domainPath);
1647 DocumentModelList domainChildrenList = repoSession.getChildren(
1649 Iterator<DocumentModel> witer = domainChildrenList.iterator();
1650 while (witer.hasNext()) {
1651 DocumentModel childNode = witer.next();
1652 if (NuxeoUtils.Workspaces.equalsIgnoreCase(childNode.getName())) {
1654 logger.trace("Found workspaces directory at: " + result.getPathAsString());
1659 if (result == null) {
1660 throw new ClientException("Could not find workspace root directory in: "
1668 * @see org.collectionspace.services.common.repository.RepositoryClient#createWorkspace(java.lang.String, java.lang.String)
1671 public String createWorkspace(RepositoryDomainType repositoryDomain, String workspaceName) throws Exception {
1672 CoreSessionInterface repoSession = null;
1673 String workspaceId = null;
1675 String repoName = repositoryDomain.getRepositoryName();
1676 repoSession = getRepositorySession(repoName);
1678 String domainStorageName = repositoryDomain.getStorageName();
1679 DocumentModel parentDoc = getWorkspacesRoot(repoSession, domainStorageName);
1680 if (logger.isTraceEnabled()) {
1681 for (String facet : parentDoc.getFacets()) {
1682 logger.trace("Facet: " + facet);
1686 DocumentModel doc = repoSession.createDocumentModel(parentDoc.getPathAsString(),
1687 workspaceName, NuxeoUtils.WORKSPACE_DOCUMENT_TYPE);
1688 doc.setPropertyValue("dc:title", workspaceName);
1689 doc.setPropertyValue("dc:description", "A CollectionSpace workspace for "
1691 doc = repoSession.createDocument(doc);
1692 workspaceId = doc.getId();
1694 if (logger.isDebugEnabled()) {
1695 logger.debug("Created workspace name=" + workspaceName
1696 + " id=" + workspaceId);
1698 } catch (Exception e) {
1699 if (logger.isDebugEnabled()) {
1700 logger.debug("createWorkspace caught exception ", e);
1704 if (repoSession != null) {
1705 releaseRepositorySession(null, repoSession);
1712 * @see org.collectionspace.services.common.repository.RepositoryClient#getWorkspaceId(java.lang.String, java.lang.String)
1716 public String getWorkspaceId(String tenantDomain, String workspaceName) throws Exception {
1717 String workspaceId = null;
1719 CoreSessionInterface repoSession = null;
1721 repoSession = getRepositorySession((ServiceContext<PoxPayloadIn, PoxPayloadOut>) null);
1722 DocumentRef docRef = new PathRef(
1724 + "/" + NuxeoUtils.Workspaces
1725 + "/" + workspaceName);
1726 DocumentModel workspace = repoSession.getDocument(docRef);
1727 workspaceId = workspace.getId();
1728 } catch (DocumentException de) {
1730 } catch (Exception e) {
1731 if (logger.isDebugEnabled()) {
1732 logger.debug("Caught exception ", e);
1734 throw new NuxeoDocumentException(e);
1736 if (repoSession != null) {
1737 releaseRepositorySession(null, repoSession);
1744 public CoreSessionInterface getRepositorySession(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {
1745 return getRepositorySession(ctx, ctx.getRepositoryName(), ctx.getTimeoutSecs());
1748 public CoreSessionInterface getRepositorySession(String repoName) throws Exception {
1749 return getRepositorySession(null, repoName, ServiceContext.DEFAULT_TX_TIMEOUT);
1753 * Gets the repository session. - Package access only. If the 'ctx' param is
1754 * null then the repo name must be non-mull and vice-versa
1756 * @return the repository session
1757 * @throws Exception the exception
1759 public CoreSessionInterface getRepositorySession(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
1761 int timeoutSeconds) throws Exception {
1762 CoreSessionInterface repoSession = null;
1764 Profiler profiler = new Profiler("getRepositorySession():", 2);
1767 // To get a connection to the Nuxeo repo, we need either a valid ServiceContext instance or a repository name
1770 repoName = ctx.getRepositoryName(); // Notice we are overriding the passed in 'repoName' since we have a valid service context passed in to us
1771 repoSession = (CoreSessionInterface) ctx.getCurrentRepositorySession(); // Look to see if one exists in the context before creating one
1772 } else if (repoName == null || repoName.trim().isEmpty()) {
1773 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.");
1774 logger.error(errMsg);
1775 throw new Exception(errMsg);
1778 // 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
1779 // just the repo name
1781 if (repoSession == null) {
1782 NuxeoClientEmbedded client = NuxeoConnectorEmbedded.getInstance().getClient();
1783 repoSession = client.openRepository(repoName, timeoutSeconds);
1785 if (logger.isDebugEnabled() == true) {
1786 logger.warn("Reusing the current context's repository session.");
1791 if (logger.isTraceEnabled()) {
1792 logger.trace("Testing call to getRepository() repository root: " + repoSession.getRootDocument());
1794 } catch (Throwable e) {
1795 logger.trace("Test call to Nuxeo's getRepository() repository root failed", e);
1801 ctx.setCurrentRepositorySession(repoSession); // For reusing, save the repository session in the current service context
1808 * Release repository session. - Package access only.
1810 * @param repoSession the repo session
1812 public void releaseRepositorySession(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, CoreSessionInterface repoSession) throws TransactionException {
1814 NuxeoClientEmbedded client = NuxeoConnectorEmbedded.getInstance().getClient();
1817 ctx.clearCurrentRepositorySession(); //clear the current context of the now closed repo session
1818 if (ctx.getCurrentRepositorySession() == null) {
1819 client.releaseRepository(repoSession); //release the repo session if the service context's ref count is zeo.
1822 client.releaseRepository(repoSession); //repo session was acquired without a service context
1824 } catch (TransactionRuntimeException tre) {
1825 String causeMsg = null;
1826 Throwable cause = tre.getCause();
1827 if (cause != null) {
1828 causeMsg = cause.getMessage();
1831 TransactionException te; // a CollectionSpace specific tx exception
1832 if (causeMsg != null) {
1833 te = new TransactionException(causeMsg, tre);
1835 te = new TransactionException(tre);
1838 logger.error(te.getMessage(), tre); // Log the standard transaction exception message, plus an exception-specific stack trace
1840 } catch (Exception e) {
1841 logger.error("Could not close the repository session.", e);
1842 // no need to throw this service specific exception
1847 public void doWorkflowTransition(ServiceContext ctx, String id,
1848 DocumentHandler handler, TransitionDef transitionDef)
1849 throws BadRequestException, DocumentNotFoundException,
1851 // 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
1854 private String handleProvidedStartingWildcard(String partialTerm) {
1855 if (Tools.notBlank(partialTerm)) {
1856 if (partialTerm.substring(0, 1).equals(USER_SUPPLIED_WILDCARD)) {
1857 StringBuffer buffer = new StringBuffer(partialTerm);
1858 buffer.setCharAt(0, JDBCTools.SQL_WILDCARD.charAt(0));
1859 partialTerm = buffer.toString();
1866 * Replaces user-supplied wildcards with SQL wildcards, in a partial term
1867 * matching search expression.
1869 * The scope of this replacement excludes the beginning character
1870 * in that search expression, as that character is treated specially.
1872 * @param partialTerm
1873 * @return the partial term, with any user-supplied wildcards replaced
1876 private String subtituteWildcardsInPartialTerm(String partialTerm) {
1877 if (Tools.isBlank(partialTerm)) {
1880 if (! partialTerm.contains(USER_SUPPLIED_WILDCARD)) {
1883 int len = partialTerm.length();
1884 // Partial term search expressions of 2 or fewer characters
1885 // currently aren't amenable to the use of wildcards
1887 logger.warn("Partial term match search expression of just 1-2 characters in length contains a user-supplied wildcard: " + partialTerm);
1888 logger.warn("Will handle that character as a literal value, rather than as a wildcard ...");
1891 return partialTerm.substring(0, 1) // first char
1892 + partialTerm.substring(1, len).replaceAll(USER_SUPPLIED_WILDCARD_REGEX, JDBCTools.SQL_WILDCARD);
1896 private int getMaxItemsLimitOnJdbcQueries(String maxListItemsLimit) {
1897 final int DEFAULT_ITEMS_LIMIT = 40;
1898 if (maxListItemsLimit == null) {
1899 return DEFAULT_ITEMS_LIMIT;
1903 itemsLimit = Integer.parseInt(maxListItemsLimit);
1904 if (itemsLimit < 1) {
1905 logger.warn("Value of configuration setting "
1906 + IQueryManager.MAX_LIST_ITEMS_RETURNED_LIMIT_ON_JDBC_QUERIES
1907 + " must be a positive integer; invalid current value is " + maxListItemsLimit);
1908 logger.warn("Reverting to default value of " + DEFAULT_ITEMS_LIMIT);
1909 itemsLimit = DEFAULT_ITEMS_LIMIT;
1911 } catch (NumberFormatException nfe) {
1912 logger.warn("Value of configuration setting "
1913 + IQueryManager.MAX_LIST_ITEMS_RETURNED_LIMIT_ON_JDBC_QUERIES
1914 + " must be a positive integer; invalid current value is " + maxListItemsLimit);
1915 logger.warn("Reverting to default value of " + DEFAULT_ITEMS_LIMIT);
1916 itemsLimit = DEFAULT_ITEMS_LIMIT;
1922 * Identifies whether a restriction on tenant ID - to return only records
1923 * pertaining to the current tenant - is required in a JDBC query.
1925 * @param tenantBinding a tenant binding configuration.
1926 * @param ctx a service context.
1927 * @return true if a restriction on tenant ID is required in the query;
1928 * false if a restriction is not required.
1930 private boolean restrictJDBCQueryByTenantID(TenantBindingType tenantBinding, ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) {
1931 boolean restrict = true;
1932 // If data for the current service, in the current tenant, is isolated
1933 // within its own separate, per-tenant repository, as contrasted with
1934 // being intermingled with other tenants' data in the default repository,
1935 // no restriction on Tenant ID is required in the query.
1936 String repositoryDomainName = ConfigUtils.getRepositoryName(tenantBinding, ctx.getRepositoryDomainName());
1937 if (!(repositoryDomainName.equals(ConfigUtils.DEFAULT_NUXEO_REPOSITORY_NAME))) {
1940 // If a configuration setting for this tenant identifies that JDBC
1941 // queries should not be restricted by tenant ID (perhaps because
1942 // there is always expected to be only one tenant's data present in
1943 // the system), no restriction on Tenant ID is required in the query.
1944 String queriesRestrictedByTenantId = TenantBindingUtils.getPropertyValue(tenantBinding,
1945 IQueryManager.JDBC_QUERIES_ARE_TENANT_ID_RESTRICTED);
1946 if (Tools.notBlank(queriesRestrictedByTenantId) &&
1947 queriesRestrictedByTenantId.equalsIgnoreCase(Boolean.FALSE.toString())) {