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