From: Aron Roberts Date: Mon, 25 Mar 2013 21:57:32 +0000 (-0700) Subject: CSPACE-5943: SQL query now invokes JDBCTools routine rather than replicating SQL... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=8ad798d37a57f30de07f5759fbec40c7bdc8cc79;p=tmp%2Fjakarta-migration.git CSPACE-5943: SQL query now invokes JDBCTools routine rather than replicating SQL execution code in RepositoryJavaClientImpl, via the use of a CachedRowSet. IDs returned are still not the correct ones; simplified SQL script used for testing needs at least one more join, a la CSPACE-5945. --- diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java index 075c35097..d6da4bea9 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java @@ -337,13 +337,13 @@ public interface DocumentHandler { */ public String getCMISQuery(QueryContext queryContext); - /* - * Returns TRUE if a CMIS query should be used (instead of an NXQL) query + /** + * Returns TRUE if a CMIS query should be used (instead of an NXQL query) */ public boolean isCMISQuery(); - /* - * Returns TRUE if an SQL query should be used (instead of an NXQL) + /** + * Returns TRUE if a JDBC/SQL query should be used (instead of an NXQL query) */ - public boolean isJDBCQuery(); + public boolean isJDBCQuery(); } diff --git a/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java b/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java index 1ed4c4c5f..8f58bd635 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java @@ -35,6 +35,9 @@ import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; +import javax.sql.rowset.CachedRowSet; +import javax.sql.rowset.RowSetFactory; +import javax.sql.rowset.RowSetProvider; /** * User: laramie @@ -164,22 +167,23 @@ public class JDBCTools { return result; } - - // Regarding the method below, we might instead identify whether we can - // return a CachedRowSet or equivalent. - // http://docs.oracle.com/javase/1.5.0/docs/api/javax/sql/rowset/CachedRowSet.html - // -- ADR 2012-12-06 - /* THIS IS BROKEN - If you close the statement, it closes the ResultSet!!! - public static ResultSet executeQuery(String repoName, String sql) throws Exception { + public static CachedRowSet executeQuery(String dataSourceName, String repositoryName, String sql) throws Exception { Connection conn = null; Statement stmt = null; try { - conn = getConnection(repoName); // If null, uses default + conn = getConnection(dataSourceName, repositoryName); stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery(sql); - stmt.close(); - return rs; //don't call rs.close() here ... Let caller close and catch any exceptions. + + RowSetFactory rowSetFactory = RowSetProvider.newFactory(); + CachedRowSet crs = rowSetFactory.createCachedRowSet(); + + stmt = conn.createStatement(); + try (ResultSet resultSet = stmt.executeQuery(sql)) { + crs.populate(resultSet); + } + stmt.close(); // also closes resultSet + return crs; } catch (SQLException sqle) { SQLException tempException = sqle; while (null != tempException) { // SQLExceptions can be chained. Loop to log all. @@ -200,7 +204,7 @@ public class JDBCTools { return null; } } - } */ + } public static int executeUpdate(String dataSourceName, String repositoryName, String sql) throws Exception { Connection conn = null; diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java index 4bde46cf5..8a8196853 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java @@ -1,25 +1,25 @@ /** - * This document is a part of the source code and related artifacts - * for CollectionSpace, an open source collections management system - * for museums and related institutions: - - * http://www.collectionspace.org - * http://wiki.collectionspace.org - - * Copyright 2009 University of California at Berkeley - - * Licensed under the Educational Community License (ECL), Version 2.0. - * You may not use this file except in compliance with this License. - - * You may obtain a copy of the ECL 2.0 License at - - * https://source.collectionspace.org/collection-space/LICENSE.txt + * This document is a part of the source code and related artifacts for + * CollectionSpace, an open source collections management system for museums and + * related institutions: + * + * http://www.collectionspace.org http://wiki.collectionspace.org + * + * Copyright 2009 University of California at Berkeley + * + * Licensed under the Educational Community License (ECL), Version 2.0. You may + * not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * + * https://source.collectionspace.org/collection-space/LICENSE.txt */ package org.collectionspace.services.nuxeo.client.java; import java.io.Serializable; import java.sql.Connection; import java.sql.ResultSet; +import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Hashtable; @@ -27,6 +27,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.UUID; +import javax.sql.rowset.CachedRowSet; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MultivaluedMap; @@ -82,60 +83,61 @@ import org.slf4j.LoggerFactory; /** * RepositoryJavaClient is used to perform CRUD operations on documents in Nuxeo - * repository using Remote Java APIs. It uses @see DocumentHandler as IOHandler - * with the client. - * + * repository using Remote Java APIs. It uses + * + * @see DocumentHandler as IOHandler with the client. + * * $LastChangedRevision: $ $LastChangedDate: $ */ public class RepositoryJavaClientImpl implements RepositoryClient { - /** The logger. */ + /** + * The logger. + */ private final Logger logger = LoggerFactory.getLogger(RepositoryJavaClientImpl.class); // private final Logger profilerLogger = LoggerFactory.getLogger("remperf"); // private String foo = Profiler.createLogger(); - public static final String NUXEO_CORE_TYPE_DOMAIN = "Domain"; public static final String NUXEO_CORE_TYPE_WORKSPACEROOT = "WorkspaceRoot"; - + private static final String ID_COLUMN_NAME = "id"; + /** * Instantiates a new repository java client impl. */ public RepositoryJavaClientImpl() { //Empty constructor - } public void assertWorkflowState(ServiceContext ctx, - DocumentModel docModel) throws DocumentNotFoundException, ClientException { - MultivaluedMap queryParams = ctx.getQueryParams(); - if (queryParams != null) { - // - // Look for the workflow "delete" query param and see if we need to assert that the - // docModel is in a non-deleted workflow state. - // - String currentState = docModel.getCurrentLifeCycleState(); - String includeDeletedStr = queryParams.getFirst(WorkflowClient.WORKFLOW_QUERY_NONDELETED); - boolean includeDeleted = includeDeletedStr == null ? true : Boolean.parseBoolean(includeDeletedStr); - if (includeDeleted == false) { - // - // We don't wanted soft-deleted object, so throw an exception if this one is soft-deleted. - // - if (currentState.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) { - String msg = "The GET assertion that docModel not be in 'deleted' workflow state failed."; - logger.debug(msg); - throw new DocumentNotFoundException(msg); - } - } - } + DocumentModel docModel) throws DocumentNotFoundException, ClientException { + MultivaluedMap queryParams = ctx.getQueryParams(); + if (queryParams != null) { + // + // Look for the workflow "delete" query param and see if we need to assert that the + // docModel is in a non-deleted workflow state. + // + String currentState = docModel.getCurrentLifeCycleState(); + String includeDeletedStr = queryParams.getFirst(WorkflowClient.WORKFLOW_QUERY_NONDELETED); + boolean includeDeleted = includeDeletedStr == null ? true : Boolean.parseBoolean(includeDeletedStr); + if (includeDeleted == false) { + // + // We don't wanted soft-deleted object, so throw an exception if this one is soft-deleted. + // + if (currentState.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) { + String msg = "The GET assertion that docModel not be in 'deleted' workflow state failed."; + logger.debug(msg); + throw new DocumentNotFoundException(msg); + } + } + } } - + /** * create document in the Nuxeo repository * * @param ctx service context under which this method is invoked - * @param handler - * should be used by the caller to provide and transform the - * document + * @param handler should be used by the caller to provide and transform the + * document * @return id in repository of the newly created document * @throws BadRequestException * @throws TransactionException @@ -146,12 +148,12 @@ public class RepositoryJavaClientImpl implements RepositoryClient wrapDoc = new DocumentWrapperImpl(doc); handler.handle(Action.CREATE, wrapDoc); @@ -194,7 +196,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient findDoc( ServiceContext ctx, String whereClause) - throws DocumentNotFoundException, TransactionException, DocumentException { + throws DocumentNotFoundException, TransactionException, DocumentException { RepositoryInstance repoSession = null; DocumentWrapper wrapDoc = null; @@ -445,22 +448,23 @@ public class RepositoryJavaClientImpl implements RepositoryClient ctx, String whereClause) throws DocumentNotFoundException, TransactionException, DocumentException { String csid = null; boolean releaseSession = false; try { - if (repoSession == null) { - repoSession = this.getRepositorySession(ctx); - releaseSession = true; - } + if (repoSession == null) { + repoSession = this.getRepositorySession(ctx); + releaseSession = true; + } DocumentWrapper wrapDoc = findDoc(repoSession, ctx, whereClause); DocumentModel docModel = wrapDoc.getWrappedObject(); csid = NuxeoUtils.getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString()); @@ -495,9 +499,9 @@ public class RepositoryJavaClientImpl implements RepositoryClient wrapDoc = null; try { @@ -523,9 +527,9 @@ public class RepositoryJavaClientImpl implements RepositoryClient(docList); } catch (IllegalArgumentException iae) { throw iae; @@ -535,39 +539,39 @@ public class RepositoryJavaClientImpl implements RepositoryClient docTypes) { - StringBuilder sb = new StringBuilder(); - sb.append("("); - boolean first = true; - for(String docType:docTypes) { - if(first) { - first = false; - } else { - sb.append(","); - } - sb.append("'"); - sb.append(docType); - sb.append("'"); - } - sb.append(")"); - return sb.toString(); + StringBuilder sb = new StringBuilder(); + sb.append("("); + boolean first = true; + for (String docType : docTypes) { + if (first) { + first = false; + } else { + sb.append(","); + } + sb.append("'"); + sb.append(docType); + sb.append("'"); + } + sb.append(")"); + return sb.toString(); } - + public DocumentWrapper findDocs( ServiceContext ctx, DocumentHandler handler, RepositoryInstance repoSession, List docTypes) - throws DocumentNotFoundException, DocumentException { + throws DocumentNotFoundException, DocumentException { DocumentWrapper wrapDoc = null; DocumentFilter filter = handler.getDocumentFilter(); String oldOrderBy = filter.getOrderByClause(); - if (isClauseEmpty(oldOrderBy) == true){ + if (isClauseEmpty(oldOrderBy) == true) { filter.setOrderByClause(DocumentFilter.ORDER_BY_LAST_UPDATED); } QueryContext queryContext = new QueryContext(ctx, handler); @@ -578,14 +582,14 @@ public class RepositoryJavaClientImpl implements RepositoryClient getDocFromCsid( - ServiceContext ctx, - RepositoryInstance repoSession, - String csid) + ServiceContext ctx, + RepositoryInstance repoSession, + String csid) throws Exception { DocumentWrapper result = null; result = new DocumentWrapperImpl(NuxeoUtils.getDocFromCsid(ctx, repoSession, csid)); - + return result; - } + } /* * A method to find a CollectionSpace document (of any type) given just a service context and @@ -770,76 +772,79 @@ public class RepositoryJavaClientImpl implements RepositoryClient getDocFromCsid(ServiceContext ctx, - String csid) + String csid) throws Exception { DocumentWrapper result = null; RepositoryInstance repoSession = null; try { - repoSession = getRepositorySession(ctx); - result = getDocFromCsid(ctx, repoSession, csid); + repoSession = getRepositorySession(ctx); + result = getDocFromCsid(ctx, repoSession, csid); } finally { if (repoSession != null) { releaseRepositorySession(ctx, repoSession); } } - + if (logger.isWarnEnabled() == true) { - logger.warn("Returned DocumentModel instance was created with a repository session that is now closed."); + logger.warn("Returned DocumentModel instance was created with a repository session that is now closed."); } - + return result; } /** * Returns a URI value for a document in the Nuxeo repository + * * @param wrappedDoc a wrapped documentModel * @throws ClientException * @return a document URI */ @Override public String getDocURI(DocumentWrapper wrappedDoc) throws ClientException { - DocumentModel docModel = wrappedDoc.getWrappedObject(); - String uri = (String)docModel.getProperty(CollectionSpaceClient.COLLECTIONSPACE_CORE_SCHEMA, - CollectionSpaceClient.COLLECTIONSPACE_CORE_URI); + DocumentModel docModel = wrappedDoc.getWrappedObject(); + String uri = (String) docModel.getProperty(CollectionSpaceClient.COLLECTIONSPACE_CORE_SCHEMA, + CollectionSpaceClient.COLLECTIONSPACE_CORE_URI); return uri; } /* * See CSPACE-5036 - How to make CMISQL queries from Nuxeo */ - private IterableQueryResult makeCMISQLQuery(RepositoryInstance repoSession, String query, QueryContext queryContext) { - IterableQueryResult result = null; - - // the NuxeoRepository should be constructed only once, then cached - // (its construction is expensive) - try { - NuxeoRepository repo = new NuxeoRepository( - repoSession.getRepositoryName(), repoSession - .getRootDocument().getId()); - logger.debug("Repository ID:" + repo.getId() + " Root folder:" - + repo.getRootFolderId()); - - CallContextImpl callContext = new CallContextImpl( - CallContext.BINDING_LOCAL, repo.getId(), false); - callContext.put(CallContext.USERNAME, repoSession.getPrincipal() - .getName()); - NuxeoCmisService cmisService = new NuxeoCmisService(repo, - callContext, repoSession); - - result = repoSession.queryAndFetch(query, "CMISQL", cmisService); - } catch (ClientException e) { - // TODO Auto-generated catch block - logger.error("Encounter trouble making the following CMIS query: " + query, e); - } - - return result; - } - + private IterableQueryResult makeCMISQLQuery(RepositoryInstance repoSession, String query, QueryContext queryContext) { + IterableQueryResult result = null; + + // the NuxeoRepository should be constructed only once, then cached + // (its construction is expensive) + try { + NuxeoRepository repo = new NuxeoRepository( + repoSession.getRepositoryName(), repoSession + .getRootDocument().getId()); + logger.debug("Repository ID:" + repo.getId() + " Root folder:" + + repo.getRootFolderId()); + + CallContextImpl callContext = new CallContextImpl( + CallContext.BINDING_LOCAL, repo.getId(), false); + callContext.put(CallContext.USERNAME, repoSession.getPrincipal() + .getName()); + NuxeoCmisService cmisService = new NuxeoCmisService(repo, + callContext, repoSession); + + result = repoSession.queryAndFetch(query, "CMISQL", cmisService); + } catch (ClientException e) { + // TODO Auto-generated catch block + logger.error("Encounter trouble making the following CMIS query: " + query, e); + } + + return result; + } + /** - * getFiltered get all documents for an entity service from the Document repository, - * given filter parameters specified by the handler. + * getFiltered get all documents for an entity service from the Document + * repository, given filter parameters specified by the handler. + * * @param ctx service context under which this method is invoked - * @param handler should be used by the caller to provide and transform the document + * @param handler should be used by the caller to provide and transform the + * document * @throws DocumentNotFoundException if workspace not found * @throws TransactionException * @throws DocumentException @@ -850,7 +855,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient 0) || (queryContext.getDocFilter().getPageSize() > 0)) { + Profiler profiler = new Profiler(this, 2); + profiler.log("Executing NXQL query: " + query.toString()); + profiler.start(); + if (handler.isJDBCQuery() == true) { + docList = getFilteredJDBC(repoSession, ctx, handler, queryContext); + } else if (handler.isCMISQuery() == true) { + docList = getFilteredCMIS(repoSession, ctx, handler, queryContext); //FIXME: REM - Need to deal with paging info in CMIS query + } else if ((queryContext.getDocFilter().getOffset() > 0) || (queryContext.getDocFilter().getPageSize() > 0)) { docList = repoSession.query(query, null, queryContext.getDocFilter().getPageSize(), queryContext.getDocFilter().getOffset(), true); } else { @@ -902,112 +907,158 @@ public class RepositoryJavaClientImpl implements RepositoryClient queryParams = ctx.getQueryParams(); + DocumentModelList result = new DocumentModelListImpl(); + + String dataSourceName = JDBCTools.NUXEO_DATASOURCE_NAME; + String repositoryName = ctx.getRepositoryName(); + + // Connection connection = JDBCTools.getConnection(dataSourceName, repositoryName); + + MultivaluedMap queryParams = ctx.getQueryParams(); String partialTerm = queryParams.getFirst(IQueryManager.SEARCH_TYPE_PARTIALTERM); - + // FIXME: Replace this placeholder with an appropriate per-authority value // obtained from the relevant document handler - String termInfoGroupTable = "loctermgroup"; - + String termInfoGroupTableName = "loctermgroup"; + // FIXME: Replace this placeholder query with an actual query from CSPACE-5945 // FIXME: Consider using a prepared statement here - String theQuery = - "SELECT termdisplayname FROM " - + termInfoGroupTable + String sql = + "SELECT id, termdisplayname FROM " + + termInfoGroupTableName + " WHERE termdisplayname LIKE '" + partialTerm + "%'"; // Make sure autocommit is off. See: - // http://jdbc.postgresql.org/documentation/80/query.html#query-with-cursor - // http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html - connection.setAutoCommit(false); - + // http://jdbc.postgresql.org/documentation/80/query.html#query-with-cursor + // http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html + // connection.setAutoCommit(false); + // FIXME: Add exception handling and 'finally' blocks to ensure we close resources // FIXME: Identify whether we can piggyback on existing JDBC method(s) in common.storage, // and if so, add whatever additional functionality may be required to those method(s) // FIXME: Add pagination handling - - Statement st = connection.createStatement(); - // Enable use of the cursor for pagination - st.setFetchSize(50); + + /* + Statement st = connection.createStatement(); + // Enable use of the cursor for pagination + st.setFetchSize(50); + List docIds = new ArrayList(); + ResultSet rs = st.executeQuery(sql); + String id; + while (rs.next()) { + id = rs.getString("id"); + if (Tools.notBlank(id)) { + docIds.add(id); + } + } + rs.close(); + + // Close the statement. + st.close(); + */ + List docIds = new ArrayList(); - ResultSet rs = st.executeQuery(theQuery); - String id; - while (rs.next()) { - id = rs.getString("id"); - if (Tools.notBlank(id)) { - docIds.add(id); + CachedRowSet crs = null; + try { + crs = JDBCTools.executeQuery(dataSourceName, repositoryName, sql); + + // If the response to the query is null or contains zero rows, + // return an empty list of document models + if (crs == null) { + return result; + } + crs.last(); + if (crs.getRow() == 0) { + return result; // empty list of document models } - } - rs.close(); - // Close the statement. - st.close(); + // Otherwise, get the document IDs from the results of the query + String id; + /* + int idColumnIndex; + try { + idColumnIndex = crs.findColumn(ID_COLUMN_NAME); + } catch (SQLException sqle) { + logger.warn("Could not find expected column '" + ID_COLUMN_NAME + "' in query results."); + return result; // return an empty list of document models + } finally { + crs.close(); + } + */ + crs.beforeFirst(); + while (crs.next()) { + // id = crs.getString(idColumnIndex); + id = crs.getString(1); + if (Tools.notBlank(id)) { + docIds.add(id); + } + } + } catch (SQLException sqle) { + logger.warn("Could not obtain document IDs via SQL query '" + sql + "': " + sqle.getMessage()); + return result; // return an empty list of document models + } finally { + crs.close(); + } // Get a list of document models, using the IDs obtained from the query for (String docId : docIds) { result.add(NuxeoUtils.getDocumentModel(repoSession, docId)); } - - return result; + + return result; } private DocumentModelList getFilteredCMIS(RepositoryInstance repoSession, ServiceContext ctx, DocumentHandler handler, QueryContext queryContext) throws DocumentNotFoundException, DocumentException { - DocumentModelList result = new DocumentModelListImpl(); + DocumentModelList result = new DocumentModelListImpl(); try { String query = handler.getCMISQuery(queryContext); - DocumentFilter docFilter = handler.getDocumentFilter(); + DocumentFilter docFilter = handler.getDocumentFilter(); int pageSize = docFilter.getPageSize(); int offset = docFilter.getOffset(); if (logger.isDebugEnabled()) { logger.debug("Executing CMIS query: " + query.toString() - + "with pageSize: "+pageSize+" at offset: "+offset); + + "with pageSize: " + pageSize + " at offset: " + offset); } // If we have limit and/or offset, then pass true to get totalSize // in returned DocumentModelList. - Profiler profiler = new Profiler(this, 2); - profiler.log("Executing CMIS query: " + query.toString()); - profiler.start(); - // - IterableQueryResult queryResult = makeCMISQLQuery(repoSession, query, queryContext); - try { - int totalSize = (int)queryResult.size(); - ((DocumentModelListImpl)result).setTotalSize(totalSize); - // Skip the rows before our offset - if (offset > 0) { - queryResult.skipTo(offset); - } - int nRows = 0; - for (Map row : queryResult) { - if (logger.isTraceEnabled()) { - logger.trace(" Hierarchy Table ID is:" + row.get(IQueryManager.CMIS_TARGET_NUXEO_ID) - + " nuxeo:pathSegment is: " + row.get(IQueryManager.CMIS_TARGET_NAME)); - } - String nuxeoId = (String) row.get(IQueryManager.CMIS_TARGET_NUXEO_ID); - DocumentModel docModel = NuxeoUtils.getDocumentModel(repoSession, nuxeoId); - result.add(docModel); - nRows++; - if (nRows >= pageSize && pageSize != 0 ) { // A page size of zero means that they want all of them - logger.debug("Got page full of items - quitting"); - break; - } - } - } finally { - queryResult.close(); - } - // + Profiler profiler = new Profiler(this, 2); + profiler.log("Executing CMIS query: " + query.toString()); + profiler.start(); + // + IterableQueryResult queryResult = makeCMISQLQuery(repoSession, query, queryContext); + try { + int totalSize = (int) queryResult.size(); + ((DocumentModelListImpl) result).setTotalSize(totalSize); + // Skip the rows before our offset + if (offset > 0) { + queryResult.skipTo(offset); + } + int nRows = 0; + for (Map row : queryResult) { + if (logger.isTraceEnabled()) { + logger.trace(" Hierarchy Table ID is:" + row.get(IQueryManager.CMIS_TARGET_NUXEO_ID) + + " nuxeo:pathSegment is: " + row.get(IQueryManager.CMIS_TARGET_NAME)); + } + String nuxeoId = (String) row.get(IQueryManager.CMIS_TARGET_NUXEO_ID); + DocumentModel docModel = NuxeoUtils.getDocumentModel(repoSession, nuxeoId); + result.add(docModel); + nRows++; + if (nRows >= pageSize && pageSize != 0) { // A page size of zero means that they want all of them + logger.debug("Got page full of items - quitting"); + break; + } + } + } finally { + queryResult.close(); + } + // profiler.stop(); } catch (Exception e) { @@ -1016,52 +1067,51 @@ public class RepositoryJavaClientImpl implements RepositoryClient docFilter.getPageSize()) { - docFilter.setPageSize(totalSize); - ((DocumentModelListImpl)result).setTotalSize(totalSize); - } - } - */ - + if (result != null) { + docFilter.setStartPage(0); + if (totalSize > docFilter.getPageSize()) { + docFilter.setPageSize(totalSize); + ((DocumentModelListImpl)result).setTotalSize(totalSize); + } + } + */ + return result; } - + private String logException(Exception e, String msg) { - String result = null; - - String exceptionMessage = e.getMessage(); - exceptionMessage = exceptionMessage != null ? exceptionMessage : ""; + String result = null; + + String exceptionMessage = e.getMessage(); + exceptionMessage = exceptionMessage != null ? exceptionMessage : ""; result = msg = msg + ". Caught exception:" + exceptionMessage; - + if (logger.isTraceEnabled() == true) { - logger.error(msg, e); + logger.error(msg, e); } else { - logger.error(msg); + logger.error(msg); } - - return result; + + return result; } - + /** * update given document in the Nuxeo repository * * @param ctx service context under which this method is invoked - * @param csid - * of the document - * @param handler - * should be used by the caller to provide and transform the - * document + * @param csid of the document + * @param handler should be used by the caller to provide and transform the + * document * @throws BadRequestException * @throws DocumentNotFoundException - * @throws TransactionException if the transaction times out or otherwise cannot be successfully completed + * @throws TransactionException if the transaction times out or otherwise + * cannot be successfully completed * @throws DocumentException */ @Override @@ -1072,7 +1122,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient ctx, RepositoryInstance repoSession, - DocumentModelList docList, + DocumentModelList docList, boolean fSaveSession) throws ClientException, DocumentException { try { DocumentModel[] docModelArray = new DocumentModel[docList.size()]; repoSession.saveDocuments(docList.toArray(docModelArray)); if (fSaveSession) { - repoSession.save(); + repoSession.save(); } } catch (ClientException ce) { throw ce; @@ -1197,9 +1249,9 @@ public class RepositoryJavaClientImpl implements RepositoryClient wrapDoc = null; try { - DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id); - wrapDoc = new DocumentWrapperImpl(repoSession.getDocument(docRef)); - ((DocumentModelHandler) handler).setRepositorySession(repoSession); - handler.handle(Action.DELETE, wrapDoc); + DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id); + wrapDoc = new DocumentWrapperImpl(repoSession.getDocument(docRef)); + ((DocumentModelHandler) handler).setRepositorySession(repoSession); + handler.handle(Action.DELETE, wrapDoc); repoSession.removeDocument(docRef); } catch (ClientException ce) { - String msg = logException(ce, "Could not find document to delete with CSID=" + id); + String msg = logException(ce, "Could not find document to delete with CSID=" + id); throw new DocumentNotFoundException(msg, ce); } repoSession.save(); @@ -1268,15 +1320,15 @@ public class RepositoryJavaClientImpl implements RepositoryClient witer = domainChildrenList.iterator(); - while (witer.hasNext()) { - DocumentModel childNode = witer.next(); - if (NuxeoUtils.Workspaces.equalsIgnoreCase(childNode.getName())) { - result = childNode; - logger.trace("Found workspaces directory at: " + result.getPathAsString()); - break; - } - } - - if (result == null) { - throw new ClientException("Could not find workspace root directory in: " - + domainPath); - } - - return result; - } - + * Returns the workspaces root directory for a given domain. + */ + private DocumentModel getWorkspacesRoot(RepositoryInstance repoSession, + String domainName) throws Exception { + DocumentModel result = null; + + String domainPath = "/" + domainName; + DocumentRef parentDocRef = new PathRef(domainPath); + DocumentModelList domainChildrenList = repoSession.getChildren( + parentDocRef); + Iterator witer = domainChildrenList.iterator(); + while (witer.hasNext()) { + DocumentModel childNode = witer.next(); + if (NuxeoUtils.Workspaces.equalsIgnoreCase(childNode.getName())) { + result = childNode; + logger.trace("Found workspaces directory at: " + result.getPathAsString()); + break; + } + } + + if (result == null) { + throw new ClientException("Could not find workspace root directory in: " + + domainPath); + } + + return result; + } + /* (non-Javadoc) * @see org.collectionspace.services.common.repository.RepositoryClient#createWorkspace(java.lang.String, java.lang.String) */ @@ -1387,17 +1439,17 @@ public class RepositoryJavaClientImpl implements RepositoryClient