Skip to content

Commit

Permalink
Merge pull request #13 from slyfer/feature/9
Browse files Browse the repository at this point in the history
Feature/9
  • Loading branch information
sciabarracom authored Apr 20, 2020
2 parents 8ffca7a + 4735024 commit de6c5b9
Show file tree
Hide file tree
Showing 16 changed files with 513 additions and 85 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ hs_err_pid*
**/org.eclipse*
**/.classpath
ws-sdk/ws/conf/dpppt-backend-sdk-ws-logback.xml

ws-sdk/ws/conf/dpppt-backend-sdk-ws.properties

*.iml
.idea
**/target/
**/target/
10 changes: 10 additions & 0 deletions dpppt-backend-sdk/dpppt-backend-sdk-data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<artifactId>dpppt-backend-sdk-data</artifactId>
<name>DP3T Backend SDK Data</name>

<properties>
<org.testcontainers.version>1.12.5</org.testcontainers.version>
</properties>

<dependencies>

<!-- dp3t models -->
Expand Down Expand Up @@ -41,6 +45,12 @@
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${org.testcontainers.version}</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ public interface DPPPTDataService {
/**
* Upserts the given exposee
*
* @param exposee
* @param appSource
* @param exposee the exposee to upsert
* @param appSource the app name
*/
void upsertExposee(Exposee exposee, String appSource);

/**
* returns all exposees for the given day [day: 00:00, day+1: 00:00)
* Returns all exposees for the given day [day: 00:00, day+1: 00:00] ordered by id
*
* @param day
* @return
* @param day the day for which exposees are requested
* @return exposee list
*/
List<Exposee> getSortedExposedForDay(DateTime day);

/**
* Returns the maximum id of the stored exposed entries for the given day date
*
* @param day
* @param day the day for which id is required
*
* @return
* @return the max id or 0
*/
Integer getMaxExposedIdForDay(DateTime day);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import org.springframework.util.DigestUtils;

public class EtagGenerator implements EtagGeneratorInterface {
private byte secret[] = new byte[]{'s', 'e', 'c', 'r', 'e', 't'};

// TODO make configurable
private byte[] secret = new byte[]{'s', 'e', 'c', 'r', 'e', 't'};

@Override
public String getEtag(int primaryKey) {
String hash = DigestUtils.md5DigestAsHex(ByteBuffer.allocate(10).putInt(primaryKey).put(secret).array());
return hash;
return DigestUtils.md5DigestAsHex(ByteBuffer.allocate(10).putInt(primaryKey).put(secret).array());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@
package org.dpppt.backend.sdk.data;

public interface EtagGeneratorInterface {
public String getEtag(int primaryKey);
}

/**
* Generates etag from primary key
*
* @param primaryKey the primary key
* @return the etag
*/
String getEtag(int primaryKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

package org.dpppt.backend.sdk.data;

import java.util.List;

import javax.sql.DataSource;

import org.dpppt.backend.sdk.model.Exposee;
import org.joda.time.DateTime;
import org.slf4j.Logger;
Expand All @@ -18,61 +14,68 @@
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;

import java.util.List;

public class JDBCDPPPTDataServiceImpl implements DPPPTDataService {

private static final Logger logger = LoggerFactory.getLogger(JDBCDPPPTDataServiceImpl.class);
private static final Logger logger = LoggerFactory.getLogger(JDBCDPPPTDataServiceImpl.class);
private static final String PGSQL = "pgsql";

private final String dbType;
private static final String PGSQL = "pgsql";
private final NamedParameterJdbcTemplate jt;
// FIXME MAKE ENUMERATION
private final String dbType;
private final NamedParameterJdbcTemplate jt;

public JDBCDPPPTDataServiceImpl(String dbType, DataSource dataSource) {
this.dbType = dbType;
this.jt = new NamedParameterJdbcTemplate(dataSource);
}
public JDBCDPPPTDataServiceImpl(String dbType, DataSource dataSource) {
this.dbType = dbType;
this.jt = new NamedParameterJdbcTemplate(dataSource);
}

@Override
@Transactional(readOnly = false)
public void upsertExposee(Exposee exposee, String appSource) {
String sql = null;
if (dbType.equals(PGSQL)) {
sql = "insert into t_exposed (key, onset, app_source) values (:key, to_date(:onset, 'yyyy-MM-dd'), :app_source)"
+ " on conflict on constraint key do nothing";
} else {
sql = "merge into t_exposed using (values(cast(:key as varchar(10000)), cast(:onset as date), cast(:app_source as varchar(50))))"
+ " as vals(key, onset, app_source) on t_exposed.key = vals.key"
+ " when not matched then insert (key, onset, app_source) values (vals.key, vals.onset, vals.app_source)";
}
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("key", exposee.getKey());
params.addValue("app_source", appSource);
params.addValue("onset", exposee.getOnset());
jt.update(sql, params);
}
@Override
@Transactional
public void upsertExposee(Exposee exposee, String appSource) {
String sql;
if (dbType.equals(PGSQL)) {
sql = "insert into t_exposed (key, onset, app_source) values (:key, to_date(:onset, 'yyyy-MM-dd'), :app_source)"
+ " on conflict on constraint key do nothing";
} else {
sql = "merge into t_exposed using (values(cast(:key as varchar(10000)), cast(:onset as date), cast(:app_source as varchar(50))))"
+ " as vals(key, onset, app_source) on t_exposed.key = vals.key"
+ " when not matched then insert (key, onset, app_source) values (vals.key, vals.onset, vals.app_source)";
}
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("key", exposee.getKey());
params.addValue("app_source", appSource);
params.addValue("onset", exposee.getOnset());
jt.update(sql, params);
}

@Override
@Transactional(readOnly = true)
public List<Exposee> getSortedExposedForDay(DateTime day) {
DateTime dayMidnight = day.minusMillis(day.getMillisOfDay());
String sql = "select pk_exposed_id, key, to_char(onset, 'yyyy-MM-dd') as onset_string from t_exposed where received_at >= :dayMidnight and received_at < :nextDayMidnight order by pk_exposed_id desc";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("dayMidnight", dayMidnight.toDate());
params.addValue("nextDayMidnight", dayMidnight.plusDays(1).toDate());
return jt.query(sql, params, new ExposeeRowMapper());
}
@Override
@Transactional(readOnly = true)
public List<Exposee> getSortedExposedForDay(DateTime day) {
DateTime dayMidnight = day.minusMillis(day.getMillisOfDay());
String sql = "select pk_exposed_id, key, to_char(onset, 'yyyy-MM-dd') as onset_string from t_exposed where received_at >= :dayMidnight and received_at < :nextDayMidnight order by pk_exposed_id desc";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("dayMidnight", dayMidnight.toDate());
params.addValue("nextDayMidnight", dayMidnight.plusDays(1).toDate());
return jt.query(sql, params, new ExposeeRowMapper());
}

@Override
@Transactional(readOnly = true)
public Integer getMaxExposedIdForDay(DateTime day) {
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("dayMidnight", day.toDate());
params.addValue("nextDayMidnight", day.plusDays(1).toDate());
String sql = "select max(pk_exposed_id) from t_exposed where received_at >= :dayMidnight and received_at < :nextDayMidnight";
Integer maxId = jt.queryForObject(sql, params, Integer.class);
if (maxId == null) {
return 0;
} else {
return maxId;
}
}
@Override
@Transactional(readOnly = true)
// FIXME replace JodaTime with java8
public Integer getMaxExposedIdForDay(DateTime day) {
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("dayMidnight", day.toDate());
params.addValue("nextDayMidnight", day.plusDays(1).toDate());
String sql = "select max(pk_exposed_id) from t_exposed where received_at >= :dayMidnight and received_at < :nextDayMidnight";
// FIXME USE LONG?
Integer maxId = jt.queryForObject(sql, params, Integer.class);
if (maxId == null) {
return 0;
} else {
return maxId;
}
}
}
Loading

0 comments on commit de6c5b9

Please sign in to comment.