]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
fd1b384949ebcb99327119b6cadebfac0092a442
[tmp/jakarta-migration.git] /
1 /**
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:
5  *
6  * http://www.collectionspace.org http://wiki.collectionspace.org
7  *
8  * Copyright 2009 University of California at Berkeley
9  *
10  * Licensed under the Educational Community License (ECL), Version 2.0. You may
11  * not use this file except in compliance with this License.
12  *
13  * You may obtain a copy of the ECL 2.0 License at
14  *
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  */
17 package org.collectionspace.services.nuxeo.client.java;
18
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;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.UUID;
31
32 import javax.sql.rowset.CachedRowSet;
33 import javax.ws.rs.core.MultivaluedMap;
34
35 import org.collectionspace.services.lifecycle.TransitionDef;
36 import org.collectionspace.services.nuxeo.util.CSReindexFulltextRoot;
37 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
38 import org.collectionspace.services.client.CollectionSpaceClient;
39 import org.collectionspace.services.client.IQueryManager;
40 import org.collectionspace.services.client.PoxPayloadIn;
41 import org.collectionspace.services.client.PoxPayloadOut;
42 import org.collectionspace.services.client.Profiler;
43 import org.collectionspace.services.client.workflow.WorkflowClient;
44 import org.collectionspace.services.common.context.ServiceContext;
45 import org.collectionspace.services.common.query.QueryContext;
46 import org.collectionspace.services.common.repository.RepositoryClient;
47 import org.collectionspace.services.common.storage.JDBCTools;
48 import org.collectionspace.services.common.storage.PreparedStatementSimpleBuilder;
49 import org.collectionspace.services.common.document.BadRequestException;
50 import org.collectionspace.services.common.document.DocumentException;
51 import org.collectionspace.services.common.document.DocumentFilter;
52 import org.collectionspace.services.common.document.DocumentHandler;
53 import org.collectionspace.services.common.document.DocumentNotFoundException;
54 import org.collectionspace.services.common.document.DocumentHandler.Action;
55 import org.collectionspace.services.common.document.DocumentWrapper;
56 import org.collectionspace.services.common.document.DocumentWrapperImpl;
57 import org.collectionspace.services.common.document.TransactionException;
58 import org.collectionspace.services.common.CSWebApplicationException;
59 import org.collectionspace.services.common.ServiceMain;
60 import org.collectionspace.services.common.api.Tools;
61 import org.collectionspace.services.common.config.ConfigUtils;
62 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
63 import org.collectionspace.services.common.config.TenantBindingUtils;
64 import org.collectionspace.services.common.storage.PreparedStatementBuilder;
65 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.Specifier;
66 import org.collectionspace.services.config.tenant.TenantBindingType;
67 import org.collectionspace.services.config.tenant.RepositoryDomainType;
68
69 //
70 // CSPACE-5036 - How to make CMISQL queries from Nuxeo
71 //
72 import org.apache.chemistry.opencmis.commons.enums.CmisVersion;
73 import org.apache.chemistry.opencmis.commons.server.CallContext;
74 import org.apache.chemistry.opencmis.server.impl.CallContextImpl;
75 import org.apache.chemistry.opencmis.server.shared.ThresholdOutputStreamFactory;
76 import org.nuxeo.common.utils.IdUtils;
77 import org.nuxeo.ecm.core.api.ClientException;
78 import org.nuxeo.ecm.core.api.DocumentModel;
79 import org.nuxeo.ecm.core.api.DocumentModelList;
80 import org.nuxeo.ecm.core.api.IterableQueryResult;
81 import org.nuxeo.ecm.core.api.VersioningOption;
82 import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
83 import org.nuxeo.ecm.core.api.DocumentRef;
84 import org.nuxeo.ecm.core.api.IdRef;
85 import org.nuxeo.ecm.core.api.PathRef;
86 import org.nuxeo.runtime.transaction.TransactionRuntimeException;
87 import org.nuxeo.ecm.core.opencmis.bindings.NuxeoCmisServiceFactory;
88 import org.nuxeo.ecm.core.opencmis.impl.server.NuxeoCmisService;
89 import org.slf4j.Logger;
90 import org.slf4j.LoggerFactory;
91
92 /**
93  * RepositoryClientImpl is used to perform CRUD operations on documents in Nuxeo
94  * repository using Remote Java APIs. It uses
95  *
96  * @see DocumentHandler as IOHandler with the client.
97  *
98  * $LastChangedRevision: $ $LastChangedDate: $
99  */
100 public class RepositoryClientImpl implements RepositoryClient<PoxPayloadIn, PoxPayloadOut> {
101         
102     /**
103      * The logger.
104      */
105     private final Logger logger = LoggerFactory.getLogger(RepositoryClientImpl.class);
106 //    private final Logger profilerLogger = LoggerFactory.getLogger("remperf");
107 //    private String foo = Profiler.createLogger();
108     public static final String NUXEO_CORE_TYPE_DOMAIN = "Domain";
109     public static final String NUXEO_CORE_TYPE_WORKSPACEROOT = "WorkspaceRoot";
110     // FIXME: Get this value from an existing constant, if available
111     public static final String BACKSLASH = "\\";
112     public static final String USER_SUPPLIED_WILDCARD = "*";
113     public static final String USER_SUPPLIED_WILDCARD_REGEX = BACKSLASH + USER_SUPPLIED_WILDCARD;
114     public static final String USER_SUPPLIED_ANCHOR_CHAR = "^";
115     public static final String USER_SUPPLIED_ANCHOR_CHAR_REGEX = BACKSLASH + USER_SUPPLIED_ANCHOR_CHAR;
116     public static final String ENDING_ANCHOR_CHAR = "$";
117     public static final String ENDING_ANCHOR_CHAR_REGEX = BACKSLASH + ENDING_ANCHOR_CHAR;
118
119     
120     /**
121      * Instantiates a new repository java client impl.
122      */
123     public RepositoryClientImpl() {
124         //Empty constructor
125     }
126
127     public void assertWorkflowState(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
128             DocumentModel docModel) throws DocumentNotFoundException, ClientException {
129         MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
130         if (queryParams != null) {
131             //
132             // Look for the workflow "delete" query param and see if we need to assert that the
133             // docModel is in a non-deleted workflow state.
134             //
135             String currentState = docModel.getCurrentLifeCycleState();
136             String includeDeletedStr = queryParams.getFirst(WorkflowClient.WORKFLOW_QUERY_NONDELETED);
137             boolean includeDeleted = includeDeletedStr == null ? true : Boolean.parseBoolean(includeDeletedStr);
138             if (includeDeleted == false) {
139                 //
140                 // We don't wanted soft-deleted object, so throw an exception if this one is soft-deleted.
141                 //
142                 if (currentState.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
143                     String msg = "The GET assertion that docModel not be in 'deleted' workflow state failed.";
144                     logger.debug(msg);
145                     throw new DocumentNotFoundException(msg);
146                 }
147             }
148         }
149     }
150
151     /**
152      * create document in the Nuxeo repository
153      *
154      * @param ctx service context under which this method is invoked
155      * @param handler should be used by the caller to provide and transform the
156      * document
157      * @return id in repository of the newly created document
158      * @throws BadRequestException
159      * @throws TransactionException
160      * @throws DocumentException
161      */
162     @Override
163     public String create(ServiceContext ctx,
164             DocumentHandler handler) throws BadRequestException,
165             TransactionException, DocumentException {
166
167         String docType = NuxeoUtils.getTenantQualifiedDocType(ctx); //ctx.getDocumentType();
168         if (docType == null) {
169             throw new IllegalArgumentException(
170                     "RepositoryJavaClient.create: docType is missing");
171         }
172
173         if (handler == null) {
174             throw new IllegalArgumentException(
175                     "RepositoryJavaClient.create: handler is missing");
176         }
177         String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
178         if (nuxeoWspaceId == null) {
179             throw new DocumentNotFoundException(
180                     "Unable to find workspace for service " + ctx.getServiceName()
181                     + " check if the workspace exists in the Nuxeo repository");
182         }
183
184         CoreSessionInterface repoSession = null;
185         try {
186             handler.prepare(Action.CREATE);
187             repoSession = getRepositorySession(ctx);
188             DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);
189             DocumentModel wspaceDoc = repoSession.getDocument(nuxeoWspace);
190             String wspacePath = wspaceDoc.getPathAsString();
191             //give our own ID so PathRef could be constructed later on
192             String id = IdUtils.generateId(UUID.randomUUID().toString());
193             // create document model
194             DocumentModel doc = repoSession.createDocumentModel(wspacePath, id, docType);
195             /* Check for a versioned document, and check In and Out before we proceed.
196              * This does not work as we do not have the uid schema on our docs.
197              if(((DocumentModelHandler) handler).supportsVersioning()) {
198              doc.setProperty("uid","major_version",1);
199              doc.setProperty("uid","minor_version",0);
200              }
201              */
202             ((DocumentModelHandler) handler).setRepositorySession(repoSession);
203             DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
204             handler.handle(Action.CREATE, wrapDoc);
205             // create document with documentmodel
206             doc = repoSession.createDocument(doc);
207             repoSession.save();
208 // TODO for sub-docs need to call into the handler to let it deal with subitems. Pass in the id,
209 // and assume the handler has the state it needs (doc fragments). 
210             handler.complete(Action.CREATE, wrapDoc);
211             return id;
212         } catch (BadRequestException bre) {
213             throw bre;
214         } catch (Exception e) {
215             throw new NuxeoDocumentException(e);
216         } finally {
217             if (repoSession != null) {
218                 releaseRepositorySession(ctx, repoSession);
219             }
220         }
221
222     }
223     
224
225     @Override
226     public boolean reindex(DocumentHandler handler, String indexid) throws DocumentNotFoundException, DocumentException
227     {
228         return reindex(handler, null, indexid);
229     }
230     
231     @Override
232     public boolean reindex(DocumentHandler handler, String csid, String indexid) throws DocumentNotFoundException, DocumentException
233     {
234         boolean result = true;
235         CoreSessionInterface repoSession = null;
236         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = handler.getServiceContext();
237         
238         try {
239             String queryString = handler.getDocumentsToIndexQuery(indexid, csid);
240             repoSession = getRepositorySession(ctx);
241             CSReindexFulltextRoot indexer = new CSReindexFulltextRoot(repoSession);
242             indexer.reindexFulltext(0, 0, queryString);
243             //
244             // Set repository session to handle the document
245             //
246         } catch (Exception e) {
247             if (logger.isDebugEnabled()) {
248                 logger.debug("Caught exception ", e);
249             }
250             throw new NuxeoDocumentException(e);
251         } finally {
252             if (repoSession != null) {
253                 releaseRepositorySession(ctx, repoSession);
254             }
255         }
256         
257         return result;
258     }
259     
260     @Override
261     public void synchronize(ServiceContext ctx, Specifier specifier, DocumentHandler handler)
262             throws DocumentNotFoundException, TransactionException, DocumentException {
263
264         if (handler == null) {
265             throw new IllegalArgumentException(
266                     "RepositoryJavaClient.get: handler is missing");
267         }
268
269         CoreSessionInterface repoSession = null;
270         try {
271             handler.prepare(Action.SYNC);
272             repoSession = getRepositorySession(ctx);
273             ((DocumentModelHandler) handler).setRepositorySession(repoSession);
274             DocumentWrapper<Specifier> wrapDoc = new DocumentWrapperImpl<Specifier>(specifier);
275             handler.handle(Action.SYNC, wrapDoc);
276             handler.complete(Action.SYNC, wrapDoc);
277         } catch (IllegalArgumentException iae) {
278             throw iae;
279         } catch (DocumentException de) {
280             throw de;
281         } catch (Exception e) {
282             if (logger.isDebugEnabled()) {
283                 logger.debug("Caught exception ", e);
284             }
285             throw new NuxeoDocumentException(e);
286         } finally {
287             if (repoSession != null) {
288                 releaseRepositorySession(ctx, repoSession);
289             }
290         }
291     }
292     
293     /**
294      * get document from the Nuxeo repository
295      *
296      * @param ctx service context under which this method is invoked
297      * @param id of the document to retrieve
298      * @param handler should be used by the caller to provide and transform the
299      * document
300      * @throws DocumentNotFoundException if the document cannot be found in the
301      * repository
302      * @throws TransactionException
303      * @throws DocumentException
304      */
305     @Override
306     public void get(ServiceContext ctx, String id, DocumentHandler handler)
307             throws DocumentNotFoundException, TransactionException, DocumentException {
308
309         if (handler == null) {
310             throw new IllegalArgumentException(
311                     "RepositoryJavaClient.get: handler is missing");
312         }
313
314         CoreSessionInterface repoSession = null;
315         try {
316             handler.prepare(Action.GET);
317             repoSession = getRepositorySession(ctx);
318             DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
319             DocumentModel docModel = null;
320             try {
321                 docModel = repoSession.getDocument(docRef);
322                 assertWorkflowState(ctx, docModel);
323             } catch (ClientException ce) {
324                 String msg = logException(ce, "Could not find document with CSID=" + id);
325                 throw new DocumentNotFoundException(msg, ce);
326             }
327             //
328             // Set repository session to handle the document
329             //
330             ((DocumentModelHandler) handler).setRepositorySession(repoSession);
331             DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(docModel);
332             handler.handle(Action.GET, wrapDoc);
333             handler.complete(Action.GET, wrapDoc);
334         } catch (IllegalArgumentException iae) {
335             throw iae;
336         } catch (DocumentException de) {
337             throw de;
338         } catch (Exception e) {
339             if (logger.isDebugEnabled()) {
340                 logger.debug("Caught exception ", e);
341             }
342             throw new NuxeoDocumentException(e);
343         } finally {
344             if (repoSession != null) {
345                 releaseRepositorySession(ctx, repoSession);
346             }
347         }
348     }
349
350     /**
351      * get a document from the Nuxeo repository, using the docFilter params.
352      *
353      * @param ctx service context under which this method is invoked
354      * @param handler should be used by the caller to provide and transform the
355      * document. Handler must have a docFilter set to return a single item.
356      * @throws DocumentNotFoundException if the document cannot be found in the
357      * repository
358      * @throws TransactionException
359      * @throws DocumentException
360      */
361     @Override
362     public void get(ServiceContext ctx, DocumentHandler handler)
363             throws DocumentNotFoundException, TransactionException, DocumentException {
364         QueryContext queryContext = new QueryContext(ctx, handler);
365         CoreSessionInterface repoSession = null;
366
367         try {
368             handler.prepare(Action.GET);
369             repoSession = getRepositorySession(ctx);
370
371             DocumentModelList docList = null;
372             // force limit to 1, and ignore totalSize
373             String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
374             docList = repoSession.query(query, null, 1, 0, false);
375             if (docList.size() != 1) {
376                 throw new DocumentNotFoundException("No document found matching filter params: " + query);
377             }
378             DocumentModel doc = docList.get(0);
379
380             if (logger.isDebugEnabled()) {
381                 logger.debug("Executed NXQL query: " + query);
382             }
383
384             //set reposession to handle the document
385             ((DocumentModelHandler) handler).setRepositorySession(repoSession);
386             DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
387             handler.handle(Action.GET, wrapDoc);
388             handler.complete(Action.GET, wrapDoc);
389         } catch (IllegalArgumentException iae) {
390             throw iae;
391         } catch (DocumentException de) {
392             throw de;
393         } catch (Exception e) {
394             if (logger.isDebugEnabled()) {
395                 logger.debug("Caught exception ", e);
396             }
397             throw new NuxeoDocumentException(e);
398         } finally {
399             if (repoSession != null) {
400                 releaseRepositorySession(ctx, repoSession);
401             }
402         }
403     }
404
405     public DocumentWrapper<DocumentModel> getDoc(
406                 CoreSessionInterface repoSession,
407             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
408             String csid) throws DocumentNotFoundException, DocumentException {
409         DocumentWrapper<DocumentModel> wrapDoc = null;
410
411         try {
412             DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
413             DocumentModel doc = null;
414             try {
415                 doc = repoSession.getDocument(docRef);
416             } catch (ClientException ce) {
417                 String msg = logException(ce, "Could not find document with CSID=" + csid);
418                 throw new DocumentNotFoundException(msg, ce);
419             }
420             wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
421         } catch (IllegalArgumentException iae) {
422             throw iae;
423         } catch (DocumentException de) {
424             throw de;
425         }
426
427         return wrapDoc;
428     }
429
430     /**
431      * Get wrapped documentModel from the Nuxeo repository. The search is
432      * restricted to the workspace of the current context.
433      *
434      * @param ctx service context under which this method is invoked
435      * @param csid of the document to retrieve
436      * @throws DocumentNotFoundException
437      * @throws TransactionException
438      * @throws DocumentException
439      * @return a wrapped documentModel
440      */
441     @Override
442     public DocumentWrapper<DocumentModel> getDoc(
443             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
444             String csid) throws DocumentNotFoundException, TransactionException, DocumentException {
445         CoreSessionInterface repoSession = null;
446         DocumentWrapper<DocumentModel> wrapDoc = null;
447
448         try {
449             // Open a new repository session
450             repoSession = getRepositorySession(ctx);
451             wrapDoc = getDoc(repoSession, ctx, csid);
452         } catch (IllegalArgumentException iae) {
453             throw iae;
454         } catch (DocumentException de) {
455             throw de;
456         } catch (Exception e) {
457             if (logger.isDebugEnabled()) {
458                 logger.debug("Caught exception ", e);
459             }
460             throw new NuxeoDocumentException(e);
461         } finally {
462             if (repoSession != null) {
463                 releaseRepositorySession(ctx, repoSession);
464             }
465         }
466
467         if (logger.isWarnEnabled() == true) {
468             logger.warn("Returned DocumentModel instance was created with a repository session that is now closed.");
469         }
470         return wrapDoc;
471     }
472
473     public DocumentWrapper<DocumentModel> findDoc(
474                 CoreSessionInterface repoSession,
475             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
476             String whereClause)
477             throws DocumentNotFoundException, DocumentException {
478         DocumentWrapper<DocumentModel> wrapDoc = null;
479
480         try {
481             QueryContext queryContext = new QueryContext(ctx, whereClause);
482             DocumentModelList docList = null;
483             // force limit to 1, and ignore totalSize
484             String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
485             docList = repoSession.query(query,
486                     null, //Filter
487                     1, //limit
488                     0, //offset
489                     false); //countTotal
490             if (docList.size() != 1) {
491                 if (logger.isDebugEnabled()) {
492                     logger.debug("findDoc: Query found: " + docList.size() + " items.");
493                     logger.debug(" Query: " + query);
494                 }
495                 throw new DocumentNotFoundException("No document found matching filter params: " + query);
496             }
497             DocumentModel doc = docList.get(0);
498             wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
499         } catch (IllegalArgumentException iae) {
500             throw iae;
501         } catch (DocumentException de) {
502             throw de;
503         } catch (Exception e) {
504             if (logger.isDebugEnabled()) {
505                 logger.debug("Caught exception ", e);
506             }
507             throw new NuxeoDocumentException(e);
508         }
509
510         return wrapDoc;
511     }
512
513     /**
514      * find wrapped documentModel from the Nuxeo repository
515      *
516      * @param ctx service context under which this method is invoked
517      * @param whereClause where NXQL where clause to get the document
518      * @throws DocumentNotFoundException
519      * @throws TransactionException
520      * @throws DocumentException
521      * @return a wrapped documentModel retrieved by the repository query
522      */
523     @Override
524     public DocumentWrapper<DocumentModel> findDoc(
525             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
526             String whereClause)
527             throws DocumentNotFoundException, TransactionException, DocumentException {
528         CoreSessionInterface repoSession = null;
529         DocumentWrapper<DocumentModel> wrapDoc = null;
530
531         try {
532             repoSession = getRepositorySession(ctx);
533             wrapDoc = findDoc(repoSession, ctx, whereClause);
534         } catch (DocumentNotFoundException dnfe) {
535                 throw dnfe;
536         } catch (DocumentException de) {
537                 throw de;
538         } catch (Exception e) {
539                 if (repoSession == null) {
540                         throw new NuxeoDocumentException("Unable to create a Nuxeo repository session.", e);
541                 } else {
542                         throw new NuxeoDocumentException("Unexpected Nuxeo exception.", e);
543                 }
544         } finally {
545             if (repoSession != null) {
546                 releaseRepositorySession(ctx, repoSession);
547             }
548         }
549
550         if (logger.isWarnEnabled() == true) {
551             logger.warn("Returned DocumentModel instance was created with a repository session that is now closed.");
552         }
553
554         return wrapDoc;
555     }
556
557     /**
558      * find doc and return CSID from the Nuxeo repository
559      *
560      * @param repoSession
561      * @param ctx service context under which this method is invoked
562      * @param whereClause where NXQL where clause to get the document
563      * @throws DocumentNotFoundException
564      * @throws TransactionException
565      * @throws DocumentException
566      * @return the CollectionSpace ID (CSID) of the requested document
567      */
568     @Override
569     public String findDocCSID(CoreSessionInterface repoSession,
570             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String whereClause)
571             throws DocumentNotFoundException, TransactionException, DocumentException {
572         String csid = null;
573         boolean releaseSession = false;
574         try {
575             if (repoSession == null) {
576                 repoSession = this.getRepositorySession(ctx);
577                 releaseSession = true;
578             }
579             DocumentWrapper<DocumentModel> wrapDoc = findDoc(repoSession, ctx, whereClause);
580             DocumentModel docModel = wrapDoc.getWrappedObject();
581             csid = NuxeoUtils.getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
582         } catch (DocumentNotFoundException dnfe) {
583             throw dnfe;
584         } catch (IllegalArgumentException iae) {
585             throw iae;
586         } catch (DocumentException de) {
587             throw de;
588         } catch (Exception e) {
589             if (logger.isDebugEnabled()) {
590                 logger.debug("Caught exception ", e);
591             }
592             throw new NuxeoDocumentException(e);
593         } finally {
594             if (releaseSession && (repoSession != null)) {
595                 this.releaseRepositorySession(ctx, repoSession);
596             }
597         }
598         return csid;
599     }
600
601     public DocumentWrapper<DocumentModelList> findDocs(
602             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
603             CoreSessionInterface repoSession,
604             List<String> docTypes,
605             String whereClause,
606             String orderByClause,
607             int pageSize,
608             int pageNum,
609             boolean computeTotal)
610             throws DocumentNotFoundException, DocumentException {
611         DocumentWrapper<DocumentModelList> wrapDoc = null;
612
613         try {
614             if (docTypes == null || docTypes.size() < 1) {
615                 throw new DocumentNotFoundException(
616                         "The findDocs() method must specify at least one DocumentType.");
617             }
618             DocumentModelList docList = null;
619             QueryContext queryContext = new QueryContext(ctx, whereClause, orderByClause);
620             String query = NuxeoUtils.buildNXQLQuery(docTypes, queryContext);
621             if (logger.isDebugEnabled()) {
622                 logger.debug("findDocs() NXQL: " + query);
623             }
624             docList = repoSession.query(query, null, pageSize, pageSize * pageNum, computeTotal);
625             wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
626         } catch (IllegalArgumentException iae) {
627             throw iae;
628         } catch (Exception e) {
629             if (logger.isDebugEnabled()) {
630                 logger.debug("Caught exception ", e);
631             }
632             throw new NuxeoDocumentException(e);
633         }
634
635         return wrapDoc;
636     }
637
638     protected static String buildInListForDocTypes(List<String> docTypes) {
639         StringBuilder sb = new StringBuilder();
640         sb.append("(");
641         boolean first = true;
642         for (String docType : docTypes) {
643             if (first) {
644                 first = false;
645             } else {
646                 sb.append(",");
647             }
648             sb.append("'");
649             sb.append(docType);
650             sb.append("'");
651         }
652         sb.append(")");
653         return sb.toString();
654     }
655
656     public DocumentWrapper<DocumentModelList> findDocs(
657             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
658             DocumentHandler handler,
659             CoreSessionInterface repoSession,
660             List<String> docTypes)
661             throws DocumentNotFoundException, DocumentException {
662         DocumentWrapper<DocumentModelList> wrapDoc = null;
663
664         DocumentFilter filter = handler.getDocumentFilter();
665         String oldOrderBy = filter.getOrderByClause();
666         if (isClauseEmpty(oldOrderBy) == true) {
667             filter.setOrderByClause(DocumentFilter.ORDER_BY_LAST_UPDATED);
668         }
669         QueryContext queryContext = new QueryContext(ctx, handler);
670
671         try {
672             if (docTypes == null || docTypes.size() < 1) {
673                 throw new DocumentNotFoundException(
674                         "The findDocs() method must specify at least one DocumentType.");
675             }
676             DocumentModelList docList = null;
677             if (handler.isCMISQuery() == true) {
678                 String inList = buildInListForDocTypes(docTypes);
679                 ctx.getQueryParams().add(IQueryManager.SEARCH_RELATED_MATCH_OBJ_DOCTYPES, inList);
680                 docList = getFilteredCMIS(repoSession, ctx, handler, queryContext);
681             } else {
682                 String query = NuxeoUtils.buildNXQLQuery(docTypes, queryContext);
683                 if (logger.isDebugEnabled()) {
684                     logger.debug("findDocs() NXQL: " + query);
685                 }
686                 docList = repoSession.query(query, null, filter.getPageSize(), filter.getOffset(), true);
687             }
688             wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
689         } catch (IllegalArgumentException iae) {
690             throw iae;
691         } catch (Exception e) {
692             if (logger.isDebugEnabled()) {
693                 logger.debug("Caught exception ", e);
694             }
695             throw new NuxeoDocumentException(e);
696         }
697
698         return wrapDoc;
699     }
700
701     /**
702      * Find a list of documentModels from the Nuxeo repository
703      *
704      * @param docTypes a list of DocType names to match
705      * @param whereClause where the clause to qualify on
706      * @throws DocumentNotFoundException
707      * @throws TransactionException
708      * @throws DocumentException
709      * @return a list of documentModels
710      */
711     @Override
712     public DocumentWrapper<DocumentModelList> findDocs(
713             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
714             List<String> docTypes,
715             String whereClause,
716             int pageSize, int pageNum, boolean computeTotal)
717             throws DocumentNotFoundException, TransactionException, DocumentException {
718         CoreSessionInterface repoSession = null;
719         DocumentWrapper<DocumentModelList> wrapDoc = null;
720
721         try {
722             repoSession = getRepositorySession(ctx);
723             wrapDoc = findDocs(ctx, repoSession, docTypes, whereClause, null,
724                     pageSize, pageNum, computeTotal);
725         } catch (IllegalArgumentException iae) {
726             throw iae;
727         } catch (Exception e) {
728             if (logger.isDebugEnabled()) {
729                 logger.debug("Caught exception ", e);
730             }
731             throw new NuxeoDocumentException(e);
732         } finally {
733             if (repoSession != null) {
734                 releaseRepositorySession(ctx, repoSession);
735             }
736         }
737
738         if (logger.isWarnEnabled() == true) {
739             logger.warn("Returned DocumentModelList instance was created with a repository session that is now closed.");
740         }
741
742         return wrapDoc;
743     }
744
745     /* (non-Javadoc)
746      * @see org.collectionspace.services.common.storage.StorageClient#get(org.collectionspace.services.common.context.ServiceContext, java.util.List, org.collectionspace.services.common.document.DocumentHandler)
747      */
748     @Override
749     public void get(ServiceContext ctx, List<String> csidList, DocumentHandler handler)
750             throws DocumentNotFoundException, TransactionException, DocumentException {
751         if (handler == null) {
752             throw new IllegalArgumentException(
753                     "RepositoryJavaClient.getAll: handler is missing");
754         }
755
756         CoreSessionInterface repoSession = null;
757         try {
758             handler.prepare(Action.GET_ALL);
759             repoSession = getRepositorySession(ctx);
760             DocumentModelList docModelList = new DocumentModelListImpl();
761             //FIXME: Should be using NuxeoUtils.createPathRef for security reasons
762             for (String csid : csidList) {
763                 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
764                 DocumentModel docModel = repoSession.getDocument(docRef);
765                 docModelList.add(docModel);
766             }
767
768             //set reposession to handle the document
769             ((DocumentModelHandler) handler).setRepositorySession(repoSession);
770             DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docModelList);
771             handler.handle(Action.GET_ALL, wrapDoc);
772             handler.complete(Action.GET_ALL, wrapDoc);
773         } catch (DocumentException de) {
774             throw de;
775         } catch (Exception e) {
776             if (logger.isDebugEnabled()) {
777                 logger.debug("Caught exception ", e);
778             }
779             throw new NuxeoDocumentException(e);
780         } finally {
781             if (repoSession != null) {
782                 releaseRepositorySession(ctx, repoSession);
783             }
784         }
785     }
786
787     /**
788      * getAll get all documents for an entity entity service from the Nuxeo
789      * repository
790      *
791      * @param ctx service context under which this method is invoked
792      * @param handler should be used by the caller to provide and transform the
793      * document
794      * @throws DocumentNotFoundException
795      * @throws TransactionException
796      * @throws DocumentException
797      */
798     @Override
799     public void getAll(ServiceContext ctx, DocumentHandler handler)
800             throws DocumentNotFoundException, TransactionException, DocumentException {
801         if (handler == null) {
802             throw new IllegalArgumentException(
803                     "RepositoryJavaClient.getAll: handler is missing");
804         }
805         String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
806         if (nuxeoWspaceId == null) {
807             throw new DocumentNotFoundException(
808                     "Unable to find workspace for service "
809                     + ctx.getServiceName()
810                     + " check if the workspace exists in the Nuxeo repository.");
811         }
812
813         CoreSessionInterface repoSession = null;
814         try {
815             handler.prepare(Action.GET_ALL);
816             repoSession = getRepositorySession(ctx);
817             DocumentRef wsDocRef = new IdRef(nuxeoWspaceId);
818             DocumentModelList docList = repoSession.getChildren(wsDocRef);
819             //set reposession to handle the document
820             ((DocumentModelHandler) handler).setRepositorySession(repoSession);
821             DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
822             handler.handle(Action.GET_ALL, wrapDoc);
823             handler.complete(Action.GET_ALL, wrapDoc);
824         } catch (DocumentException de) {
825             throw de;
826         } catch (Exception e) {
827             if (logger.isDebugEnabled()) {
828                 logger.debug("Caught exception ", e);
829             }
830             throw new NuxeoDocumentException(e);
831         } finally {
832             if (repoSession != null) {
833                 releaseRepositorySession(ctx, repoSession);
834             }
835         }
836     }
837
838     private boolean isClauseEmpty(String theString) {
839         boolean result = true;
840         if (theString != null && !theString.isEmpty()) {
841             result = false;
842         }
843         return result;
844     }
845
846     public DocumentWrapper<DocumentModel> getDocFromCsid(
847             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
848             CoreSessionInterface repoSession,
849             String csid)
850             throws Exception {
851         DocumentWrapper<DocumentModel> result = null;
852
853         result = new DocumentWrapperImpl<DocumentModel>(NuxeoUtils.getDocFromCsid(ctx, repoSession, csid));
854
855         return result;
856     }
857
858     /*
859      * A method to find a CollectionSpace document (of any type) given just a service context and
860      * its CSID.  A search across *all* service workspaces (within a given tenant context) is performed to find
861      * the document
862      * 
863      * This query searches Nuxeo's Hierarchy table where our CSIDs are stored in the "name" column.
864      */
865     @Override
866     public DocumentWrapper<DocumentModel> getDocFromCsid(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
867             String csid)
868             throws Exception {
869         DocumentWrapper<DocumentModel> result = null;
870         CoreSessionInterface repoSession = null;
871         try {
872             repoSession = getRepositorySession(ctx);
873             result = getDocFromCsid(ctx, repoSession, csid);
874         } finally {
875             if (repoSession != null) {
876                 releaseRepositorySession(ctx, repoSession);
877             }
878         }
879
880         if (logger.isWarnEnabled() == true) {
881             logger.warn("Returned DocumentModel instance was created with a repository session that is now closed.");
882         }
883
884         return result;
885     }
886
887     /**
888      * Returns a URI value for a document in the Nuxeo repository
889      *
890      * @param wrappedDoc a wrapped documentModel
891      * @throws ClientException
892      * @return a document URI
893      */
894     @Override
895     public String getDocURI(DocumentWrapper<DocumentModel> wrappedDoc) throws ClientException {
896         DocumentModel docModel = wrappedDoc.getWrappedObject();
897         String uri = (String) docModel.getProperty(CollectionSpaceClient.COLLECTIONSPACE_CORE_SCHEMA,
898                 CollectionSpaceClient.COLLECTIONSPACE_CORE_URI);
899         return uri;
900     }
901
902     /*
903      * See CSPACE-5036 - How to make CMISQL queries from Nuxeo
904      */
905     private IterableQueryResult makeCMISQLQuery(CoreSessionInterface repoSession, String query, QueryContext queryContext) throws DocumentException {
906         IterableQueryResult result = null;
907         /** Threshold over which temporary files are not kept in memory. */
908         final int THRESHOLD = 1024 * 1024;
909         
910         try {
911             logger.debug(String.format("Performing a CMIS query on Nuxeo repository named %s",
912                         repoSession.getRepositoryName()));
913
914             ThresholdOutputStreamFactory streamFactory = ThresholdOutputStreamFactory.newInstance(
915                     null, THRESHOLD, -1, false);
916             CallContextImpl callContext = new CallContextImpl(
917                     CallContext.BINDING_LOCAL,
918                     CmisVersion.CMIS_1_1,
919                     repoSession.getRepositoryName(),
920                     null, // ServletContext
921                     null, // HttpServletRequest
922                     null, // HttpServletResponse
923                     new NuxeoCmisServiceFactory(),
924                     streamFactory);
925             callContext.put(CallContext.USERNAME, repoSession.getPrincipal().getName());
926             
927             NuxeoCmisService cmisService = new NuxeoCmisService(repoSession.getCoreSession());
928             result = repoSession.queryAndFetch(query, "CMISQL", cmisService);
929         } catch (ClientException e) {
930             // TODO Auto-generated catch block
931             logger.error("Encounter trouble making the following CMIS query: " + query, e);
932             throw new NuxeoDocumentException(e);
933         }
934
935         return result;
936     }
937
938     /**
939      * getFiltered get all documents for an entity service from the Document
940      * repository, given filter parameters specified by the handler.
941      *
942      * @param ctx service context under which this method is invoked
943      * @param handler should be used by the caller to provide and transform the
944      * document
945      * @throws DocumentNotFoundException if workspace not found
946      * @throws TransactionException
947      * @throws DocumentException
948      */
949     @Override
950     public void getFiltered(ServiceContext ctx, DocumentHandler handler)
951             throws DocumentNotFoundException, TransactionException, DocumentException {
952
953         DocumentFilter filter = handler.getDocumentFilter();
954         String oldOrderBy = filter.getOrderByClause();
955         if (isClauseEmpty(oldOrderBy) == true) {
956             filter.setOrderByClause(DocumentFilter.ORDER_BY_LAST_UPDATED);
957         }
958         QueryContext queryContext = new QueryContext(ctx, handler);
959
960         CoreSessionInterface repoSession = null;
961         try {
962             handler.prepare(Action.GET_ALL);
963             repoSession = getRepositorySession(ctx); //Keeps a refcount here for the repository session so you need to release this when finished
964
965             DocumentModelList docList = null;
966             // JDBC query
967             if (handler.isJDBCQuery() == true) {
968                 docList = getFilteredJDBC(repoSession, ctx, handler);
969             // CMIS query
970             } else if (handler.isCMISQuery() == true) {
971                 docList = getFilteredCMIS(repoSession, ctx, handler, queryContext); //FIXME: REM - Need to deal with paging info in CMIS query
972             // NXQL query
973             } else {
974                 String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
975                 if (logger.isDebugEnabled()) {
976                     logger.debug("Executing NXQL query: " + query.toString());
977                 }
978                 Profiler profiler = new Profiler(this, 2);
979                 profiler.log("Executing NXQL query: " + query.toString());
980                 profiler.start();
981                 // If we have a page size and/or offset, then reflect those values
982                 // when constructing the query, and also pass 'true' to get totalSize
983                 // in the returned DocumentModelList.
984                 if ((queryContext.getDocFilter().getOffset() > 0) || (queryContext.getDocFilter().getPageSize() > 0)) {
985                     docList = repoSession.query(query, null,
986                             queryContext.getDocFilter().getPageSize(), queryContext.getDocFilter().getOffset(), true);
987                 } else {
988                     docList = repoSession.query(query);
989                 }
990                 profiler.stop();
991             }
992
993             //set repoSession to handle the document
994             ((DocumentModelHandler) handler).setRepositorySession(repoSession);
995             DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
996             handler.handle(Action.GET_ALL, wrapDoc);
997             handler.complete(Action.GET_ALL, wrapDoc);
998         } catch (DocumentException de) {
999             throw de;
1000         } catch (Exception e) {
1001             if (logger.isDebugEnabled()) {
1002                 logger.debug("Caught exception ", e); // REM - 1/17/2014: Check for org.nuxeo.ecm.core.api.ClientException and re-attempt
1003             }
1004             throw new NuxeoDocumentException(e);
1005         } finally {
1006             if (repoSession != null) {
1007                 releaseRepositorySession(ctx, repoSession);
1008             }
1009         }
1010     }
1011
1012     /**
1013      * Perform a database query, via JDBC and SQL, to retrieve matching records
1014      * based on filter criteria.
1015      * 
1016      * Although this method currently has a general-purpose name, it is
1017      * currently dedicated to a specific task: that of improving performance
1018      * for partial term matching queries on authority items / terms, via
1019      * the use of a hand-tuned SQL query, rather than via the generated SQL
1020      * produced by Nuxeo from an NXQL query.  (See CSPACE-6361 for a task
1021      * to generalize this method.)
1022      * 
1023      * @param repoSession a repository session.
1024      * @param ctx the service context.
1025      * @param handler a relevant document handler.
1026      * @return a list of document models matching the search criteria.
1027      * @throws Exception 
1028      */
1029     private DocumentModelList getFilteredJDBC(CoreSessionInterface repoSession, ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, 
1030             DocumentHandler handler) throws Exception {
1031         DocumentModelList result = new DocumentModelListImpl();
1032
1033         // FIXME: Get all of the following values from appropriate external constants.
1034         //
1035         // At present, the two constants below are duplicated in both RepositoryClientImpl
1036         // and in AuthorityItemDocumentModelHandler.
1037         final String TERM_GROUP_LIST_NAME = "TERM_GROUP_LIST_NAME";
1038         final String TERM_GROUP_TABLE_NAME_PARAM = "TERM_GROUP_TABLE_NAME";
1039         final String IN_AUTHORITY_PARAM = "IN_AUTHORITY";
1040         // Get this from a constant in AuthorityResource or equivalent
1041         final String PARENT_WILDCARD = "_ALL_";
1042         
1043         // Build two SQL statements, to be executed within a single transaction:
1044         // the first statement to control join order, and the second statement
1045         // representing the actual 'get filtered' query
1046         
1047         // Build the join control statement
1048         //
1049         // Per http://www.postgresql.org/docs/9.2/static/runtime-config-query.html#GUC-JOIN-COLLAPSE-LIMIT
1050         // "Setting [this value] to 1 prevents any reordering of explicit JOINs.
1051         // Thus, the explicit join order specified in the query will be the
1052         // actual order in which the relations are joined."
1053         // See CSPACE-5945 for further discussion of why this setting is needed.
1054         //
1055         // Adding this statement is commented out here for now.  It significantly
1056         // improved query performance for authority item / term queries where
1057         // large numbers of rows were retrieved, but appears to have resulted
1058         // in consistently slower-than-desired query performance where zero or
1059         // very few records were retrieved. See notes on CSPACE-5945. - ADR 2013-04-09
1060         // String joinControlSql = "SET LOCAL join_collapse_limit TO 1;";
1061         
1062         // Build the query statement
1063         //
1064         // Start with the default query
1065         String selectStatement =
1066                 "SELECT DISTINCT commonschema.id"
1067                 + " FROM " + handler.getServiceContext().getCommonPartLabel() + " commonschema";
1068         
1069         String joinClauses =
1070                 " INNER JOIN misc"
1071                 + "  ON misc.id = commonschema.id"
1072                 + " INNER JOIN hierarchy hierarchy_termgroup"
1073                 + "  ON hierarchy_termgroup.parentid = misc.id"
1074                 + " INNER JOIN "  + handler.getJDBCQueryParams().get(TERM_GROUP_TABLE_NAME_PARAM) + " termgroup"
1075                 + "  ON termgroup.id = hierarchy_termgroup.id ";
1076
1077         String whereClause;
1078         MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
1079         // Value for replaceable parameter 1 in the query
1080         String partialTerm = queryParams.getFirst(IQueryManager.SEARCH_TYPE_PARTIALTERM);
1081         // If the value of the partial term query parameter is blank ('pt='),
1082         // return all records, subject to restriction by any limit clause
1083         if (Tools.isBlank(partialTerm)) {
1084            whereClause = "";
1085         } else {
1086            // Otherwise, return records that match the supplied partial term
1087            whereClause =
1088                 " WHERE (termgroup.termdisplayname ILIKE ?)";
1089         }
1090         
1091         // At present, results are ordered in code, below, rather than in SQL,
1092         // and the orderByClause below is thus intentionally blank.
1093         //
1094         // To implement the orderByClause below in SQL; e.g. via
1095         // 'ORDER BY termgroup.termdisplayname', the relevant column
1096         // must be returned by the SELECT statement.
1097         String orderByClause = "";
1098         
1099         String limitClause;
1100         TenantBindingConfigReaderImpl tReader =
1101                 ServiceMain.getInstance().getTenantBindingConfigReader();
1102         TenantBindingType tenantBinding = tReader.getTenantBinding(ctx.getTenantId());
1103         String maxListItemsLimit = TenantBindingUtils.getPropertyValue(tenantBinding,
1104                 IQueryManager.MAX_LIST_ITEMS_RETURNED_LIMIT_ON_JDBC_QUERIES);
1105         limitClause =
1106                 " LIMIT " + getMaxItemsLimitOnJdbcQueries(maxListItemsLimit); // implicit int-to-String conversion
1107         
1108         // After building the individual parts of the query, set the values
1109         // of replaceable parameters that will be inserted into that query
1110         // and optionally add restrictions
1111         
1112         List<String> params = new ArrayList<>();
1113         
1114         if (Tools.notBlank(whereClause)) {
1115                         
1116             // Read tenant bindings configuration to determine whether
1117             // to automatically insert leading, as well as trailing, wildcards
1118             // into the term matching string.
1119             String usesStartingWildcard = TenantBindingUtils.getPropertyValue(tenantBinding,
1120                     IQueryManager.TENANT_USES_STARTING_WILDCARD_FOR_PARTIAL_TERM);
1121             // Handle user-provided leading wildcard characters, in the
1122             // configuration where a leading wildcard is not automatically inserted.
1123             // (The user-provided wildcard must be in the first, or "starting"
1124             // character position in the partial term value.)
1125             if (Tools.notBlank(usesStartingWildcard)) {
1126                 if (usesStartingWildcard.equalsIgnoreCase(Boolean.FALSE.toString())) {
1127                     partialTerm = handleProvidedStartingWildcard(partialTerm);
1128                     // Otherwise, in the configuration where a leading wildcard
1129                     // is usually automatically inserted, handle the cases where
1130                     // a user has entered an anchor character in the first position
1131                     // in the starting term value. In those cases, strip that
1132                     // anchor character and don't add a leading wildcard
1133                 } else {
1134                     if (partialTerm.startsWith(USER_SUPPLIED_ANCHOR_CHAR)) {
1135                         partialTerm = partialTerm.substring(1, partialTerm.length());
1136                         // Otherwise, automatically add a leading wildcard
1137                     } else {
1138                         partialTerm = JDBCTools.SQL_WILDCARD + partialTerm;
1139                     }
1140                 }
1141             }
1142             // Add SQL wildcards in the midst of the partial term match search
1143             // expression, whever user-supplied wildcards appear, except in the
1144             // first or last character positions of the search expression.
1145             partialTerm = subtituteWildcardsInPartialTerm(partialTerm);
1146
1147             // If a designated 'anchor character' is present as the last character
1148             // in the search expression, strip that character and don't add
1149             // a trailing wildcard
1150             int lastCharPos = partialTerm.length() - 1;
1151             if (partialTerm.endsWith(USER_SUPPLIED_ANCHOR_CHAR) && lastCharPos > 0) {
1152                     partialTerm = partialTerm.substring(0, lastCharPos);
1153             } else {
1154                 // Otherwise, automatically add a trailing wildcard
1155                 partialTerm = partialTerm + JDBCTools.SQL_WILDCARD;
1156             }
1157             params.add(partialTerm);
1158         }
1159         
1160         // Optionally add restrictions to the default query, based on variables
1161         // in the current request
1162         
1163         // Restrict the query to filter out deleted records, if requested
1164         String includeDeleted = queryParams.getFirst(WorkflowClient.WORKFLOW_QUERY_NONDELETED);
1165         if (includeDeleted != null && includeDeleted.equalsIgnoreCase(Boolean.FALSE.toString())) {
1166             whereClause = whereClause
1167                 + "  AND (misc.lifecyclestate <> '" + WorkflowClient.WORKFLOWSTATE_DELETED + "')";
1168         }
1169
1170         // If a particular authority is specified, restrict the query further
1171         // to return only records within that authority
1172         String inAuthorityValue = (String) handler.getJDBCQueryParams().get(IN_AUTHORITY_PARAM);
1173         if (Tools.notBlank(inAuthorityValue)) {
1174             // Handle the '_ALL_' case for inAuthority
1175             if (inAuthorityValue.equals(PARENT_WILDCARD)) {
1176                 // Add nothing to the query here if it should match within all authorities
1177             } else {
1178                 whereClause = whereClause
1179                     + "  AND (commonschema.inauthority = ?)";
1180                 params.add(inAuthorityValue); // Value for replaceable parameter 2 in the query
1181             }
1182         }
1183         
1184         // Restrict the query further to return only records pertaining to
1185         // the current tenant, unless:
1186         // * Data for this service, in this tenant, is stored in its own,
1187         //   separate repository, rather than being intermingled with other
1188         //   tenants' data in the default repository; or
1189         // * Restriction by tenant ID in JDBC queries has been disabled,
1190         //   via configuration for this tenant, 
1191         if (restrictJDBCQueryByTenantID(tenantBinding, ctx)) {
1192                 joinClauses = joinClauses
1193                     + " INNER JOIN collectionspace_core core"
1194                     + "  ON core.id = hierarchy_termgroup.parentid";
1195                 whereClause = whereClause
1196                     + "  AND (core.tenantid = ?)";
1197                 params.add(ctx.getTenantId()); // Value for replaceable parameter 3 in the query
1198         }
1199         
1200         // Piece together the SQL query from its parts
1201         String querySql = selectStatement + joinClauses + whereClause + orderByClause + limitClause;
1202         
1203         // Note: PostgreSQL 9.2 introduced a change that may improve performance
1204         // of certain queries using JDBC PreparedStatements.  See comments on
1205         // CSPACE-5943 for details.
1206         //
1207         // See a comment above for the reason that the joinControl SQL statement,
1208         // along with its corresponding prepared statement builder, is commented out for now.
1209         // PreparedStatementBuilder joinControlBuilder = new PreparedStatementBuilder(joinControlSql);
1210         PreparedStatementSimpleBuilder queryBuilder = new PreparedStatementSimpleBuilder(querySql, params);
1211         List<PreparedStatementBuilder> builders = new ArrayList<>();
1212         // builders.add(joinControlBuilder);
1213         builders.add(queryBuilder);
1214         String dataSourceName = JDBCTools.NUXEO_DATASOURCE_NAME;
1215         String repositoryName = ctx.getRepositoryName();
1216         final Boolean EXECUTE_WITHIN_TRANSACTION = true;
1217         Set<String> docIds = new HashSet<>();
1218         try {
1219                 String cspaceInstanceId = ServiceMain.getInstance().getCspaceInstanceId();
1220             List<CachedRowSet> resultsList = JDBCTools.executePreparedQueries(builders,
1221                 dataSourceName, repositoryName, cspaceInstanceId, EXECUTE_WITHIN_TRANSACTION);
1222
1223             // At least one set of results is expected, from the second prepared
1224             // statement to be executed.
1225             // If fewer results are returned, return an empty list of document models
1226             if (resultsList == null || resultsList.size() < 1) {
1227                 return result; // return an empty list of document models
1228             }
1229             // The join control query (if enabled - it is currently commented
1230             // out as per comments above) will not return results, so query results
1231             // will be the first set of results (rowSet) returned in the list
1232             CachedRowSet queryResults = resultsList.get(0);
1233             
1234             // If the result from executing the query is null or contains zero rows,
1235             // return an empty list of document models
1236             if (queryResults == null) {
1237                 return result; // return an empty list of document models
1238             }
1239             queryResults.last();
1240             if (queryResults.getRow() == 0) {
1241                 return result; // return an empty list of document models
1242             }
1243
1244             // Otherwise, get the document IDs from the results of the query
1245             String id;
1246             queryResults.beforeFirst();
1247             while (queryResults.next()) {
1248                 id = queryResults.getString(1);
1249                 if (Tools.notBlank(id)) {
1250                     docIds.add(id);
1251                 }
1252             }
1253         } catch (SQLException sqle) {
1254             logger.warn("Could not obtain document IDs via SQL query '" + querySql + "': " + sqle.getMessage());
1255             return result; // return an empty list of document models
1256         } 
1257
1258         // Get a list of document models, using the list of IDs obtained from the query
1259         //
1260         // FIXME: Check whether we have a 'get document models from list of CSIDs'
1261         // utility method like this, and if not, add this to the appropriate
1262         // framework class
1263         DocumentModel docModel;
1264         for (String docId : docIds) {
1265             docModel = NuxeoUtils.getDocumentModel(repoSession, docId);
1266             if (docModel == null) {
1267                 logger.warn("Could not obtain document model for document with ID " + docId);
1268             } else {
1269                 result.add(docModel);
1270             }
1271         }
1272         
1273         // Order the results
1274         final String COMMON_PART_SCHEMA = handler.getServiceContext().getCommonPartLabel();
1275         final String DISPLAY_NAME_XPATH =
1276                 "//" + handler.getJDBCQueryParams().get(TERM_GROUP_LIST_NAME) + "/[0]/termDisplayName";
1277         Collections.sort(result, new Comparator<DocumentModel>() {
1278             @Override
1279             public int compare(DocumentModel doc1, DocumentModel doc2) {
1280                 String termDisplayName1 = null;
1281                 String termDisplayName2 = null;
1282                 try {
1283                         termDisplayName1 = (String) NuxeoUtils.getXPathValue(doc1, COMMON_PART_SCHEMA, DISPLAY_NAME_XPATH);
1284                         termDisplayName2 = (String) NuxeoUtils.getXPathValue(doc2, COMMON_PART_SCHEMA, DISPLAY_NAME_XPATH);
1285                 } catch (NuxeoDocumentException e) {
1286                         throw new RuntimeException(e);  // We need to throw a RuntimeException because the compare() method of the Comparator interface does not support throwing an Exception
1287                 }
1288                 return termDisplayName1.compareToIgnoreCase(termDisplayName2);
1289             }
1290         });
1291
1292         return result;
1293     }
1294     
1295
1296     private DocumentModelList getFilteredCMIS(CoreSessionInterface repoSession, 
1297                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, DocumentHandler handler, QueryContext queryContext)
1298             throws DocumentNotFoundException, DocumentException {
1299
1300         DocumentModelList result = new DocumentModelListImpl();
1301         try {
1302             String query = handler.getCMISQuery(queryContext);
1303
1304             DocumentFilter docFilter = handler.getDocumentFilter();
1305             int pageSize = docFilter.getPageSize();
1306             int offset = docFilter.getOffset();
1307             if (logger.isDebugEnabled()) {
1308                 logger.debug("Executing CMIS query: " + query.toString()
1309                         + "with pageSize: " + pageSize + " at offset: " + offset);
1310             }
1311
1312             // If we have limit and/or offset, then pass true to get totalSize
1313             // in returned DocumentModelList.
1314             Profiler profiler = new Profiler(this, 2);
1315             profiler.log("Executing CMIS query: " + query.toString());
1316             profiler.start();
1317             //
1318             IterableQueryResult queryResult = makeCMISQLQuery(repoSession, query, queryContext);
1319             try {
1320                 int totalSize = (int) queryResult.size();
1321                 ((DocumentModelListImpl) result).setTotalSize(totalSize);
1322                 // Skip the rows before our offset
1323                 if (offset > 0) {
1324                     queryResult.skipTo(offset);
1325                 }
1326                 int nRows = 0;
1327                 for (Map<String, Serializable> row : queryResult) {
1328                     if (logger.isTraceEnabled()) {
1329                         logger.trace(" Hierarchy Table ID is:" + row.get(IQueryManager.CMIS_TARGET_NUXEO_ID)
1330                                 + " nuxeo:pathSegment is: " + row.get(IQueryManager.CMIS_TARGET_NAME));
1331                     }
1332                     String nuxeoId = (String) row.get(IQueryManager.CMIS_TARGET_NUXEO_ID);
1333                     DocumentModel docModel = NuxeoUtils.getDocumentModel(repoSession, nuxeoId);
1334                     result.add(docModel);
1335                     nRows++;
1336                     if (nRows >= pageSize && pageSize != 0) { // A page size of zero means that they want all of them
1337                         logger.debug("Got page full of items - quitting");
1338                         break;
1339                     }
1340                 }
1341             } finally {
1342                 queryResult.close();
1343             }
1344             //
1345             profiler.stop();
1346
1347         } catch (Exception e) {
1348             if (logger.isDebugEnabled()) {
1349                 logger.debug("Caught exception ", e);
1350             }
1351             throw new NuxeoDocumentException(e);
1352         }
1353
1354         //
1355         // Since we're not supporting paging yet for CMIS queries, we need to perform
1356         // a workaround for the paging information we return in our list of results
1357         //
1358         /*
1359          if (result != null) {
1360          docFilter.setStartPage(0);
1361          if (totalSize > docFilter.getPageSize()) {
1362          docFilter.setPageSize(totalSize);
1363          ((DocumentModelListImpl)result).setTotalSize(totalSize);
1364          }
1365          }
1366          */
1367
1368         return result;
1369     }
1370
1371     private String logException(Exception e, String msg) {
1372         String result = null;
1373
1374         String exceptionMessage = e.getMessage();
1375         exceptionMessage = exceptionMessage != null ? exceptionMessage : "<No details provided>";
1376         result = msg = msg + ". Caught exception:" + exceptionMessage;
1377
1378         if (logger.isTraceEnabled() == true) {
1379             logger.error(msg, e);
1380         } else {
1381             logger.error(msg);
1382         }
1383
1384         return result;
1385     }
1386
1387     /**
1388      * update given document in the Nuxeo repository
1389      *
1390      * @param ctx service context under which this method is invoked
1391      * @param csid of the document
1392      * @param handler should be used by the caller to provide and transform the
1393      * document
1394      * @throws BadRequestException
1395      * @throws DocumentNotFoundException
1396      * @throws TransactionException if the transaction times out or otherwise
1397      * cannot be successfully completed
1398      * @throws DocumentException
1399      */
1400     @Override
1401     public void update(ServiceContext ctx, String csid, DocumentHandler handler)
1402             throws BadRequestException, DocumentNotFoundException, TransactionException,
1403             DocumentException {
1404         if (handler == null) {
1405             throw new IllegalArgumentException(
1406                     "RepositoryJavaClient.update: document handler is missing.");
1407         }
1408
1409         CoreSessionInterface repoSession = null;
1410         try {
1411             handler.prepare(Action.UPDATE);
1412             repoSession = getRepositorySession(ctx);
1413             DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
1414             DocumentModel doc = null;
1415             try {
1416                 doc = repoSession.getDocument(docRef);
1417             } catch (ClientException ce) {
1418                 String msg = logException(ce, "Could not find document to update with CSID=" + csid);
1419                 throw new DocumentNotFoundException(msg, ce);
1420             }
1421             // Check for a versioned document, and check In and Out before we proceed.
1422             if (((DocumentModelHandler) handler).supportsVersioning()) {
1423                 /* Once we advance to 5.5 or later, we can add this. 
1424                  * See also https://jira.nuxeo.com/browse/NXP-8506
1425                  if(!doc.isVersionable()) {
1426                  throw new NuxeoDocumentException("Configuration for: "
1427                  +handler.getServiceContextPath()+" supports versioning, but Nuxeo config does not!");
1428                  }
1429                  */
1430                 /* Force a version number - Not working. Apparently we need to configure the uid schema??
1431                  if(doc.getProperty("uid","major_version") == null) {
1432                  doc.setProperty("uid","major_version",1);
1433                  }
1434                  if(doc.getProperty("uid","minor_version") == null) {
1435                  doc.setProperty("uid","minor_version",0);
1436                  }
1437                  */
1438                 doc.checkIn(VersioningOption.MINOR, null);
1439                 doc.checkOut();
1440             }
1441
1442             //
1443             // Set reposession to handle the document
1444             //
1445             ((DocumentModelHandler) handler).setRepositorySession(repoSession);
1446             DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
1447             handler.handle(Action.UPDATE, wrapDoc);
1448             repoSession.saveDocument(doc);
1449             repoSession.save();
1450             handler.complete(Action.UPDATE, wrapDoc);
1451         } catch (BadRequestException bre) {
1452             throw bre;
1453         } catch (DocumentException de) {
1454             throw de;
1455         } catch (CSWebApplicationException wae) {
1456             throw wae;
1457         } catch (Exception e) {
1458             throw new NuxeoDocumentException(e);
1459         } finally {
1460             if (repoSession != null) {
1461                 releaseRepositorySession(ctx, repoSession);
1462             }
1463         }
1464     }
1465
1466     /**
1467      * Save a documentModel to the Nuxeo repository.
1468      *
1469      * @param ctx service context under which this method is invoked
1470      * @param repoSession
1471      * @param docModel the document to save
1472      * @param fSaveSession if TRUE, will call CoreSessionInterface.save() to save
1473      * accumulated changes.
1474      * @throws ClientException
1475      * @throws DocumentException
1476      */
1477     public void saveDocWithoutHandlerProcessing(
1478             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
1479             CoreSessionInterface repoSession,
1480             DocumentModel docModel,
1481             boolean fSaveSession)
1482             throws ClientException, DocumentException {
1483
1484         try {
1485             repoSession.saveDocument(docModel);
1486             if (fSaveSession) {
1487                 repoSession.save();
1488             }
1489         } catch (ClientException ce) {
1490             throw ce;
1491         } catch (Exception e) {
1492             if (logger.isDebugEnabled()) {
1493                 logger.debug("Caught exception ", e);
1494             }
1495             throw new NuxeoDocumentException(e);
1496         }
1497     }
1498
1499     /**
1500      * Save a list of documentModels to the Nuxeo repository.
1501      *
1502      * @param ctx service context under which this method is invoked
1503      * @param repoSession a repository session
1504      * @param docModelList a list of document models
1505      * @param fSaveSession if TRUE, will call CoreSessionInterface.save() to save
1506      * accumulated changes.
1507      * @throws ClientException
1508      * @throws DocumentException
1509      */
1510     public void saveDocListWithoutHandlerProcessing(
1511             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
1512             CoreSessionInterface repoSession,
1513             DocumentModelList docList,
1514             boolean fSaveSession)
1515             throws ClientException, DocumentException {
1516         try {
1517             DocumentModel[] docModelArray = new DocumentModel[docList.size()];
1518             repoSession.saveDocuments(docList.toArray(docModelArray));
1519             if (fSaveSession) {
1520                 repoSession.save();
1521             }
1522         } catch (ClientException ce) {
1523             throw ce;
1524         } catch (Exception e) {
1525             logger.error("Caught exception ", e);
1526             throw new NuxeoDocumentException(e);
1527         }
1528     }
1529
1530     @Override
1531         public void deleteWithWhereClause(@SuppressWarnings("rawtypes") ServiceContext ctx, String whereClause, 
1532                         @SuppressWarnings("rawtypes") DocumentHandler handler) throws 
1533                         DocumentNotFoundException, DocumentException {
1534         if (ctx == null) {
1535             throw new IllegalArgumentException(
1536                     "delete(ctx, specifier): ctx is missing");
1537         }
1538         if (logger.isDebugEnabled()) {
1539             logger.debug("Deleting document with whereClause=" + whereClause);
1540         }
1541         
1542         DocumentWrapper<DocumentModel> foundDocWrapper = this.findDoc(ctx, whereClause);
1543         if (foundDocWrapper != null) {
1544                 DocumentModel docModel = foundDocWrapper.getWrappedObject();
1545                 String csid = docModel.getName();
1546                 this.delete(ctx, csid, handler);
1547         }
1548     }
1549     
1550     /**
1551      * delete a document from the Nuxeo repository
1552      *
1553      * @param ctx service context under which this method is invoked
1554      * @param id of the document
1555      * @throws DocumentException
1556      */
1557     @Override
1558     public void delete(ServiceContext ctx, String id, DocumentHandler handler) throws DocumentNotFoundException,
1559             DocumentException, TransactionException {
1560         if (ctx == null) {
1561             throw new IllegalArgumentException(
1562                     "delete(ctx, ix, handler): ctx is missing");
1563         }
1564         if (handler == null) {
1565             throw new IllegalArgumentException(
1566                     "delete(ctx, ix, handler): handler is missing");
1567         }
1568         if (logger.isDebugEnabled()) {
1569             logger.debug("Deleting document with CSID=" + id);
1570         }
1571         CoreSessionInterface repoSession = null;
1572         try {
1573             handler.prepare(Action.DELETE);
1574             repoSession = getRepositorySession(ctx);
1575             DocumentWrapper<DocumentModel> wrapDoc = null;
1576             try {
1577                 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
1578                 wrapDoc = new DocumentWrapperImpl<DocumentModel>(repoSession.getDocument(docRef));
1579                 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
1580                 handler.handle(Action.DELETE, wrapDoc);
1581                 repoSession.removeDocument(docRef);
1582             } catch (ClientException ce) {
1583                 String msg = logException(ce, "Could not find document to delete with CSID=" + id);
1584                 throw new DocumentNotFoundException(msg, ce);
1585             }
1586             repoSession.save();
1587             handler.complete(Action.DELETE, wrapDoc);
1588         } catch (DocumentException de) {
1589             throw de;
1590         } catch (Exception e) {
1591             if (logger.isDebugEnabled()) {
1592                 logger.debug("Caught exception ", e);
1593             }
1594             throw new NuxeoDocumentException(e);
1595         } finally {
1596             if (repoSession != null) {
1597                 releaseRepositorySession(ctx, repoSession);
1598             }
1599         }
1600     }
1601
1602     /* (non-Javadoc)
1603      * @see org.collectionspace.services.common.storage.StorageClient#delete(org.collectionspace.services.common.context.ServiceContext, java.lang.String, org.collectionspace.services.common.document.DocumentHandler)
1604      */
1605     @Override
1606     @Deprecated
1607     public void delete(@SuppressWarnings("rawtypes") ServiceContext ctx, String id)
1608             throws DocumentNotFoundException, DocumentException {
1609         throw new UnsupportedOperationException();
1610         // Use the other delete instead
1611     }
1612
1613     @Override
1614     public Hashtable<String, String> retrieveWorkspaceIds(RepositoryDomainType repoDomain) throws Exception {
1615         return NuxeoConnectorEmbedded.getInstance().retrieveWorkspaceIds(repoDomain);
1616     }
1617
1618     @Override
1619     public String createDomain(RepositoryDomainType repositoryDomain) throws Exception {
1620         CoreSessionInterface repoSession = null;
1621         String domainId = null;
1622         try {
1623             //
1624             // Open a connection to the domain's repo/db
1625             //
1626             String repoName = repositoryDomain.getRepositoryName();
1627             repoSession = getRepositorySession(repoName); // domainName=storageName=repoName=databaseName
1628             //
1629             // First create the top-level domain directory
1630             //
1631             String domainName = repositoryDomain.getStorageName();
1632             DocumentRef parentDocRef = new PathRef("/");
1633             DocumentModel parentDoc = repoSession.getDocument(parentDocRef);
1634             DocumentModel domainDoc = repoSession.createDocumentModel(parentDoc.getPathAsString(),
1635                     domainName, NUXEO_CORE_TYPE_DOMAIN);
1636             domainDoc.setPropertyValue("dc:title", domainName);
1637             domainDoc.setPropertyValue("dc:description", "A CollectionSpace domain "
1638                     + domainName);
1639             domainDoc = repoSession.createDocument(domainDoc);
1640             domainId = domainDoc.getId();
1641             repoSession.save();
1642             //
1643             // Next, create a "Workspaces" root directory to contain the workspace folders for the individual service documents
1644             //
1645             DocumentModel workspacesRoot = repoSession.createDocumentModel(domainDoc.getPathAsString(),
1646                     NuxeoUtils.Workspaces, NUXEO_CORE_TYPE_WORKSPACEROOT);
1647             workspacesRoot.setPropertyValue("dc:title", NuxeoUtils.Workspaces);
1648             workspacesRoot.setPropertyValue("dc:description", "A CollectionSpace workspaces directory for "
1649                     + domainDoc.getPathAsString());
1650             workspacesRoot = repoSession.createDocument(workspacesRoot);
1651             String workspacesRootId = workspacesRoot.getId();
1652             repoSession.save();
1653
1654             if (logger.isDebugEnabled()) {
1655                 logger.debug("Created tenant domain name=" + domainName
1656                         + " id=" + domainId + " "
1657                         + NuxeoUtils.Workspaces + " id=" + workspacesRootId);
1658                 logger.debug("Path to Domain: " + domainDoc.getPathAsString());
1659                 logger.debug("Path to Workspaces root: " + workspacesRoot.getPathAsString());
1660             }
1661         } catch (Exception e) {
1662             if (logger.isDebugEnabled()) {
1663                 logger.debug("Could not create tenant domain name=" + repositoryDomain.getStorageName() + " caught exception ", e);
1664             }
1665             throw e;
1666         } finally {
1667             if (repoSession != null) {
1668                 releaseRepositorySession(null, repoSession);
1669             }
1670         }
1671
1672         return domainId;
1673     }
1674
1675     @Override
1676     public String getDomainId(RepositoryDomainType repositoryDomain) throws Exception {
1677         String domainId = null;
1678         CoreSessionInterface repoSession = null;
1679
1680         String repoName = repositoryDomain.getRepositoryName();
1681         String domainStorageName = repositoryDomain.getStorageName();
1682         if (domainStorageName != null && !domainStorageName.isEmpty()) {
1683             try {
1684                 repoSession = getRepositorySession(repoName);
1685                 DocumentRef docRef = new PathRef("/" + domainStorageName);
1686                 DocumentModel domain = repoSession.getDocument(docRef);
1687                 domainId = domain.getId();
1688             } catch (Exception e) {
1689                 if (logger.isTraceEnabled()) {
1690                     logger.trace("Caught exception ", e);  // The document doesn't exist, this let's us know we need to create it
1691                 }
1692                 //there is no way to identify if document does not exist due to
1693                 //lack of typed exception for getDocument method
1694                 return null;
1695             } finally {
1696                 if (repoSession != null) {
1697                     releaseRepositorySession(null, repoSession);
1698                 }
1699             }
1700         }
1701
1702         return domainId;
1703     }
1704
1705     /*
1706      * Returns the workspaces root directory for a given domain.
1707      */
1708     private DocumentModel getWorkspacesRoot(CoreSessionInterface repoSession,
1709             String domainName) throws Exception {
1710         DocumentModel result = null;
1711
1712         String domainPath = "/" + domainName;
1713         DocumentRef parentDocRef = new PathRef(domainPath);
1714         DocumentModelList domainChildrenList = repoSession.getChildren(
1715                 parentDocRef);
1716         Iterator<DocumentModel> witer = domainChildrenList.iterator();
1717         while (witer.hasNext()) {
1718             DocumentModel childNode = witer.next();
1719             if (NuxeoUtils.Workspaces.equalsIgnoreCase(childNode.getName())) {
1720                 result = childNode;
1721                 logger.trace("Found workspaces directory at: " + result.getPathAsString());
1722                 break;
1723             }
1724         }
1725
1726         if (result == null) {
1727             throw new ClientException("Could not find workspace root directory in: "
1728                     + domainPath);
1729         }
1730
1731         return result;
1732     }
1733
1734     /* (non-Javadoc)
1735      * @see org.collectionspace.services.common.repository.RepositoryClient#createWorkspace(java.lang.String, java.lang.String)
1736      */
1737     @Override
1738     public String createWorkspace(RepositoryDomainType repositoryDomain, String workspaceName) throws Exception {
1739         CoreSessionInterface repoSession = null;
1740         String workspaceId = null;
1741         try {
1742             String repoName = repositoryDomain.getRepositoryName();
1743             repoSession = getRepositorySession(repoName);
1744
1745             String domainStorageName = repositoryDomain.getStorageName();
1746             DocumentModel parentDoc = getWorkspacesRoot(repoSession, domainStorageName);
1747             if (logger.isTraceEnabled()) {
1748                 for (String facet : parentDoc.getFacets()) {
1749                     logger.trace("Facet: " + facet);
1750                 }
1751             }
1752
1753             DocumentModel doc = repoSession.createDocumentModel(parentDoc.getPathAsString(),
1754                     workspaceName, NuxeoUtils.WORKSPACE_DOCUMENT_TYPE);
1755             doc.setPropertyValue("dc:title", workspaceName);
1756             doc.setPropertyValue("dc:description", "A CollectionSpace workspace for "
1757                     + workspaceName);
1758             doc = repoSession.createDocument(doc);
1759             workspaceId = doc.getId();
1760             repoSession.save();
1761             if (logger.isDebugEnabled()) {
1762                 logger.debug("Created workspace name=" + workspaceName
1763                         + " id=" + workspaceId);
1764             }
1765         } catch (Exception e) {
1766             if (logger.isDebugEnabled()) {
1767                 logger.debug("createWorkspace caught exception ", e);
1768             }
1769             throw e;
1770         } finally {
1771             if (repoSession != null) {
1772                 releaseRepositorySession(null, repoSession);
1773             }
1774         }
1775         return workspaceId;
1776     }
1777
1778     /* (non-Javadoc)
1779      * @see org.collectionspace.services.common.repository.RepositoryClient#getWorkspaceId(java.lang.String, java.lang.String)
1780      */
1781     @Override
1782     @Deprecated
1783     public String getWorkspaceId(String tenantDomain, String workspaceName) throws Exception {
1784         String workspaceId = null;
1785
1786         CoreSessionInterface repoSession = null;
1787         try {
1788             repoSession = getRepositorySession((ServiceContext<PoxPayloadIn, PoxPayloadOut>) null);
1789             DocumentRef docRef = new PathRef(
1790                     "/" + tenantDomain
1791                     + "/" + NuxeoUtils.Workspaces
1792                     + "/" + workspaceName);
1793             DocumentModel workspace = repoSession.getDocument(docRef);
1794             workspaceId = workspace.getId();
1795         } catch (DocumentException de) {
1796             throw de;
1797         } catch (Exception e) {
1798             if (logger.isDebugEnabled()) {
1799                 logger.debug("Caught exception ", e);
1800             }
1801             throw new NuxeoDocumentException(e);
1802         } finally {
1803             if (repoSession != null) {
1804                 releaseRepositorySession(null, repoSession);
1805             }
1806         }
1807
1808         return workspaceId;
1809     }
1810
1811     public CoreSessionInterface getRepositorySession(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {
1812         return getRepositorySession(ctx, ctx.getRepositoryName(), ctx.getTimeoutSecs());
1813     }
1814
1815     public CoreSessionInterface getRepositorySession(String repoName) throws Exception {
1816         return getRepositorySession(null, repoName, ServiceContext.DEFAULT_TX_TIMEOUT);
1817     }
1818
1819     /**
1820      * Gets the repository session. - Package access only. If the 'ctx' param is
1821      * null then the repo name must be non-mull and vice-versa
1822      *
1823      * @return the repository session
1824      * @throws Exception the exception
1825      */
1826     public CoreSessionInterface getRepositorySession(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
1827                 String repoName,
1828                 int timeoutSeconds) throws Exception {
1829         CoreSessionInterface repoSession = null;
1830
1831         Profiler profiler = new Profiler("getRepositorySession():", 2);
1832         profiler.start();
1833         //
1834         // To get a connection to the Nuxeo repo, we need either a valid ServiceContext instance or a repository name
1835         //
1836         if (ctx != null) {
1837             repoName = ctx.getRepositoryName(); // Notice we are overriding the passed in 'repoName' since we have a valid service context passed in to us
1838             repoSession = (CoreSessionInterface) ctx.getCurrentRepositorySession(); // Look to see if one exists in the context before creating one
1839         } else if (repoName == null || repoName.trim().isEmpty()) {
1840             String errMsg = String.format("We can't get a connection to the Nuxeo repo because the service context passed in was null and no repository name was passed in either.");
1841             logger.error(errMsg);
1842             throw new Exception(errMsg);
1843         }
1844         //
1845         // If we couldn't find a repoSession from the service context (or the context was null) then we need to create a new one using
1846         // just the repo name
1847         //
1848         if (repoSession == null) {
1849             NuxeoClientEmbedded client = NuxeoConnectorEmbedded.getInstance().getClient();
1850             repoSession = client.openRepository(repoName, timeoutSeconds);
1851         } else {
1852             if (logger.isDebugEnabled() == true) {
1853                 logger.warn("Reusing the current context's repository session.");
1854             }
1855         }
1856
1857         try {
1858                 if (logger.isTraceEnabled()) {
1859                     logger.trace("Testing call to getRepository() repository root: " + repoSession.getRootDocument());
1860                 }
1861         } catch (Throwable e) {
1862                 logger.trace("Test call to Nuxeo's getRepository() repository root failed", e);
1863         }
1864
1865         profiler.stop();
1866
1867         if (ctx != null) {
1868             ctx.setCurrentRepositorySession(repoSession); // For reusing, save the repository session in the current service context
1869         }
1870
1871         return repoSession;
1872     }
1873
1874     /**
1875      * Release repository session. - Package access only.
1876      *
1877      * @param repoSession the repo session
1878      */
1879     public void releaseRepositorySession(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, CoreSessionInterface repoSession) throws TransactionException {
1880         try {
1881             NuxeoClientEmbedded client = NuxeoConnectorEmbedded.getInstance().getClient();
1882             // release session
1883             if (ctx != null) {
1884                 ctx.clearCurrentRepositorySession(); //clear the current context of the now closed repo session
1885                 if (ctx.getCurrentRepositorySession() == null) {
1886                     client.releaseRepository(repoSession); //release the repo session if the service context's ref count is zeo.
1887                 }
1888             } else {
1889                 client.releaseRepository(repoSession); //repo session was acquired without a service context
1890             }
1891         } catch (TransactionRuntimeException tre) {
1892                 String causeMsg = null;
1893                 Throwable cause = tre.getCause();
1894                 if (cause != null) {
1895                         causeMsg = cause.getMessage();
1896                 }
1897                 
1898             TransactionException te; // a CollectionSpace specific tx exception
1899             if (causeMsg != null) {
1900                 te = new TransactionException(causeMsg, tre);
1901             } else {
1902                 te = new TransactionException(tre);
1903             }
1904             
1905             logger.error(te.getMessage(), tre); // Log the standard transaction exception message, plus an exception-specific stack trace
1906             throw te;
1907         } catch (Exception e) {
1908             logger.error("Could not close the repository session.", e);
1909             // no need to throw this service specific exception
1910         }
1911     }
1912
1913     @Override
1914     public void doWorkflowTransition(ServiceContext ctx, String id,
1915             DocumentHandler handler, TransitionDef transitionDef)
1916             throws BadRequestException, DocumentNotFoundException,
1917             DocumentException {
1918         // This is a placeholder for when we change the StorageClient interface to treat workflow transitions as 1st class operations like 'get', 'create', 'update, 'delete', etc
1919     }
1920
1921     private String handleProvidedStartingWildcard(String partialTerm) {
1922         if (Tools.notBlank(partialTerm)) {
1923             if (partialTerm.substring(0, 1).equals(USER_SUPPLIED_WILDCARD)) {
1924                 StringBuffer buffer = new StringBuffer(partialTerm);
1925                 buffer.setCharAt(0, JDBCTools.SQL_WILDCARD.charAt(0));
1926                 partialTerm = buffer.toString();
1927             }
1928         }
1929         return partialTerm;
1930     }
1931     
1932     /**
1933      * Replaces user-supplied wildcards with SQL wildcards, in a partial term
1934      * matching search expression.
1935      * 
1936      * The scope of this replacement excludes the beginning character
1937      * in that search expression, as that character is treated specially.
1938      * 
1939      * @param partialTerm
1940      * @return the partial term, with any user-supplied wildcards replaced
1941      * by SQL wildcards.
1942      */
1943     private String subtituteWildcardsInPartialTerm(String partialTerm) {
1944         if (Tools.isBlank(partialTerm)) {
1945             return partialTerm;
1946         }
1947         if (! partialTerm.contains(USER_SUPPLIED_WILDCARD)) {
1948             return partialTerm;
1949         }
1950         int len = partialTerm.length();
1951         // Partial term search expressions of 2 or fewer characters
1952         // currently aren't amenable to the use of wildcards
1953         if (len <= 2)  {
1954             logger.warn("Partial term match search expression of just 1-2 characters in length contains a user-supplied wildcard: " + partialTerm);
1955             logger.warn("Will handle that character as a literal value, rather than as a wildcard ...");
1956             return partialTerm;
1957         }
1958         return partialTerm.substring(0, 1) // first char
1959                 + partialTerm.substring(1, len).replaceAll(USER_SUPPLIED_WILDCARD_REGEX, JDBCTools.SQL_WILDCARD);
1960
1961     }
1962
1963     private int getMaxItemsLimitOnJdbcQueries(String maxListItemsLimit) {
1964         final int DEFAULT_ITEMS_LIMIT = 40;
1965         if (maxListItemsLimit == null) {
1966             return DEFAULT_ITEMS_LIMIT;
1967         }
1968         int itemsLimit;
1969         try {
1970             itemsLimit = Integer.parseInt(maxListItemsLimit);
1971             if (itemsLimit < 1) {
1972                 logger.warn("Value of configuration setting "
1973                         + IQueryManager.MAX_LIST_ITEMS_RETURNED_LIMIT_ON_JDBC_QUERIES
1974                         + " must be a positive integer; invalid current value is " + maxListItemsLimit);
1975                 logger.warn("Reverting to default value of " + DEFAULT_ITEMS_LIMIT);
1976                 itemsLimit = DEFAULT_ITEMS_LIMIT;
1977             }
1978         } catch (NumberFormatException nfe) {
1979             logger.warn("Value of configuration setting "
1980                         + IQueryManager.MAX_LIST_ITEMS_RETURNED_LIMIT_ON_JDBC_QUERIES
1981                         + " must be a positive integer; invalid current value is " + maxListItemsLimit);
1982             logger.warn("Reverting to default value of " + DEFAULT_ITEMS_LIMIT);
1983             itemsLimit = DEFAULT_ITEMS_LIMIT;
1984         }
1985         return itemsLimit;
1986     }
1987
1988     /**
1989      * Identifies whether a restriction on tenant ID - to return only records
1990      * pertaining to the current tenant - is required in a JDBC query.
1991      * 
1992      * @param tenantBinding a tenant binding configuration.
1993      * @param ctx a service context.
1994      * @return true if a restriction on tenant ID is required in the query;
1995      * false if a restriction is not required.
1996      */
1997     private boolean restrictJDBCQueryByTenantID(TenantBindingType tenantBinding, ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) {
1998         boolean restrict = true;
1999         // If data for the current service, in the current tenant, is isolated
2000         // within its own separate, per-tenant repository, as contrasted with
2001         // being intermingled with other tenants' data in the default repository,
2002         // no restriction on Tenant ID is required in the query.
2003         String repositoryDomainName = ConfigUtils.getRepositoryName(tenantBinding, ctx.getRepositoryDomainName());
2004         if (!(repositoryDomainName.equals(ConfigUtils.DEFAULT_NUXEO_REPOSITORY_NAME))) {
2005             restrict = false;
2006         }
2007         // If a configuration setting for this tenant identifies that JDBC
2008         // queries should not be restricted by tenant ID (perhaps because
2009         // there is always expected to be only one tenant's data present in
2010         // the system), no restriction on Tenant ID is required in the query.
2011         String queriesRestrictedByTenantId = TenantBindingUtils.getPropertyValue(tenantBinding,
2012                 IQueryManager.JDBC_QUERIES_ARE_TENANT_ID_RESTRICTED);
2013         if (Tools.notBlank(queriesRestrictedByTenantId) &&
2014                 queriesRestrictedByTenantId.equalsIgnoreCase(Boolean.FALSE.toString())) {
2015             restrict = false;
2016         }
2017         return restrict;
2018     }
2019 }