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