Skip to content

Commit

Permalink
#3 Create logon table if it ddoes not exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
baardl committed Sep 14, 2017
1 parent b784f2b commit a06b04b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
52 changes: 37 additions & 15 deletions src/main/java/org/valuereporter/activity/ActivitiesDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -37,27 +39,33 @@ protected long insertActivities(String tableName, final List<String> 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) {
Expand All @@ -84,4 +92,18 @@ protected String buildSql(String tableName, List<String> columnNames) {
sql += ")";
return sql;
}

public void createTable(String tableName, ArrayList<String> 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);
}
}
22 changes: 21 additions & 1 deletion src/main/java/org/valuereporter/activity/ActivitiesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,9 +40,28 @@ public long updateActivities(String prefix, List<ObservedActivity> 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<String> columnNames, List<ObservedActivity> 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;
}
}

0 comments on commit a06b04b

Please sign in to comment.