From ee3729b52bff62360229637cfd1d912c1b247dc9 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Fri, 16 Oct 2009 19:28:54 +0000 Subject: [PATCH] CSPACE-520: Provide full details when requesting a single ID generator instance by CSID, with related refactoring to eliminate duplicate code. --- .../services/id/IDResource.java | 143 +++++++++++------- .../services/id/IDService.java | 5 +- .../services/id/IDServiceJdbcImpl.java | 23 +-- .../id/test/IDServiceJdbcImplTest.java | 6 +- 4 files changed, 109 insertions(+), 68 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 f7a187d9e..abbe7e11b 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 @@ -69,12 +69,19 @@ public class IDResource { final Logger logger = LoggerFactory.getLogger(IDResource.class); final static IDService service = new IDServiceJdbcImpl(); + // Query parameter names and values. + final static String QUERY_PARAM_LIST_FORMAT = "format"; + final static String LIST_FORMAT_SUMMARY = "summary"; + final static String LIST_FORMAT_FULL = "full"; + final static String QUERY_PARAM_ID_GENERATOR_ROLE = "role"; + // XML namespace for the ID Service. final static String ID_SERVICE_NAMESPACE = "http://collectionspace.org/services/id"; final static String ID_SERVICE_NAMESPACE_PREFIX = "ns2"; - // Names of elements for ID generator lists and list items. + // Names of elements for ID generator instances, lists and list items. + final static String ID_GENERATOR_NAME = "idgenerator"; final static String ID_GENERATOR_LIST_NAME = "idgenerator-list"; final static String ID_GENERATOR_LIST_ITEM_NAME = "idgenerator-list-item"; @@ -244,7 +251,27 @@ public class IDResource { String resourceRepresentation = ""; try { - resourceRepresentation = service.readIDGenerator(csid); + IDGeneratorInstance instance = service.readIDGenerator(csid); + + Document doc = DocumentHelper.createDocument(); + Element root = doc.addElement(ID_GENERATOR_NAME); + Namespace namespace = + new Namespace(ID_SERVICE_NAMESPACE_PREFIX, ID_SERVICE_NAMESPACE); + doc.getRootElement().add(namespace); + + Element generatorElement = root.addElement(ID_GENERATOR_NAME); + // Add summary data about this ID generator instance. + generatorElement = addInstanceElementSummary(generatorElement, csid); + // Add detailed data about this ID generator instance. + generatorElement = + addInstanceElementDetails(generatorElement, csid, instance); + + try { + resourceRepresentation = prettyPrintXML(doc); + } catch(Exception e) { + logger.debug("Error pretty-printing XML: " + e.getMessage()); + resourceRepresentation = doc.asXML(); + } if ( resourceRepresentation == null || @@ -317,7 +344,8 @@ public class IDResource { @Path("") @Produces(MediaType.APPLICATION_XML) public Response readIDGeneratorsList( - @QueryParam("format") String format, @QueryParam("role") String role) { + @QueryParam(QUERY_PARAM_LIST_FORMAT) String listFormat, + @QueryParam(QUERY_PARAM_ID_GENERATOR_ROLE) String role) { logger.debug("> in readIDGeneratorsList()"); @@ -331,9 +359,6 @@ public class IDResource { String resourceRepresentation = ""; - final String LIST_FORMAT_SUMMARY = "summary"; - final String LIST_FORMAT_FULL = "full"; - // @TODO We're currently overloading the String items in // the 'generators' list with distinctly different types of // list data, depending on the list format requested. This may @@ -346,17 +371,17 @@ public class IDResource { // @TODO Filtering by role will likely take place here ... // Default to summary list if no list format is specified. - if (format == null || format.trim().isEmpty()) { + if (listFormat == null || listFormat.trim().isEmpty()) { resourceRepresentation = formattedSummaryList(generators); - } else if (format.equalsIgnoreCase(LIST_FORMAT_SUMMARY)) { + } else if (listFormat.equalsIgnoreCase(LIST_FORMAT_SUMMARY)) { resourceRepresentation = formattedSummaryList(generators); - } else if (format.equalsIgnoreCase(LIST_FORMAT_FULL)) { + } else if (listFormat.equalsIgnoreCase(LIST_FORMAT_FULL)) { resourceRepresentation = formattedFullList(generators); // Return an error if the value of the query parameter // is unrecognized. } else { // @TODO Return an appropriate XML-based entity body upon error. - String msg = "Query parameter '" + format + "' was not recognized."; + String msg = "Query parameter '" + listFormat + "' was not recognized."; logger.debug(msg); response = Response.status(Response.Status.BAD_REQUEST) @@ -485,15 +510,11 @@ public class IDResource { doc.getRootElement().add(namespace); Element listitem = null; - Element csid = null; - Element uri = null; - for (String csidValue : generators.keySet() ) + for (String csid : generators.keySet() ) { listitem = root.addElement(ID_GENERATOR_LIST_ITEM_NAME); - csid = listitem.addElement("csid"); - csid.addText(csidValue); - uri = listitem.addElement("uri"); - uri.addText(getRelativePath(csidValue)); + // Add summary data about this ID generator instance. + listitem = addInstanceElementSummary(listitem, csid); } String summaryList = ""; @@ -529,45 +550,14 @@ public class IDResource { doc.getRootElement().add(namespace); Element listitem = null; - Element csid = null; - Element uri = null; - Element displayname = null; - Element description = null; - Element generator = null; - Element generatorRoot = null; - String generatorStr = ""; - Document generatorDoc = null; - for (String csidValue : generators.keySet() ) + for (String csid : generators.keySet() ) { listitem = root.addElement(ID_GENERATOR_LIST_ITEM_NAME); - csid = listitem.addElement("csid"); - csid.addText(csidValue); - uri = listitem.addElement("uri"); - uri.addText(getRelativePath(csidValue)); - displayname = listitem.addElement("displayname"); - // Retrieve the matching ID generator instance by CSID, - // and then pull out several of its values. - displayname.addText(generators.get(csidValue).getDisplayName()); - description = listitem.addElement("description"); - description.addText(generators.get(csidValue).getDescription()); - generator = listitem.addElement("idgenerator"); - // Using the CSID as a key, get the XML string - // representation of the ID generator. - generatorStr = generators.get(csidValue).getGeneratorState(); - // Convert the XML string representation of the - // ID generator to a new XML document, copy its - // root element, and append it to the relevant location - // in the current list item. - try { - generatorDoc = textToXMLDocument(generatorStr); - generatorRoot = generatorDoc.getRootElement(); - generator.add(generatorRoot.createCopy()); - // If an error occurs parsing the XML string representation, - // the text of the ID generator element will remain empty. - } catch (Exception e) { - logger.warn("Error parsing XML text: " + generatorStr); - } - + // Add summary data about this ID generator instance. + listitem = addInstanceElementSummary(listitem, csid); + // Add detailed data about this ID generator instance. + listitem = + addInstanceElementDetails(listitem, csid, generators.get(csid)); } String summaryList = ""; @@ -581,6 +571,49 @@ public class IDResource { return summaryList; } + private Element addInstanceElementSummary(Element instanceElement, + String csidValue) { + + Element csid = null; + Element uri = null; + csid = instanceElement.addElement("csid"); + csid.addText(csidValue); + uri = instanceElement.addElement("uri"); + uri.addText(getRelativePath(csidValue)); + + return instanceElement; + } + + + private Element addInstanceElementDetails(Element instanceElement, + String csidValue, IDGeneratorInstance generatorInstance) { + + Element displayname = instanceElement.addElement("displayname"); + displayname.addText(generatorInstance.getDisplayName()); + Element description = instanceElement.addElement("description"); + description.addText(generatorInstance.getDescription()); + Element generator = instanceElement.addElement("idgenerator"); + // Using the CSID as a key, get the XML string + // representation of the ID generator. + String generatorStr = generatorInstance.getGeneratorState(); + // Convert the XML string representation of the + // ID generator to a new XML document, copy its + // root element, and append it to the relevant location + // in the current list item. + try { + Document generatorDoc = textToXMLDocument(generatorStr); + Element generatorRoot = generatorDoc.getRootElement(); + generator.add(generatorRoot.createCopy()); + // If an error occurs parsing the XML string representation, + // the text of the ID generator element will remain empty. + } catch (Exception e) { + logger.warn("Error parsing XML text: " + generatorStr); + } + + return instanceElement; + } + + // @TODO Refactoring opportunity: the utility methods below // might be moved into the 'common' module. @@ -614,7 +647,7 @@ public class IDResource { return sw.toString(); } - ////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////// /** * Returns an XML document, when provided with a String * representation of that XML document. 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 26a1a8a46..d93a2a04c 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 @@ -64,8 +64,9 @@ public interface IDService { throws IllegalArgumentException, IllegalStateException; // Read single object - public String readIDGenerator(String csid) throws DocumentNotFoundException, - IllegalArgumentException, IllegalStateException; + public IDGeneratorInstance readIDGenerator(String csid) + throws DocumentNotFoundException, IllegalArgumentException, + IllegalStateException; // Read a list of objects (aka read multiple) // and return a list (map) of those objects and their identifiers. 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 e42f01afe..2a50a78ca 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 @@ -214,7 +214,7 @@ public class IDServiceJdbcImpl implements IDService { String serializedGenerator = ""; try { - serializedGenerator = readIDGenerator(csid); + serializedGenerator = readIDGenerator(csid).getGeneratorState(); } catch (DocumentNotFoundException e ) { throw e; } catch (IllegalArgumentException e ) { @@ -561,22 +561,23 @@ public class IDServiceJdbcImpl implements IDService { * @throws IllegalStateException if a storage-related error occurred. */ @Override - public String readIDGenerator (String csid) throws + public IDGeneratorInstance readIDGenerator (String csid) throws DocumentNotFoundException, IllegalArgumentException, IllegalStateException { logger.debug("> in readIDGenerator"); - String serializedGenerator = null; + IDGeneratorInstance instance = null; Connection conn = null; try { conn = getJdbcConnection(); Statement stmt = conn.createStatement(); - - ResultSet rs = stmt.executeQuery( - "SELECT id_generator_state FROM id_generators " + + + ResultSet rs = stmt.executeQuery( + "SELECT csid, displayname, description, " + + "id_generator_state FROM id_generators " + "WHERE csid='" + csid + "'"); boolean moreRows = rs.next(); @@ -587,7 +588,10 @@ public class IDServiceJdbcImpl implements IDService { " could not be found."); } - serializedGenerator = rs.getString(1); + instance = new IDGeneratorInstance(); + instance.setDisplayName(rs.getString(2)); + instance.setDescription(rs.getString(3)); + instance.setGeneratorState(rs.getString(4)); rs.close(); @@ -606,9 +610,10 @@ public class IDServiceJdbcImpl implements IDService { } } - logger.debug("> retrieved SettableIDGenerator: " + serializedGenerator); + logger.debug("> retrieved SettableIDGenerator: " + + instance.getGeneratorState()); - return serializedGenerator; + return instance; } 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 f6f2874fa..30d8b032c 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 @@ -47,6 +47,7 @@ public class IDServiceJdbcImplTest { String nextId; String serializedGenerator; SettableIDGenerator generator; + IDGeneratorInstance instance; IDServiceJdbcImpl jdbc = new IDServiceJdbcImpl(); IDService service = jdbc; @@ -82,8 +83,9 @@ public class IDServiceJdbcImplTest { public void readIDGenerator() throws DocumentNotFoundException, IllegalArgumentException, IllegalStateException { - serializedGenerator = jdbc.readIDGenerator(DEFAULT_CSID); - Assert.assertTrue(serializedGenerator != null); + instance = jdbc.readIDGenerator(DEFAULT_CSID); + Assert.assertTrue(instance != null); + serializedGenerator = instance.getGeneratorState(); Assert.assertTrue(! serializedGenerator.equals("")); generator = IDGeneratorSerializer.deserialize(serializedGenerator); } -- 2.47.3