From: Richard Millet Date: Mon, 3 Aug 2009 23:57:44 +0000 (+0000) Subject: cspace-271: Adding classes for generic relation service. Still does *not* use the... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=a62f6e0899c04c5c58ccb13a381115bd765f6397;p=tmp%2Fjakarta-migration.git cspace-271: Adding classes for generic relation service. Still does *not* use the new "handler" framework for CSpace services. --- diff --git a/services/relation/client/.classpath b/services/relation/client/.classpath new file mode 100644 index 000000000..bc47e4555 --- /dev/null +++ b/services/relation/client/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/services/relation/client/.project b/services/relation/client/.project new file mode 100644 index 000000000..f44220ec1 --- /dev/null +++ b/services/relation/client/.project @@ -0,0 +1,23 @@ + + + javaee-addressbook-client + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.maven.ide.eclipse.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.maven.ide.eclipse.maven2Nature + + diff --git a/services/relation/client/.settings/org.eclipse.jdt.core.prefs b/services/relation/client/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..0899025a2 --- /dev/null +++ b/services/relation/client/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +#Thu Mar 26 13:03:04 PDT 2009 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/services/relation/client/.settings/org.maven.ide.eclipse.prefs b/services/relation/client/.settings/org.maven.ide.eclipse.prefs new file mode 100644 index 000000000..c6e4123cd --- /dev/null +++ b/services/relation/client/.settings/org.maven.ide.eclipse.prefs @@ -0,0 +1,8 @@ +#Thu Feb 26 16:30:28 PST 2009 +activeProfiles= +eclipse.preferences.version=1 +fullBuildGoals=process-test-resources +includeModules=false +resolveWorkspaceProjects=true +resourceFilterGoals=process-resources resources\:testResources +version=1 diff --git a/services/relation/client/pom.xml b/services/relation/client/pom.xml new file mode 100644 index 000000000..771dd5c6d --- /dev/null +++ b/services/relation/client/pom.xml @@ -0,0 +1,105 @@ + + + + + org.collectionspace.services.relation + org.collectionspace.services + 1.0 + + + 4.0.0 + org.collectionspace.services + org.collectionspace.services.relation.client + 1.0 + services.relation.client + + + + + org.collectionspace.services + org.collectionspace.services.common + 1.0 + + + + org.collectionspace.services + org.collectionspace.services.client + 1.0 + + + + org.testng + testng + 5.6 + + + org.slf4j + slf4j-api + 1.5.2 + + + org.slf4j + slf4j-log4j12 + 1.5.2 + + + org.jboss.resteasy + resteasy-jaxrs + 1.0.2.GA + + + + tjws + webserver + + + + + org.jboss.resteasy + resteasy-jaxb-provider + 1.0.2.GA + + + org.jboss.resteasy + resteasy-multipart-provider + 1.0.2.GA + + + commons-httpclient + commons-httpclient + 3.1 + + + + + cspace-services-relation-client + + + org.apache.maven.plugins + maven-surefire-plugin + + + + log4j.configuration + log4j.xml + + + + + + maven-compiler-plugin + 2.0.2 + + 1.5 + 1.5 + + + + + diff --git a/services/relation/client/src/main/java/org/collectionspace/services/client/RelationClient.java b/services/relation/client/src/main/java/org/collectionspace/services/client/RelationClient.java new file mode 100644 index 000000000..a9efddc08 --- /dev/null +++ b/services/relation/client/src/main/java/org/collectionspace/services/client/RelationClient.java @@ -0,0 +1,99 @@ +package org.collectionspace.services.client; + +import javax.ws.rs.core.Response; + +import org.collectionspace.services.relation.Relation; +import org.collectionspace.services.relation.RelationList; + +import org.jboss.resteasy.client.ProxyFactory; +import org.jboss.resteasy.plugins.providers.RegisterBuiltin; +import org.jboss.resteasy.client.ClientResponse; +import org.jboss.resteasy.spi.ResteasyProviderFactory; + +/** + * A RelationClient. + + * @version $Revision:$ + */ +public class RelationClient extends BaseServiceClient { + + /** + * + */ + private static final RelationClient instance = new RelationClient(); + /** + * + */ + private RelationProxy relationProxy; + + /** + * + * Default constructor for RelationClient class. + * + */ + private RelationClient() { + ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance(); + RegisterBuiltin.register(factory); + if(useAuth()){ + relationProxy = ProxyFactory.create(RelationProxy.class, + getBaseURL(), getHttpClient()); + }else{ + relationProxy = ProxyFactory.create(RelationProxy.class, + getBaseURL()); + } + } + + /** + * FIXME Comment this + * + * @return + */ + public static RelationClient getInstance() { + return instance; + } + + /** + * @return + * @see org.collectionspace.hello.client.RelationProxy#getRelation() + */ + public ClientResponse getRelationList() { + return relationProxy.getRelationList(); + } + + /** + * @param csid + * @return + * @see org.collectionspace.hello.client.RelationProxy#getRelation(java.lang.String) + */ + public ClientResponse getRelation(String csid) { + return relationProxy.getRelation(csid); + } + + /** + * @param relation + * @return + * @see org.collectionspace.hello.client.RelationProxy#createRelation(org.collectionspace.hello.Relation) + */ + public ClientResponse createRelation(Relation relation) { + return relationProxy.createRelation(relation); + } + + /** + * @param csid + * @param relation + * @return + * @see org.collectionspace.hello.client.RelationProxy#updateRelation(java.lang.Long, org.collectionspace.hello.Relation) + */ + public ClientResponse updateRelation(String csid, Relation relation) { + return relationProxy.updateRelation(csid, relation); + } + + /** + * @param csid + * @return + * @see org.collectionspace.hello.client.RelationProxy#deleteRelation(java.lang.Long) + */ + public ClientResponse deleteRelation(String csid) { + return relationProxy.deleteRelation(csid); + } +} diff --git a/services/relation/client/src/main/java/org/collectionspace/services/client/RelationProxy.java b/services/relation/client/src/main/java/org/collectionspace/services/client/RelationProxy.java new file mode 100644 index 000000000..bc075cec1 --- /dev/null +++ b/services/relation/client/src/main/java/org/collectionspace/services/client/RelationProxy.java @@ -0,0 +1,46 @@ +package org.collectionspace.services.client; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Response; + +import org.collectionspace.services.relation.Relation; +import org.collectionspace.services.relation.RelationList; +import org.jboss.resteasy.client.ClientResponse; + +/** + * @version $Revision:$ + */ +@Path("/relations/") +@Produces({"application/xml"}) +@Consumes({"application/xml"}) +public interface RelationProxy { + + @GET + ClientResponse getRelationList(); + + //(C)reate + @POST + ClientResponse createRelation(Relation co); + + //(R)ead + @GET + @Path("/{csid}") + ClientResponse getRelation(@PathParam("csid") String csid); + + //(U)pdate + @PUT + @Path("/{csid}") + ClientResponse updateRelation(@PathParam("csid") String csid, Relation co); + + //(D)elete + @DELETE + @Path("/{csid}") + ClientResponse deleteRelation(@PathParam("csid") String csid); +} \ No newline at end of file diff --git a/services/relation/client/src/test/java/org/collectionspace/services/client/test/RelationServiceTest.java b/services/relation/client/src/test/java/org/collectionspace/services/client/test/RelationServiceTest.java new file mode 100644 index 000000000..cd839839e --- /dev/null +++ b/services/relation/client/src/test/java/org/collectionspace/services/client/test/RelationServiceTest.java @@ -0,0 +1,243 @@ +package org.collectionspace.services.client.test; + +import java.util.ArrayList; +import java.util.List; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; +import org.jboss.resteasy.client.ClientResponse; +import org.testng.Assert; +import org.testng.annotations.Test; + +import org.collectionspace.services.relation.Relation; +import org.collectionspace.services.relation.RelationList; +import org.collectionspace.services.relation.RelationshipType; + +import org.collectionspace.services.client.RelationClient; +import org.collectionspace.services.RelationJAXBSchema; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A RelationNuxeoServiceTest. + * + * @version $Revision:$ + */ +public class RelationServiceTest { + + /** The relation client. */ + private RelationClient relationClient = RelationClient.getInstance(); + + /** The update id. */ + private String updateId = null; + + /** The delete id. */ + private String deleteId = null; + + /** The logger. */ + final Logger logger = LoggerFactory.getLogger(RelationServiceTest.class); + + /** + * Creates the relation. + */ + @Test + public void createRelation() { + long identifier = this.createIdentifier(); + + Relation relation = createRelation(identifier); + ClientResponse res = relationClient.createRelation(relation); + Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode()); + + String responseString = res.toString(); + System.out.println(responseString); + + //store updateId locally for "update" test + if(updateId == null){ + updateId = extractId(res); + }else{ + deleteId = extractId(res); + verbose("Set deleteId: " + deleteId); + } + } + + /** + * Update relation. + */ + @Test(dependsOnMethods = {"createRelation"}) + public void updateRelation() { + ClientResponse res = relationClient.getRelation(updateId); + Relation relation = res.getEntity(); + verbose("Got Relation to update with ID: " + updateId, + relation, Relation.class); + + //relation.setCsid("updated-" + updateId); + relation.setDocumentId1("updated-" + relation.getDocumentId1()); + relation.setDocumentType1("updated-" + relation.getDocumentType1()); + + // make call to update service + res = relationClient.updateRelation(updateId, relation); + + // check the response + Relation updatedRelation = res.getEntity(); + Assert.assertEquals(updatedRelation.getDocumentId1(), relation.getDocumentId1()); + verbose("updateRelation: ", updatedRelation, Relation.class); + + return; + } + + /** + * Creates the collection. + */ + @Test(dependsOnMethods = {"createRelation"}) + public void createCollection() { + for(int i = 0; i < 3; i++){ + this.createRelation(); + } + } + + /** + * Gets the relation list. + * + * @return the relation list + */ + @Test(dependsOnMethods = {"createCollection"}) + public void getRelationList() { + //the resource method is expected to return at least an empty list + RelationList coList = relationClient.getRelationList().getEntity(); + List coItemList = coList.getRelationListItem(); + int i = 0; + for(RelationList.RelationListItem pli : coItemList){ + verbose("getRelationList: list-item[" + i + "] csid=" + pli.getCsid()); + verbose("getRelationList: list-item[" + i + "] URI=" + pli.getUri()); + i++; + System.out.println(); + } + } + + /** + * Delete relation. + */ + @Test(dependsOnMethods = {"createCollection"}) + public void deleteRelation() { + verbose("Calling deleteRelation:" + deleteId); + ClientResponse res = relationClient.deleteRelation(deleteId); + verbose("deleteRelation: csid=" + deleteId); + verbose("deleteRelation: status = " + res.getStatus()); + Assert.assertEquals(res.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); + } + + /** + * Creates the relation. + * + * @param identifier the identifier + * + * @return the relation + */ + private Relation createRelation(long identifier) { + Relation relation = createRelation("documentId1-" + identifier, + "documentType1-" + identifier + "-type", + "documentType1-" + identifier + "-type", + "documentType1-" + identifier + "-type", + RelationshipType.fromValue( + RelationJAXBSchema.ENUM_RELATIONSHIP_TYPE_ASSOC)); + + return relation; + } + + /** + * Creates the relation. + * + * @param documentId1 the document id1 + * @param documentType1 the document type1 + * @param documentId2 the document id2 + * @param documentType2 the document type2 + * @param rt the rt + * + * @return the relation + */ + private Relation createRelation(String documentId1, String documentType1, + String documentId2, String documentType2, RelationshipType rt) { + Relation relation = new Relation(); + + relation.setDocumentId1(documentId1); + relation.setDocumentType1(documentType1); + relation.setDocumentId2(documentId2); + relation.setDocumentType2(documentType2); + + relation.setRelationshipType(rt); + + return relation; + } + + /** + * Extract id. + * + * @param res the res + * + * @return the string + */ + private String extractId(ClientResponse res) { + MultivaluedMap mvm = res.getMetadata(); + String uri = (String) ((ArrayList) mvm.get("Location")).get(0); + String[] segments = uri.split("/"); + String id = segments[segments.length - 1]; + verbose("id=" + id); + return id; + } + + /** + * Verbose. + * + * @param msg the msg + */ + private void verbose(String msg) { +// if(logger.isInfoEnabled()){ +// logger.debug(msg); +// } + System.out.println(msg); + } + + /** + * Verbose. + * + * @param msg the msg + * @param o the o + * @param clazz the clazz + */ + private void verbose(String msg, Object o, Class clazz) { + try{ + verbose(msg); + JAXBContext jc = JAXBContext.newInstance(clazz); + Marshaller m = jc.createMarshaller(); + m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, + Boolean.TRUE); + m.marshal(o, System.out); + }catch(Exception e){ + e.printStackTrace(); + } + } + + /** + * Verbose map. + * + * @param map the map + */ + private void verboseMap(MultivaluedMap map) { + for(Object entry : map.entrySet()){ + MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry; + verbose(" name=" + mentry.getKey() + " value=" + mentry.getValue()); + } + } + + /** + * Creates the identifier. + * + * @return the long + */ + private long createIdentifier() { + long identifier = System.currentTimeMillis(); + return identifier; + } +} diff --git a/services/relation/client/src/test/resources/log4j.xml b/services/relation/client/src/test/resources/log4j.xml new file mode 100644 index 000000000..b26abaa9b --- /dev/null +++ b/services/relation/client/src/test/resources/log4j.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/services/relation/pom.xml b/services/relation/pom.xml index 2efad1e23..bb78e64aa 100644 --- a/services/relation/pom.xml +++ b/services/relation/pom.xml @@ -5,6 +5,7 @@ org.collectionspace.services 1.0 + 4.0.0 org.collectionspace.services 1.0 @@ -18,7 +19,9 @@ + service 3rdparty + client diff --git a/services/relation/service/.classpath b/services/relation/service/.classpath new file mode 100644 index 000000000..505c7192a --- /dev/null +++ b/services/relation/service/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/services/relation/service/.project b/services/relation/service/.project new file mode 100644 index 000000000..7ae230acc --- /dev/null +++ b/services/relation/service/.project @@ -0,0 +1,23 @@ + + + org.collectionspace.services.relation.service + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.maven.ide.eclipse.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.maven.ide.eclipse.maven2Nature + + diff --git a/services/relation/service/.settings/org.eclipse.jdt.core.prefs b/services/relation/service/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..051982c16 --- /dev/null +++ b/services/relation/service/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +#Thu Jul 16 13:51:06 PDT 2009 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/services/relation/service/.settings/org.maven.ide.eclipse.prefs b/services/relation/service/.settings/org.maven.ide.eclipse.prefs new file mode 100644 index 000000000..86c513b9c --- /dev/null +++ b/services/relation/service/.settings/org.maven.ide.eclipse.prefs @@ -0,0 +1,8 @@ +#Thu Jul 16 13:50:16 PDT 2009 +activeProfiles= +eclipse.preferences.version=1 +fullBuildGoals=process-test-resources +includeModules=false +resolveWorkspaceProjects=true +resourceFilterGoals=process-resources resources\:testResources +version=1 diff --git a/services/relation/service/pom.xml b/services/relation/service/pom.xml new file mode 100644 index 000000000..147f85cfd --- /dev/null +++ b/services/relation/service/pom.xml @@ -0,0 +1,135 @@ + + + + + org.collectionspace.services.relation + org.collectionspace.services + 1.0 + + + 4.0.0 + org.collectionspace.services + org.collectionspace.services.relation.service + jar + 1.0 + services.relation.service + + + + org.collectionspace.services + org.collectionspace.services.common + 1.0 + + + + + junit + junit + 4.1 + test + + + org.testng + testng + 5.6 + + + + + + + + javax.security + jaas + 1.0.01 + provided + + + + dom4j + dom4j + 1.6.1 + provided + + + + + + org.jboss.resteasy + resteasy-jaxrs + 1.0.2.GA + + + tjws + webserver + + + + + org.jboss.resteasy + resteasy-jaxb-provider + 1.0.2.GA + + + org.jboss.resteasy + resteasy-multipart-provider + 1.0.2.GA + + + + + + org.nuxeo.ecm.core + nuxeo-core-api + ${nuxeo.version.1.5} + + + jboss-remoting + jboss + + + + + + org.restlet + org.restlet + 1.0.7 + + + com.noelios.restlet + com.noelios.restlet.ext.httpclient + 1.0.7 + + + com.noelios.restlet + com.noelios.restlet + 1.0.7 + + + + + + collectionspace-services-relation + + + org.codehaus.mojo + jboss-maven-plugin + + ${jboss.dir} + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + + diff --git a/services/relation/service/src/main/java/org/collectionspace/services/RelationResource.java b/services/relation/service/src/main/java/org/collectionspace/services/RelationResource.java new file mode 100644 index 000000000..0cb9d5408 --- /dev/null +++ b/services/relation/service/src/main/java/org/collectionspace/services/RelationResource.java @@ -0,0 +1,346 @@ +/** + * RelationResource.java + * + * {Purpose of This Class} + * + * {Other Notes Relating to This Class (Optional)} + * + * $LastChangedBy: $ + * $LastChangedRevision: $ + * $LastChangedDate: $ + * + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + * + * http://www.collectionspace.org + * http://wiki.collectionspace.org + * + * Copyright © 2009 {Contributing Institution} + * + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * https://source.collectionspace.org/collection-space/LICENSE.txt + */ +package org.collectionspace.services; + +import java.util.Iterator; +import java.util.List; +import java.net.URI; + +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.RelationService; +import org.collectionspace.services.relation.*; +import org.collectionspace.services.relation.RelationList.*; +import org.collectionspace.services.RelationJAXBSchema; +import org.collectionspace.services.common.ServiceMain; + +import org.dom4j.Document; +import org.dom4j.Element; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Class RelationResource. + */ +@Path("/relations") +@Consumes("application/xml") +@Produces("application/xml") +public class RelationResource { + + /** The logger. */ + final Logger logger = LoggerFactory + .getLogger(RelationResource.class); + + // This should be a DI wired by a container like Spring, Seam, or EJB3 + /** The Constant service. */ + final static RelationService service = new RelationServiceNuxeoImpl(); + + /** + * Instantiates a new relation resource. + */ + public RelationResource() { + // do nothing + } + + /** + * Gets the relation list. + * + * @param ui the ui + * + * @return the relation list + */ + @GET + public RelationList getRelationList(@Context UriInfo ui) { + + URI absoluteURI = ui.getAbsolutePath(); + String uriString = absoluteURI.toString(); + + RelationList p = new RelationList(); + try { + Document document = service.getRelationList(); + Element root = document.getRootElement(); + + // debug + System.err.println(document.asXML()); + + List list = p + .getRelationListItem(); + for (Iterator i = root.elementIterator(); i.hasNext();) { + Element element = (Element) i.next(); + // debug + System.err.println(); + element.asXML(); + + // set the Relation list item entity elements + RelationListItem pli = new RelationListItem(); + pli.setUri(element.attributeValue("url")); + pli.setCsid(element.attributeValue("id")); + list.add(pli); + } + + } catch (Exception e) { + e.printStackTrace(); + } + + return p; + } + + /** + * Creates the relation. + * + * @param ui the ui + * @param co the co + * + * @return the response + */ + @POST + public Response createRelation(@Context UriInfo ui, Relation co) { + String csid = null; + + try { + Document document = service.postRelation(co); + Element root = document.getRootElement(); + csid = root.attributeValue("id"); + co.setCsid(csid); + } catch (Exception e) { + Response response = Response.status(Response.Status.NOT_FOUND) + .entity("Create failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + + //debug + verbose("createRelation: ", co); + + UriBuilder uriBuilder = ui.getAbsolutePathBuilder(); + uriBuilder.path(csid); + URI uri = uriBuilder.build(); + + //debug + System.out.println(uri.toString()); + + Response response = Response.created(uri).build(); + return response; + } + + /** + * Gets the relation. + * + * @param csid the csid + * + * @return the relation + */ + @GET + @Path("{csid}") + public Relation getRelation(@PathParam("csid") String csid) { + + Relation co = null; + try { + Document document = service.getRelation(csid); + Element root = document.getRootElement(); + co = new Relation(); + + // TODO: recognize schema thru namespace uri + // Namespace ns = new Namespace("relation", + // "http://collectionspace.org/relation"); + + Iterator siter = root.elementIterator("schema"); + while (siter.hasNext()) { + Element schemaElement = siter.next(); + System.err.println("Relation.getRelation() called."); + + // TODO: recognize schema thru namespace uri + if (RelationService.REL_SCHEMA_NAME.equals(schemaElement + .attribute("name").getValue())) { + Iterator relIter = schemaElement + .elementIterator(RelationJAXBSchema.REL_ROOT_ELEM_NAME); + Iterator relIter2 = schemaElement + .elementIterator("rel:" + RelationJAXBSchema.REL_ROOT_ELEM_NAME); + + while (relIter.hasNext()) { + Element relElement = relIter.next(); + + Element ele = relElement + .element(RelationJAXBSchema.DOCUMENT_ID_1); + if (ele != null) { + co.setDocumentId1((String) ele.getData()); + } + ele = relElement + .element(RelationJAXBSchema.DOCUMENT_TYPE_1); + if (ele != null) { + co.setDocumentType1((String) ele.getData()); + } + ele = relElement + .element(RelationJAXBSchema.DOCUMENT_ID_2); + if (ele != null) { + co.setDocumentId2((String) ele.getData()); + } + ele = relElement + .element(RelationJAXBSchema.DOCUMENT_TYPE_2); + if (ele != null) { + co.setDocumentType2((String) ele.getData()); + } + ele = relElement + .element(RelationJAXBSchema.RELATIONSHIP_TYPE); + if (ele != null) { + RelationshipType rt = RelationshipType + .fromValue((String) ele.getData()); + co.setRelationshipType(rt); + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed") + .type("text/plain").build(); + throw new WebApplicationException(response); + } + if (co == 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); + } + verbose("getRelation: ", co); + + return co; + } + + /** + * Update relation. + * + * @param csid the csid + * @param theUpdate the the update + * + * @return the relation + */ + @PUT + @Path("{csid}") + public Relation updateRelation( + @PathParam("csid") String csid, Relation theUpdate) { + + verbose("updateRelation with input: ", theUpdate); + + String status = null; + try { + + Document document = service.putRelation(csid, theUpdate); + Element root = document.getRootElement(); + for (Iterator i = root.elementIterator(); i.hasNext();) { + Element element = (Element) i.next(); + if ("docRef".equals(element.getName())) { + status = (String) element.getData(); + verbose("updateRelation response: " + status); + } + } + } catch (Exception e) { + // FIXME: NOT_FOUND? + Response response = Response.status(Response.Status.NOT_FOUND) + .entity("Update failed ").type("text/plain").build(); + throw new WebApplicationException(response); + } + + return theUpdate; + } + + /** + * Delete relation. + * + * @param csid the csid + */ + @DELETE + @Path("{csid}") + public void deleteRelation(@PathParam("csid") String csid) { + + verbose("deleteRelation with csid=" + csid); + try { + + Document document = service.deleteRelation(csid); + Element root = document.getRootElement(); + for (Iterator i = root.elementIterator(); i.hasNext();) { + Element element = (Element) i.next(); + if ("docRef".equals(element.getName())) { + String status = (String) element.getData(); + verbose("deleteRelationt response: " + status); + } + } + } catch (Exception e) { + // FIXME: NOT_FOUND? + Response response = Response.status(Response.Status.NOT_FOUND) + .entity("Delete failed ").type("text/plain").build(); + throw new WebApplicationException(response); + } + + } + + /** + * Verbose. + * + * @param msg the msg + * @param co the co + */ + private void verbose(String msg, Relation co) { + try { + verbose(msg); + JAXBContext jc = JAXBContext.newInstance(Relation.class); + + Marshaller m = jc.createMarshaller(); + m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + m.marshal(co, System.out); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Verbose. + * + * @param msg the msg + */ + private void verbose(String msg) { + System.out.println("RelationResource. " + msg); + } + +} diff --git a/services/relation/service/src/main/java/org/collectionspace/services/RelationService.java b/services/relation/service/src/main/java/org/collectionspace/services/RelationService.java new file mode 100644 index 000000000..f9f857b56 --- /dev/null +++ b/services/relation/service/src/main/java/org/collectionspace/services/RelationService.java @@ -0,0 +1,111 @@ +/** + * RelationService.java + * + * {Purpose of This Class} + * + * {Other Notes Relating to This Class (Optional)} + * + * $LastChangedBy: $ + * $LastChangedRevision: $ + * $LastChangedDate: $ + * + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + * + * http://www.collectionspace.org + * http://wiki.collectionspace.org + * + * Copyright © 2009 {Contributing Institution} + * + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * https://source.collectionspace.org/collection-space/LICENSE.txt + */ +package org.collectionspace.services; + +import java.io.IOException; +import org.dom4j.Document; +import org.dom4j.DocumentException; + +import org.collectionspace.services.relation.Relation; + +/** + * @author remillet + * + */ +public interface RelationService { + + /** The Constant REL_SCHEMA_NAME. */ + public final static String REL_SCHEMA_NAME = "relation"; + + // Create + /** + * Post relation. + * + * @param co the co + * + * @return the document + * + * @throws DocumentException the document exception + * @throws IOException Signals that an I/O exception has occurred. + */ + Document postRelation(Relation co) + throws DocumentException, IOException; + + // Read single object + /** + * 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. + */ + Document getRelation(String csid) throws DocumentException, + IOException; + + // Read a list of objects + /** + * Gets the relation list. + * + * @return the relation list + * + * @throws DocumentException the document exception + * @throws IOException Signals that an I/O exception has occurred. + */ + Document getRelationList() throws DocumentException, IOException; + + // Update + /** + * 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. + */ + Document putRelation(String csid, Relation theUpdate) + throws DocumentException, IOException; + + // Delete + /** + * Delete relation. + * + * @param csid the csid + * + * @return the document + * + * @throws DocumentException the document exception + * @throws IOException Signals that an I/O exception has occurred. + */ + Document deleteRelation(String csid) throws DocumentException, + IOException; +} diff --git a/services/relation/service/src/main/java/org/collectionspace/services/RelationServiceNuxeoImpl.java b/services/relation/service/src/main/java/org/collectionspace/services/RelationServiceNuxeoImpl.java new file mode 100644 index 000000000..95821949e --- /dev/null +++ b/services/relation/service/src/main/java/org/collectionspace/services/RelationServiceNuxeoImpl.java @@ -0,0 +1,265 @@ +/** + * RelationServiceNuxeoImpl.java + * + * {Purpose of This Class} + * + * {Other Notes Relating to This Class (Optional)} + * + * $LastChangedBy: $ + * $LastChangedRevision: $ + * $LastChangedDate: $ + * + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + * + * http://www.collectionspace.org + * http://wiki.collectionspace.org + * + * Copyright © 2009 {Contributing Institution} + * + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * https://source.collectionspace.org/collection-space/LICENSE.txt + */ +package org.collectionspace.services; + +//import java.io.ByteArrayInputStream; +import java.io.IOException; +//import java.io.Serializable; +//import java.util.ArrayList; +//import java.util.Arrays; +//import java.util.HashMap; +//import java.util.List; +//import java.util.Map; +//import java.util.Iterator; + +//import org.collectionspace.services.nuxeo.NuxeoRESTClient; +import org.collectionspace.services.nuxeo.CollectionSpaceServiceNuxeoImpl; +import org.collectionspace.services.nuxeo.RelationUtilsNuxeoImpl; +import org.collectionspace.services.relation.Relation; +//import org.collectionspace.services.relation.RelationList; +//import org.collectionspace.services.relation.RelationshipType; +//import org.collectionspace.services.RelationJAXBSchema; +import org.collectionspace.services.nuxeo.util.NuxeoUtils; +//import org.collectionspace.services.common.ServiceMain; + +import org.dom4j.Document; +//import org.dom4j.Element; +//import org.dom4j.dom.DOMDocument; +import org.dom4j.DocumentException; +//import org.dom4j.io.SAXReader; +//import org.restlet.resource.Representation; + +import org.nuxeo.common.utils.IdUtils; +//import org.nuxeo.ecm.core.api.ClientException; +import org.nuxeo.ecm.core.api.IdRef; +import org.nuxeo.ecm.core.api.repository.RepositoryInstance; +import org.nuxeo.ecm.core.api.DocumentModel; +//import org.nuxeo.ecm.core.api.DocumentModelList; +//import org.nuxeo.ecm.core.api.model.DocumentPart; +import org.nuxeo.ecm.core.api.DocumentRef; + + + +/** + * @author remillet + * + */ +public class RelationServiceNuxeoImpl extends + CollectionSpaceServiceNuxeoImpl implements RelationService { + + // 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. + */ + public Document deleteRelation(String csid) + throws DocumentException, IOException { + Document result = null; + + RepositoryInstance repoSession = null; + try { + repoSession = getRepositorySession(); + result = deleteDocument(repoSession, csid); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if(repoSession != null) { + releaseRepositorySession(repoSession); + } + } + + 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. + */ + public Document getRelation(String csid) + throws DocumentException, IOException { + Document result = null; + RepositoryInstance repoSession = null; + + try { + repoSession = getRepositorySession(); + result = NuxeoUtils.getDocument(repoSession, csid); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (repoSession != null) { + releaseRepositorySession(repoSession); + } + } + + // Dump out the contents of the result + if (logger.isDebugEnabled() == true) { + System.out.println(result.asXML()); + } + + 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. + */ + public Document getRelationList() throws DocumentException, IOException { + Document result = null; + RepositoryInstance repoSession = null; + + try { + repoSession = getRepositorySession(); + RelationUtilsNuxeoImpl.getRelationships(repoSession); + + result = this.browseWorkspace(repoSession, RelationUtilsNuxeoImpl.CS_RELATION_SERVICE_NAME); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (repoSession != null) { + releaseRepositorySession(repoSession); + } + } + + if (logger.isDebugEnabled() == true) { + System.out.println(result.asXML()); + } + + return result; + } + + // 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. + */ + public Document postRelation(Relation co) throws DocumentException, + IOException { + Document result = null; + RepositoryInstance repoSession = null; + + try { + repoSession = getRepositorySession(); + + // get the Nuxeo 'Relations' workspace + DocumentModel workspaceModel = NuxeoUtils.getWorkspaceModel(repoSession, + RelationUtilsNuxeoImpl.CS_RELATION_SERVICE_NAME); + + String docType = RelationUtilsNuxeoImpl.REL_NUXEO_DOCTYPE; + String id = IdUtils.generateId("New " + docType); + + //create document model + String workspacePath = workspaceModel.getPathAsString(); + DocumentModel newRelDocModel = repoSession.createDocumentModel(workspacePath, id, docType); + + //debug + if (logger.isDebugEnabled() == true) { + RelationUtilsNuxeoImpl.printDocumentModel(newRelDocModel); + } + RelationUtilsNuxeoImpl.fillDocModelFromRelation(co, newRelDocModel); + + //create document with the new DocumentModel + DocumentModel resultDocModel = repoSession.createDocument(newRelDocModel); + repoSession.save(); + + result = NuxeoUtils.getDocument(repoSession, resultDocModel); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (repoSession != null) { + releaseRepositorySession(repoSession); + } + } + + // Dump out the contents of the result + if (logger.isDebugEnabled() == true) { + System.out.println(result.asXML()); + } + + 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. + */ + public Document putRelation(String csid, Relation theUpdate) + throws DocumentException, IOException { + + Document result = null; + RepositoryInstance repoSession = null; + try{ + repoSession = getRepositorySession(); + DocumentRef documentRef = new IdRef(csid); + DocumentModel documentModel = repoSession.getDocument(documentRef); + RelationUtilsNuxeoImpl.fillDocModelFromRelation(theUpdate, documentModel); + repoSession.saveDocument(documentModel); + repoSession.save(); + result = NuxeoUtils.getDocument(repoSession, documentModel); + } catch(Exception e){ + e.printStackTrace(); + } finally{ + if(repoSession != null){ + releaseRepositorySession(repoSession); + } + } + + return result; + } + +} diff --git a/services/relation/service/src/test/java/org/collectionspace/services/test/RelationServiceTest.java b/services/relation/service/src/test/java/org/collectionspace/services/test/RelationServiceTest.java new file mode 100644 index 000000000..7817f8c34 --- /dev/null +++ b/services/relation/service/src/test/java/org/collectionspace/services/test/RelationServiceTest.java @@ -0,0 +1,39 @@ +/** + * RelationServiceTest.java + * + * {Purpose of This Class} + * + * {Other Notes Relating to This Class (Optional)} + * + * $LastChangedBy: $ + * $LastChangedRevision: $ + * $LastChangedDate: $ + * + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + * + * http://www.collectionspace.org + * http://wiki.collectionspace.org + * + * Copyright © 2009 {Contributing Institution} + * + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * https://source.collectionspace.org/collection-space/LICENSE.txt + */ +package org.collectionspace.services.test; + +//import org.collectionspace.services.collectionobject.CollectionObject; +//import org.collectionspace.services.collectionobject.CollectionObjectList; + +/** + * A RelationServiceTest. + * + * @version $Revision:$ + */ +public class RelationServiceTest { + //empty +} diff --git a/services/relation/service/src/test/resources/log4j.xml b/services/relation/service/src/test/resources/log4j.xml new file mode 100644 index 000000000..52121cb83 --- /dev/null +++ b/services/relation/service/src/test/resources/log4j.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +