import org.collectionspace.services.client.PoxPayloadIn;
import org.collectionspace.services.client.PoxPayloadOut;
+import org.collectionspace.services.common.ServiceMain;
+import org.collectionspace.services.common.api.Tools;
import org.collectionspace.services.common.context.ServiceContext;
import org.collectionspace.services.common.document.BadRequestException;
import org.collectionspace.services.common.document.DocumentNotFoundException;
final String TABLE_NAME = "id_generator";
boolean jdbcDriverInstantiated = false;
boolean hasPreconditions = true;
+
+ final static String CSPACE_INSTANCE_ID = ServiceMain.getInstance().getCspaceInstanceId();
+
//////////////////////////////////////////////////////////////////////
/**
Connection conn = null;
try {
- conn = getJdbcConnection(ctx.getRepositoryName());
+ String repositoryName = ctx.getRepositoryName();
+ conn = getJdbcConnection(getDatabaseName(repositoryName));
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
String lastId = null;
Connection conn = null;
try {
- String repositoryName = ctx.getRepositoryName();
- conn = getJdbcConnection(repositoryName);
+ String repositoryName = ctx.getRepositoryName();
+ conn = getJdbcConnection(getDatabaseName(repositoryName));
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(
// @TODO Add checks for authorization to perform this operation.
- if (serializedIDGenerator == null || serializedIDGenerator.equals("")) {
- throw new BadRequestException(
- "Could not understand or parse this representation "
- + "of an ID generator.");
+ if (Tools.isBlank(serializedIDGenerator)) {
+ String msg = String.format("ID generator payload is null or empty.");
+ logger.warn(msg);
+ throw new BadRequestException(msg);
}
Connection conn = null;
try {
- String repositoryName = ctx.getRepositoryName();
- conn = getJdbcConnection(repositoryName);
+ String repositoryName = ctx.getRepositoryName();
+ conn = getJdbcConnection(getDatabaseName(repositoryName));
Statement stmt = conn.createStatement();
// Test whether this ID generator already exists in the database.
// conflict has occurred, so that this status can be reported
// to the client.
if (idGeneratorFound) {
- throw new IllegalStateException(
- "Conflict with existing generator when attempting to add "
- + "new ID generator with ID '"
- + csid
- + "' to the database.");
+ String msg = String.format("Conflict with existing generator when attempting to add "
+ + "new ID generator with ID '%s' to the database.", csid);
+ logger.warn(msg);
+ throw new IllegalStateException(msg);
// Otherwise, add this new ID generator, as a new record to
// the database.
int rowsUpdated = ps.executeUpdate();
if (rowsUpdated != 1) {
- throw new IllegalStateException(
- "Error adding new ID generator '" + csid
- + "'" + " to the database.");
+ String msg =
+ String.format("Error adding new ID generator '%s'to the database.", csid);
+ logger.warn(msg);
+ throw new IllegalStateException(msg);
}
} // end if (idGeneratorFound)
} catch (IllegalStateException ise) {
throw ise;
} catch (SQLException e) {
- throw new IllegalStateException("Error adding new ID "
- + "generator to the database: " + e.getMessage());
+ String msg = "Error adding new ID generator to the database: " + e.getMessage();
+ logger.warn(msg);
+ throw new IllegalStateException(msg);
} finally {
try {
if (conn != null) {
Connection conn = null;
try {
- conn = getJdbcConnection(ctx.getRepositoryName());
+ String repositoryName = ctx.getRepositoryName();
+ conn = getJdbcConnection(getDatabaseName(repositoryName));
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(
Connection conn = null;
try {
-
- conn = getJdbcConnection(ctx.getRepositoryName());
+ String repositoryName = ctx.getRepositoryName();
+ conn = getJdbcConnection(getDatabaseName(repositoryName));
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(
Connection conn = null;
try {
- conn = getJdbcConnection(ctx.getRepositoryName());
+ String repositoryName = ctx.getRepositoryName();
+ conn = getJdbcConnection(getDatabaseName(repositoryName));
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
Connection conn = null;
try {
- conn = getJdbcConnection(ctx.getRepositoryName());
+ String repositoryName = ctx.getRepositoryName();
+ conn = getJdbcConnection(getDatabaseName(repositoryName));
Statement stmt = conn.createStatement();
// Test whether this ID generator already exists in the database.
/**
* Opens a connection to the database and returns a JDBC Connection object.
*
- * @return A JDBC database Connection object.
+ * @param databaseName a JDBC database name.
+ * @return a JDBC database Connection object.
*
* @throws LoginException
* @throws SQLException if a storage-related error occurred.
*/
- public Connection getJdbcConnection(String repositoryName) throws NamingException, SQLException {
+ public Connection getJdbcConnection(String databaseName) throws NamingException, SQLException {
logger.debug("> in getJdbcConnection");
Connection conn = null;
try {
- conn = JDBCTools.getConnection(JDBCTools.NUXEO_DATASOURCE_NAME, repositoryName);
+ conn = JDBCTools.getConnection(JDBCTools.NUXEO_DATASOURCE_NAME, databaseName);
} catch (NamingException e) {
throw e;
} catch (SQLException e) {
Connection conn = null;
try {
- conn = getJdbcConnection(ctx.getRepositoryName());
+ String repositoryName = ctx.getRepositoryName();
+ conn = getJdbcConnection(getDatabaseName(repositoryName));
// Retrieve a list of tables in the current database.
final String CATALOG_NAME = null;
}
}
+
+ private String getDatabaseName(String repositoryName) {
+ return JDBCTools.getDatabaseName(repositoryName, CSPACE_INSTANCE_ID);
+ }
}