String primaryKeyName,
Map columnMap)
throws ControllerException, UpdaterException {
Connection conn = null;
QueryRunner queryRunner = new QueryRunner(dataSource);
ResultSetHandler rsh = new MapHandler();
String sql = null;
try {
conn = dataSource.getConnection();
conn.setAutoCommit(false); // Start transaction
sql = "select * from " + tableName;
sql += " where (" + primaryKeyName + "=?)";
Map columnMapTemp =
(Map) queryRunner.query(
sql,
columnMap.get(primaryKeyName),
rsh);
if (columnMapTemp == null) {
conn.rollback();
throw new ControllerException("GenericController: Cannot find record.");
}
Date oldDate = (java.util.Date) columnMap.get("date_updated");
Date newDate = (java.util.Date) columnMapTemp.get("date_updated");
if (!oldDate.equals(newDate)) {
conn.rollback();
throw new UpdaterException();
// Signal that record has already been changed
}
sql = "update " + tableName;
String parameter = null;
String columnName = null;
List columnNameList = null;
List paramList = new ArrayList();
// Iterate over columns
Set columnSet = columnMapTemp.keySet();
Iterator iterator = columnSet.iterator();
int i = 0;
while (iterator.hasNext()) {
columnName = (String) iterator.next();
if (columnName.equals("date_updated")) {
parameter = "CURRENT_TIMESTAMP";
} else {
parameter = "?";
paramList.add(columnMap.get(columnName));
}
if (i++ == 0) {
sql += " set " + columnName + " = " + parameter;
} else {
sql += " ," + columnName + " = " + parameter;
}
}
sql += " where (" + primaryKeyName + "=?)";
paramList.add(columnMap.get(primaryKeyName));
// Add primaryKey value to paramList
int result = queryRunner.update(sql, paramList.toArray());
if (result == 0) {
conn.rollback();
} else {
conn.commit();