private void checkForHistory(String operation, String procName, Object ... args) {
if (this instanceof DaoWithHistory) {
DaoWithHistory dwh = (DaoWithHistory) this;
if (!Strings.isNullOrEmpty(procName) && !procName.equals(PROC_NAME_HISTORIZE)) {
try {
MapSqlParameterSource params = prepParams(args);
Object original = null;
original = dwh.getRowForHistory(params);
if (insertHistorySql == null) {
String insertSql = dwh.getInsertSql();
Pattern p1 = Pattern.compile("(?i)insert[^\\(]*\\(");
Pattern p2 = Pattern.compile("(?i)values[^\\(]*\\(");
Matcher m = p1.matcher(insertSql);
if (!m.find()) {
logger.warn("Could not find pattern \"INSERT INTO ... (\" in insert SQL: {}. Ignoring history.", insertSql);
return;
}
int re = m.end();
insertSql = insertSql.substring(0, re-1).replace("[\n\r]", "").trim() +"Hist (" +"histId, histOperation, histStamp, histUserId," +insertSql.substring(re);
m = p2.matcher(insertSql);
if (!m.find()) {
logger.warn("Could not find pattern \"VALUES (\" in insert SQL: {}. Ignoring history.", insertSql);
return;
}
re = m.end();
insertSql = insertSql.substring(0, re) +":histId, :histOperation, CURRENT_TIMESTAMP, :histUserId," +insertSql.substring(re);
insertHistorySql = insertSql;
}
params.addValue("histId",0);
params.addValue("histOperation", operation);
params.addValue("histStamp","");
params.addValue("histUserId", (String)params.getValue("userId"));
insert("__insertHistory", insertHistorySql, params, original);
} catch (Throwable e) {
logger.warn("Failed to insert history!", e);
}
}