String statementSql;
Connection conn = null;
PreparedStatement ps = null;
try {
conn = rhqDs.getConnection();
DatabaseType dbType = DatabaseTypeFactory.getDefaultDatabaseType();
if (dbType instanceof PostgresqlDatabaseType || dbType instanceof OracleDatabaseType
|| dbType instanceof H2DatabaseType) {
String nextvalSql = JDBCUtil.getNextValSql(conn, EventSource.TABLE_NAME);
statementSql = String.format(EVENT_SOURCE_INSERT_STMT, nextvalSql);
} else if (dbType instanceof SQLServerDatabaseType) {
statementSql = EVENT_SOURCE_INSERT_STMT_AUTOINC;
} else {
throw new IllegalArgumentException("Unknown database type, can't continue: " + dbType);
}
// First insert the "keys" (i.e. the EventSources).
ps = conn.prepareStatement(statementSql);
try {
for (EventSource eventSource : events.keySet()) {
int paramIndex = 1;
ps.setString(paramIndex++, eventSource.getEventDefinition().getName());
ps.setString(paramIndex++, eventSource.getEventDefinition().getResourceType().getName());
ps.setString(paramIndex++, eventSource.getEventDefinition().getResourceType().getPlugin());
ps.setInt(paramIndex++, eventSource.getResource().getId());
ps.setString(paramIndex++, eventSource.getLocation());
ps.setString(paramIndex++, eventSource.getEventDefinition().getName());
ps.setString(paramIndex++, eventSource.getEventDefinition().getResourceType().getName());
ps.setString(paramIndex++, eventSource.getEventDefinition().getResourceType().getPlugin());
ps.setInt(paramIndex++, eventSource.getResource().getId());
ps.setString(paramIndex++, eventSource.getLocation());
ps.addBatch();
}
ps.executeBatch();
} finally {
JDBCUtil.safeClose(ps);
}
if (dbType instanceof PostgresqlDatabaseType || dbType instanceof OracleDatabaseType
|| dbType instanceof H2DatabaseType) {
String nextvalSql = JDBCUtil.getNextValSql(conn, Event.TABLE_NAME);
statementSql = String.format(EVENT_INSERT_STMT, nextvalSql);
} else if (dbType instanceof SQLServerDatabaseType) {
statementSql = EVENT_INSERT_STMT_AUTOINC;
} else {
throw new IllegalArgumentException("Unknown database type, can't continue: " + dbType);
}
// Then insert the "values" (i.e. the Events).
ps = conn.prepareStatement(statementSql);
try {
for (EventSource eventSource : events.keySet()) {
Set<Event> eventData = events.get(eventSource);
for (Event event : eventData) {
int paramIndex = 1;
ps.setString(paramIndex++, eventSource.getEventDefinition().getName());
ps.setString(paramIndex++, eventSource.getEventDefinition().getResourceType().getName());
ps.setString(paramIndex++, eventSource.getEventDefinition().getResourceType().getPlugin());
ps.setInt(paramIndex++, eventSource.getResource().getId());
ps.setString(paramIndex++, eventSource.getLocation());
ps.setLong(paramIndex++, event.getTimestamp());
ps.setString(paramIndex++, event.getSeverity().toString());
String detail = dbType.getString(event.getDetail(), Event.DETAIL_MAX_LENGTH);
ps.setString(paramIndex++, detail);
ps.addBatch();
}
// We may have trimmed the event detail for storage reasons, but for alerting use the