import org.collectionspace.services.common.relation.RelationJAXBSchema;
import org.collectionspace.services.common.repository.DocumentException;
import org.collectionspace.services.common.repository.DocumentWrapper;
+import org.collectionspace.services.nuxeo.util.NuxeoUtils;
import org.collectionspace.services.relation.RelationsCommonList;
import org.collectionspace.services.relation.RelationsCommonList.RelationListItem;
-import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelList;
import org.slf4j.Logger;
//FIXME: iterating over a long list of documents is not a long term
//strategy...need to change to more efficient iterating in future
Iterator<DocumentModel> iter = docList.iterator();
- while(iter.hasNext()){
+ while (iter.hasNext()) {
DocumentModel docModel = iter.next();
RelationListItem relationListItem = getRelationListItem(docModel,
serviceContextPath);
public static RelationListItem getRelationListItem(DocumentModel docModel,
String serviceContextPath) throws Exception {
RelationListItem relationListItem = new RelationListItem();
- relationListItem.setUri(serviceContextPath + docModel.getId());
- relationListItem.setCsid(docModel.getId());
+ String id = NuxeoUtils.extractId(docModel.getPathAsString());
+ relationListItem.setCsid(id);
+ relationListItem.setUri(serviceContextPath + id);
return relationListItem;
}
throws Exception {
boolean result = false;
Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME, RelationJAXBSchema.DOCUMENT_ID_1);
- if(valueObject != null && csid != null){
+ if (valueObject != null && csid != null) {
String subjectID = (String) valueObject;
result = subjectID.equals(csid);
}
Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
RelationJAXBSchema.DOCUMENT_ID_2);
- if(valueObject != null && csid != null){
+ if (valueObject != null && csid != null) {
String subjectID = (String) valueObject;
result = subjectID.equals(csid);
}
Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
RelationJAXBSchema.RELATIONSHIP_TYPE);
- if(valueObject != null && predicate != null){
+ if (valueObject != null && predicate != null) {
String relationType = (String) valueObject;
result = predicate.equalsIgnoreCase(relationType);
}
Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
RelationJAXBSchema.DOCUMENT_ID_1);
- if(valueObject != null){
+ if (valueObject != null) {
String subjectID = (String) valueObject;
- if(subjectID.equals(csid) == true){
+ if (subjectID.equals(csid) == true) {
valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
RelationJAXBSchema.DOCUMENT_ID_2);
- if(valueObject != null){
+ if (valueObject != null) {
result = (String) valueObject;
}
}
String objectCsid) throws DocumentException {
boolean result = true;
- try{
+ try {
block:
{
- if(subjectCsid != null){
- if(isSubjectOfRelation(subjectCsid, documentModel) == false){
+ if (subjectCsid != null) {
+ if (isSubjectOfRelation(subjectCsid, documentModel) == false) {
result = false;
break block;
}
}
- if(predicate != null){
- if(isPredicateOfRelation(predicate, documentModel) == false){
+ if (predicate != null) {
+ if (isPredicateOfRelation(predicate, documentModel) == false) {
result = false;
break block;
}
}
- if(objectCsid != null){
- if(isObjectOfRelation(objectCsid, documentModel) == false){
+ if (objectCsid != null) {
+ if (isObjectOfRelation(objectCsid, documentModel) == false) {
result = false;
break block;
}
}
}
- }catch(Exception e){
- if(logger.isDebugEnabled() == true){
+ } catch (Exception e) {
+ if (logger.isDebugEnabled() == true) {
e.printStackTrace();
}
throw new DocumentException(e);
*/
package org.collectionspace.services.nuxeo.client.java;
-import java.util.Hashtable;
+
+import java.util.UUID;
import org.collectionspace.services.common.repository.RepositoryClient;
import org.collectionspace.services.common.context.ServiceContext;
import org.collectionspace.services.common.repository.BadRequestException;
import org.collectionspace.services.common.repository.DocumentHandler;
import org.collectionspace.services.common.repository.DocumentException;
import org.collectionspace.services.common.repository.DocumentHandler.Action;
+import org.collectionspace.services.nuxeo.util.NuxeoUtils;
import org.nuxeo.common.utils.IdUtils;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.DocumentModel;
DocumentHandler handler) throws BadRequestException,
DocumentException {
- if(handler.getDocumentType() == null){
+ if (handler.getDocumentType() == null) {
throw new IllegalArgumentException(
"RemoteRepositoryClient.create: docType is missing");
}
- if(handler == null){
+ if (handler == null) {
throw new IllegalArgumentException(
"RemoteRepositoryClient.create: handler is missing");
}
String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
- if(nuxeoWspaceId == null){
+ if (nuxeoWspaceId == null) {
throw new DocumentNotFoundException(
"Unable to find workspace for service " + ctx.getServiceName() +
" check if the workspace exists in the Nuxeo repository");
}
RepositoryInstance repoSession = null;
- try{
+ try {
handler.prepare(Action.CREATE);
repoSession = getRepositorySession();
DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);
DocumentModel wspaceDoc = repoSession.getDocument(nuxeoWspace);
String wspacePath = wspaceDoc.getPathAsString();
- String id = IdUtils.generateId("New " + handler.getDocumentType());
+ //give our own ID so PathRef could be constructed later on
+ String id = IdUtils.generateId(UUID.randomUUID().toString());
// create document model
DocumentModel doc = repoSession.createDocumentModel(wspacePath, id,
handler.getDocumentType());
doc = repoSession.createDocument(doc);
repoSession.save();
handler.complete(Action.CREATE, wrapDoc);
- return doc.getId();
- }catch(Exception e){
- if(logger.isDebugEnabled()){
+ return id;
+ } catch (Exception e) {
+ if (logger.isDebugEnabled()) {
logger.debug("Caught exception ", e);
}
throw new DocumentException(e);
- }finally{
- if(repoSession != null){
+ } finally {
+ if (repoSession != null) {
releaseRepositorySession(repoSession);
}
}
public void get(ServiceContext ctx, String id, DocumentHandler handler)
throws DocumentNotFoundException, DocumentException {
- if(handler == null){
+ if (handler == null) {
throw new IllegalArgumentException(
"RemoteRepositoryClient.get: handler is missing");
}
RepositoryInstance repoSession = null;
- try{
+ try {
handler.prepare(Action.GET);
repoSession = getRepositorySession();
- //FIXME, there is a potential privacy violation here, one tenant could
- //retrieve doc id of another tenant and could retrieve the document
- //PathRef does not seem to come to rescue as expected. Needs more thoughts.
- DocumentRef docRef = new IdRef(id);
+ DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
DocumentModel doc = null;
- try{
+ try {
doc = repoSession.getDocument(docRef);
- }catch(ClientException ce){
+ } catch (ClientException ce) {
String msg = "could not find document with id=" + id;
logger.error(msg, ce);
throw new DocumentNotFoundException(msg, ce);
DocumentModelWrapper wrapDoc = new DocumentModelWrapper(doc);
handler.handle(Action.GET, wrapDoc);
handler.complete(Action.GET, wrapDoc);
- }catch(IllegalArgumentException iae){
+ } catch (IllegalArgumentException iae) {
throw iae;
- }catch(DocumentException de){
+ } catch (DocumentException de) {
throw de;
- }catch(Exception e){
- if(logger.isDebugEnabled()){
+ } catch (Exception e) {
+ if (logger.isDebugEnabled()) {
logger.debug("Caught exception ", e);
}
throw new DocumentException(e);
- }finally{
- if(repoSession != null){
+ } finally {
+ if (repoSession != null) {
releaseRepositorySession(repoSession);
}
}
@Override
public void getAll(ServiceContext ctx, DocumentHandler handler)
throws DocumentNotFoundException, DocumentException {
- if(handler == null){
+ if (handler == null) {
throw new IllegalArgumentException(
"RemoteRepositoryClient.getAll: handler is missing");
}
String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
- if(nuxeoWspaceId == null){
+ if (nuxeoWspaceId == null) {
throw new DocumentNotFoundException(
"Unable to find workspace for service " +
ctx.getServiceName() +
}
RepositoryInstance repoSession = null;
- try{
+ try {
handler.prepare(Action.GET_ALL);
repoSession = getRepositorySession();
DocumentRef wsDocRef = new IdRef(nuxeoWspaceId);
docList);
handler.handle(Action.GET_ALL, wrapDoc);
handler.complete(Action.GET_ALL, wrapDoc);
- }catch(DocumentException de){
+ } catch (DocumentException de) {
throw de;
- }catch(Exception e){
- if(logger.isDebugEnabled()){
+ } catch (Exception e) {
+ if (logger.isDebugEnabled()) {
logger.debug("Caught exception ", e);
}
throw new DocumentException(e);
- }finally{
- if(repoSession != null){
+ } finally {
+ if (repoSession != null) {
releaseRepositorySession(repoSession);
}
}
public void update(ServiceContext ctx, String id, DocumentHandler handler)
throws BadRequestException, DocumentNotFoundException,
DocumentException {
- if(id == null){
+ if (id == null) {
throw new BadRequestException(
"RemoteRepositoryClient.update: id is missing");
}
- if(handler == null){
+ if (handler == null) {
throw new IllegalArgumentException(
"RemoteRepositoryClient.update: handler is missing");
}
RepositoryInstance repoSession = null;
- try{
+ try {
handler.prepare(Action.UPDATE);
repoSession = getRepositorySession();
- //FIXME, there is a potential privacy violation here, one tenant could
- //retrieve doc id of another tenant and could retrieve the document
- //PathRef does not seem to come to rescue as expected. Needs more thoughts.
- DocumentRef docRef = new IdRef(id);
+ DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
DocumentModel doc = null;
- try{
+ try {
doc = repoSession.getDocument(docRef);
- }catch(ClientException ce){
+ } catch (ClientException ce) {
String msg = "Could not find document to update with id=" + id;
logger.error(msg, ce);
throw new DocumentNotFoundException(msg, ce);
repoSession.saveDocument(doc);
repoSession.save();
handler.complete(Action.UPDATE, wrapDoc);
- }catch(DocumentException de){
+ } catch (DocumentException de) {
throw de;
- }catch(Exception e){
- if(logger.isDebugEnabled()){
+ } catch (Exception e) {
+ if (logger.isDebugEnabled()) {
logger.debug("Caught exception ", e);
}
throw new DocumentException(e);
- }finally{
- if(repoSession != null){
+ } finally {
+ if (repoSession != null) {
releaseRepositorySession(repoSession);
}
}
public void delete(ServiceContext ctx, String id) throws DocumentNotFoundException,
DocumentException {
- if(logger.isDebugEnabled()){
+ if (logger.isDebugEnabled()) {
logger.debug("deleting document with id=" + id);
}
RepositoryInstance repoSession = null;
- try{
+ try {
repoSession = getRepositorySession();
- //FIXME, there is a potential privacy violation here, one tenant could
- //retrieve doc id of another tenant and could retrieve the document
- //PathRef does not seem to come to rescue as expected. needs more thoughts.
- DocumentRef docRef = new IdRef(id);
- try{
+ DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
+ try {
repoSession.removeDocument(docRef);
- }catch(ClientException ce){
+ } catch (ClientException ce) {
String msg = "could not find document to delete with id=" + id;
logger.error(msg, ce);
throw new DocumentNotFoundException(msg, ce);
}
repoSession.save();
- }catch(DocumentException de){
+ } catch (DocumentException de) {
throw de;
- }catch(Exception e){
- if(logger.isDebugEnabled()){
+ } catch (Exception e) {
+ if (logger.isDebugEnabled()) {
logger.debug("Caught exception ", e);
}
throw new DocumentException(e);
- }finally{
- if(repoSession != null){
+ } finally {
+ if (repoSession != null) {
releaseRepositorySession(repoSession);
}
}
// Authentication failures happen while trying to reuse the session
NuxeoClient client = NuxeoConnector.getInstance().getClient();
RepositoryInstance repoSession = client.openRepository();
- if(logger.isDebugEnabled()){
+ if (logger.isDebugEnabled()) {
logger.debug("getRepository() repository root: " + repoSession.getRootDocument());
}
return repoSession;
}
private void releaseRepositorySession(RepositoryInstance repoSession) {
- try{
+ try {
NuxeoClient client = NuxeoConnector.getInstance().getClient();
// release session
client.releaseRepository(repoSession);
- }catch(Exception e){
+ } catch (Exception e) {
logger.error("Could not close the repository session", e);
// no need to throw this service specific exception
}
}
+
}
import java.io.Serializable;
import java.util.Map;
+import java.util.StringTokenizer;
+import org.collectionspace.services.common.context.ServiceContext;
import org.collectionspace.services.common.repository.DocumentException;
import org.dom4j.Document;
import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
import org.nuxeo.ecm.core.api.DocumentRef;
import org.nuxeo.ecm.core.api.IdRef;
+import org.nuxeo.ecm.core.api.PathRef;
import org.nuxeo.ecm.core.api.model.DocumentPart;
import org.nuxeo.ecm.core.io.DocumentPipe;
import org.nuxeo.ecm.core.io.DocumentReader;
DocumentReader reader = null;
ByteArrayOutputStream baos = null;
ByteArrayInputStream bais = null;
- try{
+ try {
baos = new ByteArrayOutputStream();
//nuxeo io.impl begin
reader = new SingleDocumentReader(repoSession, nuxeoDoc);
bais = new ByteArrayInputStream(baos.toByteArray());
SAXReader saxReader = new SAXReader();
doc = saxReader.read(bais);
- }catch(Exception e){
- if(logger.isDebugEnabled()){
+ } catch (Exception e) {
+ if (logger.isDebugEnabled()) {
logger.debug("Caught exception while processing document ", e);
}
throw new DocumentException(e);
- }finally{
- if(reader != null){
+ } finally {
+ if (reader != null) {
reader.close();
}
- if(writer != null){
+ if (writer != null) {
writer.close();
}
- try{
- if(bais != null){
+ try {
+ if (bais != null) {
bais.close();
}
- if(baos != null){
+ if (baos != null) {
baos.close();
}
- }catch(IOException ioe){
+ } catch (IOException ioe) {
String msg = "Failed to close io streams";
logger.error(msg + " {}", ioe);
throw new DocumentException(ioe);
throws DocumentException {
DocumentModel result = null;
- try{
+ try {
DocumentRef documentRef = new IdRef(csid);
result = repoSession.getDocument(documentRef);
- }catch(ClientException e){
+ } catch (ClientException e) {
e.printStackTrace();
}
public static void printDocumentModel(DocumentModel docModel) throws Exception {
String[] schemas = docModel.getDeclaredSchemas();
- for(int i = 0; schemas != null && i < schemas.length; i++){
+ for (int i = 0; schemas != null && i < schemas.length; i++) {
logger.debug("Schema-" + i + "=" + schemas[i]);
}
DocumentPart[] parts = docModel.getParts();
Map<String, Serializable> propertyValues = null;
- for(int i = 0; parts != null && i < parts.length; i++){
+ for (int i = 0; parts != null && i < parts.length; i++) {
logger.debug("Part-" + i + " name =" + parts[i].getName());
logger.debug("Part-" + i + " path =" + parts[i].getPath());
logger.debug("Part-" + i + " schema =" + parts[i].getSchema().getName());
}
}
+
+ /**
+ * createPathRef creates a PathRef for given service context using given id
+ * @param ctx
+ * @param id
+ * @return PathRef
+ */
+ public static DocumentRef createPathRef(ServiceContext ctx, String id) {
+ return new PathRef("/" + ctx.getRepositoryDomainName() +
+ "/" + "workspaces" +
+ "/" + ctx.getRepositoryWorkspaceName() +
+ "/" + id);
+ }
+
+ /**
+ * extractId extracts id from given path string
+ * @param pathString
+ * @return
+ */
+ public static String extractId(String pathString) {
+ if (pathString == null) {
+ throw new IllegalArgumentException("empty pathString");
+ }
+ String id = null;
+ StringTokenizer stz = new StringTokenizer(pathString, "/");
+ int tokens = stz.countTokens();
+ for (int i = 0; i < tokens - 1; i++) {
+ stz.nextToken();
+ }
+ id = stz.nextToken(); //last token is id
+ return id;
+ }
}