Skip to content
This repository was archived by the owner on Aug 13, 2020. It is now read-only.

Commit 809e2d6

Browse files
authored
Merge pull request #201 from CJSCommonPlatform/add_eventlog_timestamp
Add date_created column to the Event_Log
2 parents 5d233a9 + e427789 commit 809e2d6

File tree

17 files changed

+220
-49
lines changed

17 files changed

+220
-49
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
55

66
## [Un-released]
77

8+
### Added
9+
- Timestamp field to the EventLog (Event Store).
10+
- ZonedDateTimes method to convert to legacy Date format
11+
812
## [0.28.0] - 2016-09-21
913

1014
### Fixed

common/src/main/java/uk/gov/justice/services/common/converter/ZonedDateTimes.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import static java.time.ZonedDateTime.parse;
55
import static java.time.format.DateTimeFormatter.ofPattern;
66

7+
import java.sql.Timestamp;
78
import java.time.ZonedDateTime;
8-
import java.time.format.DateTimeFormatter;
99

1010
import javax.json.JsonString;
1111

@@ -51,4 +51,26 @@ public static String toString(final ZonedDateTime source) {
5151
public static ZonedDateTime fromString(final String iso8601DateTimeString) {
5252
return parse(iso8601DateTimeString).withZoneSameInstant(UTC);
5353
}
54+
55+
/**
56+
* Converts the framework date time standard {@link ZonedDateTime} to the SQL {@link Timestamp}
57+
* format.
58+
*
59+
* @param source the date time
60+
* @return the date time as a Timestamp
61+
*/
62+
public static Timestamp toSqlTimestamp(final ZonedDateTime source) {
63+
return Timestamp.from(source.toInstant());
64+
}
65+
66+
/**
67+
* Converts from an {@link Timestamp} to the framework date-time standard {@link
68+
* ZonedDateTime}.
69+
*
70+
* @param timestamp the date time
71+
* @return the date time converted to UTC Zoned Date Time.
72+
*/
73+
public static ZonedDateTime fromSqlTimestamp(final Timestamp timestamp) {
74+
return ZonedDateTime.ofInstant(timestamp.toInstant(), UTC);
75+
}
5476
}

common/src/main/java/uk/gov/justice/services/common/util/DateTimeProvider.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import static java.time.ZoneOffset.UTC;
44
import static java.time.ZonedDateTime.of;
5-
import static java.time.format.DateTimeFormatter.ofPattern;
6-
import static uk.gov.justice.services.common.converter.ZonedDateTimes.ISO_8601;
75

86
import uk.gov.justice.services.common.converter.ZonedDateTimes;
97

8+
import java.sql.Timestamp;
109
import java.time.LocalDateTime;
1110
import java.time.ZonedDateTime;
1211

@@ -53,4 +52,24 @@ public String toString(final ZonedDateTime source) {
5352
public ZonedDateTime fromString(final String iso8601DateTimeString) {
5453
return ZonedDateTimes.fromString(iso8601DateTimeString);
5554
}
55+
56+
/**
57+
* Convert a {@link Timestamp} to a UTC {@link ZonedDateTime}.
58+
*
59+
* @param timestamp the timestamp to be covereted
60+
* @return the date time as a UTC ZoneDateTIme
61+
*/
62+
public ZonedDateTime fromSqlTimestamp(final Timestamp timestamp) {
63+
return ZonedDateTimes.fromSqlTimestamp(timestamp);
64+
}
65+
66+
/**
67+
* Convert a UTC {@link ZonedDateTime} to a {@link Timestamp}.
68+
*
69+
* @param dateTime the dateTime to be covereted
70+
* @return the date time as a UTC ZoneDateTIme
71+
*/
72+
public Timestamp toSqlTimestamp(final ZonedDateTime dateTime) {
73+
return ZonedDateTimes.toSqlTimestamp(dateTime);
74+
}
5675
}

common/src/test/java/uk/gov/justice/services/common/converter/ZonedDateTimesTest.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package uk.gov.justice.services.common.converter;
22

3+
import static java.time.ZoneOffset.UTC;
4+
import static java.time.ZonedDateTime.of;
5+
import static java.time.ZonedDateTime.parse;
36
import static javax.json.Json.createObjectBuilder;
47
import static net.trajano.commons.testing.UtilityClassTestUtil.assertUtilityClassWellDefined;
58
import static org.hamcrest.CoreMatchers.equalTo;
69
import static org.hamcrest.MatcherAssert.assertThat;
710

11+
import java.sql.Timestamp;
12+
import java.time.LocalDateTime;
813
import java.time.ZoneId;
914
import java.time.ZonedDateTime;
1015

@@ -37,15 +42,37 @@ public void shouldConvertJsonStringToUtc() {
3742
@Test
3843
public void shouldKeepCorrectTimeWhenConvertingStringToUtc() {
3944
final ZonedDateTime dateTime = ZonedDateTimes.fromJsonString(createJsonString("2016-01-21T23:42:03.522+07:00"));
40-
assertThat(dateTime.toInstant(), equalTo(ZonedDateTime.parse("2016-01-21T16:42:03.522Z").toInstant()));
45+
assertThat(dateTime.toInstant(), equalTo(parse("2016-01-21T16:42:03.522Z").toInstant()));
4146
}
4247

4348
@Test
4449
public void shouldConvertNonUtcToUtcString() {
45-
final String dateTime = ZonedDateTimes.toString(ZonedDateTime.parse("2016-01-21T23:42:03.522+07:00"));
50+
final String dateTime = ZonedDateTimes.toString(parse("2016-01-21T23:42:03.522+07:00"));
4651
assertThat(dateTime, equalTo("2016-01-21T16:42:03.522Z"));
4752
}
4853

54+
@Test
55+
public void shouldConvertNonUtcToUtcString2() {
56+
final String dateTime = ZonedDateTimes.toString(parse("2016-07-25T23:09:01.0+05:00"));
57+
assertThat(dateTime, equalTo("2016-07-25T18:09:01.000Z"));
58+
}
59+
60+
@Test
61+
public void shouldConvertZoneDateTimeToSqlTimestamp() {
62+
final ZonedDateTime zonedDateTime = of(LocalDateTime.now(), UTC);
63+
final Timestamp dateTime = ZonedDateTimes.toSqlTimestamp(zonedDateTime);
64+
65+
assertThat(dateTime.toInstant().getEpochSecond(), equalTo(zonedDateTime.toInstant().getEpochSecond()));
66+
}
67+
68+
@Test
69+
public void shouldConvertSqlTimestampToZoneDateTime() {
70+
final Timestamp timestamp = Timestamp.valueOf("2016-07-25 23:09:00.123");
71+
final ZonedDateTime dateTime = ZonedDateTimes.fromSqlTimestamp(timestamp);
72+
73+
assertThat(dateTime.toInstant().getEpochSecond(), equalTo(timestamp.toInstant().getEpochSecond()));
74+
}
75+
4976
private JsonString createJsonString(final String source) {
5077
return createObjectBuilder().add("tmp", source).build().getJsonString("tmp");
5178
}

common/src/test/java/uk/gov/justice/services/common/util/DateTimeProviderTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import static java.time.ZoneOffset.UTC;
44
import static java.time.ZonedDateTime.now;
55
import static javax.json.Json.createObjectBuilder;
6+
import static org.hamcrest.CoreMatchers.equalTo;
67
import static org.hamcrest.CoreMatchers.is;
78
import static org.hamcrest.CoreMatchers.notNullValue;
89
import static org.junit.Assert.assertThat;
910

10-
import uk.gov.justice.services.common.converter.ZonedDateTimes;
11-
11+
import java.sql.Timestamp;
1212
import java.time.ZoneId;
1313
import java.time.ZonedDateTime;
1414

@@ -60,6 +60,18 @@ public void shouldConvertNonUtcToUtcString() {
6060
assertThat(dateTime, is("2016-01-21T16:42:03.522Z"));
6161
}
6262

63+
@Test
64+
public void shouldConvertTimestampToZoneDateTime() {
65+
final Timestamp dateTime = dateTimeProvider.toSqlTimestamp(ZonedDateTime.of(2016, 12, 31, 23, 59, 59, 0, UTC));
66+
assertThat(dateTime.toString(), equalTo("2016-12-31 23:59:59.0"));
67+
}
68+
69+
@Test
70+
public void shouldConertZoneDateTimeToTimestamp() {
71+
final ZonedDateTime dateTime = dateTimeProvider.fromSqlTimestamp(Timestamp.valueOf("2016-12-31 23:59:59.123"));
72+
assertThat(dateTime.toString(), equalTo("2016-12-31T23:59:59.123Z"));
73+
}
74+
6375
private JsonString createJsonString(final String source) {
6476
return createObjectBuilder().add("tmp", source).build().getJsonString("tmp");
6577
}

event-sourcing/event-repository/event-repository-jdbc/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@
6969
<artifactId>hamcrest-library</artifactId>
7070
<scope>test</scope>
7171
</dependency>
72+
<dependency>
73+
<groupId>org.exparity</groupId>
74+
<artifactId>hamcrest-date</artifactId>
75+
<scope>test</scope>
76+
</dependency>
7277
<dependency>
7378
<groupId>uk.gov.justice.services</groupId>
7479
<artifactId>event-repository-liquibase</artifactId>

event-sourcing/event-repository/event-repository-jdbc/src/main/java/uk/gov/justice/services/eventsourcing/repository/jdbc/eventlog/EventLog.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package uk.gov.justice.services.eventsourcing.repository.jdbc.eventlog;
22

3+
import uk.gov.justice.services.common.converter.ZonedDateTimes;
4+
5+
import java.time.ZonedDateTime;
36
import java.util.Objects;
47
import java.util.UUID;
58

@@ -14,14 +17,16 @@ public class EventLog {
1417
private final String name;
1518
private final String payload;
1619
private final String metadata;
20+
private final ZonedDateTime dateCreated;
1721

18-
public EventLog(final UUID id, final UUID streamId, final Long sequenceId, final String name, final String metadata, final String payload) {
22+
public EventLog(final UUID id, final UUID streamId, final Long sequenceId, final String name, final String metadata, final String payload, final ZonedDateTime timestamp) {
1923
this.id = id;
2024
this.streamId = streamId;
2125
this.sequenceId = sequenceId;
2226
this.name = name;
2327
this.metadata = metadata;
2428
this.payload = payload;
29+
this.dateCreated = timestamp;
2530
}
2631

2732
public UUID getId() {
@@ -48,6 +53,10 @@ public String getMetadata() {
4853
return metadata;
4954
}
5055

56+
public ZonedDateTime getDateCreated() {
57+
return dateCreated;
58+
}
59+
5160
@Override
5261
@SuppressWarnings({"squid:MethodCyclomaticComplexity", "squid:S1067", "squid:S00122"})
5362
public boolean equals(Object o) {
@@ -63,18 +72,19 @@ public boolean equals(Object o) {
6372
Objects.equals(sequenceId, eventLog.sequenceId) &&
6473
Objects.equals(payload, eventLog.payload) &&
6574
Objects.equals(metadata, eventLog.metadata) &&
66-
Objects.equals(name, eventLog.name);
75+
Objects.equals(name, eventLog.name) &&
76+
Objects.equals(dateCreated, eventLog.dateCreated);
6777
}
6878

6979
@Override
7080
public int hashCode() {
71-
return Objects.hash(id, streamId, sequenceId, payload, name, metadata);
81+
return Objects.hash(id, streamId, sequenceId, payload, name, metadata, dateCreated);
7282
}
7383

7484
@Override
7585
public String toString() {
76-
return String.format("EventLog [id=%s, streamId=%s, sequenceId=%s, name=%s, payload=%s, metadata=%s]", id,
77-
streamId, sequenceId, name, payload, metadata);
86+
return String.format("EventLog [id=%s, streamId=%s, sequenceId=%s, name=%s, payload=%s, metadata=%s, dateCreated=$s]", id,
87+
streamId, sequenceId, name, payload, metadata, ZonedDateTimes.toString(dateCreated));
7888
}
7989

8090
}

event-sourcing/event-repository/event-repository-jdbc/src/main/java/uk/gov/justice/services/eventsourcing/repository/jdbc/eventlog/EventLogConverter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package uk.gov.justice.services.eventsourcing.repository.jdbc.eventlog;
22

33
import uk.gov.justice.services.common.converter.StringToJsonObjectConverter;
4+
import uk.gov.justice.services.common.util.DateTimeProvider;
45
import uk.gov.justice.services.eventsourcing.common.exception.InvalidStreamIdException;
56
import uk.gov.justice.services.messaging.DefaultJsonEnvelope;
67
import uk.gov.justice.services.messaging.JsonEnvelope;
@@ -26,6 +27,9 @@ public class EventLogConverter {
2627
@Inject
2728
StringToJsonObjectConverter stringToJsonObjectConverter;
2829

30+
@Inject
31+
DateTimeProvider dateTimeProvider;
32+
2933
/**
3034
* Creates an {@link EventLog} object from the <code>eventEnvelope</code>.
3135
*
@@ -47,8 +51,8 @@ public EventLog createEventLog(final JsonEnvelope envelope, final UUID streamId,
4751
version,
4852
eventMetadata.name(),
4953
envelope.metadata().asJsonObject().toString(),
50-
extractPayloadAsString(envelope));
51-
54+
extractPayloadAsString(envelope),
55+
dateTimeProvider.now());
5256
}
5357

5458
/**

event-sourcing/event-repository/event-repository-jdbc/src/main/java/uk/gov/justice/services/eventsourcing/repository/jdbc/eventlog/EventLogJdbcRepository.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
import static java.lang.String.format;
5+
import static uk.gov.justice.services.common.converter.ZonedDateTimes.fromSqlTimestamp;
6+
import static uk.gov.justice.services.common.converter.ZonedDateTimes.toSqlTimestamp;
57

68
import uk.gov.justice.services.eventsourcing.common.exception.InvalidSequenceIdException;
79
import uk.gov.justice.services.jdbc.persistence.AbstractJdbcRepository;
@@ -32,6 +34,7 @@ public class EventLogJdbcRepository extends AbstractJdbcRepository {
3234
static final String COL_NAME = "name";
3335
static final String COL_METADATA = "metadata";
3436
static final String COL_PAYLOAD = "payload";
37+
static final String COL_TIMESTAMP = "date_created";
3538

3639
static final long INITIAL_VERSION = 0L;
3740

@@ -41,8 +44,8 @@ public class EventLogJdbcRepository extends AbstractJdbcRepository {
4144
static final String SQL_FIND_BY_STREAM_ID = "SELECT * FROM event_log WHERE stream_id=? ORDER BY sequence_id ASC";
4245
static final String SQL_FIND_BY_STREAM_ID_AND_SEQUENCE_ID = "SELECT * FROM event_log WHERE stream_id=? AND sequence_id>=? ORDER BY sequence_id ASC";
4346
static final String SQL_FIND_LATEST_SEQUENCE_ID = "SELECT MAX(sequence_id) FROM event_log WHERE stream_id=?";
44-
static final String SQL_INSERT_EVENT_LOG = "INSERT INTO event_log (id, stream_id, sequence_id, name, metadata, payload ) " +
45-
"VALUES(?, ?, ?, ?, ?, ?)";
47+
static final String SQL_INSERT_EVENT_LOG = "INSERT INTO event_log (id, stream_id, sequence_id, name, metadata, payload, date_created ) " +
48+
"VALUES(?, ?, ?, ?, ?, ?, ?)";
4649

4750
private static final String READING_STREAM_EXCEPTION = "Exception while reading stream %s";
4851
private static final String JNDI_DS_EVENT_STORE_PATTERN = "java:/app/%s/DS.eventstore";
@@ -69,6 +72,7 @@ public void insert(final EventLog eventLog) throws InvalidSequenceIdException {
6972
ps.setString(4, eventLog.getName());
7073
ps.setString(5, eventLog.getMetadata());
7174
ps.setString(6, eventLog.getPayload());
75+
ps.setTimestamp(7, toSqlTimestamp(eventLog.getDateCreated()));
7276

7377
ps.executeUpdate();
7478
} catch (SQLException | NamingException e) {
@@ -149,7 +153,6 @@ public Long getLatestSequenceIdForStream(final UUID streamId) {
149153
return INITIAL_VERSION;
150154
}
151155

152-
153156
protected List<EventLog> extractResults(final PreparedStatement preparedStatement) throws SQLException {
154157
List<EventLog> events = new ArrayList<>();
155158

@@ -167,7 +170,8 @@ private EventLog createEventLog(final ResultSet resultSet) throws SQLException {
167170
resultSet.getLong(COL_SEQUENCE_ID),
168171
resultSet.getString(COL_NAME),
169172
resultSet.getString(COL_METADATA),
170-
resultSet.getString(COL_PAYLOAD));
173+
resultSet.getString(COL_PAYLOAD),
174+
fromSqlTimestamp(resultSet.getTimestamp(COL_TIMESTAMP)));
171175
}
172176

173177
@Override

event-sourcing/event-repository/event-repository-jdbc/src/test/java/uk/gov/justice/services/eventsourcing/repository/jdbc/eventlog/EventLogConverterTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package uk.gov.justice.services.eventsourcing.repository.jdbc.eventlog;
22

3+
import static java.time.temporal.ChronoUnit.SECONDS;
4+
import static org.exparity.hamcrest.date.ZonedDateTimeMatchers.within;
35
import static org.hamcrest.CoreMatchers.equalTo;
6+
import static org.hamcrest.CoreMatchers.is;
47
import static org.junit.Assert.assertThat;
58
import static uk.gov.justice.services.messaging.JsonObjectMetadata.metadataFrom;
69

710
import uk.gov.justice.services.common.converter.StringToJsonObjectConverter;
11+
import uk.gov.justice.services.common.util.DateTimeProvider;
812
import uk.gov.justice.services.eventsourcing.common.exception.InvalidStreamIdException;
913
import uk.gov.justice.services.messaging.DefaultJsonEnvelope;
1014
import uk.gov.justice.services.messaging.JsonEnvelope;
@@ -45,6 +49,7 @@ public void setup() {
4549
eventLogConverter = new EventLogConverter();
4650
eventLogConverter.stringToJsonObjectConverter = new StringToJsonObjectConverter();
4751
eventLogConverter.jsonObjectEnvelopeConverter = new JsonObjectEnvelopeConverter();
52+
eventLogConverter.dateTimeProvider = new DateTimeProvider();
4853
}
4954

5055
@Test
@@ -59,6 +64,7 @@ public void shouldCreateEventLog() throws Exception {
5964
assertThat(eventLog.getSequenceId(), equalTo(SEQUENCE_ID));
6065
JSONAssert.assertEquals(METADATA_JSON, eventLog.getMetadata(), false);
6166
JSONAssert.assertEquals(expectedPayloadAsJsonString, eventLog.getPayload(), false);
67+
assertThat(eventLog.getDateCreated(), is(within(5L, SECONDS, new DateTimeProvider().now())));
6268
}
6369

6470
@Test(expected = InvalidStreamIdException.class)
@@ -77,7 +83,7 @@ public void shouldCreateEnvelope() throws Exception {
7783
}
7884

7985
private EventLog createEventLog() {
80-
return new EventLog(ID, STREAM_ID, SEQUENCE_ID, NAME, METADATA_JSON, PAYLOAD_JSON);
86+
return new EventLog(ID, STREAM_ID, SEQUENCE_ID, NAME, METADATA_JSON, PAYLOAD_JSON, new DateTimeProvider().now());
8187
}
8288

8389
private JsonEnvelope createTestEnvelope() throws IOException {

0 commit comments

Comments
 (0)