]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2de822bdfafb66250fb3810183fea5815d26279f
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.nuxeo.client.java;
2
3 import java.security.Principal;
4
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;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class CoreSessionWrapper implements CoreSessionInterface {
20
21         private CoreSession repoSession;
22         private boolean transactionSetForRollback = false;
23         
24     /** The logger. */
25     private static Logger logger = LoggerFactory.getLogger(CoreSessionWrapper.class);
26     
27     private void logQuery(String query) {
28         logger.debug(String.format("NXQL: %s", query));
29     }
30     
31     private void logQuery(String query, String queryType) {
32         logger.debug(String.format("Query Type: '%s' NXQL: %s", queryType, query));
33     }
34     
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));
39     }
40     
41         public CoreSessionWrapper(CoreSession repoSession) {
42                 this.repoSession = repoSession;
43         }
44
45         /*
46          * Mark this session's transaction for rollback only
47          */
48         @Override
49     public void setTransactionRollbackOnly() {
50                 TransactionHelper.setTransactionRollbackOnly();
51         transactionSetForRollback = true;
52     }
53         
54         @Override
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()));
59                 }
60         return transactionSetForRollback;
61     }
62         
63         @Override
64         public  CoreSession getCoreSession() {
65                 return repoSession;
66         }
67         
68         @Override
69     public String getSessionId() {
70         return repoSession.getSessionId();
71     }
72     
73     @Override
74     public void close() throws Exception {
75         try {
76                 repoSession.close();
77         } catch (Throwable t) {
78                 logger.error(String.format("Could not close session for repository '%s'.", this.repoSession.getRepositoryName()),
79                                 t);
80         }
81     }
82
83     /**
84      * Gets the root document of this repository.
85      *
86      * @return the root document. cannot be null
87      * @throws ClientException
88      * @throws SecurityException
89      */
90         @Override
91     public DocumentModel getRootDocument() throws ClientException {
92         return repoSession.getRootDocument();
93     }
94     
95     /**
96      * Returns the repository name against which this core session is bound.
97      *
98      * @return the repository name used currently used as an identifier
99      */
100         @Override
101     public String getRepositoryName() {
102                 return repoSession.getRepositoryName();
103         }
104     
105     /**
106      * Gets the principal that created the client session.
107      *
108      * @return the principal
109      */
110         @Override
111         public Principal getPrincipal() {
112                 return repoSession.getPrincipal();
113         }
114
115         @Override
116         public IterableQueryResult queryAndFetch(String query, String queryType,
117             Object... params) throws ClientException {
118                 logQuery(query, queryType);
119                 return repoSession.queryAndFetch(query, queryType, params);
120         }
121
122         @Override
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);
127         }
128
129         @Override
130     public DocumentModelList query(String query, int max) throws ClientException {
131                 logQuery(query);
132         return repoSession.query(query, max);
133     }
134     
135         @Override
136         public DocumentModelList query(String query) throws ClientException {
137                 logQuery(query);
138                 return repoSession.query(query);
139         }
140         
141         @Override
142         public DocumentModelList query(String query, LifeCycleFilter workflowStateFilter) {
143                 return repoSession.query(query, workflowStateFilter);
144         }
145
146     /**
147      * Gets a document model given its reference.
148      * <p>
149      * The default schemas are used to populate the returned document model.
150      * Default schemas are configured via the document type manager.
151      * <p>
152      * Any other data model not part of the default schemas will be lazily
153      * loaded as needed.
154      *
155      * @param docRef the document reference
156      * @return the document
157      * @throws ClientException
158      * @throws SecurityException
159      */
160     @Override
161     public DocumentModel getDocument(DocumentRef docRef) throws ClientException {
162             return repoSession.getDocument(docRef);
163     }
164
165     @Override
166     public DocumentModel saveDocument(DocumentModel docModel) throws ClientException {
167         DocumentModel result = null;
168         
169         try {
170                 if (isTransactionMarkedForRollbackOnly() == false) {
171                         result = repoSession.saveDocument(docModel);
172                 } else {
173                         logger.trace(String.format("The repository session on thread '%d' has a transaction that is marked for rollback.",
174                                         Thread.currentThread().getId()));
175                 }
176         } catch (Throwable t) {
177                 setTransactionRollbackOnly();
178                 throw t;
179         }
180         
181         return result;
182     }
183
184     @Override
185     public void save() throws ClientException {
186         try {
187                 if (isTransactionMarkedForRollbackOnly() == false) {
188                         repoSession.save();
189                 } else {
190                         logger.trace(String.format("The repository session on thread '%d' has a transaction that is marked for rollback.",
191                                         Thread.currentThread().getId()));
192                 }
193         } catch (Throwable t) {
194                 setTransactionRollbackOnly();
195                 throw t;
196         }
197     }
198
199     /**
200      * Bulk document saving.
201      *
202      * @param docModels the document models that needs to be saved
203      * @throws ClientException
204      */
205     @Override
206     public void saveDocuments(DocumentModel[] docModels) throws ClientException {
207         try {
208                 if (isTransactionMarkedForRollbackOnly() == false) {
209                         repoSession.saveDocuments(docModels);
210                 } else {
211                         logger.trace(String.format("The repository session on thread '%d' has a transaction that is marked for rollback.",
212                                         Thread.currentThread().getId()));
213                 }
214         } catch (Throwable t) {
215                 setTransactionRollbackOnly();
216                 throw t;
217         }
218     }
219
220     /**
221      * Removes this document and all its children, if any.
222      *
223      * @param docRef the reference to the document to remove
224      * @throws ClientException
225      */
226     @Override
227     public void removeDocument(DocumentRef docRef) throws ClientException {
228         repoSession.removeDocument(docRef);
229     }
230
231     /**
232      * Creates a document model using required information.
233      * <p>
234      * Used to fetch initial datamodels from the type definition.
235      * <p>
236      * DocumentModel creation notifies a
237      * {@link DocumentEventTypes.EMPTY_DOCUMENTMODEL_CREATED} so that core event
238      * listener can initialize its content with computed properties.
239      *
240      * @param parentPath
241      * @param id
242      * @param typeName
243      * @return the initial document model
244      * @throws ClientException
245      */
246     @Override
247     public DocumentModel createDocumentModel(String parentPath, String id,
248             String typeName) throws ClientException {
249         return repoSession.createDocumentModel(parentPath, id, typeName);
250     }
251     
252     /**
253      * Creates a document using given document model for initialization.
254      * <p>
255      * The model contains path of the new document, its type and optionally the
256      * initial data models of the document.
257      * <p>
258      *
259      * @param model the document model to use for initialization
260      * @return the created document
261      * @throws ClientException
262      */
263     @Override
264     public DocumentModel createDocument(DocumentModel model) throws ClientException {
265         return repoSession.createDocument(model);
266     }
267     
268     /**
269      * Gets the children of the given parent.
270      *
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
275      */
276     @Override
277     public DocumentModelList getChildren(DocumentRef parent) throws ClientException {
278         return repoSession.getChildren(parent);
279     }
280
281     
282         
283 }