diff --git a/pom.xml b/pom.xml index ae9d0b7..2c2d218 100755 --- a/pom.xml +++ b/pom.xml @@ -339,8 +339,8 @@ org.apache.maven.plugins maven-compiler-plugin - 1.7 - 1.7 + 1.8 + 1.8 diff --git a/src/main/java/org/valuereporter/activity/ActivitiesDao.java b/src/main/java/org/valuereporter/activity/ActivitiesDao.java new file mode 100644 index 0000000..db542bd --- /dev/null +++ b/src/main/java/org/valuereporter/activity/ActivitiesDao.java @@ -0,0 +1,85 @@ +package org.valuereporter.activity; + +import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +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.List; +import java.util.stream.IntStream; + +import static org.slf4j.LoggerFactory.getLogger; + +/** + * Created by t-blind5-01 on 02.03.2016. + */ +@Component +public class ActivitiesDao { + private static final Logger log = getLogger(ActivitiesDao.class); + + private final JdbcTemplate jdbcTemplate; + + @Autowired + public ActivitiesDao(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + protected long insertActivities(String tableName, final List columnNames, final List activities) { + if (tableName == null || tableName.isEmpty()) { + throw new IllegalArgumentException("tableName must have a value"); + } + if (columnNames == null) { + throw new IllegalArgumentException("columntNames must not be null"); + } + String sql = buildSql(tableName, columnNames); + + int [] updatePrStatement = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { + + @Override + public void setValues(PreparedStatement ps, int i) throws SQLException { + ObservedActivity activity = activities.get(i); + int paramNum = 1; + 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(); + } + }); + + int sum = 0; + if (updatePrStatement != null) { + sum = IntStream.of(updatePrStatement).sum(); + } + return sum; + } + + protected String buildSql(String tableName, List columnNames) { + String sql = "INSERT INTO " + tableName + + "("; + for (String columnName : columnNames) { + sql += columnName + ", "; + } + sql = sql.substring(0,sql.length() - 2); + sql += ") VALUES ("; + for (int i = 0; i < columnNames.size(); i++) { + if (i < (columnNames.size() - 1)) { + sql += "?,"; + } else { + sql += "?"; + } + } + sql += ")"; + return sql; + } +} diff --git a/src/main/java/org/valuereporter/activity/ActivitiesResource.java b/src/main/java/org/valuereporter/activity/ActivitiesResource.java index e935029..489fcf4 100644 --- a/src/main/java/org/valuereporter/activity/ActivitiesResource.java +++ b/src/main/java/org/valuereporter/activity/ActivitiesResource.java @@ -35,6 +35,7 @@ public ActivitiesResource(ObjectMapper mapper) { this.mapper = mapper; } + //Available at http://localhost:4901/reporter/observe/activities/{prefix} @POST @Path("/{prefix}") @Produces(MediaType.APPLICATION_JSON) diff --git a/src/main/java/org/valuereporter/activity/ActivitiesService.java b/src/main/java/org/valuereporter/activity/ActivitiesService.java new file mode 100644 index 0000000..ef08aa8 --- /dev/null +++ b/src/main/java/org/valuereporter/activity/ActivitiesService.java @@ -0,0 +1,7 @@ +package org.valuereporter.activity; + +/** + * Created by t-blind5-01 on 02.03.2016. + */ +public class ActivitiesService { +} diff --git a/src/main/java/org/valuereporter/activity/ObservedActivity.java b/src/main/java/org/valuereporter/activity/ObservedActivity.java index 3b049c4..106d3d2 100755 --- a/src/main/java/org/valuereporter/activity/ObservedActivity.java +++ b/src/main/java/org/valuereporter/activity/ObservedActivity.java @@ -55,4 +55,12 @@ public String toString() { ", data=" + data + '}'; } + + public Object getValue(String key) { + Object value = null; + if (data != null && data.containsKey(key)) { + value = data.get(key); + } + return value; + } } diff --git a/src/test/java/org/valuereporter/activity/ActivitiesDaoTest.java b/src/test/java/org/valuereporter/activity/ActivitiesDaoTest.java new file mode 100644 index 0000000..c404332 --- /dev/null +++ b/src/test/java/org/valuereporter/activity/ActivitiesDaoTest.java @@ -0,0 +1,33 @@ +package org.valuereporter.activity; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.testng.Assert.assertEquals; + +/** + * Created by t-blind5-01 on 02.03.2016. + */ +public class ActivitiesDaoTest { + + @BeforeMethod + public void setUp() throws Exception { + + } + + @Test + public void testBuildSql() throws Exception { + ActivitiesDao activitiesDao = new ActivitiesDao(null); + List activities = new ArrayList<>(); + activities.add("userId"); + activities.add("applicationId"); + + String expectedSql = "INSERT INTO logonByApplication(userId, applicationId) VALUES (?,?)"; + String sql = activitiesDao.buildSql("logonByApplication", activities); + assertEquals(sql, expectedSql); + + } +} \ No newline at end of file