2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 package org.collectionspace.services.nuxeo.client.java;
20 import java.util.Hashtable;
21 import java.util.List;
23 import java.util.UUID;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import java.util.regex.PatternSyntaxException;
28 import javax.ws.rs.WebApplicationException;
29 import javax.ws.rs.core.MultivaluedMap;
31 import org.collectionspace.services.client.IQueryManager;
32 import org.collectionspace.services.client.PoxPayloadIn;
33 import org.collectionspace.services.client.PoxPayloadOut;
34 import org.collectionspace.services.client.workflow.WorkflowClient;
35 import org.collectionspace.services.common.context.ServiceContext;
36 import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils;
37 import org.collectionspace.services.common.query.QueryContext;
38 import org.collectionspace.services.common.repository.RepositoryClient;
39 import org.collectionspace.services.common.workflow.service.nuxeo.WorkflowDocumentModelHandler;
40 import org.collectionspace.services.common.profile.Profiler;
41 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
43 import org.collectionspace.services.common.document.BadRequestException;
44 import org.collectionspace.services.common.document.DocumentException;
45 import org.collectionspace.services.common.document.DocumentFilter;
46 import org.collectionspace.services.common.document.DocumentHandler;
47 import org.collectionspace.services.common.document.DocumentNotFoundException;
48 import org.collectionspace.services.common.document.DocumentHandler.Action;
49 import org.collectionspace.services.common.document.DocumentWrapper;
50 import org.collectionspace.services.common.document.DocumentWrapperImpl;
52 import org.nuxeo.common.utils.IdUtils;
53 import org.nuxeo.ecm.core.api.ClientException;
54 import org.nuxeo.ecm.core.api.DocumentModel;
55 import org.nuxeo.ecm.core.api.DocumentModelList;
56 import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
57 import org.nuxeo.ecm.core.api.DocumentRef;
58 import org.nuxeo.ecm.core.api.IdRef;
59 import org.nuxeo.ecm.core.api.PathRef;
60 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
61 import org.nuxeo.ecm.core.client.NuxeoClient;
62 import org.nuxeo.ecm.core.schema.SchemaManager;
63 import org.nuxeo.runtime.api.Framework;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
69 * RepositoryJavaClient is used to perform CRUD operations on documents in Nuxeo
70 * repository using Remote Java APIs. It uses @see DocumentHandler as IOHandler
73 * $LastChangedRevision: $ $LastChangedDate: $
75 public class RepositoryJavaClientImpl implements RepositoryClient<PoxPayloadIn, PoxPayloadOut> {
78 private final Logger logger = LoggerFactory.getLogger(RepositoryJavaClientImpl.class);
79 // private final Logger profilerLogger = LoggerFactory.getLogger("remperf");
80 // private String foo = Profiler.createLogger();
83 * Instantiates a new repository java client impl.
85 public RepositoryJavaClientImpl() {
90 public void assertWorkflowState(ServiceContext ctx,
91 DocumentModel docModel) throws DocumentNotFoundException, ClientException {
92 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
93 if (queryParams != null) {
95 // Look for the workflow "delete" query param and see if we need to assert that the
96 // docModel is in a non-deleted workflow state.
98 String currentState = docModel.getCurrentLifeCycleState();
99 String includeDeletedStr = queryParams.getFirst(WorkflowClient.WORKFLOW_QUERY_NONDELETED);
100 boolean includeDeleted = includeDeletedStr == null ? true : Boolean.parseBoolean(includeDeletedStr);
101 if (includeDeleted == false) {
103 // We don't wanted soft-deleted object, so throw an exception if this one is soft-deleted.
105 if (currentState.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
106 String msg = "GET assertion that docModel not be in 'deleted' workflow state failed.";
108 throw new DocumentNotFoundException(msg);
115 * create document in the Nuxeo repository
117 * @param ctx service context under which this method is invoked
119 * should be used by the caller to provide and transform the
121 * @return id in repository of the newly created document
122 * @throws DocumentException
125 public String create(ServiceContext ctx,
126 DocumentHandler handler) throws BadRequestException,
129 String docType = NuxeoUtils.getTenantQualifiedDocType(ctx); //ctx.getDocumentType();
130 if (docType == null) {
131 throw new IllegalArgumentException(
132 "RepositoryJavaClient.create: docType is missing");
135 if (handler == null) {
136 throw new IllegalArgumentException(
137 "RepositoryJavaClient.create: handler is missing");
139 String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
140 if (nuxeoWspaceId == null) {
141 throw new DocumentNotFoundException(
142 "Unable to find workspace for service " + ctx.getServiceName()
143 + " check if the workspace exists in the Nuxeo repository");
145 RepositoryInstance repoSession = null;
147 handler.prepare(Action.CREATE);
148 repoSession = getRepositorySession();
149 DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);
150 DocumentModel wspaceDoc = repoSession.getDocument(nuxeoWspace);
151 String wspacePath = wspaceDoc.getPathAsString();
152 //give our own ID so PathRef could be constructed later on
153 String id = IdUtils.generateId(UUID.randomUUID().toString());
154 // create document model
155 DocumentModel doc = repoSession.createDocumentModel(wspacePath, id, docType);
156 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
157 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
158 handler.handle(Action.CREATE, wrapDoc);
159 // create document with documentmodel
160 doc = repoSession.createDocument(doc);
162 // TODO for sub-docs need to call into the handler to let it deal with subitems. Pass in the id,
163 // and assume the handler has the state it needs (doc fragments).
164 handler.complete(Action.CREATE, wrapDoc);
166 } catch (BadRequestException bre) {
168 } catch (Exception e) {
169 if (logger.isDebugEnabled()) {
170 logger.debug("Caught exception ", e);
172 throw new DocumentException(e);
174 if (repoSession != null) {
175 releaseRepositorySession(repoSession);
182 * get document from the Nuxeo repository
183 * @param ctx service context under which this method is invoked
185 * of the document to retrieve
187 * should be used by the caller to provide and transform the
189 * @throws DocumentException
192 public void get(ServiceContext ctx, String id, DocumentHandler handler)
193 throws DocumentNotFoundException, DocumentException {
195 if (handler == null) {
196 throw new IllegalArgumentException(
197 "RepositoryJavaClient.get: handler is missing");
199 RepositoryInstance repoSession = null;
202 handler.prepare(Action.GET);
203 repoSession = getRepositorySession();
204 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
205 DocumentModel docModel = null;
207 docModel = repoSession.getDocument(docRef);
208 assertWorkflowState(ctx, docModel);
209 } catch (ClientException ce) {
210 String msg = "Could not find document with id=" + id;
211 logger.error(msg, ce);
212 throw new DocumentNotFoundException(msg, ce);
214 //set reposession to handle the document
215 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
216 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(docModel);
217 handler.handle(Action.GET, wrapDoc);
218 handler.complete(Action.GET, wrapDoc);
219 } catch (IllegalArgumentException iae) {
221 } catch (DocumentException de) {
223 } catch (Exception e) {
224 if (logger.isDebugEnabled()) {
225 logger.debug("Caught exception ", e);
227 throw new DocumentException(e);
229 if (repoSession != null) {
230 releaseRepositorySession(repoSession);
236 * get document from the Nuxeo repository, using the docFilter params.
237 * @param ctx service context under which this method is invoked
239 * should be used by the caller to provide and transform the
240 * document. Handler must have a docFilter set to return a single item.
241 * @throws DocumentException
244 public void get(ServiceContext ctx, DocumentHandler handler)
245 throws DocumentNotFoundException, DocumentException {
246 QueryContext queryContext = new QueryContext(ctx, handler);
247 RepositoryInstance repoSession = null;
250 handler.prepare(Action.GET);
251 repoSession = getRepositorySession();
253 DocumentModelList docList = null;
254 // force limit to 1, and ignore totalSize
255 String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
256 docList = repoSession.query(query, null, 1, 0, false);
257 if (docList.size() != 1) {
258 throw new DocumentNotFoundException("No document found matching filter params.");
260 DocumentModel doc = docList.get(0);
262 if (logger.isDebugEnabled()) {
263 logger.debug("Executed NXQL query: " + query);
266 //set reposession to handle the document
267 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
268 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
269 handler.handle(Action.GET, wrapDoc);
270 handler.complete(Action.GET, wrapDoc);
271 } catch (IllegalArgumentException iae) {
273 } catch (DocumentException de) {
275 } catch (Exception e) {
276 if (logger.isDebugEnabled()) {
277 logger.debug("Caught exception ", e);
279 throw new DocumentException(e);
281 if (repoSession != null) {
282 releaseRepositorySession(repoSession);
288 * Get wrapped documentModel from the Nuxeo repository. The search is restricted to the workspace
289 * of the current context.
291 * @param ctx service context under which this method is invoked
293 * of the document to retrieve
294 * @throws DocumentException
297 public DocumentWrapper<DocumentModel> getDoc(
298 ServiceContext ctx, String csid)
299 throws DocumentNotFoundException, DocumentException {
300 RepositoryInstance repoSession = null;
301 DocumentWrapper<DocumentModel> wrapDoc = null;
304 repoSession = getRepositorySession();
305 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
306 DocumentModel doc = null;
308 doc = repoSession.getDocument(docRef);
309 } catch (ClientException ce) {
310 String msg = "could not find document with id=" + csid;
311 logger.error(msg, ce);
312 throw new DocumentNotFoundException(msg, ce);
314 wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
315 } catch (IllegalArgumentException iae) {
317 } catch (DocumentException de) {
319 } catch (Exception e) {
320 if (logger.isDebugEnabled()) {
321 logger.debug("Caught exception ", e);
323 throw new DocumentException(e);
325 if (repoSession != null) {
326 releaseRepositorySession(repoSession);
333 * find wrapped documentModel from the Nuxeo repository
334 * @param ctx service context under which this method is invoked
335 * @param whereClause where NXQL where clause to get the document
336 * @throws DocumentException
339 public DocumentWrapper<DocumentModel> findDoc(
340 ServiceContext ctx, String whereClause)
341 throws DocumentNotFoundException, DocumentException {
342 RepositoryInstance repoSession = null;
343 DocumentWrapper<DocumentModel> wrapDoc = null;
346 QueryContext queryContext = new QueryContext(ctx, whereClause);
347 repoSession = getRepositorySession();
348 DocumentModelList docList = null;
349 // force limit to 1, and ignore totalSize
350 String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
351 docList = repoSession.query(query,
356 if (docList.size() != 1) {
357 if (logger.isDebugEnabled()) {
358 logger.debug("findDoc: Query found: " + docList.size() + " items.");
359 logger.debug(" Query: " + query);
361 throw new DocumentNotFoundException("No document found matching filter params.");
363 DocumentModel doc = docList.get(0);
364 wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
365 } catch (IllegalArgumentException iae) {
367 } catch (DocumentException de) {
369 } catch (Exception e) {
370 if (logger.isDebugEnabled()) {
371 logger.debug("Caught exception ", e);
373 throw new DocumentException(e);
375 if (repoSession != null) {
376 releaseRepositorySession(repoSession);
383 * find doc and return CSID from the Nuxeo repository
384 * @param ctx service context under which this method is invoked
385 * @param whereClause where NXQL where clause to get the document
386 * @throws DocumentException
389 public String findDocCSID(
390 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String whereClause)
391 throws DocumentNotFoundException, DocumentException {
394 DocumentWrapper<DocumentModel> wrapDoc = findDoc(ctx, whereClause);
395 DocumentModel docModel = wrapDoc.getWrappedObject();
396 csid = NuxeoUtils.getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
397 } catch (DocumentNotFoundException dnfe) {
399 } catch (IllegalArgumentException iae) {
401 } catch (DocumentException de) {
403 } catch (Exception e) {
404 if (logger.isDebugEnabled()) {
405 logger.debug("Caught exception ", e);
407 throw new DocumentException(e);
413 * Find a list of documentModels from the Nuxeo repository
414 * @param docTypes a list of DocType names to match
415 * @param whereClause where the clause to qualify on
419 public DocumentWrapper<DocumentModelList> findDocs(
420 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
421 List<String> docTypes,
423 int pageSize, int pageNum, boolean computeTotal)
424 throws DocumentNotFoundException, DocumentException {
425 RepositoryInstance repoSession = null;
426 DocumentWrapper<DocumentModelList> wrapDoc = null;
429 if (docTypes == null || docTypes.size() < 1) {
430 throw new DocumentNotFoundException(
431 "findDocs must specify at least one DocumentType.");
433 repoSession = getRepositorySession();
434 DocumentModelList docList = null;
435 // force limit to 1, and ignore totalSize
436 QueryContext queryContext = new QueryContext(ctx, whereClause);
437 String query = NuxeoUtils.buildNXQLQuery(docTypes, queryContext);
438 docList = repoSession.query(query, null, pageSize, pageNum, computeTotal);
439 wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
440 } catch (IllegalArgumentException iae) {
442 } catch (Exception e) {
443 if (logger.isDebugEnabled()) {
444 logger.debug("Caught exception ", e);
446 throw new DocumentException(e);
448 if (repoSession != null) {
449 releaseRepositorySession(repoSession);
456 * @see org.collectionspace.services.common.storage.StorageClient#get(org.collectionspace.services.common.context.ServiceContext, java.util.List, org.collectionspace.services.common.document.DocumentHandler)
459 public void get(ServiceContext ctx, List<String> csidList, DocumentHandler handler)
460 throws DocumentNotFoundException, DocumentException {
461 if (handler == null) {
462 throw new IllegalArgumentException(
463 "RepositoryJavaClient.getAll: handler is missing");
466 RepositoryInstance repoSession = null;
469 handler.prepare(Action.GET_ALL);
470 repoSession = getRepositorySession();
471 DocumentModelList docModelList = new DocumentModelListImpl();
472 //FIXME: Should be using NuxeoUtils.createPathRef for security reasons
473 for (String csid : csidList) {
474 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid);
475 DocumentModel docModel = repoSession.getDocument(docRef);
476 docModelList.add(docModel);
479 //set reposession to handle the document
480 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
481 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docModelList);
482 handler.handle(Action.GET_ALL, wrapDoc);
483 handler.complete(Action.GET_ALL, wrapDoc);
484 } catch (DocumentException de) {
486 } catch (Exception e) {
487 if (logger.isDebugEnabled()) {
488 logger.debug("Caught exception ", e);
490 throw new DocumentException(e);
492 if (repoSession != null) {
493 releaseRepositorySession(repoSession);
499 * getAll get all documents for an entity entity service from the Nuxeo
502 * @param ctx service context under which this method is invoked
504 * should be used by the caller to provide and transform the
506 * @throws DocumentException
509 public void getAll(ServiceContext ctx, DocumentHandler handler)
510 throws DocumentNotFoundException, DocumentException {
511 if (handler == null) {
512 throw new IllegalArgumentException(
513 "RepositoryJavaClient.getAll: handler is missing");
515 String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
516 if (nuxeoWspaceId == null) {
517 throw new DocumentNotFoundException(
518 "Unable to find workspace for service "
519 + ctx.getServiceName()
520 + " check if the workspace exists in the Nuxeo repository");
522 RepositoryInstance repoSession = null;
525 handler.prepare(Action.GET_ALL);
526 repoSession = getRepositorySession();
527 DocumentRef wsDocRef = new IdRef(nuxeoWspaceId);
528 DocumentModelList docList = repoSession.getChildren(wsDocRef);
529 //set reposession to handle the document
530 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
531 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
532 handler.handle(Action.GET_ALL, wrapDoc);
533 handler.complete(Action.GET_ALL, wrapDoc);
534 } catch (DocumentException de) {
536 } catch (Exception e) {
537 if (logger.isDebugEnabled()) {
538 logger.debug("Caught exception ", e);
540 throw new DocumentException(e);
542 if (repoSession != null) {
543 releaseRepositorySession(repoSession);
548 private boolean isClauseEmpty(String theString) {
549 boolean result = true;
550 if (theString != null && !theString.isEmpty()) {
557 * A method to find a CollectionSpace document (of any type) given just a service context and
558 * its CSID. A search across *all* service workspaces (within a given tenant context) is performed to find
561 * This query searches Nuxeo's Hierarchy table where our CSIDs are stored in the "name" column.
564 public DocumentWrapper<DocumentModel> getDocFromCsid(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
567 DocumentWrapper<DocumentModel> result = null;
568 RepositoryInstance repoSession = getRepositorySession();
570 result = new DocumentWrapperImpl(NuxeoUtils.getDocFromCsid(repoSession, ctx, csid));
572 if (repoSession != null) {
573 releaseRepositorySession(repoSession);
580 * find doc and return CSID from the Nuxeo repository
581 * @param ctx service context under which this method is invoked
582 * @param whereClause where NXQL where clause to get the document
583 * @throws DocumentException
586 public String getDocURI(DocumentWrapper<DocumentModel> wrappedDoc) throws ClientException {
587 DocumentModel docModel = wrappedDoc.getWrappedObject();
588 String uri = (String)docModel.getProperty(DocumentModelHandler.COLLECTIONSPACE_CORE_SCHEMA,
589 DocumentModelHandler.COLLECTIONSPACE_CORE_URI);
595 * getFiltered get all documents for an entity service from the Document repository,
596 * given filter parameters specified by the handler.
597 * @param ctx service context under which this method is invoked
598 * @param handler should be used by the caller to provide and transform the document
599 * @throws DocumentNotFoundException if workspace not found
600 * @throws DocumentException
603 public void getFiltered(ServiceContext ctx, DocumentHandler handler)
604 throws DocumentNotFoundException, DocumentException {
606 DocumentFilter filter = handler.getDocumentFilter();
607 String oldOrderBy = filter.getOrderByClause();
608 if (isClauseEmpty(oldOrderBy) == true){
609 filter.setOrderByClause(DocumentFilter.ORDER_BY_LAST_UPDATED); //per http://issues.collectionspace.org/browse/CSPACE-705
611 QueryContext queryContext = new QueryContext(ctx, handler);
612 RepositoryInstance repoSession = null;
614 handler.prepare(Action.GET_ALL);
615 repoSession = getRepositorySession();
616 DocumentModelList docList = null;
617 String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
619 if (logger.isDebugEnabled()) {
620 logger.debug("Executing NXQL query: " + query.toString());
623 // If we have limit and/or offset, then pass true to get totalSize
624 // in returned DocumentModelList.
625 Profiler profiler = new Profiler(this, 2);
626 profiler.log("Executing NXQL query: " + query.toString());
628 if ((queryContext.getDocFilter().getOffset() > 0) || (queryContext.getDocFilter().getPageSize() > 0)) {
629 docList = repoSession.query(query, null,
630 queryContext.getDocFilter().getPageSize(), queryContext.getDocFilter().getOffset(), true);
632 docList = repoSession.query(query);
636 //set repoSession to handle the document
637 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
638 DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
639 handler.handle(Action.GET_ALL, wrapDoc);
640 handler.complete(Action.GET_ALL, wrapDoc);
641 } catch (DocumentException de) {
643 } catch (Exception e) {
644 if (logger.isDebugEnabled()) {
645 logger.debug("Caught exception ", e);
647 throw new DocumentException(e);
649 if (repoSession != null) {
650 releaseRepositorySession(repoSession);
656 * update given document in the Nuxeo repository
658 * @param ctx service context under which this method is invoked
662 * should be used by the caller to provide and transform the
664 * @throws DocumentException
667 public void update(ServiceContext ctx, String id, DocumentHandler handler)
668 throws BadRequestException, DocumentNotFoundException,
670 if (handler == null) {
671 throw new IllegalArgumentException(
672 "RepositoryJavaClient.update: handler is missing");
674 RepositoryInstance repoSession = null;
676 handler.prepare(Action.UPDATE);
677 repoSession = getRepositorySession();
678 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
679 DocumentModel doc = null;
681 doc = repoSession.getDocument(docRef);
682 } catch (ClientException ce) {
683 String msg = "Could not find document to update with id=" + id;
684 logger.error(msg, ce);
685 throw new DocumentNotFoundException(msg, ce);
687 //set reposession to handle the document
688 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
689 DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
690 handler.handle(Action.UPDATE, wrapDoc);
691 repoSession.saveDocument(doc);
693 handler.complete(Action.UPDATE, wrapDoc);
694 } catch (BadRequestException bre) {
696 } catch (DocumentException de) {
698 } catch (WebApplicationException wae){
700 } catch (Exception e) {
701 if (logger.isDebugEnabled()) {
702 logger.debug("Caught exception ", e);
704 throw new DocumentException(e);
706 if (repoSession != null) {
707 releaseRepositorySession(repoSession);
713 * delete a document from the Nuxeo repository
714 * @param ctx service context under which this method is invoked
717 * @throws DocumentException
720 public void delete(ServiceContext ctx, String id) throws DocumentNotFoundException,
723 if (logger.isDebugEnabled()) {
724 logger.debug("deleting document with id=" + id);
726 RepositoryInstance repoSession = null;
728 repoSession = getRepositorySession();
729 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
731 repoSession.removeDocument(docRef);
732 } catch (ClientException ce) {
733 String msg = "could not find document to delete with id=" + id;
734 logger.error(msg, ce);
735 throw new DocumentNotFoundException(msg, ce);
738 } catch (DocumentException de) {
740 } catch (Exception e) {
741 if (logger.isDebugEnabled()) {
742 logger.debug("Caught exception ", e);
744 throw new DocumentException(e);
746 if (repoSession != null) {
747 releaseRepositorySession(repoSession);
753 * @see org.collectionspace.services.common.storage.StorageClient#delete(org.collectionspace.services.common.context.ServiceContext, java.lang.String, org.collectionspace.services.common.document.DocumentHandler)
756 public void delete(ServiceContext ctx, String id, DocumentHandler handler)
757 throws DocumentNotFoundException, DocumentException {
758 throw new UnsupportedOperationException();
762 public Hashtable<String, String> retrieveWorkspaceIds(String domainName) throws Exception {
763 return NuxeoConnector.getInstance().retrieveWorkspaceIds(domainName);
767 public String createDomain(String domainName) throws Exception {
768 RepositoryInstance repoSession = null;
769 String domainId = null;
771 repoSession = getRepositorySession();
772 DocumentRef parentDocRef = new PathRef("/");
773 DocumentModel parentDoc = repoSession.getDocument(parentDocRef);
774 DocumentModel doc = repoSession.createDocumentModel(parentDoc.getPathAsString(),
775 domainName, "Domain");
776 doc.setPropertyValue("dc:title", domainName);
777 doc.setPropertyValue("dc:description", "A CollectionSpace domain "
779 doc = repoSession.createDocument(doc);
780 domainId = doc.getId();
782 if (logger.isDebugEnabled()) {
783 logger.debug("created tenant domain name=" + domainName
784 + " id=" + domainId);
786 } catch (Exception e) {
787 if (logger.isDebugEnabled()) {
788 logger.debug("createTenantSpace caught exception ", e);
792 if (repoSession != null) {
793 releaseRepositorySession(repoSession);
800 public String getDomainId(String domainName) throws Exception {
801 String domainId = null;
802 RepositoryInstance repoSession = null;
804 if (domainName != null && !domainName.isEmpty()) {
806 repoSession = getRepositorySession();
807 DocumentRef docRef = new PathRef(
809 DocumentModel domain = repoSession.getDocument(docRef);
810 domainId = domain.getId();
811 } catch (Exception e) {
812 if (logger.isTraceEnabled()) {
813 logger.trace("Caught exception ", e);
815 //there is no way to identify if document does not exist due to
816 //lack of typed exception for getDocument method
819 if (repoSession != null) {
820 releaseRepositorySession(repoSession);
829 * @see org.collectionspace.services.common.repository.RepositoryClient#createWorkspace(java.lang.String, java.lang.String)
832 public String createWorkspace(String domainName, String workspaceName) throws Exception {
833 RepositoryInstance repoSession = null;
834 String workspaceId = null;
836 repoSession = getRepositorySession();
837 DocumentRef parentDocRef = new PathRef(
839 + "/" + NuxeoUtils.WORKSPACES);
840 DocumentModel parentDoc = repoSession.getDocument(parentDocRef);
841 DocumentModel doc = repoSession.createDocumentModel(parentDoc.getPathAsString(),
842 workspaceName, "Workspace");
843 doc.setPropertyValue("dc:title", workspaceName);
844 doc.setPropertyValue("dc:description", "A CollectionSpace workspace for "
846 doc = repoSession.createDocument(doc);
847 workspaceId = doc.getId();
849 if (logger.isDebugEnabled()) {
850 logger.debug("created workspace name=" + workspaceName
851 + " id=" + workspaceId);
853 } catch (Exception e) {
854 if (logger.isDebugEnabled()) {
855 logger.debug("createWorkspace caught exception ", e);
859 if (repoSession != null) {
860 releaseRepositorySession(repoSession);
867 * @see org.collectionspace.services.common.repository.RepositoryClient#getWorkspaceId(java.lang.String, java.lang.String)
870 public String getWorkspaceId(String tenantDomain, String workspaceName) throws Exception {
871 String workspaceId = null;
872 RepositoryInstance repoSession = null;
874 repoSession = getRepositorySession();
875 DocumentRef docRef = new PathRef(
877 + "/" + NuxeoUtils.WORKSPACES
878 + "/" + workspaceName);
879 DocumentModel workspace = repoSession.getDocument(docRef);
880 workspaceId = workspace.getId();
881 } catch (DocumentException de) {
883 } catch (Exception e) {
884 if (logger.isDebugEnabled()) {
885 logger.debug("Caught exception ", e);
887 throw new DocumentException(e);
889 if (repoSession != null) {
890 releaseRepositorySession(repoSession);
898 * Gets the repository session.
900 * @return the repository session
901 * @throws Exception the exception
903 private RepositoryInstance getRepositorySession() throws Exception {
904 // FIXME: is it possible to reuse repository session?
905 // Authentication failures happen while trying to reuse the session
906 Profiler profiler = new Profiler("getRepositorySession():", 2);
908 NuxeoClient client = NuxeoConnector.getInstance().getClient();
909 RepositoryInstance repoSession = client.openRepository();
910 if (logger.isTraceEnabled()) {
911 logger.debug("getRepository() repository root: " + repoSession.getRootDocument());
918 * Release repository session.
920 * @param repoSession the repo session
922 private void releaseRepositorySession(RepositoryInstance repoSession) {
924 NuxeoClient client = NuxeoConnector.getInstance().getClient();
926 client.releaseRepository(repoSession);
927 } catch (Exception e) {
928 logger.error("Could not close the repository session", e);
929 // no need to throw this service specific exception