Skip to content

Commit

Permalink
#3 Building dynamic sql for ObservedActivity.
Browse files Browse the repository at this point in the history
  • Loading branch information
baardl committed Mar 2, 2016
1 parent 8e38f32 commit b854695
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
Expand Down
85 changes: 85 additions & 0 deletions src/main/java/org/valuereporter/activity/ActivitiesDao.java
Original file line number Diff line number Diff line change
@@ -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<String> columnNames, final List<ObservedActivity> 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<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.valuereporter.activity;

/**
* Created by t-blind5-01 on 02.03.2016.
*/
public class ActivitiesService {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
33 changes: 33 additions & 0 deletions src/test/java/org/valuereporter/activity/ActivitiesDaoTest.java
Original file line number Diff line number Diff line change
@@ -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<String> 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);

}
}

0 comments on commit b854695

Please sign in to comment.