I should've been more clear... I'm not adding a record to the internal db through the PersistentRecord
interface. What I am doing is adding a record to a datasource I added through the database connections config page. In other words, I added a new DB called ModuleDev
through SSMS, then added it as a connection on the gateway, and now I reference that connection and run some prepared statements in GatewayHook.java
like so:
// listen for updates to the settings record
SMSettingsRecord.META.addRecordListener(new IRecordListener<SMSettingsRecord>() {
@Override
public void recordUpdated(SMSettingsRecord sMSettingsRecord) {
SRConnection con = null;
int minVal;
try {
String createTableStatement = "CREATE TABLE dbo.Location (LocationID INT NOT NULL PRIMARY KEY, Name VARCHAR(255), CreatedOn DATETIME);";
String insertFirstRecord = "INSERT INTO dbo.Location (LocationID, Name, CreatedOn) VALUES (1, 'OriginalName', CURRENT_TIMESTAMP);";
String retrieveRecord = "SELECT TOP 1 LocationID FROM dbo.Location ORDER BY LocationID ASC;";
con = context.getDatasourceManager().getConnection(sMSettingsRecord.getConnectionName());
con.runPrepUpdate(createTableStatement);
con.runPrepUpdate(insertFirstRecord);
minVal = (Integer) con.runScalarPrepQuery(retrieveRecord);
log.info("Update ran from recordUpdated!", minVal);
} catch (SQLException e) {
log.error("Unable to update records in recordUpdated! ", e);
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
log.error("Unable to close connection con on recordUpdated!", e);
}
}
}
log.info("recordUpdated()");
}
@Override
public void recordAdded(SMSettingsRecord sMSettingsRecord) {
// add a table to the database ModuleDev on localhost
SRConnection con = null;
int minVal;
try{
String createTableStatement = "CREATE TABLE dbo.Location (LocationID INT NOT NULL PRIMARY KEY, Name VARCHAR(255), CreatedOn DATETIME);";
String insertFirstRecord = "INSERT INTO dbo.Location (LocationID, Name, CreatedOn) VALUES (1, 'OriginalName', CURRENT_TIMESTAMP);";
String retrieveRecord = "SELECT TOP 1 LocationID FROM dbo.Location ORDER BY LocationID ASC;";
con = context.getDatasourceManager().getConnection(sMSettingsRecord.getConnectionName());
con.runPrepUpdate(createTableStatement);
con.runPrepUpdate(insertFirstRecord);
minVal = (Integer) con.runScalarPrepQuery(retrieveRecord);
log.info("minVal: ", minVal);
} catch (SQLException e) {
log.error("Error creating table, inserting record or retrieving record! Code: ", e);
} finally {
if(con != null){
try {
con.close();
} catch (SQLException e) {
log.error("Unable to close the db connection con! Code: ", e);
}
}
}
log.info("recordAdded()");
}
@Override
public void recordDeleted(KeyValue keyValue) {
log.info("recordDeleted()");
}
});
log.debug("Setup complete.");
}
The issue I'm having right now is I'm not understanding (a lot of things but also) why recordAdded
isn't being executed when the first record is created. Again, I have a single static config page with 3 properties, one of which is used, namely, getConnectionName()
. When I go to that page, fill out the form and then Save Changes
the first time, I would expect the recordAdded
method to execute. What happens is only the recordUpdated
method executes after I save (and with the snippet above, it isn't hard to see that its going to fail because dbo.Location
already exists).
Is this because I specified default values for the form fields in my SettingsRecord.java
file? Or is it because, even though I uninstalled the module and reinstalled it many times, the table inside the internal DB is still there so the recordAdded
method won't execute?