From a33e620ee1801d0201929eb215b65eb7701bd2bb Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Sat, 8 Aug 2009 07:00:16 +0000 Subject: [PATCH] CSPACE-332: Majority of this task is now finished. Need to qualify with more testing now. --- .../CollectionObjectResource.java | 2 +- .../nuxeo/CollectionObjectConstants.java | 6 +- .../CollectionObjectDocumentModelHandler.java | 10 +- .../CollectionObjectRepresenationHandler.java | 27 +- .../common/relation/RelationsManager.java | 38 +- .../nuxeo/RelationUtilsNuxeoImpl.java | 95 ++- .../common/repository/DocumentHandler.java | 7 + .../common/repository/RepositoryClient.java | 2 +- .../client/java/DocumentModelHandler.java | 8 +- .../client/java/RepositoryJavaClient.java | 545 ++++++++++-------- .../client/rest/RepositoryRESTClient.java | 8 +- .../services/intake/IntakeResource.java | 2 +- .../intake/nuxeo/IntakeConstants.java | 6 +- .../nuxeo/IntakeDocumentModelHandler.java | 14 +- .../nuxeo/IntakeRepresenationHandler.java | 31 +- .../relation/NewRelationResource.java | 243 ++++++++ .../nuxeo/RelationDocumentModelHandler.java | 24 +- .../nuxeo/RelationNuxeoConstants.java | 3 + .../nuxeo/RelationServiceNuxeoImpl.java | 57 +- 19 files changed, 751 insertions(+), 377 deletions(-) create mode 100644 services/relation/service/src/main/java/org/collectionspace/services/relation/NewRelationResource.java diff --git a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java index 0b7505a51..cff244d51 100644 --- a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java +++ b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java @@ -55,7 +55,7 @@ public class CollectionObjectResource { CollectionObjectHandlerFactory handlerFactory = CollectionObjectHandlerFactory.getInstance(); DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString()); handler.setCommonObject(collectionObject); - csid = client.create(CO_SERVICE_NAME, CollectionObjectConstants.CO_NUXEO_DOCTYPE, handler); + csid = client.create(CO_SERVICE_NAME, handler); collectionObject.setCsid(csid); if(logger.isDebugEnabled()){ verbose("createCollectionObject: ", collectionObject); diff --git a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectConstants.java b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectConstants.java index 9911f4244..b93c26975 100644 --- a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectConstants.java +++ b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectConstants.java @@ -29,8 +29,8 @@ package org.collectionspace.services.collectionobject.nuxeo; */ public class CollectionObjectConstants { - public final static String CO_NUXEO_DOCTYPE = "CollectionObject"; - public final static String CO_NUXEO_SCHEMA_NAME = "collectionobject"; - public final static String CO_NUXEO_DC_TITLE = "CollectionSpace-CollectionObject"; + public final static String NUXEO_DOCTYPE = "CollectionObject"; + public final static String NUXEO_SCHEMA_NAME = "collectionobject"; + public final static String NUXEO_DC_TITLE = "CollectionSpace-CollectionObject"; } diff --git a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectDocumentModelHandler.java b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectDocumentModelHandler.java index 263c36afc..4e593f19d 100644 --- a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectDocumentModelHandler.java +++ b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectDocumentModelHandler.java @@ -25,6 +25,8 @@ package org.collectionspace.services.collectionobject.nuxeo; import java.util.Iterator; import java.util.List; + +import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectConstants; import org.collectionspace.services.CollectionObjectJAXBSchema; import org.collectionspace.services.collectionobject.CollectionObject; import org.collectionspace.services.collectionobject.CollectionObjectList; @@ -132,7 +134,7 @@ public class CollectionObjectDocumentModelHandler //so it does not require hard coding // a default title for the Dublin Core schema - docModel.setPropertyValue("dublincore:title", CollectionObjectConstants.CO_NUXEO_DC_TITLE); + docModel.setPropertyValue("dublincore:title", CollectionObjectConstants.NUXEO_DC_TITLE); // CollectionObject core values if(co.getObjectNumber() != null){ @@ -205,6 +207,10 @@ public class CollectionObjectDocumentModelHandler public void fillCommonObjectList(CollectionObjectList obj, DocumentWrapper wrapDoc) throws Exception { throw new UnsupportedOperationException(); } + + public String getDocumentType() { + return CollectionObjectConstants.NUXEO_DOCTYPE; + } /** * getQProperty converts the given property to qualified schema property @@ -212,7 +218,7 @@ public class CollectionObjectDocumentModelHandler * @return */ private String getQProperty(String prop) { - return CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + ":" + prop; + return CollectionObjectConstants.NUXEO_SCHEMA_NAME + ":" + prop; } } diff --git a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectRepresenationHandler.java b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectRepresenationHandler.java index db1e31fea..44dd741a3 100644 --- a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectRepresenationHandler.java +++ b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectRepresenationHandler.java @@ -26,12 +26,15 @@ package org.collectionspace.services.collectionobject.nuxeo; import java.util.Iterator; import java.util.List; import java.util.Map; + import org.collectionspace.services.CollectionObjectJAXBSchema; import org.collectionspace.services.collectionobject.CollectionObject; import org.collectionspace.services.collectionobject.CollectionObjectList; import org.collectionspace.services.collectionobject.CollectionObjectList.CollectionObjectListItem; import org.collectionspace.services.common.repository.DocumentWrapper; import org.collectionspace.services.nuxeo.client.rest.RepresentationHandler; +import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectConstants; + import org.dom4j.Document; import org.dom4j.Element; import org.slf4j.Logger; @@ -73,42 +76,42 @@ public class CollectionObjectRepresenationHandler CollectionObject co = getCommonObject(); // todo: intelligent merge needed if(co.getObjectNumber() != null){ - queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + + queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OBJECT_NUMBER, co.getObjectNumber()); } if(co.getOtherNumber() != null){ - queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + + queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OTHER_NUMBER, co.getOtherNumber()); } if(co.getBriefDescription() != null){ - queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + + queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.BRIEF_DESCRIPTION, co.getBriefDescription()); } if(co.getComments() != null){ - queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + + queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.COMMENTS, co.getComments()); } if(co.getDistFeatures() != null){ - queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + + queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.DIST_FEATURES, co.getDistFeatures()); } if(co.getObjectName() != null){ - queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + + queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OBJECT_NAME, co.getObjectName()); } if(co.getResponsibleDept() != null){ - queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + + queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.RESPONSIBLE_DEPT, co.getResponsibleDept()); } if(co.getTitle() != null){ - queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + + queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.TITLE, co.getTitle()); } } @@ -135,7 +138,7 @@ public class CollectionObjectRepresenationHandler logger.debug("getCommonObject() populating Common Object"); } // TODO: recognize schema thru namespace uri - if(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME.equals(schemaElement.attribute("name").getValue())){ + if(CollectionObjectConstants.NUXEO_SCHEMA_NAME.equals(schemaElement.attribute("name").getValue())){ Element ele = schemaElement.element(CollectionObjectJAXBSchema.OBJECT_NUMBER); if(ele != null){ co.setObjectNumber((String) ele.getData()); @@ -232,6 +235,10 @@ public class CollectionObjectRepresenationHandler public void setCommonObjectList(CollectionObjectList obj) { this.collectionObjectList = obj; } + + public String getDocumentType() { + return CollectionObjectConstants.NUXEO_DOCTYPE; + } /** * getQProperty converts the given property to qualified schema property @@ -239,7 +246,7 @@ public class CollectionObjectRepresenationHandler * @return */ private String getQProperty(String prop) { - return CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + ":" + prop; + return CollectionObjectConstants.NUXEO_SCHEMA_NAME + ":" + prop; } } diff --git a/services/common/src/main/java/org/collectionspace/services/common/relation/RelationsManager.java b/services/common/src/main/java/org/collectionspace/services/common/relation/RelationsManager.java index 95e1cfde8..0a944c9c2 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/relation/RelationsManager.java +++ b/services/common/src/main/java/org/collectionspace/services/common/relation/RelationsManager.java @@ -38,15 +38,6 @@ import org.dom4j.Document; /** * The Class RelationsManager. - * - * This class loosely uses the RDF terms subject, predicate, object to describe - * relationships between entity objects. For example, if a CollectionObject entity named - * CO#1 is related to a Intake entity named IN#1 then the corresponding - * RDF-like term would be: (Subject) CO#1 (Predicate) has-intake (Object) IN#1. - * - * Many of the methods below, refer to RDF-like terms such as "Subject" and "Object" and - * "Predicate." - * */ public class RelationsManager { @@ -54,15 +45,13 @@ public class RelationsManager { static private RelationUtils relationUtils = new RelationUtilsNuxeoImpl(); /** - * Gets the ALL relationships in the system. + * Gets the relationships. * - * @param repoSession - * the repo session + * @param repoSession the repo session * * @return the relationships * - * @throws DocumentException - * the document exception + * @throws DocumentException the document exception */ static public List getRelationships(Object repoSession) throws DocumentException { @@ -70,7 +59,7 @@ public class RelationsManager { } /** - * Gets the relationships. Null values act as wild card and match everything. + * Gets the relationships. * * @param repoSession the repo session * @param subjectCsid the subject csid @@ -87,18 +76,14 @@ public class RelationsManager { } /** - * Gets the relationships for the entity corresponding to the CSID=csid. - * The csid refers to either the subject OR the object + * Gets the relationships. * - * @param repoSession - * the repo session - * @param csid - * the csid + * @param repoSession the repo session + * @param csid the csid * * @return the relationships * - * @throws DocumentException - * the document exception + * @throws DocumentException the document exception */ static public List getRelationships(Object repoSession, String csid) throws DocumentException { @@ -125,6 +110,13 @@ public class RelationsManager { predicate, objectCsid); } + /** + * Gets the q property name. + * + * @param propertyName the property name + * + * @return the q property name + */ static public String getQPropertyName(String propertyName) { return relationUtils.getQPropertyName(propertyName); } diff --git a/services/common/src/main/java/org/collectionspace/services/common/relation/nuxeo/RelationUtilsNuxeoImpl.java b/services/common/src/main/java/org/collectionspace/services/common/relation/nuxeo/RelationUtilsNuxeoImpl.java index 115994d9a..eb963480f 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/relation/nuxeo/RelationUtilsNuxeoImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/relation/nuxeo/RelationUtilsNuxeoImpl.java @@ -29,6 +29,7 @@ package org.collectionspace.services.common.relation.nuxeo; import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -37,7 +38,9 @@ import org.collectionspace.services.common.relation.RelationListItemJAXBSchema; import org.collectionspace.services.common.relation.RelationUtils; import org.collectionspace.services.relation.Relation; +import org.collectionspace.services.relation.RelationList; import org.collectionspace.services.relation.RelationshipType; +import org.collectionspace.services.relation.RelationList.RelationListItem; import org.collectionspace.services.nuxeo.util.NuxeoUtils; import org.collectionspace.services.common.repository.DocumentException; @@ -66,6 +69,8 @@ public class RelationUtilsNuxeoImpl implements RelationUtils { /** The C s_ relatio n_ servic e_ name. */ static public String CS_RELATION_SERVICE_NAME = "relations"; + + /** The C s_ empt y_ string. */ static public String CS_EMPTY_STRING = ""; /** The Constant REL_NUXEO_DOCTYPE. */ @@ -128,6 +133,44 @@ public class RelationUtilsNuxeoImpl implements RelationUtils { } } + /** + * Fill relation list item from doc model. + * + * @param relationListItem the relation list item + * @param relDocModel the rel doc model + * + * @throws DocumentException the document exception + */ + static public void fillRelationListItemFromDocModel(RelationListItem relationListItem, + DocumentModel relDocModel) + throws DocumentException { + + try { + relationListItem.setUri( + relDocModel.getId()); + relationListItem.setCsid( + getRelURL(CS_RELATION_SERVICE_NAME, relDocModel.getId())); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception in fillRelationListItemFromDocModel", e); + } + throw new DocumentException(e); + } + } + + /** + * Fill doc model list from relation list. + * + * @param relationList the relation list + * @param relDocModelList the rel doc model list + * + * @throws Exception the exception + */ + static public void fillDocModelListFromRelationList(RelationList relationList, + DocumentModelList relDocModelList) + throws Exception { + } + /** * Fill doc model from relation. * @@ -364,7 +407,10 @@ public class RelationUtilsNuxeoImpl implements RelationUtils { return result; } - + + /* (non-Javadoc) + * @see org.collectionspace.services.common.relation.RelationUtils#getQPropertyName(java.lang.String) + */ public String getQPropertyName(String propertyName) { return "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/" + propertyName; } @@ -419,6 +465,16 @@ public class RelationUtilsNuxeoImpl implements RelationUtils { return result; } + /** + * Checks if is predicate of relation. + * + * @param predicate the predicate + * @param documentModel the document model + * + * @return true, if is predicate of relation + * + * @throws ClientException the client exception + */ private boolean isPredicateOfRelation(String predicate, DocumentModel documentModel) throws ClientException { boolean result = false; @@ -435,12 +491,12 @@ public class RelationUtilsNuxeoImpl implements RelationUtils { } /** - * Gets the object ID from of the (Subject-Predicate-Object) relationship. + * Gets the object from subject. * - * @param csid the ID of the 'Subject' (Subject-Predicate-Object) - * @param documentModel represents the relation entry. + * @param csid the csid + * @param documentModel the document model * - * @return the object csid of the relationship + * @return the object from subject * * @throws ClientException the client exception */ @@ -465,6 +521,15 @@ public class RelationUtilsNuxeoImpl implements RelationUtils { return result; } + /** + * Gets the document. + * + * @param relationList the relation list + * + * @return the document + * + * @throws DocumentException the document exception + */ static public Document getDocument(List relationList) throws DocumentException { DOMDocumentFactory domfactory = new DOMDocumentFactory(); @@ -504,6 +569,18 @@ public class RelationUtilsNuxeoImpl implements RelationUtils { return result; } + /** + * Checks if is query match. + * + * @param documentModel the document model + * @param subjectCsid the subject csid + * @param predicate the predicate + * @param objectCsid the object csid + * + * @return true, if is query match + * + * @throws ClientException the client exception + */ private boolean isQueryMatch(DocumentModel documentModel, String subjectCsid, String predicate, @@ -534,6 +611,14 @@ public class RelationUtilsNuxeoImpl implements RelationUtils { return result; } + /** + * Gets the rel url. + * + * @param repo the repo + * @param uuid the uuid + * + * @return the rel url + */ private static String getRelURL(String repo, String uuid) { return '/' + repo + '/' + uuid; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/repository/DocumentHandler.java b/services/common/src/main/java/org/collectionspace/services/common/repository/DocumentHandler.java index 3dc2b7fbb..f3f3e2512 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/repository/DocumentHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/common/repository/DocumentHandler.java @@ -153,6 +153,13 @@ public interface DocumentHandler { * @throws DocumentException */ public Document getDocument(DocumentWrapper docWrap) throws DocumentException; + + /** + * Gets the document type. + * + * @return the document type + */ + public String getDocumentType(); /** * getProperties diff --git a/services/common/src/main/java/org/collectionspace/services/common/repository/RepositoryClient.java b/services/common/src/main/java/org/collectionspace/services/common/repository/RepositoryClient.java index a5a7e8335..167261501 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/repository/RepositoryClient.java +++ b/services/common/src/main/java/org/collectionspace/services/common/repository/RepositoryClient.java @@ -47,7 +47,7 @@ public interface RepositoryClient { * @throws BadRequestException data input is bad * @throws DocumentException */ - String create(String serviceName, String docType, DocumentHandler handler) throws BadRequestException, DocumentException; + String create(String serviceName, DocumentHandler handler) throws BadRequestException, DocumentException; /** * delete a document from the Document repository diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java index 9738000c8..3bb10b395 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java @@ -150,7 +150,13 @@ public abstract class DocumentModelHandler DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject(); return NuxeoUtils.getDocument(getRepositorySession(), docModel); } - + + /* (non-Javadoc) + * @see org.collectionspace.services.common.repository.DocumentHandler#getDocumentType() + */ + @Override + public abstract String getDocumentType(); + /** * @return the properties */ diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java index 3831c4565..330661c07 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java @@ -36,275 +36,316 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * RepositoryJavaClient is used to perform CRUD operations on documents - * in Nuxeo repository using Remote Java APIs. It uses @see DocumentHandler - * as IOHandler with the client. - * - * $LastChangedRevision: $ - * $LastChangedDate: $ + * RepositoryJavaClient is used to perform CRUD operations on documents in Nuxeo + * repository using Remote Java APIs. It uses @see DocumentHandler as IOHandler + * with the client. + * + * $LastChangedRevision: $ $LastChangedDate: $ */ public class RepositoryJavaClient implements RepositoryClient { - private final Logger logger = LoggerFactory.getLogger(RepositoryJavaClient.class); + private final Logger logger = LoggerFactory + .getLogger(RepositoryJavaClient.class); - public RepositoryJavaClient() { - } + public RepositoryJavaClient() { + } - /** - * create document in the Nuxeo repository - * @param serviceName entity service for which document is created. this is used to find mapping - * to a Nuxeo workspace using service-config.xml - * @param docType of the document created - * @param handler should be used by the caller to provide and transform the document - * @return id in repository of the newly created document - * @throws DocumentException - */ - @Override - public String create(String serviceName, String docType, DocumentHandler handler) throws BadRequestException, DocumentException { + /** + * create document in the Nuxeo repository + * + * @param serviceName + * entity service for which document is created. this is used to + * find mapping to a Nuxeo workspace using service-config.xml + * @param docType + * of the document created + * @param handler + * should be used by the caller to provide and transform the + * document + * @return id in repository of the newly created document + * @throws DocumentException + */ + @Override + public String create(String serviceName, + DocumentHandler handler) throws BadRequestException, + DocumentException { - if(serviceName == null){ - throw new IllegalArgumentException("RemoteRepositoryClient.create: serviceName is missing"); - } - if(docType == null){ - throw new IllegalArgumentException("RemoteRepositoryClient.create: docType is missing"); - } - if(handler == null){ - throw new IllegalArgumentException("RemoteRepositoryClient.create: handler is missing"); - } - ServiceMain smain = ServiceMain.getInstance(); - String nuxeoWspaceId = smain.getWorkspaceId(serviceName); - if(nuxeoWspaceId == null){ - throw new DocumentNotFoundException("Unable to find workspace for service " + serviceName + - " check if the mapping exists in service-config.xml or " + - " the the mapped workspace exists in the Nuxeo repository"); - } - RepositoryInstance repoSession = null; - try{ - handler.prepare(Action.CREATE); - repoSession = getRepositorySession(); + if (serviceName == null) { + throw new IllegalArgumentException( + "RemoteRepositoryClient.create: serviceName is missing"); + } + if (handler.getDocumentType() == null) { + throw new IllegalArgumentException( + "RemoteRepositoryClient.create: docType is missing"); + } + if (handler == null) { + throw new IllegalArgumentException( + "RemoteRepositoryClient.create: handler is missing"); + } + ServiceMain smain = ServiceMain.getInstance(); + String nuxeoWspaceId = smain.getWorkspaceId(serviceName); + if (nuxeoWspaceId == null) { + throw new DocumentNotFoundException( + "Unable to find workspace for service " + + serviceName + + " check if the mapping exists in service-config.xml or " + + " the the mapped workspace exists in the Nuxeo repository"); + } + RepositoryInstance repoSession = null; + 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 " + docType); - //create document model - DocumentModel doc = repoSession.createDocumentModel(wspacePath, id, docType); - ((DocumentModelHandler) handler).setRepositorySession(repoSession); - DocumentModelWrapper wrapDoc = new DocumentModelWrapper(doc); - handler.handle(Action.CREATE, wrapDoc); - //create document with documentmodel - doc = repoSession.createDocument(doc); - repoSession.save(); - return doc.getId(); - }catch(Exception e){ - if(logger.isDebugEnabled()){ - logger.debug("Caught exception ", e); - } - throw new DocumentException(e); - }finally{ - if(repoSession != null){ - releaseRepositorySession(repoSession); - } - } + DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId); + DocumentModel wspaceDoc = repoSession.getDocument(nuxeoWspace); + String wspacePath = wspaceDoc.getPathAsString(); + String id = IdUtils.generateId("New " + handler.getDocumentType()); + // create document model + DocumentModel doc = repoSession.createDocumentModel(wspacePath, id, + handler.getDocumentType()); + ((DocumentModelHandler) handler).setRepositorySession(repoSession); + DocumentModelWrapper wrapDoc = new DocumentModelWrapper(doc); + handler.handle(Action.CREATE, wrapDoc); + // create document with documentmodel + doc = repoSession.createDocument(doc); + repoSession.save(); + return doc.getId(); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } finally { + if (repoSession != null) { + releaseRepositorySession(repoSession); + } + } - } + } - /** - * get document from the Nuxeo repository - * @param id of the document to retrieve - * @param handler should be used by the caller to provide and transform the document - * @throws DocumentException - */ - @Override - public void get(String id, DocumentHandler handler) throws DocumentNotFoundException, DocumentException { + /** + * get document from the Nuxeo repository + * + * @param id + * of the document to retrieve + * @param handler + * should be used by the caller to provide and transform the + * document + * @throws DocumentException + */ + @Override + public void get(String id, DocumentHandler handler) + throws DocumentNotFoundException, DocumentException { - if(handler == null){ - throw new IllegalArgumentException("RemoteRepositoryClient.get: handler is missing"); - } - RepositoryInstance repoSession = null; + if (handler == null) { + throw new IllegalArgumentException( + "RemoteRepositoryClient.get: handler is missing"); + } + RepositoryInstance repoSession = null; - try{ - handler.prepare(Action.GET); - repoSession = getRepositorySession(); - DocumentRef docRef = new IdRef(id); - DocumentModel doc = null; - try{ - doc = repoSession.getDocument(docRef); - }catch(ClientException ce){ - String msg = "could not find document with id=" + id; - logger.error(msg, ce); - throw new DocumentNotFoundException(msg, ce); - } - ((DocumentModelHandler) handler).setRepositorySession(repoSession); - DocumentModelWrapper wrapDoc = new DocumentModelWrapper(doc); - handler.handle(Action.GET, wrapDoc); - }catch(IllegalArgumentException iae) { - throw iae; - }catch(DocumentException de){ - throw de; - }catch(Exception e){ - if(logger.isDebugEnabled()){ - logger.debug("Caught exception ", e); - } - throw new DocumentException(e); - }finally{ - if(repoSession != null){ - releaseRepositorySession(repoSession); - } - } - } + try { + handler.prepare(Action.GET); + repoSession = getRepositorySession(); + DocumentRef docRef = new IdRef(id); + DocumentModel doc = null; + try { + doc = repoSession.getDocument(docRef); + } catch (ClientException ce) { + String msg = "could not find document with id=" + id; + logger.error(msg, ce); + throw new DocumentNotFoundException(msg, ce); + } + ((DocumentModelHandler) handler).setRepositorySession(repoSession); + DocumentModelWrapper wrapDoc = new DocumentModelWrapper(doc); + handler.handle(Action.GET, wrapDoc); + } catch (IllegalArgumentException iae) { + throw iae; + } catch (DocumentException de) { + throw de; + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } finally { + if (repoSession != null) { + releaseRepositorySession(repoSession); + } + } + } - /** - * getAll get all documents for an entity entity service from the Nuxeo repository - * @param serviceName entity service for which documents are retrieved. this is used to find mapping - * to a Nuxeo workspace using service-config.xml - * @param handler should be used by the caller to provide and transform the document - * @throws DocumentException - */ - @Override - public void getAll(String serviceName, DocumentHandler handler) throws DocumentNotFoundException, DocumentException { - if(serviceName == null){ - throw new IllegalArgumentException("RemoteRepositoryClient.getAll: serviceName is missing"); - } - if(handler == null){ - throw new IllegalArgumentException("RemoteRepositoryClient.getAll: handler is missing"); - } - ServiceMain smain = ServiceMain.getInstance(); - String nuxeoWspaceId = smain.getWorkspaceId(serviceName); - if(nuxeoWspaceId == null){ - throw new DocumentNotFoundException("Unable to find workspace for service " + serviceName + - " check if the mapping exists in service-config.xml or " + - " the the mapped workspace exists in the Nuxeo repository"); - } - RepositoryInstance repoSession = null; + /** + * getAll get all documents for an entity entity service from the Nuxeo + * repository + * + * @param serviceName + * entity service for which documents are retrieved. this is used + * to find mapping to a Nuxeo workspace using service-config.xml + * @param handler + * should be used by the caller to provide and transform the + * document + * @throws DocumentException + */ + @Override + public void getAll(String serviceName, DocumentHandler handler) + throws DocumentNotFoundException, DocumentException { + if (serviceName == null) { + throw new IllegalArgumentException( + "RemoteRepositoryClient.getAll: serviceName is missing"); + } + if (handler == null) { + throw new IllegalArgumentException( + "RemoteRepositoryClient.getAll: handler is missing"); + } + ServiceMain smain = ServiceMain.getInstance(); + String nuxeoWspaceId = smain.getWorkspaceId(serviceName); + if (nuxeoWspaceId == null) { + throw new DocumentNotFoundException( + "Unable to find workspace for service " + + serviceName + + " check if the mapping exists in service-config.xml or " + + " the the mapped workspace exists in the Nuxeo repository"); + } + RepositoryInstance repoSession = null; - try{ - handler.prepare(Action.GET_ALL); - repoSession = getRepositorySession(); - DocumentRef wsDocRef = new IdRef(nuxeoWspaceId); - DocumentModelList docList = repoSession.getChildren(wsDocRef); + try { + handler.prepare(Action.GET_ALL); + repoSession = getRepositorySession(); + DocumentRef wsDocRef = new IdRef(nuxeoWspaceId); + DocumentModelList docList = repoSession.getChildren(wsDocRef); - ((DocumentModelHandler) handler).setRepositorySession(repoSession); - DocumentModelListWrapper wrapDoc = new DocumentModelListWrapper(docList); - handler.handle(Action.GET_ALL, wrapDoc); - - }catch(DocumentException de){ - throw de; - }catch(Exception e){ - if(logger.isDebugEnabled()){ - logger.debug("Caught exception ", e); - } - throw new DocumentException(e); - }finally{ - if(repoSession != null){ - releaseRepositorySession(repoSession); - } - } - } + ((DocumentModelHandler) handler).setRepositorySession(repoSession); + DocumentModelListWrapper wrapDoc = new DocumentModelListWrapper( + docList); + handler.handle(Action.GET_ALL, wrapDoc); - /** - * update given document in the Nuxeo repository - * @param id of the document - * @param handler should be used by the caller to provide and transform the document - * @throws DocumentException - */ - @Override - public void update(String id, DocumentHandler handler) throws BadRequestException, DocumentNotFoundException, DocumentException { - if(id == null){ - throw new BadRequestException("RemoteRepositoryClient.update: id is missing"); - } - if(handler == null){ - throw new IllegalArgumentException("RemoteRepositoryClient.update: handler is missing"); - } - RepositoryInstance repoSession = null; - try{ - handler.prepare(Action.UPDATE); - repoSession = getRepositorySession(); - DocumentRef docRef = new IdRef(id); - DocumentModel doc = null; - try{ - doc = repoSession.getDocument(docRef); - }catch(ClientException ce){ - String msg = "Could not find document to update with id=" + id; - logger.error(msg, ce); - throw new DocumentNotFoundException(msg, ce); - } - ((DocumentModelHandler) handler).setRepositorySession(repoSession); - DocumentModelWrapper wrapDoc = new DocumentModelWrapper(doc); - handler.handle(Action.UPDATE, wrapDoc); - repoSession.saveDocument(doc); - repoSession.save(); - }catch(DocumentException de){ - throw de; - }catch(Exception e){ - if(logger.isDebugEnabled()){ - logger.debug("Caught exception ", e); - } - throw new DocumentException(e); - }finally{ - if(repoSession != null){ - releaseRepositorySession(repoSession); - } - } - } + } catch (DocumentException de) { + throw de; + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } finally { + if (repoSession != null) { + releaseRepositorySession(repoSession); + } + } + } - /** - * delete a document from the Nuxeo repository - * @param id of the document - * @throws DocumentException - */ - @Override - public void delete(String id) throws DocumentNotFoundException, DocumentException { + /** + * update given document in the Nuxeo repository + * + * @param id + * of the document + * @param handler + * should be used by the caller to provide and transform the + * document + * @throws DocumentException + */ + @Override + public void update(String id, DocumentHandler handler) + throws BadRequestException, DocumentNotFoundException, + DocumentException { + if (id == null) { + throw new BadRequestException( + "RemoteRepositoryClient.update: id is missing"); + } + if (handler == null) { + throw new IllegalArgumentException( + "RemoteRepositoryClient.update: handler is missing"); + } + RepositoryInstance repoSession = null; + try { + handler.prepare(Action.UPDATE); + repoSession = getRepositorySession(); + DocumentRef docRef = new IdRef(id); + DocumentModel doc = null; + try { + doc = repoSession.getDocument(docRef); + } catch (ClientException ce) { + String msg = "Could not find document to update with id=" + id; + logger.error(msg, ce); + throw new DocumentNotFoundException(msg, ce); + } + ((DocumentModelHandler) handler).setRepositorySession(repoSession); + DocumentModelWrapper wrapDoc = new DocumentModelWrapper(doc); + handler.handle(Action.UPDATE, wrapDoc); + repoSession.saveDocument(doc); + repoSession.save(); + } catch (DocumentException de) { + throw de; + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } finally { + if (repoSession != null) { + releaseRepositorySession(repoSession); + } + } + } - if(logger.isDebugEnabled()){ - logger.debug("deleting document with id=" + id); - } - RepositoryInstance repoSession = null; - try{ - repoSession = getRepositorySession(); - DocumentRef docRef = new IdRef(id); - try{ - repoSession.removeDocument(docRef); - }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){ - throw de; - }catch(Exception e){ - if(logger.isDebugEnabled()){ - logger.debug("Caught exception ", e); - } - throw new DocumentException(e); - }finally{ - if(repoSession != null){ - releaseRepositorySession(repoSession); - } - } - } + /** + * delete a document from the Nuxeo repository + * + * @param id + * of the document + * @throws DocumentException + */ + @Override + public void delete(String id) throws DocumentNotFoundException, + DocumentException { + + if (logger.isDebugEnabled()) { + logger.debug("deleting document with id=" + id); + } + RepositoryInstance repoSession = null; + try { + repoSession = getRepositorySession(); + DocumentRef docRef = new IdRef(id); + try { + repoSession.removeDocument(docRef); + } 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) { + throw de; + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } finally { + if (repoSession != null) { + releaseRepositorySession(repoSession); + } + } + } - private RepositoryInstance getRepositorySession() throws Exception { - //FIXME: is it possible to reuse repository session? - //Authentication failures happen while trying to reuse the session - NuxeoClient client = NuxeoConnector.getInstance().getClient(); - RepositoryInstance repoSession = client.openRepository(); - if(logger.isDebugEnabled()){ - logger.debug("getRepository() repository root: " + - repoSession.getRootDocument()); - } - return repoSession; - } + private RepositoryInstance getRepositorySession() throws Exception { + // FIXME: is it possible to reuse repository session? + // Authentication failures happen while trying to reuse the session + NuxeoClient client = NuxeoConnector.getInstance().getClient(); + RepositoryInstance repoSession = client.openRepository(); + if (logger.isDebugEnabled()) { + logger.debug("getRepository() repository root: " + + repoSession.getRootDocument()); + } + return repoSession; + } - private void releaseRepositorySession(RepositoryInstance repoSession) { - try{ - NuxeoClient client = NuxeoConnector.getInstance().getClient(); - //release session - client.releaseRepository(repoSession); - }catch(Exception e){ - logger.error("Could not close the repository session", e); - //no need to throw this service specific exception - } - } + private void releaseRepositorySession(RepositoryInstance repoSession) { + try { + NuxeoClient client = NuxeoConnector.getInstance().getClient(); + // release session + client.releaseRepository(repoSession); + } catch (Exception e) { + logger.error("Could not close the repository session", e); + // no need to throw this service specific exception + } + } } diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/rest/RepositoryRESTClient.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/rest/RepositoryRESTClient.java index ec755214a..9a762fd1d 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/rest/RepositoryRESTClient.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/rest/RepositoryRESTClient.java @@ -78,11 +78,11 @@ public class RepositoryRESTClient implements RepositoryClient { } @Override - public String create(String serviceName, String docType, DocumentHandler handler) throws BadRequestException, DocumentException { + public String create(String serviceName, DocumentHandler handler) throws BadRequestException, DocumentException { if(serviceName == null){ throw new IllegalArgumentException("RepositoryRESTClient.create: serviceName is missing"); } - if(docType == null){ + if(handler.getDocumentType() == null){ throw new IllegalArgumentException("RepositoryRESTClient.create: docType is missing"); } if(handler == null){ @@ -106,9 +106,9 @@ public class RepositoryRESTClient implements RepositoryClient { pathParams.addAll(repHandler.getPathParams()); } Map queryParams = new HashMap(); - queryParams.put("docType", docType); + queryParams.put("docType", handler.getDocumentType()); // a default title for the Dublin Core schema - queryParams.put("dublincore:title", "CollectionSpace-" + docType); + queryParams.put("dublincore:title", "CollectionSpace-" + handler.getDocumentType()); if(repHandler.getQueryParams().size() > 0){ queryParams.putAll(repHandler.getQueryParams()); } diff --git a/services/intake/service/src/main/java/org/collectionspace/services/intake/IntakeResource.java b/services/intake/service/src/main/java/org/collectionspace/services/intake/IntakeResource.java index 028f7f5da..d7189ccd8 100644 --- a/services/intake/service/src/main/java/org/collectionspace/services/intake/IntakeResource.java +++ b/services/intake/service/src/main/java/org/collectionspace/services/intake/IntakeResource.java @@ -78,7 +78,7 @@ public class IntakeResource { IntakeHandlerFactory handlerFactory = IntakeHandlerFactory.getInstance(); DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString()); handler.setCommonObject(intakeObject); - csid = client.create(INTAKE_SERVICE_NAME, IntakeConstants.INTAKE_NUXEO_DOCTYPE, handler); + csid = client.create(INTAKE_SERVICE_NAME, handler); intakeObject.setCsid(csid); if(logger.isDebugEnabled()){ verbose("createIntake: ", intakeObject); diff --git a/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeConstants.java b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeConstants.java index 306f7a8e8..0c9d3cf66 100644 --- a/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeConstants.java +++ b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeConstants.java @@ -29,7 +29,7 @@ package org.collectionspace.services.intake.nuxeo; */ public class IntakeConstants { - public final static String INTAKE_NUXEO_DOCTYPE = "Intake"; - public final static String INTAKE_NUXEO_SCHEMA_NAME = "intake"; - public final static String INTAKE_NUXEO_DC_TITLE = "CollectionSpace-Intake"; + public final static String NUXEO_DOCTYPE = "Intake"; + public final static String NUXEO_SCHEMA_NAME = "intake"; + public final static String NUXEO_DC_TITLE = "CollectionSpace-Intake"; } diff --git a/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeDocumentModelHandler.java b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeDocumentModelHandler.java index ae87f9684..0d0c55355 100644 --- a/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeDocumentModelHandler.java +++ b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeDocumentModelHandler.java @@ -25,12 +25,15 @@ package org.collectionspace.services.intake.nuxeo; import java.util.Iterator; import java.util.List; + import org.collectionspace.services.IntakeJAXBSchema; import org.collectionspace.services.common.repository.DocumentWrapper; import org.collectionspace.services.intake.Intake; import org.collectionspace.services.intake.IntakeList; import org.collectionspace.services.intake.IntakeList.IntakeListItem; import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler; +import org.collectionspace.services.intake.nuxeo.IntakeConstants; + import org.nuxeo.ecm.core.api.DocumentModel; import org.nuxeo.ecm.core.api.DocumentModelList; import org.slf4j.Logger; @@ -144,7 +147,7 @@ public class IntakeDocumentModelHandler //so it does not require hard coding // a default title for the Dublin Core schema - docModel.setPropertyValue("dublincore:title", IntakeConstants.INTAKE_NUXEO_DC_TITLE); + docModel.setPropertyValue("dublincore:title", IntakeConstants.NUXEO_DC_TITLE); // intake core values if(intakeObject.getCurrentOwner() != null){ @@ -229,13 +232,20 @@ public class IntakeDocumentModelHandler throw new UnsupportedOperationException(); } + /* (non-Javadoc) + * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#getDocumentType() + */ + public String getDocumentType() { + return IntakeConstants.NUXEO_DOCTYPE; + } + /** * getQProperty converts the given property to qualified schema property * @param prop * @return */ private String getQProperty(String prop) { - return IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + prop; + return IntakeConstants.NUXEO_SCHEMA_NAME + ":" + prop; } } diff --git a/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeRepresenationHandler.java b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeRepresenationHandler.java index 720022cfe..1723126f6 100644 --- a/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeRepresenationHandler.java +++ b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeRepresenationHandler.java @@ -72,52 +72,52 @@ public class IntakeRepresenationHandler Map queryParams = getQueryParams(); Intake intakeObject = getCommonObject(); if(intakeObject.getCurrentOwner() != null){ - queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" + IntakeJAXBSchema.CURRENT_OWNER, intakeObject.getCurrentOwner()); } if(intakeObject.getDepositor() != null){ - queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" + IntakeJAXBSchema.DEPOSITOR, intakeObject.getDepositor()); } if(intakeObject.getDepositorsRequirements() != null){ - queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" + IntakeJAXBSchema.DEPOSITORS_REQUIREMENTS, intakeObject.getDepositorsRequirements()); } if(intakeObject.getEntryDate() != null){ - queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" + IntakeJAXBSchema.ENTRY_DATE, intakeObject.getEntryDate()); } if(intakeObject.getEntryMethod() != null){ - queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" + IntakeJAXBSchema.ENTRY_METHOD, intakeObject.getEntryMethod()); } if(intakeObject.getEntryNote() != null){ - queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" + IntakeJAXBSchema.ENTRY_NOTE, intakeObject.getEntryNote()); } if(intakeObject.getEntryNumber() != null){ - queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" + IntakeJAXBSchema.ENTRY_NUMBER, intakeObject.getEntryNumber()); } if(intakeObject.getEntryReason() != null){ - queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" + IntakeJAXBSchema.ENTRY_REASON, intakeObject.getEntryReason()); } if(intakeObject.getPackingNote() != null){ - queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" + IntakeJAXBSchema.PACKING_NOTE, intakeObject.getPackingNote()); } if(intakeObject.getReturnDate() != null){ - queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" + IntakeJAXBSchema.RETURN_DATE, intakeObject.getReturnDate()); } } @@ -144,7 +144,7 @@ public class IntakeRepresenationHandler logger.debug("getCommonObject() populating Common Object"); } // TODO: recognize schema thru namespace uri - if(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME.equals(schemaElement.attribute("name").getValue())){ + if(IntakeConstants.NUXEO_SCHEMA_NAME.equals(schemaElement.attribute("name").getValue())){ Element ele = schemaElement.element(IntakeJAXBSchema.CURRENT_OWNER); if(ele != null){ intakeObj.setCurrentOwner((String) ele.getData()); @@ -248,13 +248,20 @@ public class IntakeRepresenationHandler this.intakeList = obj; } + /* (non-Javadoc) + * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#getDocumentType() + */ + public String getDocumentType() { + return IntakeConstants.NUXEO_DOCTYPE; + } + /** * getQProperty converts the given property to qualified schema property * @param prop * @return */ private String getQProperty(String prop) { - return IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + prop; + return IntakeConstants.NUXEO_SCHEMA_NAME + ":" + prop; } } diff --git a/services/relation/service/src/main/java/org/collectionspace/services/relation/NewRelationResource.java b/services/relation/service/src/main/java/org/collectionspace/services/relation/NewRelationResource.java new file mode 100644 index 000000000..f77645fb7 --- /dev/null +++ b/services/relation/service/src/main/java/org/collectionspace/services/relation/NewRelationResource.java @@ -0,0 +1,243 @@ +package org.collectionspace.services.relation; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.DELETE; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.PathParam; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import javax.ws.rs.core.UriInfo; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; + +import org.collectionspace.services.relation.RelationList.RelationListItem; + +import org.collectionspace.services.relation.nuxeo.RelationNuxeoConstants; +import org.collectionspace.services.relation.nuxeo.RelationHandlerFactory; +import org.collectionspace.services.common.NuxeoClientType; +import org.collectionspace.services.common.ServiceMain; +import org.collectionspace.services.common.repository.DocumentNotFoundException; +import org.collectionspace.services.common.repository.DocumentHandler; +import org.collectionspace.services.common.repository.RepositoryClient; +import org.collectionspace.services.common.repository.RepositoryClientFactory; +import org.jboss.resteasy.util.HttpResponseCodes; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Path("/relations") +@Consumes("application/xml") +@Produces("application/xml") +public class NewRelationResource { + + public final static String SERVICE_NAME = "relations"; + final Logger logger = LoggerFactory.getLogger(NewRelationResource.class); + //FIXME retrieve client type from configuration + final static NuxeoClientType CLIENT_TYPE = ServiceMain.getInstance().getNuxeoClientType(); + + public NewRelationResource() { + // do nothing + } + + @POST + public Response createRelation( + Relation relation) { + + String csid = null; + try{ + RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance(); + RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString()); + RelationHandlerFactory handlerFactory = RelationHandlerFactory.getInstance(); + DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString()); + handler.setCommonObject(relation); + csid = client.create(SERVICE_NAME, handler); + relation.setCsid(csid); + if(logger.isDebugEnabled()){ + verbose("createRelation: ", relation); + } + UriBuilder path = UriBuilder.fromResource(RelationResource.class); + path.path("" + csid); + Response response = Response.created(path.build()).build(); + return response; + }catch(Exception e){ + if(logger.isDebugEnabled()){ + logger.debug("Caught exception in createRelation", e); + } + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + } + + @GET + @Path("{csid}") + public Relation getRelation( + @PathParam("csid") String csid) { + if(logger.isDebugEnabled()){ + verbose("getRelation with csid=" + csid); + } + if(csid == null || "".equals(csid)){ + logger.error("getRelation: missing csid!"); + Response response = Response.status(Response.Status.BAD_REQUEST).entity( + "get failed on Relation csid=" + csid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } + Relation relation = null; + try{ + RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance(); + RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString()); + RelationHandlerFactory handlerFactory = RelationHandlerFactory.getInstance(); + DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString()); + client.get(csid, handler); + relation = (Relation) handler.getCommonObject(); + }catch(DocumentNotFoundException dnfe){ + if(logger.isDebugEnabled()){ + logger.debug("getRelation", dnfe); + } + Response response = Response.status(Response.Status.NOT_FOUND).entity( + "Get failed on Relation csid=" + csid).type( + "text/plain").build(); + throw new WebApplicationException(response); + }catch(Exception e){ + if(logger.isDebugEnabled()){ + logger.debug("getRelation", e); + } + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + + if(relation == null){ + Response response = Response.status(Response.Status.NOT_FOUND).entity( + "Get failed, the requested Relation CSID:" + csid + ": was not found.").type( + "text/plain").build(); + throw new WebApplicationException(response); + } + if(logger.isDebugEnabled()){ + verbose("getRelation: ", relation); + } + return relation; + } + + @GET + public RelationList getRelationList(@Context UriInfo ui) { + RelationList relationList = new RelationList(); + try{ + RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance(); + RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString()); + RelationHandlerFactory handlerFactory = RelationHandlerFactory.getInstance(); + DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString()); + client.getAll(SERVICE_NAME, handler); + relationList = (RelationList) handler.getCommonObjectList(); + }catch(Exception e){ + if(logger.isDebugEnabled()){ + logger.debug("Caught exception in getRelationList", e); + } + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + return relationList; + } + + @PUT + @Path("{csid}") + public Relation updateRelation( + @PathParam("csid") String csid, + Relation theUpdate) { + if(logger.isDebugEnabled()){ + verbose("updateRelation with csid=" + csid); + } + if(csid == null || "".equals(csid)){ + logger.error("updateRelation: missing csid!"); + Response response = Response.status(Response.Status.BAD_REQUEST).entity( + "update failed on Relation csid=" + csid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } + if(logger.isDebugEnabled()){ + verbose("updateRelation with input: ", theUpdate); + } + try{ + RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance(); + RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString()); + RelationHandlerFactory handlerFactory = RelationHandlerFactory.getInstance(); + DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString()); + handler.setCommonObject(theUpdate); + client.update(csid, handler); + }catch(DocumentNotFoundException dnfe){ + if(logger.isDebugEnabled()){ + logger.debug("caugth exception in updateRelation", dnfe); + } + Response response = Response.status(Response.Status.NOT_FOUND).entity( + "Update failed on Relation csid=" + csid).type( + "text/plain").build(); + throw new WebApplicationException(response); + }catch(Exception e){ + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + return theUpdate; + } + + @DELETE + @Path("{csid}") + public Response deleteRelation(@PathParam("csid") String csid) { + + if(logger.isDebugEnabled()){ + verbose("deleteRelation with csid=" + csid); + } + if(csid == null || "".equals(csid)){ + logger.error("deleteRelation: missing csid!"); + Response response = Response.status(Response.Status.BAD_REQUEST).entity( + "delete failed on Relation csid=" + csid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } + try{ + RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance(); + RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString()); + client.delete(csid); + return Response.status(HttpResponseCodes.SC_OK).build(); + }catch(DocumentNotFoundException dnfe){ + if(logger.isDebugEnabled()){ + logger.debug("caught exception in deleteRelation", dnfe); + } + Response response = Response.status(Response.Status.NOT_FOUND).entity( + "Delete failed on Relation csid=" + csid).type( + "text/plain").build(); + throw new WebApplicationException(response); + }catch(Exception e){ + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + + } + + private void verbose(String msg, Relation relation) { + try{ + verbose(msg); + JAXBContext jc = JAXBContext.newInstance( + Relation.class); + + Marshaller m = jc.createMarshaller(); + m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + m.marshal(relation, System.out); + }catch(Exception e){ + e.printStackTrace(); + } + + } + + private void verbose(String msg) { + System.out.println("RelationResource. " + msg); + } +} diff --git a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java index d88983c0c..0bb0abe59 100644 --- a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java +++ b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java @@ -26,13 +26,14 @@ package org.collectionspace.services.relation.nuxeo; import java.util.Iterator; import java.util.List; import org.collectionspace.services.common.relation.RelationJAXBSchema; -//import org.collectionspace.services.common.relation.nuxeo.RelationUtilsNuxeoImpl; +import org.collectionspace.services.common.relation.nuxeo.RelationUtilsNuxeoImpl; import org.collectionspace.services.common.relation.RelationsManager; import org.collectionspace.services.relation.Relation; import org.collectionspace.services.relation.RelationList; import org.collectionspace.services.relation.RelationList.RelationListItem; +import org.collectionspace.services.relation.nuxeo.RelationNuxeoConstants; import org.collectionspace.services.common.repository.DocumentWrapper; import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler; import org.nuxeo.ecm.core.api.DocumentModel; @@ -103,17 +104,18 @@ public class RelationDocumentModelHandler public Relation extractCommonObject(DocumentWrapper wrapDoc) throws Exception { DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject(); - Relation co = new Relation(); + Relation theRelation = new Relation(); + + RelationUtilsNuxeoImpl.fillRelationFromDocModel(theRelation, docModel); - return co; + return theRelation; } @Override - public void fillCommonObject(Relation co, DocumentWrapper wrapDoc) throws Exception { + public void fillCommonObject(Relation relation, DocumentWrapper wrapDoc) throws Exception { DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject(); - //FIXME property setter should be dynamically set using schema inspection - //so it does not require hard coding + RelationUtilsNuxeoImpl.fillDocModelFromRelation(relation, docModel); } @Override @@ -130,17 +132,17 @@ public class RelationDocumentModelHandler DocumentModel docModel = iter.next(); RelationListItem coListItem = new RelationListItem(); - coListItem.setCsid((String) docModel.getPropertyValue( - getQProperty(RelationJAXBSchema.DOCUMENT_ID_1))); + RelationUtilsNuxeoImpl.fillRelationListItemFromDocModel(coListItem, docModel); - //need fully qualified context for URI - coListItem.setUri("/relations/" + docModel.getId()); - coListItem.setCsid(docModel.getId()); list.add(coListItem); } return coList; } + + public String getDocumentType() { + return RelationNuxeoConstants.NUXEO_DOCTYPE; + } @Override public void fillCommonObjectList(RelationList obj, DocumentWrapper wrapDoc) throws Exception { diff --git a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationNuxeoConstants.java b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationNuxeoConstants.java index e67a951e0..cc974846f 100644 --- a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationNuxeoConstants.java +++ b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationNuxeoConstants.java @@ -1,4 +1,7 @@ package org.collectionspace.services.relation.nuxeo; +import org.collectionspace.services.common.relation.nuxeo.RelationUtilsNuxeoImpl; + public class RelationNuxeoConstants { + static String NUXEO_DOCTYPE = RelationUtilsNuxeoImpl.REL_NUXEO_DOCTYPE; } \ No newline at end of file diff --git a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationServiceNuxeoImpl.java b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationServiceNuxeoImpl.java index e3c6127b8..85273c211 100644 --- a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationServiceNuxeoImpl.java +++ b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationServiceNuxeoImpl.java @@ -68,8 +68,7 @@ import org.nuxeo.ecm.core.api.DocumentRef; /** - * @author remillet - * + * The Class RelationServiceNuxeoImpl. */ public class RelationServiceNuxeoImpl extends CollectionSpaceServiceNuxeoImpl implements RelationService { @@ -77,15 +76,8 @@ public class RelationServiceNuxeoImpl extends // replace WORKSPACE_UID for resource workspace // static String CS_RELATION_WORKSPACE_UID = "55f99358-5dbe-4462-8000-c5c3c2063919"; - /** - * Delete relation. - * - * @param csid the csid - * - * @return the document - * - * @throws DocumentException the document exception - * @throws IOException Signals that an I/O exception has occurred. + /* (non-Javadoc) + * @see org.collectionspace.services.relation.RelationService#deleteRelation(java.lang.String) */ public Document deleteRelation(String csid) throws DocumentException { Document result = null; @@ -108,15 +100,8 @@ public class RelationServiceNuxeoImpl extends return result; } - /** - * Gets the relation. - * - * @param csid the csid - * - * @return the relation - * - * @throws DocumentException the document exception - * @throws IOException Signals that an I/O exception has occurred. + /* (non-Javadoc) + * @see org.collectionspace.services.relation.RelationService#getRelation(java.lang.String) */ public Document getRelation(String csid) throws DocumentException, IOException { @@ -145,13 +130,8 @@ public class RelationServiceNuxeoImpl extends return result; } - /** - * Gets the relation list. - * - * @return the relation list - * - * @throws DocumentException the document exception - * @throws IOException Signals that an I/O exception has occurred. + /* (non-Javadoc) + * @see org.collectionspace.services.relation.RelationService#getRelationList() */ public Document getRelationList() throws DocumentException, IOException { @@ -215,15 +195,8 @@ public class RelationServiceNuxeoImpl extends } // Create a new relation document - /** - * Post relation. - * - * @param co the co - * - * @return the document - * - * @throws DocumentException the document exception - * @throws IOException Signals that an I/O exception has occurred. + /* (non-Javadoc) + * @see org.collectionspace.services.relation.RelationService#postRelation(org.collectionspace.services.relation.Relation) */ public Document postRelation(Relation co) throws DocumentException, IOException { @@ -254,16 +227,8 @@ public class RelationServiceNuxeoImpl extends return result; } - /** - * Put relation. - * - * @param csid the csid - * @param theUpdate the the update - * - * @return the document - * - * @throws DocumentException the document exception - * @throws IOException Signals that an I/O exception has occurred. + /* (non-Javadoc) + * @see org.collectionspace.services.relation.RelationService#putRelation(java.lang.String, org.collectionspace.services.relation.Relation) */ public Document putRelation(String csid, Relation theUpdate) throws DocumentException, IOException { -- 2.47.3