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.nuxeo.client.rest.NuxeoRESTClient;
\r
10 import org.collectionspace.services.nuxeo.client.java.NuxeoConnector;
\r
11 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
\r
13 import org.dom4j.Document;
\r
14 import org.dom4j.DocumentException;
\r
15 import org.dom4j.dom.DOMDocument;
\r
16 import org.dom4j.dom.DOMDocumentFactory;
\r
18 import org.w3c.dom.DOMException;
\r
19 import org.w3c.dom.Element;
\r
21 import org.nuxeo.ecm.core.api.DocumentModel;
\r
22 import org.nuxeo.ecm.core.api.DocumentModelList;
\r
23 import org.nuxeo.ecm.core.api.DocumentRef;
\r
24 import org.nuxeo.ecm.core.api.IdRef;
\r
25 import org.nuxeo.ecm.core.api.ClientException;
\r
26 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
\r
27 import org.nuxeo.ecm.core.client.NuxeoClient;
\r
29 import org.slf4j.Logger;
\r
30 import org.slf4j.LoggerFactory;
\r
36 public abstract class CollectionSpaceServiceNuxeoImpl {
\r
38 // replace host if not running on localhost
\r
39 // static String CS_NUXEO_HOST = "173.45.234.217";
\r
40 static String CS_NUXEO_HOST = "localhost";
\r
41 static String CS_NUXEO_URI = "http://" + CS_NUXEO_HOST + ":8080/nuxeo";
\r
43 protected Logger logger = LoggerFactory
\r
44 .getLogger(CollectionSpaceServiceNuxeoImpl.class);
\r
46 public NuxeoRESTClient getClient() {
\r
47 NuxeoRESTClient nxClient = new NuxeoRESTClient(CS_NUXEO_URI);
\r
49 nxClient.setAuthType(NuxeoRESTClient.AUTH_TYPE_BASIC);
\r
50 nxClient.setBasicAuthentication("Administrator", "Administrator");
\r
55 // FIXME: Replace this method after integration of the relation code
\r
56 protected RepositoryInstance getRepositorySession() throws Exception {
\r
57 // FIXME: is it possible to reuse repository session?
\r
58 // Authentication failures happen while trying to reuse the session
\r
59 NuxeoConnector nuxeoConnector = NuxeoConnector.getInstance();
\r
60 return nuxeoConnector.getRepositorySession();
\r
63 protected Document deleteDocument(RepositoryInstance repoSession, String csid) throws DocumentException,
\r
65 Document result = null;
\r
68 repoSession = getRepositorySession();
\r
69 DocumentRef relDocumentRef = new IdRef(csid);
\r
70 repoSession.removeDocument(relDocumentRef);
\r
72 result = new DOMDocument(); // set to non-null to indicate success
\r
73 } catch (Exception e) {
\r
74 e.printStackTrace();
\r
80 protected Document browseWorkspace(RepositoryInstance repoSession,
\r
81 String workspaceName) {
\r
83 DOMDocumentFactory domfactory = new DOMDocumentFactory();
\r
84 DOMDocument result = (DOMDocument) domfactory.createDocument();
\r
87 repoSession = getRepositorySession();
\r
88 DocumentModel workspaceModel = NuxeoUtils.getWorkspaceModel(repoSession,
\r
91 Element current = result.createElement("document");
\r
93 current.setAttribute("title", workspaceModel.getTitle());
\r
94 } catch (Exception e) {
\r
95 e.printStackTrace();
\r
97 current.setAttribute("type", workspaceModel.getType());
\r
98 current.setAttribute("id", workspaceModel.getId());
\r
99 current.setAttribute("name", workspaceModel.getName());
\r
100 current.setAttribute("url", getRelURL(workspaceName, workspaceModel.getRef().toString()));
\r
101 result.setRootElement((org.dom4j.Element) current);
\r
103 if (workspaceModel.isFolder()) {
\r
104 // Element childrenElem = result.createElement("children");
\r
105 // root.appendChild(childrenElem);
\r
107 DocumentModelList children = null;
\r
109 children = repoSession.getChildren(workspaceModel.getRef());
\r
110 } catch (ClientException e) {
\r
111 e.printStackTrace();
\r
114 for (DocumentModel child : children) {
\r
115 Element el = result.createElement("document");
\r
117 el.setAttribute("title", child.getTitle());
\r
118 } catch (DOMException e) {
\r
119 e.printStackTrace();
\r
120 } catch (ClientException e) {
\r
121 e.printStackTrace();
\r
123 el.setAttribute("type", child.getType());
\r
124 el.setAttribute("id", child.getId());
\r
125 el.setAttribute("name", child.getName());
\r
126 el.setAttribute("url", getRelURL(workspaceName, child.getRef()
\r
128 current.appendChild(el);
\r
132 } catch (Exception e) {
\r
133 e.printStackTrace();
\r
136 if (logger.isDebugEnabled() == true) {
\r
137 System.out.println(result.asXML());
\r
143 protected void releaseRepositorySession(RepositoryInstance repoSession) {
\r
146 NuxeoConnector nuxeoConnector = NuxeoConnector.getInstance();
\r
147 nuxeoConnector.releaseRepositorySession(repoSession);
\r
148 } catch (Exception e) {
\r
149 logger.error("Could not close the repository session", e);
\r
150 // no need to throw this service specific exception
\r
154 private static String getRelURL(String repo, String uuid) {
\r
155 return '/' + repo + '/' + uuid;
\r