2 * Copyright 2009 University of California at Berkeley
\r
4 package org.collectionspace.services.nuxeo;
\r
6 import java.io.IOException;
\r
8 import org.collectionspace.services.common.ServiceMain;
\r
9 import org.collectionspace.services.common.repository.DocumentNotFoundException;
\r
10 import org.collectionspace.services.nuxeo.client.rest.NuxeoRESTClient;
\r
11 import org.collectionspace.services.nuxeo.client.java.NuxeoConnector;
\r
12 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
\r
14 import org.dom4j.Document;
\r
15 import org.dom4j.DocumentException;
\r
16 import org.dom4j.dom.DOMDocument;
\r
17 import org.dom4j.dom.DOMDocumentFactory;
\r
19 import org.w3c.dom.DOMException;
\r
20 import org.w3c.dom.Element;
\r
22 import org.nuxeo.ecm.core.api.DocumentModel;
\r
23 import org.nuxeo.ecm.core.api.DocumentModelList;
\r
24 import org.nuxeo.ecm.core.api.DocumentRef;
\r
25 import org.nuxeo.ecm.core.api.IdRef;
\r
26 import org.nuxeo.ecm.core.api.ClientException;
\r
27 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
\r
28 import org.nuxeo.ecm.core.client.NuxeoClient;
\r
30 import org.slf4j.Logger;
\r
31 import org.slf4j.LoggerFactory;
\r
37 public abstract class CollectionSpaceServiceNuxeoImpl {
\r
39 // replace host if not running on localhost
\r
40 // static String CS_NUXEO_HOST = "173.45.234.217";
\r
41 static String CS_NUXEO_HOST = "localhost";
\r
42 static String CS_NUXEO_URI = "http://" + CS_NUXEO_HOST + ":8080/nuxeo";
\r
44 protected Logger logger = LoggerFactory
\r
45 .getLogger(CollectionSpaceServiceNuxeoImpl.class);
\r
47 public NuxeoRESTClient getClient() {
\r
48 NuxeoRESTClient nxClient = new NuxeoRESTClient(CS_NUXEO_URI);
\r
50 nxClient.setAuthType(NuxeoRESTClient.AUTH_TYPE_BASIC);
\r
51 nxClient.setBasicAuthentication("Administrator", "Administrator");
\r
56 // FIXME: Replace this method after integration of the relation code
\r
57 protected RepositoryInstance getRepositorySession() throws Exception {
\r
58 // FIXME: is it possible to reuse repository session?
\r
59 // Authentication failures happen while trying to reuse the session
\r
60 NuxeoConnector nuxeoConnector = NuxeoConnector.getInstance();
\r
61 return nuxeoConnector.getRepositorySession();
\r
64 protected Document deleteDocument(RepositoryInstance repoSession, String csid)
\r
65 throws DocumentException, IOException {
\r
66 Document result = null;
\r
69 repoSession = getRepositorySession();
\r
70 DocumentRef relDocumentRef = new IdRef(csid);
\r
73 repoSession.removeDocument(relDocumentRef);
\r
74 }catch(ClientException ce){
\r
75 String msg = "could not find document to delete with id=" + csid;
\r
76 logger.error(msg, ce);
\r
77 throw new DocumentNotFoundException(msg, ce);
\r
79 repoSession.save();
\r
80 } catch (Exception e) {
\r
81 e.printStackTrace();
\r
87 protected Document listWorkspaceContent(RepositoryInstance repoSession,
\r
88 String workspaceName) {
\r
90 DOMDocumentFactory domfactory = new DOMDocumentFactory();
\r
91 DOMDocument result = (DOMDocument) domfactory.createDocument();
\r
94 repoSession = getRepositorySession();
\r
95 DocumentModel workspaceModel = NuxeoUtils.getWorkspaceModel(repoSession,
\r
98 Element current = result.createElement("document");
\r
100 current.setAttribute("title", workspaceModel.getTitle());
\r
101 } catch (Exception e) {
\r
102 e.printStackTrace();
\r
104 current.setAttribute("type", workspaceModel.getType());
\r
105 current.setAttribute("id", workspaceModel.getId());
\r
106 current.setAttribute("name", workspaceModel.getName());
\r
107 current.setAttribute("url", getRelURL(workspaceName, workspaceModel.getRef().toString()));
\r
108 result.setRootElement((org.dom4j.Element) current);
\r
110 if (workspaceModel.isFolder()) {
\r
111 // Element childrenElem = result.createElement("children");
\r
112 // root.appendChild(childrenElem);
\r
114 DocumentModelList children = null;
\r
116 children = repoSession.getChildren(workspaceModel.getRef());
\r
117 } catch (ClientException e) {
\r
118 e.printStackTrace();
\r
121 for (DocumentModel child : children) {
\r
122 Element el = result.createElement("document");
\r
124 el.setAttribute("title", child.getTitle());
\r
125 } catch (DOMException e) {
\r
126 e.printStackTrace();
\r
127 } catch (ClientException e) {
\r
128 e.printStackTrace();
\r
130 el.setAttribute("type", child.getType());
\r
131 el.setAttribute("id", child.getId());
\r
132 el.setAttribute("name", child.getName());
\r
133 el.setAttribute("url", getRelURL(workspaceName, child.getRef()
\r
135 current.appendChild(el);
\r
139 } catch (Exception e) {
\r
140 e.printStackTrace();
\r
143 if (logger.isDebugEnabled() == true) {
\r
144 System.out.println(result.asXML());
\r
150 protected void releaseRepositorySession(RepositoryInstance repoSession) {
\r
153 NuxeoConnector nuxeoConnector = NuxeoConnector.getInstance();
\r
154 nuxeoConnector.releaseRepositorySession(repoSession);
\r
155 } catch (Exception e) {
\r
156 logger.error("Could not close the repository session", e);
\r
157 // no need to throw this service specific exception
\r
161 private static String getRelURL(String repo, String uuid) {
\r
162 return '/' + repo + '/' + uuid;
\r