1 package org.collectionspace.services.nuxeo.client.java;
3 import java.security.Principal;
5 import org.nuxeo.ecm.core.api.ClientException;
6 import org.nuxeo.ecm.core.api.DocumentModel;
7 import org.nuxeo.ecm.core.api.DocumentModelList;
8 import org.nuxeo.ecm.core.api.DocumentRef;
9 import org.nuxeo.ecm.core.api.Filter;
10 import org.nuxeo.ecm.core.api.IterableQueryResult;
11 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
12 import org.nuxeo.ecm.core.api.impl.LifeCycleFilter;
13 import org.nuxeo.ecm.core.api.CoreSession;
14 import org.nuxeo.runtime.transaction.TransactionHelper;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 public class CoreSessionWrapper implements CoreSessionInterface {
21 private CoreSession repoSession;
22 private boolean transactionSetForRollback = false;
25 private static Logger logger = LoggerFactory.getLogger(CoreSessionWrapper.class);
27 private void logQuery(String query) {
28 logger.debug(String.format("NXQL: %s", query));
31 private void logQuery(String query, String queryType) {
32 logger.debug(String.format("Query Type: '%s' NXQL: %s", queryType, query));
35 private void logQuery(String query, Filter filter, long limit,
36 long offset, boolean countTotal) {
37 logger.debug(String.format("Filter: '%s', Limit: '%d', Offset: '%d', Count Total?: %b, NXQL: %s",
38 filter != null ? filter.toString() : "none", limit, offset, countTotal, query));
41 public CoreSessionWrapper(CoreSession repoSession) {
42 this.repoSession = repoSession;
46 * Mark this session's transaction for rollback only
49 public void setTransactionRollbackOnly() {
50 TransactionHelper.setTransactionRollbackOnly();
51 transactionSetForRollback = true;
55 public boolean isTransactionMarkedForRollbackOnly() {
56 if (transactionSetForRollback != TransactionHelper.isTransactionMarkedRollback()) {
57 logger.error(String.format("Transaction status is in an inconsistent state. Internal state is '%b'. TransactionHelper statis is '%b'.",
58 transactionSetForRollback, TransactionHelper.isTransactionMarkedRollback()));
60 return transactionSetForRollback;
64 public CoreSession getCoreSession() {
69 public String getSessionId() {
70 return repoSession.getSessionId();
74 public void close() throws Exception {
77 } catch (Throwable t) {
78 logger.error(String.format("Could not close session for repository '%s'.", this.repoSession.getRepositoryName()),
84 * Gets the root document of this repository.
86 * @return the root document. cannot be null
87 * @throws ClientException
88 * @throws SecurityException
91 public DocumentModel getRootDocument() throws ClientException {
92 return repoSession.getRootDocument();
96 * Returns the repository name against which this core session is bound.
98 * @return the repository name used currently used as an identifier
101 public String getRepositoryName() {
102 return repoSession.getRepositoryName();
106 * Gets the principal that created the client session.
108 * @return the principal
111 public Principal getPrincipal() {
112 return repoSession.getPrincipal();
116 public IterableQueryResult queryAndFetch(String query, String queryType,
117 Object... params) throws ClientException {
118 logQuery(query, queryType);
119 return repoSession.queryAndFetch(query, queryType, params);
123 public DocumentModelList query(String query, Filter filter, long limit,
124 long offset, boolean countTotal) throws ClientException {
125 logQuery(query, filter, limit, offset, countTotal);
126 return repoSession.query(query, filter, limit, offset, countTotal);
130 public DocumentModelList query(String query, int max) throws ClientException {
132 return repoSession.query(query, max);
136 public DocumentModelList query(String query) throws ClientException {
138 return repoSession.query(query);
142 public DocumentModelList query(String query, LifeCycleFilter workflowStateFilter) {
143 return repoSession.query(query, workflowStateFilter);
147 * Gets a document model given its reference.
149 * The default schemas are used to populate the returned document model.
150 * Default schemas are configured via the document type manager.
152 * Any other data model not part of the default schemas will be lazily
155 * @param docRef the document reference
156 * @return the document
157 * @throws ClientException
158 * @throws SecurityException
161 public DocumentModel getDocument(DocumentRef docRef) throws ClientException {
162 return repoSession.getDocument(docRef);
166 public DocumentModel saveDocument(DocumentModel docModel) throws ClientException {
167 DocumentModel result = null;
170 if (isTransactionMarkedForRollbackOnly() == false) {
171 result = repoSession.saveDocument(docModel);
173 logger.trace(String.format("The repository session on thread '%d' has a transaction that is marked for rollback.",
174 Thread.currentThread().getId()));
176 } catch (Throwable t) {
177 setTransactionRollbackOnly();
185 public void save() throws ClientException {
187 if (isTransactionMarkedForRollbackOnly() == false) {
190 logger.trace(String.format("The repository session on thread '%d' has a transaction that is marked for rollback.",
191 Thread.currentThread().getId()));
193 } catch (Throwable t) {
194 setTransactionRollbackOnly();
200 * Bulk document saving.
202 * @param docModels the document models that needs to be saved
203 * @throws ClientException
206 public void saveDocuments(DocumentModel[] docModels) throws ClientException {
208 if (isTransactionMarkedForRollbackOnly() == false) {
209 repoSession.saveDocuments(docModels);
211 logger.trace(String.format("The repository session on thread '%d' has a transaction that is marked for rollback.",
212 Thread.currentThread().getId()));
214 } catch (Throwable t) {
215 setTransactionRollbackOnly();
221 * Removes this document and all its children, if any.
223 * @param docRef the reference to the document to remove
224 * @throws ClientException
227 public void removeDocument(DocumentRef docRef) throws ClientException {
228 repoSession.removeDocument(docRef);
232 * Creates a document model using required information.
234 * Used to fetch initial datamodels from the type definition.
236 * DocumentModel creation notifies a
237 * {@link DocumentEventTypes.EMPTY_DOCUMENTMODEL_CREATED} so that core event
238 * listener can initialize its content with computed properties.
243 * @return the initial document model
244 * @throws ClientException
247 public DocumentModel createDocumentModel(String parentPath, String id,
248 String typeName) throws ClientException {
249 return repoSession.createDocumentModel(parentPath, id, typeName);
253 * Creates a document using given document model for initialization.
255 * The model contains path of the new document, its type and optionally the
256 * initial data models of the document.
259 * @param model the document model to use for initialization
260 * @return the created document
261 * @throws ClientException
264 public DocumentModel createDocument(DocumentModel model) throws ClientException {
265 return repoSession.createDocument(model);
269 * Gets the children of the given parent.
271 * @param parent the parent reference
272 * @return the children if any, an empty list if no children or null if the
273 * specified parent document is not a folder
274 * @throws ClientException
277 public DocumentModelList getChildren(DocumentRef parent) throws ClientException {
278 return repoSession.getChildren(parent);