From c6bd35ccb0384f9ab6a5bc9df5c678624b8e520e Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Thu, 8 Oct 2009 02:14:23 +0000 Subject: [PATCH] CSPACE-497: Added REST call to return a full list of serialized ID generator instances. --- .../services/id/IDResource.java | 77 +++++++++++++++---- .../services/id/IDService.java | 3 + .../services/id/IDServiceJdbcImpl.java | 65 +++++++++++++++- .../id/test/IDServiceJdbcImplTest.java | 16 +++- 4 files changed, 142 insertions(+), 19 deletions(-) diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/IDResource.java b/services/id/service/src/main/java/org/collectionspace/services/id/IDResource.java index 251745c4b..254dcbc3f 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/IDResource.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/IDResource.java @@ -23,6 +23,7 @@ package org.collectionspace.services.id; +import java.util.List; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -160,9 +161,10 @@ public class IDResource { ////////////////////////////////////////////////////////////////////// /** - * Creates a new ID generator. + * Creates a new ID generator instance. * - * @param generatorRepresentation A representation of an ID generator. + * @param generatorRepresentation + * A representation of an ID generator instance. */ @POST @Path("") @@ -195,11 +197,11 @@ public class IDResource { ////////////////////////////////////////////////////////////////////// /** - * Returns a representation of a single ID Generator resource. + * Returns a representation of a single ID generator instance resource. * - * @param csid An identifier for an ID generator. + * @param csid An identifier for an ID generator instance. * - * @return A representation of an ID generator resource. + * @return A representation of an ID generator instance resource. */ @GET @Path("/{csid}") @@ -257,26 +259,67 @@ public class IDResource { ////////////////////////////////////////////////////////////////////// /** - * Placeholder for retrieving a list of available ID Generator resources. - * Currently returns an empty entity body. + * Placeholder for retrieving a list of available ID Generator + * instance resources. * - * Implemented to facilitate a HEAD method test in ServiceLayerTest. + * Required to facilitate a HEAD method test in ServiceLayerTest. * - * @return An empty entity body (for now). + * @return A list of representations of ID generator instance resources. */ @GET @Path("") @Produces(MediaType.APPLICATION_XML) public Response readIDGeneratorsList() { - - logger.debug("> in readIDGeneratorsList()"); - - // @TODO Replace this placeholder code. - Response response = Response.status(Response.Status.NO_CONTENT) - .entity("").type(MediaType.TEXT_PLAIN).build(); - + + logger.debug("> in readIDGeneratorsList()"); + + Response response = null; + response = response.ok().build(); + String resourceRepresentation = ""; + + // @TODO Replace these placeholders/expedients + // with a schema-defined list format. + final String LIST_ROOT_START = ""; + final String LIST_ROOT_END = ""; + final String LIST_ITEM_START = ""; + final String LIST_ITEM_END = ""; + + try { + + List generators = service.readIDGeneratorsList(); + + // @TODO Replace this placeholder/expedient, as per above + StringBuffer sb = new StringBuffer(""); + sb.append(LIST_ROOT_START); + for (String generator : generators) { + sb.append(LIST_ITEM_START); + sb.append(generator); + sb.append(LIST_ITEM_END); + } + sb.append(LIST_ROOT_END); + + resourceRepresentation = sb.toString(); + + response = Response.status(Response.Status.OK) + .entity(resourceRepresentation) + .type(MediaType.APPLICATION_XML) + .build(); + + // @TODO Return an XML-based error results format with the + // responses below. + + } catch (IllegalStateException ise) { + response = Response.status(Response.Status.BAD_REQUEST) + .entity(ise.getMessage()).type(MediaType.TEXT_PLAIN).build(); + + // This is guard code that should never be reached. + } catch (Exception e) { + response = Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build(); + } + return response; - } + } } diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/IDService.java b/services/id/service/src/main/java/org/collectionspace/services/id/IDService.java index 05a41c049..95223fbec 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/IDService.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/IDService.java @@ -19,6 +19,7 @@ package org.collectionspace.services.id; // May at some point instead use // org.jboss.resteasy.spi.NotFoundException +import java.util.List; import org.collectionspace.services.common.repository.DocumentNotFoundException; import org.collectionspace.services.common.repository.BadRequestException; @@ -67,6 +68,8 @@ public interface IDService { IllegalArgumentException, IllegalStateException; // Read a list of objects (aka read multiple) + // Read single object + public List readIDGeneratorsList() throws IllegalStateException; // Update (may need to check for changes in the ID generator structure) public void updateIDGenerator(String csid, String serializedIDGenerator) diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/IDServiceJdbcImpl.java b/services/id/service/src/main/java/org/collectionspace/services/id/IDServiceJdbcImpl.java index 88ea343b2..ea27a17aa 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/IDServiceJdbcImpl.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/IDServiceJdbcImpl.java @@ -86,6 +86,8 @@ import java.sql.Statement; // May at some point instead use // org.jboss.resteasy.spi.NotFoundException +import java.util.ArrayList; +import java.util.List; import org.collectionspace.services.common.repository.BadRequestException; import org.collectionspace.services.common.repository.DocumentNotFoundException; @@ -607,7 +609,67 @@ public class IDServiceJdbcImpl implements IDService { return serializedGenerator; } - + + ////////////////////////////////////////////////////////////////////// + /** + * Returns a list of ID generator instances from persistent storage. + * + * @return A list of ID generator instances, with each list item + * constituting a serialized representation of an + * ID generator instance. + * + * @throws IllegalStateException if a storage-related error occurred. + */ + @Override + public List readIDGeneratorsList() throws IllegalStateException { + + logger.debug("> in readIDGeneratorList"); + + List generators = new ArrayList(); + + Connection conn = null; + try { + + conn = getJdbcConnection(); + Statement stmt = conn.createStatement(); + + ResultSet rs = stmt.executeQuery( + "SELECT id_generator_state FROM id_generators"); + + boolean moreRows = rs.next(); + if (! moreRows) { + return generators; + } + + while (moreRows = rs.next()) { + generators.add(rs.getString(1)); + } + + rs.close(); + + } catch (SQLException e) { + throw new IllegalStateException( + "Error retrieving ID generators " + + " from database: " + e.getMessage()); + } finally { + try { + if (conn != null) { + conn.close(); + } + } catch(SQLException e) { + // Do nothing here + } + } + + logger.debug("> retrieved list: "); + for (String generator : generators) { + logger.debug("generator=\n" + generator); + } + + return generators; + } + + ////////////////////////////////////////////////////////////////////// /** * Updates an existing ID generator in persistent storage. @@ -949,5 +1011,6 @@ public class IDServiceJdbcImpl implements IDService { } } + } \ No newline at end of file diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/test/IDServiceJdbcImplTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/test/IDServiceJdbcImplTest.java index c786ab360..82d3d9cab 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/test/IDServiceJdbcImplTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/test/IDServiceJdbcImplTest.java @@ -23,6 +23,7 @@ package org.collectionspace.services.id.test; +import java.util.List; import org.collectionspace.services.common.repository.BadRequestException; import org.collectionspace.services.common.repository.DocumentNotFoundException; @@ -75,6 +76,7 @@ public class IDServiceJdbcImplTest { jdbc.createIDGenerator(DEFAULT_CSID, getSpectrumEntryNumberGenerator()); } + @Test(dependsOnMethods = {"hasRequiredDatabaseTable", "createIDGenerator"}) public void readIDGenerator() throws DocumentNotFoundException, IllegalArgumentException, IllegalStateException { @@ -82,7 +84,19 @@ public class IDServiceJdbcImplTest { serializedGenerator = jdbc.readIDGenerator(DEFAULT_CSID); generator = IDGeneratorSerializer.deserialize(serializedGenerator); Assert.assertEquals(generator.getCsid(), DEFAULT_CSID); - + } + + + @Test(dependsOnMethods = + {"hasRequiredDatabaseTable", "createIDGenerator", "readIDGenerator"}) + public void readIDGeneratorList() throws IllegalStateException { + + List generators = jdbc.readIDGeneratorsList(); + + // @TODO Replace this placeholder test, which just + // verifies that no error occurred while retrieving the list, + // with a more meaningful test. + Assert.assertTrue(generators.size() > 0); } @Test(dependsOnMethods = {"hasRequiredDatabaseTable", "createIDGenerator", -- 2.47.3