From a06b04b8b6b04d9ce1a29c084982ae90aa101e80 Mon Sep 17 00:00:00 2001 From: baardl Date: Wed, 2 Mar 2016 12:21:50 +0100 Subject: [PATCH] #3 Create logon table if it ddoes not exist. --- .../valuereporter/activity/ActivitiesDao.java | 52 +++++++++++++------ .../activity/ActivitiesService.java | 22 +++++++- 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/valuereporter/activity/ActivitiesDao.java b/src/main/java/org/valuereporter/activity/ActivitiesDao.java index 9972c23..e807e26 100644 --- a/src/main/java/org/valuereporter/activity/ActivitiesDao.java +++ b/src/main/java/org/valuereporter/activity/ActivitiesDao.java @@ -2,12 +2,14 @@ import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.BatchPreparedStatementSetter; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; import java.sql.PreparedStatement; import java.sql.SQLException; +import java.util.ArrayList; import java.util.List; import java.util.stream.IntStream; @@ -37,27 +39,33 @@ protected long insertActivities(String tableName, final List columnNames } String sql = buildSql(tableName, columnNames); - int [] updatePrStatement = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { + int[] updatePrStatement = null; + try { + updatePrStatement = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { - @Override - public void setValues(PreparedStatement ps, int i) throws SQLException { - ObservedActivity activity = activities.get(i); - ps.setTimestamp(1,new java.sql.Timestamp(activity.getStartTime())); - int paramNum = 2; - for (String columnName : columnNames) { - ps.setObject(paramNum, activity.getValue(columnName)); - } + @Override + public void setValues(PreparedStatement ps, int i) throws SQLException { + ObservedActivity activity = activities.get(i); + ps.setTimestamp(1, new java.sql.Timestamp(activity.getStartTime())); + int paramNum = 2; + for (String columnName : columnNames) { + ps.setObject(paramNum, activity.getValue(columnName)); + } // ps.setObject(1, activity.getValue(columnNames.get(0))); // ps.setLong(1, customer.getCustId()); // ps.setString(2, customer.getName()); // ps.setInt(3, customer.getAge() ); - } + } - @Override - public int getBatchSize() { - return activities.size(); - } - }); + @Override + public int getBatchSize() { + return activities.size(); + } + }); + } catch (DataAccessException de) { + log.warn("Failed to update activities {}, reason {}", activities, de.getMessage(), de); + throw de; + } int sum = 0; if (updatePrStatement != null) { @@ -84,4 +92,18 @@ protected String buildSql(String tableName, List columnNames) { sql += ")"; return sql; } + + public void createTable(String tableName, ArrayList columnNames, ObservedActivity observedActivity) { + //TODO + String tableSql = ""; + if (tableName.equalsIgnoreCase("logon")){ + tableSql = "CREATE TABLE logon(\n" + + " id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,\n" + +// " prefix varchar(255) NOT NULL,\n" + + " starttime TIMESTAMP NOT NULL,\n" + + " userid varchar(255) NOT NULL,\n" + + " );"; + } + jdbcTemplate.execute(tableSql); + } } diff --git a/src/main/java/org/valuereporter/activity/ActivitiesService.java b/src/main/java/org/valuereporter/activity/ActivitiesService.java index 9c51085..7cb41d9 100644 --- a/src/main/java/org/valuereporter/activity/ActivitiesService.java +++ b/src/main/java/org/valuereporter/activity/ActivitiesService.java @@ -2,6 +2,7 @@ import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -39,9 +40,28 @@ public long updateActivities(String prefix, List observedActiv for (String key : keys) { columnNames.add(key); } - updatedActivities = activitiesDao.insertActivities(tableName, columnNames, observedActivities); + try { + updatedActivities = activitiesDao.insertActivities(tableName, columnNames, observedActivities); + } catch (DataAccessException de) { + if (isMissingTablexeption(de)) { + createTable(tableName, columnNames, observedActivities); + updatedActivities = activitiesDao.insertActivities(tableName, columnNames, observedActivities); + } + } } } return updatedActivities; } + + private void createTable(String tableName, ArrayList columnNames, List observedActivities) { + activitiesDao.createTable(tableName, columnNames, observedActivities.get(0)); + } + + private boolean isMissingTablexeption(DataAccessException de) { + boolean missingTable = false; + if (de.getCause()!= null) { + missingTable = de.getCause().getMessage().contains("object not found"); + } + return missingTable; + } }