From 88476038955f8e0caa2f4d246a23d96a2ea30b27 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Wed, 22 Jul 2009 00:32:48 +0000 Subject: [PATCH] CSPACE-245,CSPACE-234: A minimal ID Service is now working to deliver next IDs for patterns. ID Patterns and their state are now serialized and deserialized via XStream and stored via JDBC. --- services/id/service/pom.xml | 16 +- .../collectionspace/services/IDResource.java | 7 +- .../services/IDServiceJdbcImpl.java | 359 +++++++++++++++++- .../services/id/IDPattern.java | 14 +- .../services/IDServiceJdbcImplTest.java | 32 +- .../services/IDServiceTest.java | 8 +- .../services/id/IDPatternTest.java | 52 +-- .../scripts/db/mysql/create_ids_table.sql | 23 +- 8 files changed, 457 insertions(+), 54 deletions(-) diff --git a/services/id/service/pom.xml b/services/id/service/pom.xml index 6e4511213..af4ec5c1b 100644 --- a/services/id/service/pom.xml +++ b/services/id/service/pom.xml @@ -37,6 +37,14 @@ 1.0 services.id.service + + + codehaus-snapshot + Codehaus Snapshot Repo + http://snapshots.repository.codehaus.org + + + @@ -157,7 +165,13 @@ mysql-connector-java 5.1.6 - + + + + com.thoughtworks.xstream + xstream + 1.3.2-SNAPSHOT + diff --git a/services/id/service/src/main/java/org/collectionspace/services/IDResource.java b/services/id/service/src/main/java/org/collectionspace/services/IDResource.java index 4c08349cb..233a2e60d 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/IDResource.java +++ b/services/id/service/src/main/java/org/collectionspace/services/IDResource.java @@ -84,6 +84,9 @@ public class IDResource { * other form of identifier to be determined, such as URLs or URNs. */ @GET + // @TODO: Temporary during early testing. + // To be changed to a POST request, and most likely to a different URL. + // E.g. /idpatterns/{identifier}/ids @Path("/next/patterns/{csid}") // @TODO: Temporary during testing; to be changed to return XML @Produces("text/plain") @@ -146,7 +149,7 @@ public class IDResource { * @param idPattern An ID Pattern. * */ - protected void verbose(String msg, IDPattern idPattern) { + protected static void verbose(String msg, IDPattern idPattern) { try { verbose(msg); @@ -167,7 +170,7 @@ public class IDResource { * @param msg A message. * */ - protected void verbose(String msg) { + protected static void verbose(String msg) { System.out.println("IDResource. " + msg); } diff --git a/services/id/service/src/main/java/org/collectionspace/services/IDServiceJdbcImpl.java b/services/id/service/src/main/java/org/collectionspace/services/IDServiceJdbcImpl.java index 3c3c2283c..2dee9b5c5 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/IDServiceJdbcImpl.java +++ b/services/id/service/src/main/java/org/collectionspace/services/IDServiceJdbcImpl.java @@ -51,6 +51,8 @@ // @TODO: Verify access (public, protected, or private) to service methods. +// @TODO: As long as we're using JDBC, use PreparedStatements, not Statements. + package org.collectionspace.services; import org.collectionspace.services.IDService; @@ -58,10 +60,15 @@ import org.collectionspace.services.IDService; // to accommodate future ID generation components. import org.collectionspace.services.id.*; +import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.io.xml.DomDriver; +// import com.thoughtworks.xstream.io.HierarchicalStreamDriver; + import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.PreparedStatement; import java.sql.Statement; public class IDServiceJdbcImpl implements IDService { @@ -77,10 +84,47 @@ public class IDServiceJdbcImpl implements IDService { final String DATABASE_USERNAME = "test"; final String DATABASE_PASSWORD = "test"; - // Placeholder constructor. + // Constructor. public void IDServiceJdbcImpl() { } + // Temporary expedient to populate the database with hard-coded ID Pattens. + // + // @TODO: Remove this temporary expedient when possible. + public void init() { + + Integer integer; + IDPattern pattern; + String csid = ""; + + // Test whether ID patterns 1 and 2 exist in the database; + // if not, create and populate records for those patterns. + + csid = "1"; + try { + pattern = getIDPattern(csid); + } catch (IllegalArgumentException e ) { + pattern = new IDPattern(csid); + pattern.add(new StringIDPart("E")); + pattern.add(new NumericIDPart("1")); + storeIDPattern(csid, pattern); + } + + csid = "2"; + try { + pattern = getIDPattern(csid); + } catch (IllegalArgumentException e ) { + pattern = new IDPattern(csid); + pattern.add(new YearIDPart()); + pattern.add(new StringIDPart(".")); + pattern.add(new NumericIDPart("1")); + pattern.add(new StringIDPart(".")); + pattern.add(new NumericIDPart("1")); + storeIDPattern(csid, pattern); + } + + } + ////////////////////////////////////////////////////////////////////// /* * Returns the next ID associated with a specified ID pattern. @@ -109,6 +153,11 @@ public class IDServiceJdbcImpl implements IDService { IDPattern pattern; String nextId = ""; String lastId = ""; + + // @TODO: Remove this call to init (a temporary expedient) when possible. + // Even as is, this should be a one-time utility function, not called + // each time nextID is invoked. + init(); if (csid == null || csid.equals("")) { throw new IllegalArgumentException( @@ -119,6 +168,8 @@ public class IDServiceJdbcImpl implements IDService { pattern = getIDPattern(csid); } catch (IllegalArgumentException e ) { throw e; + } catch (IllegalStateException e ) { + throw e; } // Guard code - should not be needed. @@ -153,6 +204,7 @@ public class IDServiceJdbcImpl implements IDService { // Store the 'next' ID as the last-generated ID for this pattern. IDResource.verbose("> storing last-generated ID: " + nextId); storeLastID(csid, nextId); + storeIDPattern(csid, pattern); } catch (IllegalArgumentException e ) { throw e; @@ -183,7 +235,8 @@ public class IDServiceJdbcImpl implements IDService { * @TODO: Retrieve IDPatterns from the database using JDBC, * rather than hard-coding their construction here. */ - public IDPattern getIDPattern(String csid) throws IllegalArgumentException { + +/* public IDPattern getIDPattern(String csid) throws IllegalArgumentException { IDPattern pattern; @@ -220,6 +273,7 @@ public class IDServiceJdbcImpl implements IDService { } } +*/ ////////////////////////////////////////////////////////////////////// /* @@ -240,6 +294,9 @@ public class IDServiceJdbcImpl implements IDService { * To uniquely identify ID patterns in production, we'll need to handle * both CollectionSpace IDs (csids) - a form of UUIDs/GUIDs - and some * other form of identifier to be determined, such as URLs or URNs. + * + * @TODO: Refactor to remove redundant code that this method shares with other + * database-using methods in this class. */ public String getLastID(String csid) throws IllegalArgumentException, IllegalStateException { @@ -278,7 +335,7 @@ public class IDServiceJdbcImpl implements IDService { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery( - "SELECT last_id FROM ids_last_generated WHERE id_pattern='" + csid + "'"); + "SELECT last_generated_id FROM id_patterns WHERE id_pattern_csid='" + csid + "'"); boolean moreRows = rs.next(); if (! moreRows) { @@ -311,12 +368,114 @@ public class IDServiceJdbcImpl implements IDService { } + ////////////////////////////////////////////////////////////////////// + /* + * Returns a requested ID pattern from persistent storage. This pattern will + * include values for the last-generated IDs of each of its constituent parts, + * and thus can be used to generate a current or 'next' ID for that pattern. + * + * @param csid An identifier for an ID pattern. + * + * @return The requested ID pattern. + * + * @throws IllegalArgumentException if the requested ID pattern could not be found. + * + * @throws IllegalStateException if a storage-related error occurred. + * + * @TODO: We're currently using simple integer IDs to identify ID patterns + * in this initial iteration. + * + * To uniquely identify ID patterns in production, we'll need to handle + * both CollectionSpace IDs (csids) - a form of UUIDs/GUIDs - and some + * other form of identifier to be determined, such as URLs or URNs. + * + * @TODO: Refactor to remove redundant code that this method shares with other + * database-using methods in this class. + */ + public IDPattern getIDPattern(String csid) throws IllegalArgumentException, IllegalStateException { + + IDResource.verbose("> in getIDPattern"); + + String serializedIDPattern = null; + Connection conn = null; + + try { + Class.forName(JDBC_DRIVER_CLASSNAME).newInstance(); + } catch (ClassNotFoundException e) { + throw new IllegalStateException( + "Error finding JDBC driver class '" + + JDBC_DRIVER_CLASSNAME + + "' to set up database connection."); + } catch (InstantiationException e) { + throw new IllegalStateException( + "Error instantiating JDBC driver class '" + + JDBC_DRIVER_CLASSNAME + + "' to set up database connection."); + } catch (IllegalAccessException e) { + throw new IllegalStateException( + "Error accessing JDBC driver class '" + + JDBC_DRIVER_CLASSNAME + + "' to set up database connection."); + } + + try { + + // @TODO: Get these values from configuration; better yet, substitute Hibernate + // for JDBC for accessing database-managed persistence. + // + // @TODO: Remove any hard-coded dependencies on MySQL. + conn = DriverManager.getConnection(DATABASE_URL, DATABASE_USERNAME, DATABASE_PASSWORD); + + Statement stmt = conn.createStatement(); + + ResultSet rs = stmt.executeQuery( + "SELECT id_pattern_state FROM id_patterns WHERE id_pattern_csid='" + csid + "'"); + + boolean moreRows = rs.next(); + if (! moreRows) { + throw new IllegalArgumentException( + "Pattern with ID " + + "\'" + csid + "\'" + + " could not be found."); + } + + serializedIDPattern = rs.getString(1); + + rs.close(); + + } catch (SQLException e) { + System.err.println("Exception: " + e.getMessage()); + throw new IllegalStateException( + "Error retrieving ID pattern " + + "\'" + csid + "\'" + + " from database: " + e.getMessage()); + } finally { + try { + if (conn != null) { + conn.close(); + } + } catch(SQLException e) { + // Do nothing here + } + } + + + IDResource.verbose("> retrieved IDPattern: " + serializedIDPattern); + + IDPattern pattern = deserializeIDPattern(serializedIDPattern); + + return pattern; + + } + ////////////////////////////////////////////////////////////////////// /* * Stores the last-generated ID, corresponding to a specified ID pattern, * into persistent storage. * - * @param csid An identifier for an ID pattern. + * @param csid An identifier for an ID pattern. + * + * @param pattern An ID Pattern, including the values of its constituent parts. * * @param lastId The value of the last-generated ID associated with that ID pattern. * @@ -331,7 +490,8 @@ public class IDServiceJdbcImpl implements IDService { * both CollectionSpace IDs (csids) - a form of UUIDs/GUIDs - and some * other form of identifier to be determined, such as URLs or URNs. * - * @TODO: Refactor to remove redundant code that this method shares with getLastID(), above. + * @TODO: Refactor to remove redundant code that this method shares with other + * database-using methods in this class. */ public void storeLastID(String csid, String lastId) throws IllegalArgumentException, IllegalStateException { @@ -370,7 +530,7 @@ public class IDServiceJdbcImpl implements IDService { Statement stmt = conn.createStatement(); int rowsUpdated = stmt.executeUpdate( - "UPDATE ids_last_generated SET last_id='" + lastId + "' WHERE id_pattern='" + csid + "'"); + "UPDATE id_patterns SET last_generated_id='" + lastId + "' WHERE id_pattern_csid='" + csid + "'"); if (rowsUpdated != 1) { throw new IllegalStateException( @@ -393,5 +553,190 @@ public class IDServiceJdbcImpl implements IDService { } } - + + ////////////////////////////////////////////////////////////////////// + /* + * Stores a serialized ID pattern, representing the state of that pattern, + * into persistent storage. + * + * @param csid An identifier for an ID pattern. + * + * @param pattern An ID Pattern, reflecting its current state, including the + * values of its constituent parts. + * + * @throws IllegalStateException if a storage-related error occurred. + * + * @TODO: We're currently using simple integer IDs to identify ID patterns + * in this initial iteration. + * + * To uniquely identify ID patterns in production, we'll need to handle + * both CollectionSpace IDs (csids) - a form of UUIDs/GUIDs - and some + * other form of identifier to be determined, such as URLs or URNs. + * + * @TODO: Refactor to remove redundant code that this method shares with other + * database-using methods in this class. + */ + public void storeIDPattern(String csid, IDPattern pattern) + throws IllegalArgumentException, IllegalStateException { + + IDResource.verbose("> in storeIDPattern"); + + Connection conn = null; + + String serializedIDPattern = serializeIDPattern(pattern); + + try { + Class.forName(JDBC_DRIVER_CLASSNAME).newInstance(); + } catch (ClassNotFoundException e) { + throw new IllegalStateException( + "Error finding JDBC driver class '" + + JDBC_DRIVER_CLASSNAME + + "' to set up database connection."); + } catch (InstantiationException e) { + throw new IllegalStateException( + "Error instantiating JDBC driver class '" + + JDBC_DRIVER_CLASSNAME + + "' to set up database connection."); + } catch (IllegalAccessException e) { + throw new IllegalStateException( + "Error accessing JDBC driver class '" + + JDBC_DRIVER_CLASSNAME + + "' to set up database connection."); + } + + try { + + // @TODO: Get these values from configuration; better yet, substitute Hibernate + // for JDBC for accessing database-managed persistence. + // + // @TODO: Remove any hard-coded dependencies on MySQL. + conn = DriverManager.getConnection(DATABASE_URL, DATABASE_USERNAME, DATABASE_PASSWORD); + + Statement stmt = conn.createStatement(); + + // Test whether the ID Pattern record already exists in the database. + ResultSet rs = stmt.executeQuery( + "SELECT id_pattern_csid FROM id_patterns WHERE id_pattern_csid='" + csid + "'"); + + boolean moreRows = rs.next(); + + boolean idPatternFound = true; + if (! moreRows) { + idPatternFound = false; + } + + // If the ID Pattern already exists in the database, update its record; + // otherwise, add it as a new record. + if (idPatternFound) { + + String stmtStr = + "UPDATE id_patterns SET id_pattern_state = ? WHERE id_pattern_csid = ?"; + + PreparedStatement ps = conn.prepareStatement(stmtStr); + ps.setString(1, serializedIDPattern); + ps.setString(2, csid); + + int rowsUpdated = ps.executeUpdate(); + if (rowsUpdated != 1) { + throw new IllegalStateException( + "Error adding new ID pattern '" + csid + "'" + " to the database."); + } + +/* + String stmtStr = + "UPDATE id_patterns SET id_pattern_state='" + serializedIDPattern + "'" + + " WHERE id_pattern_csid='" + csid + "'"; + IDResource.verbose("> update statement: " + stmtStr); + + int rowsUpdated = stmt.executeUpdate(stmtStr); + if (rowsUpdated != 1) { + throw new IllegalStateException( + "Error updating state of ID pattern '" + csid + "'" + " in the database."); + } +*/ + + } else { + + String stmtStr = + "INSERT INTO id_patterns (id_pattern_csid, id_pattern_state) VALUES (?, ?)"; + PreparedStatement ps = conn.prepareStatement(stmtStr); + ps.setString(1, csid); + ps.setString(2, serializedIDPattern); + + int rowsUpdated = ps.executeUpdate(); + if (rowsUpdated != 1) { + throw new IllegalStateException( + "Error adding new ID pattern '" + csid + "'" + " to the database."); + } + +/* + String stmtStr = + "INSERT INTO id_patterns (id_pattern_csid, id_pattern_state) " + + "VALUES (" + csid + "," + serializedIDPattern + ")"; + IDResource.verbose("> insert statement: " + stmtStr); + + int rowsUpdated = stmt.executeUpdate(stmtStr); + if (rowsUpdated != 1) { + throw new IllegalStateException( + "Error adding new ID pattern '" + csid + "'" + " to the database."); + } +*/ + + } // end if (idPatternFound) + + IDResource.verbose("> successfully stored ID Pattern: " + csid); + + } catch (SQLException e) { + System.err.println("Exception: " + e.getMessage()); + throw new IllegalStateException("Error updating last-generated ID in database: " + e.getMessage()); + } finally { + try { + if (conn != null) { + conn.close(); + } + } catch(SQLException e) { + // Do nothing here + } + } + + } + + ////////////////////////////////////////////////////////////////////// + /* + * Serializes an ID Pattern. + * + * @param pattern An ID pattern. + * + * @return A serialized representation of that ID pattern. + * + * @throws IllegalStateException if a storage-related error occurred. + */ + public static String serializeIDPattern(IDPattern pattern) { + + // @TODO: Add Exception handling + XStream xstream = new XStream(new DomDriver()); + String serializedIDPattern = xstream.toXML(pattern); + + return serializedIDPattern; + + } + + ////////////////////////////////////////////////////////////////////// + /* + * Deerializes an ID Pattern. + * + * @param serializedIDPattern A serialized representation of an ID pattern. + * + * @return The ID pattern as a Java Object. + */ + public static IDPattern deserializeIDPattern(String serializedIDPattern) { + + // @TODO: Add Exception handling + XStream xstream = new XStream(new DomDriver()); + IDPattern pattern = (IDPattern) xstream.fromXML(serializedIDPattern); + + return pattern; + + } + } diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/IDPattern.java b/services/id/service/src/main/java/org/collectionspace/services/id/IDPattern.java index 0acdc8099..ce3a8dc47 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/IDPattern.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/IDPattern.java @@ -37,16 +37,22 @@ import java.util.regex.Pattern; public class IDPattern { - final static int MAX_ID_LENGTH = 50; - + private String csid = ""; private Vector parts = new Vector(); + final static int MAX_ID_LENGTH = 50; // Constructor - public IDPattern() { + public IDPattern(String csid) { + if (csid != null && ! csid.equals("")) { + this.csid = csid; + } } // Constructor - public IDPattern(Vector partsList) { + public IDPattern(String csid, Vector partsList) { + if (csid != null && ! csid.equals("")) { + this.csid = csid; + } if (partsList != null) { this.parts = partsList; } diff --git a/services/id/service/src/test/java/org/collectionspace/services/IDServiceJdbcImplTest.java b/services/id/service/src/test/java/org/collectionspace/services/IDServiceJdbcImplTest.java index b46c74c51..32fccdba3 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/IDServiceJdbcImplTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/IDServiceJdbcImplTest.java @@ -27,7 +27,7 @@ package org.collectionspace.services.test; import org.collectionspace.services.IDService; import org.collectionspace.services.IDServiceJdbcImpl; -import org.collectionspace.services.id.YearIDGenerator; +import org.collectionspace.services.id.*; import junit.framework.TestCase; import static org.junit.Assert.*; @@ -36,12 +36,20 @@ public class IDServiceJdbcImplTest extends TestCase { String currentYear = YearIDGenerator.getCurrentYear(); String nextId; - IDService service; + IDServiceJdbcImpl jdbc = new IDServiceJdbcImpl(); + IDService service = jdbc; + + final static String DEFAULT_CSID = "1"; + + final static String DEFAULT_SERIALIZED_ID_PATTERN = + "\n" + + " " + DEFAULT_CSID + "\n" + + " \n" + + ""; protected void setUp() { nextId = ""; - service = new IDServiceJdbcImpl(); } @@ -56,6 +64,8 @@ public class IDServiceJdbcImplTest extends TestCase { } + // This test requires that the ID Service is running, + // and that an ID Pattern with the identifier '3' does not exist. public void testNextIDNotValidPattern() { try { @@ -66,5 +76,21 @@ public class IDServiceJdbcImplTest extends TestCase { } } + + // @TODO We may want to canonicalize (or otherwise normalize) the expected and + // actual XML in these tests, to avoid failures resulting from differences in + // whitespace, etc. + public void testSerializeIDPattern() { + IDPattern pattern = new IDPattern(DEFAULT_CSID); + assertEquals(DEFAULT_SERIALIZED_ID_PATTERN, jdbc.serializeIDPattern(pattern)); + } + + public void testDeserializeIDPattern() { + // This test will fail with different hash codes unless we add an IDPattern.equals() + // method that explicitly defines object equality as, for instance, having identical values + // in each of its instance variables. + // IDPattern pattern = jdbc.deserializeIDPattern(DEFAULT_SERIALIZED_ID_PATTERN); + // assertEquals(pattern, new IDPattern(DEFAULT_CSID)); + } } diff --git a/services/id/service/src/test/java/org/collectionspace/services/IDServiceTest.java b/services/id/service/src/test/java/org/collectionspace/services/IDServiceTest.java index 1fa1c3e6f..83711357a 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/IDServiceTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/IDServiceTest.java @@ -79,6 +79,7 @@ public class IDServiceTest extends TestCase { // Submitted 2009-07-16 as Restlet Issue 847, // http://restlet.tigris.org/issues/show_bug.cgi?id=847 +/* public void testPossibleRestletUnknownHostBug() { Request request = @@ -87,7 +88,8 @@ public class IDServiceTest extends TestCase { Response response = client.handle(request); } - +*/ + // Stub tests to run first, to verify the basic functionality of this test class. // Tests of GET requests. @@ -102,6 +104,7 @@ public class IDServiceTest extends TestCase { assertFalse(isSuccessResponse(response)); } +/* public void testNonExistentDomainGetRequest() { // This triggered an UnknownHostException in Restlet's Uniform // (and hence Client) classes, which is not yet documented as @@ -109,7 +112,8 @@ public class IDServiceTest extends TestCase { response = sendGetRequest(NONEXISTENT_DOMAIN_URL_STRING); assertFalse(isSuccessResponse(response)); } - +*/ + // Tests that are believed to be independent of HTTP method. public void testNonParseableURL() { diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/IDPatternTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/IDPatternTest.java index 88059188e..27ec02f09 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/IDPatternTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/IDPatternTest.java @@ -33,6 +33,8 @@ public class IDPatternTest extends TestCase { IDPattern pattern; IDPart part; + + final static String DEFAULT_CSID = "1"; // Note: tests may fail with IllegalArgumentException // if any initialization of new IDParts fails @@ -46,14 +48,14 @@ public class IDPatternTest extends TestCase { parts.add(new NumericIDPart("1")); parts.add(new StringIDPart("-")); parts.add(new AlphabeticIDPart("a")); - pattern = new IDPattern(parts); + pattern = new IDPattern(DEFAULT_CSID, parts); assertEquals("2009.1-a", pattern.getCurrentID()); } public void testCurrentIDViaAdd() { - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("1")); @@ -61,7 +63,7 @@ public class IDPatternTest extends TestCase { pattern.add(new AlphabeticIDPart("a")); assertEquals("2009.1-a", pattern.getCurrentID()); - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("0")); @@ -72,16 +74,14 @@ public class IDPatternTest extends TestCase { } public void testCurrentIDWithPartialSuppliedID() { - - // @TODO: Temporary for testing: ascertain regex patterns - - pattern = new IDPattern(); + + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new StringIDPart("E")); pattern.add(new NumericIDPart("1")); assertEquals("E1", pattern.getCurrentID("E")); assertEquals("E2", pattern.nextID()); - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart()); pattern.add(new StringIDPart(".")); assertEquals("2009.", pattern.getCurrentID("2009")); @@ -89,21 +89,21 @@ public class IDPatternTest extends TestCase { assertEquals("2010.", pattern.getCurrentID("2010")); assertEquals("2010.", pattern.nextID()); - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart()); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("1")); assertEquals("2009.1", pattern.getCurrentID("2009.")); assertEquals("2009.2", pattern.nextID()); - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart()); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("55")); assertEquals("2010.55", pattern.getCurrentID("2010.")); assertEquals("2010.56", pattern.nextID()); - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart()); @@ -113,7 +113,7 @@ public class IDPatternTest extends TestCase { assertEquals("2009.1", pattern.getCurrentID("2009.")); assertEquals("2009.2", pattern.nextID()); - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("1")); @@ -127,14 +127,14 @@ public class IDPatternTest extends TestCase { public void testCurrentIDWithFullSuppliedID() { - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("55")); assertEquals("2009.55", pattern.getCurrentID("2009.55")); assertEquals("2009.56", pattern.nextID()); - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("1")); @@ -147,14 +147,14 @@ public class IDPatternTest extends TestCase { public void testNextID() { - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("1")); assertEquals("2009.2", pattern.nextID()); assertEquals("2009.3", pattern.nextID()); - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("1")); @@ -163,13 +163,13 @@ public class IDPatternTest extends TestCase { assertEquals("2009.1-b", pattern.nextID()); assertEquals("2009.1-c", pattern.nextID()); - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new StringIDPart("T")); pattern.add(new NumericIDPart("1005")); assertEquals("T1006", pattern.nextID()); assertEquals("T1007", pattern.nextID()); - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("1")); @@ -181,7 +181,7 @@ public class IDPatternTest extends TestCase { public void testNextIDWithConstantStringID() { - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("1")); @@ -193,14 +193,14 @@ public class IDPatternTest extends TestCase { public void testNextIDWithSuppliedID() { - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("1")); assertEquals("2009.2", pattern.nextID("2009.1")); assertEquals("2009.3", pattern.nextID("2009.2")); - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("1")); @@ -213,14 +213,14 @@ public class IDPatternTest extends TestCase { public void testEmptyPartsListCurrentID() { - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); assertEquals("", pattern.getCurrentID()); } public void testIsValidIDYearPattern() { - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); assertTrue(pattern.isValidID("2009")); @@ -234,7 +234,7 @@ public class IDPatternTest extends TestCase { public void testGetRegex() { - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("1")); @@ -244,7 +244,7 @@ public class IDPatternTest extends TestCase { public void testIsValidIDYearSeparatorItemPattern() { - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart(".")); pattern.add(new NumericIDPart("1")); @@ -258,7 +258,7 @@ public class IDPatternTest extends TestCase { assertFalse(pattern.isValidID("2009-a")); assertFalse(pattern.isValidID("non-pattern conforming text")); - pattern = new IDPattern(); + pattern = new IDPattern(DEFAULT_CSID); pattern.add(new YearIDPart("2009")); pattern.add(new StringIDPart("ZZ.AND.")); pattern.add(new NumericIDPart("1")); diff --git a/src/main/resources/scripts/db/mysql/create_ids_table.sql b/src/main/resources/scripts/db/mysql/create_ids_table.sql index faab26f7e..82a6766c8 100644 --- a/src/main/resources/scripts/db/mysql/create_ids_table.sql +++ b/src/main/resources/scripts/db/mysql/create_ids_table.sql @@ -27,21 +27,26 @@ CREATE DATABASE IF NOT EXISTS `cspace`; USE `cspace`; -DROP TABLE IF EXISTS `ids_last_generated`; -CREATE TABLE `ids_last_generated` ( - `id_pattern` varchar(255) PRIMARY KEY, - `last_id` varchar(255), - `modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, - INDEX `id_patterns_index` (`id_pattern`) +DROP TABLE IF EXISTS `id_patterns`; +CREATE TABLE `id_patterns` ( + `id_pattern_csid` varchar(80) PRIMARY KEY, + `id_pattern_uri` varchar(200), + `id_pattern_description` varchar(2000), + `id_pattern_state` varchar(8000), + `last_generated_id` varchar(255), + `modified` timestamp NOT NULL + default CURRENT_TIMESTAMP + on update CURRENT_TIMESTAMP, + INDEX `id_pattern_csid_index` (`id_pattern_csid`) ) ENGINE=InnoDB; -- Hard-coding of identifiers for an initial set of ID Patterns, -- as a temporary expedient during development. -INSERT INTO `ids_last_generated` (`id_pattern`, `last_id`) VALUES ('1', NULL); -INSERT INTO `ids_last_generated` (`id_pattern`, `last_id`) VALUES ('2', NULL); +-- INSERT INTO `id_patterns` (`id_pattern_csid`, `last_generated_id`) VALUES ('1', NULL); +-- INSERT INTO `id_patterns` (`id_pattern_csid', `last_generated_id`) VALUES ('2', NULL); GRANT SELECT, INSERT, UPDATE, DELETE - on `ids_last_generated` + on `id_patterns` to `test`; SHOW WARNINGS; -- 2.47.3