-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
974 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...t-publisher-process/src/main/java/uk/gov/justice/services/eventsourcing/EventFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package uk.gov.justice.services.eventsourcing; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.Optional.empty; | ||
import static java.util.Optional.of; | ||
|
||
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event; | ||
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.LinkedEvent; | ||
import uk.gov.justice.subscription.registry.SubscriptionDataSourceProvider; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import javax.inject.Inject; | ||
import javax.sql.DataSource; | ||
|
||
public class EventFetcher { | ||
|
||
@Inject | ||
SubscriptionDataSourceProvider subscriptionDataSourceProvider; | ||
|
||
@Inject | ||
EventFetcherRepository eventFetcherRepository; | ||
|
||
/** | ||
* Method that gets an event from the event_log table by id. | ||
* | ||
* @return Optional<Event> | ||
*/ | ||
public Optional<Event> getEvent(final UUID id) { | ||
|
||
final DataSource eventStoreDataSource = subscriptionDataSourceProvider.getEventStoreDataSource(); | ||
try (final Connection connection = eventStoreDataSource.getConnection()) { | ||
return eventFetcherRepository.getEvent(id, connection); | ||
} catch (final SQLException e) { | ||
throw new EventFetchingException(format("Failed to get Event with id '%s'", id), e); | ||
} | ||
} | ||
|
||
/** | ||
* Method that gets an LinkedEvent from the linked_event table by id. | ||
* | ||
* @return Optional<Event> | ||
*/ | ||
public Optional<LinkedEvent> getLinkedEvent(final UUID id) { | ||
|
||
final DataSource eventStoreDataSource = subscriptionDataSourceProvider.getEventStoreDataSource(); | ||
try (final Connection connection = eventStoreDataSource.getConnection()) { | ||
return eventFetcherRepository.getLinkedEvent(id, connection); | ||
} catch (final SQLException e) { | ||
throw new EventFetchingException(format("Failed to get LinkedEvent with id '%s'", id), e); | ||
} | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...r-process/src/main/java/uk/gov/justice/services/eventsourcing/EventFetcherRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package uk.gov.justice.services.eventsourcing; | ||
|
||
import static java.util.Optional.empty; | ||
import static java.util.Optional.of; | ||
import static java.util.UUID.fromString; | ||
import static uk.gov.justice.services.common.converter.ZonedDateTimes.fromSqlTimestamp; | ||
|
||
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event; | ||
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.LinkedEvent; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.time.ZonedDateTime; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
public class EventFetcherRepository { | ||
|
||
private static final String SELECT_FROM_EVENT_LOG_QUERY = | ||
"SELECT stream_id, position_in_stream, name, payload, metadata, date_created " + | ||
"FROM event_log " + | ||
"WHERE id = ?"; | ||
|
||
private static final String SELECT_FROM_LINKED_EVENT_QUERY = | ||
"SELECT stream_id, position_in_stream, name, payload, metadata, date_created, event_number, previous_event_number " + | ||
"FROM linked_event " + | ||
"WHERE id = ?"; | ||
|
||
/** | ||
* Method that gets an event from the event_log table by id. | ||
* | ||
* @return Optional<Event> | ||
*/ | ||
public Optional<Event> getEvent(final UUID id, final Connection connection) throws SQLException { | ||
|
||
try (final PreparedStatement preparedStatement = connection.prepareStatement(SELECT_FROM_EVENT_LOG_QUERY)) { | ||
|
||
preparedStatement.setObject(1, id); | ||
|
||
try (final ResultSet resultSet = preparedStatement.executeQuery()) { | ||
|
||
if (resultSet.next()) { | ||
final UUID streamId = fromString(resultSet.getString("stream_id")); | ||
final Long positionInStream = resultSet.getLong("position_in_stream"); | ||
final String name = resultSet.getString("name"); | ||
final String metadata = resultSet.getString("metadata"); | ||
final String payload = resultSet.getString("payload"); | ||
final ZonedDateTime createdAt = fromSqlTimestamp(resultSet.getTimestamp("date_created")); | ||
|
||
return of(new Event( | ||
id, | ||
streamId, | ||
positionInStream, | ||
name, | ||
metadata, | ||
payload, | ||
createdAt) | ||
); | ||
} | ||
} | ||
} | ||
|
||
return empty(); | ||
} | ||
|
||
public Optional<LinkedEvent> getLinkedEvent(final UUID id, final Connection connection) throws SQLException { | ||
|
||
try (final PreparedStatement preparedStatement = connection.prepareStatement(SELECT_FROM_LINKED_EVENT_QUERY)) { | ||
|
||
preparedStatement.setObject(1, id); | ||
|
||
try (final ResultSet resultSet = preparedStatement.executeQuery()) { | ||
|
||
if (resultSet.next()) { | ||
final UUID streamId = fromString(resultSet.getString("stream_id")); | ||
final Long positionInStream = resultSet.getLong("position_in_stream"); | ||
final String name = resultSet.getString("name"); | ||
final String metadata = resultSet.getString("metadata"); | ||
final String payload = resultSet.getString("payload"); | ||
final ZonedDateTime createdAt = fromSqlTimestamp(resultSet.getTimestamp("date_created")); | ||
final long eventNumber = resultSet.getLong("event_number"); | ||
final long previousEventNumber = resultSet.getLong("previous_event_number"); | ||
|
||
return of(new LinkedEvent( | ||
id, | ||
streamId, | ||
positionInStream, | ||
name, | ||
metadata, | ||
payload, | ||
createdAt, | ||
eventNumber, | ||
previousEventNumber) | ||
); | ||
} | ||
} | ||
} | ||
|
||
return empty(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...r-process/src/main/java/uk/gov/justice/services/eventsourcing/EventFetchingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package uk.gov.justice.services.eventsourcing; | ||
|
||
public class EventFetchingException extends RuntimeException { | ||
|
||
public EventFetchingException(final String message) { | ||
super(message); | ||
} | ||
|
||
public EventFetchingException(final String message, final Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ss/src/main/java/uk/gov/justice/services/eventsourcing/prepublish/LinkedEventFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package uk.gov.justice.services.eventsourcing.prepublish; | ||
|
||
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event; | ||
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.LinkedEvent; | ||
import uk.gov.justice.services.messaging.Metadata; | ||
|
||
public class LinkedEventFactory { | ||
|
||
public LinkedEvent create( | ||
final Event event, | ||
final Metadata updatedMetadata, | ||
final long eventNumber, | ||
final Long previousEventNumber) { | ||
|
||
return new LinkedEvent( | ||
event.getId(), | ||
event.getStreamId(), | ||
event.getSequenceId(), | ||
event.getName(), | ||
updatedMetadata.asJsonObject().toString(), | ||
event.getPayload(), | ||
event.getCreatedAt(), | ||
eventNumber, | ||
previousEventNumber); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...in/java/uk/gov/justice/services/eventsourcing/prepublish/MissingEventNumberException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package uk.gov.justice.services.eventsourcing.prepublish; | ||
|
||
public class MissingEventNumberException extends RuntimeException { | ||
|
||
public MissingEventNumberException(final String message) { | ||
super(message); | ||
} | ||
} |
Oops, something went wrong.