-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MODAUD-174] - Consume piece change events and implement endpoints #155
Merged
Merged
Changes from 16 commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
854e82a
[MODAUD-174] - Consume piece change events and implement endpoints
azizbekxm 3a35fb9
[MODAUD-174] - Fixed unit tests
azizbekxm dbaaf9f
[MODAUD-174] - Fixed Code smell
azizbekxm 38c6613
[MODAUD-174] - Fixed Code smell
azizbekxm 11961c6
[MODAUD-174] - Fixed Unit tests
azizbekxm 066fbac
[MODAUD-174] - Minor improvements
azizbekxm 536e9ea
[MODAUD-174] - Implements kafka handler and creating table
azizbekxm 97e24a2
[MODAUD-174] - Increased code coverage
azizbekxm 0d3017b
[MODAUD-174] - Increased code coverage
azizbekxm 3262ab6
[MODAUD-174] - Minor improvements
azizbekxm 42fbbad
[MODAUD-174] - Minor improvements
azizbekxm a2d8ae2
[MODAUD-174] - Minor improvements
azizbekxm 7516c51
[MODAUD-174] - Minor improvements
azizbekxm 90c5205
[MODAUD-174] - Minor improvements
azizbekxm 9a12aac
[MODAUD-174] - Minor improvements
azizbekxm 20a1b7f
[MODAUD-174] - Implemented uniq-status api
azizbekxm ecd4f24
[MODAUD-174] - Implemented status change history feature and cleaned …
azizbekxm 3811d8a
[MODAUD-174] - Implemented status change history feature and cleaned …
azizbekxm 1646698
[MODAUD-174] - Implemented status change history feature and cleaned …
azizbekxm df807cc
[MODAUD-174] - draft changes
azizbekxm aa96b19
[MODAUD-174] - Fixed error
azizbekxm 821a46a
[MODAUD-174] - minor improvements
azizbekxm d43d0ee
[MODAUD-174] - minor improvements
azizbekxm d32bc03
[MODAUD-174] - minor improvements
azizbekxm 6aef666
[MODAUD-174] - minor improvements
azizbekxm 86d171f
[MODAUD-174] - minor improvements
azizbekxm 18d7d11
[MODAUD-174] - Fixed unit tests
azizbekxm ba9d212
[MODAUD-174] - Fixed unit tests
azizbekxm 97307d7
[MODAUD-174] - Fixed unit tests
azizbekxm 3152592
[MODAUD-174] - Fixed unit tests
azizbekxm d3f3a3d
[MODAUD-174] - draft changes
azizbekxm c30ce60
[MODAUD-174] - Fixed unit tests
azizbekxm 08482dc
[MODAUD-174] - Minor improvements
azizbekxm a462a7c
[MODAUD-174] - Minor improvements
azizbekxm 2376e3c
[MODAUD-174] - Minor improvements
azizbekxm 9d66e94
[MODAUD-174] - Improved code by changing field injection to construct…
azizbekxm f7e9581
[MODAUD-174] - Fixed unit tests
azizbekxm 19d7e9f
[MODAUD-174] - Fixed unit tests
azizbekxm c72b7b8
[MODAUD-174] - Fixed sql query
azizbekxm 6438c25
[MODAUD-174] - Extracted duplicate methods
azizbekxm 49b39b2
[MODAUD-174] - Added java docs
azizbekxm d04fa1f
[MODAUD-174] - Improved java integration test
azizbekxm 3132186
[MODAUD-174] - Extracted handleFauilures and optimized codebase
azizbekxm f8c661e
[MODAUD-174] - Fixed sql query
azizbekxm 82b48db
[MODAUD-174] - Fixed sql query
azizbekxm af27863
[MODAUD-174] - Minor improvement
azizbekxm ec50fb3
[MODAUD-174] - Improved AuditDataAcquisitionImpl
azizbekxm 26bcef4
[MODAUD-174] - Improved AuditDataAcquisitionImpl and changed debug to…
azizbekxm 1f00735
[MODAUD-174] - Fixed ZoneOffset
azizbekxm fda19eb
[MODAUD-174] - Improved log.error message
azizbekxm 3881bdc
[MODAUD-174] - Minor improvements
azizbekxm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ nbproject/ | |
.settings/ | ||
.classpath | ||
/bin/ | ||
/src/main/resources/postgres-conf.json |
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
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
36 changes: 36 additions & 0 deletions
36
mod-audit-server/src/main/java/org/folio/dao/acquisition/PieceEventsDao.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,36 @@ | ||
package org.folio.dao.acquisition; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.sqlclient.Row; | ||
import io.vertx.sqlclient.RowSet; | ||
import org.folio.rest.jaxrs.model.PieceAuditEvent; | ||
import org.folio.rest.jaxrs.model.PieceAuditEventCollection; | ||
|
||
public interface PieceEventsDao { | ||
|
||
/** | ||
* Saves pieceAuditEvent entity to DB | ||
* | ||
* @param pieceAuditEvent pieceAuditEvent entity to save | ||
* @param tenantId tenant id | ||
* @return future with created row | ||
*/ | ||
Future<RowSet<Row>> save(PieceAuditEvent pieceAuditEvent, String tenantId); | ||
|
||
/** | ||
* Searches for piece audit events by id | ||
* | ||
* @param pieceId piece id | ||
* @param sortBy sort by | ||
* @param sortOrder sort order | ||
* @param limit limit | ||
* @param offset offset | ||
* @param tenantId tenant id | ||
* @return future with PieceAuditEventCollection | ||
*/ | ||
Future<PieceAuditEventCollection> getAuditEventsByPieceId(String pieceId, String sortBy, String sortOrder, | ||
int limit, int offset, String tenantId); | ||
|
||
Future<PieceAuditEventCollection> getAuditEventsWithUniqueStatusByPieceId(String pieceId, String sortBy, String sortOrder, | ||
SerhiiNosko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int limit, int offset, String tenantId); | ||
} |
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
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
156 changes: 156 additions & 0 deletions
156
mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/PieceEventsDaoImpl.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,156 @@ | ||
package org.folio.dao.acquisition.impl; | ||
|
||
import static java.lang.String.format; | ||
import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard; | ||
import static org.folio.util.AuditEventDBConstants.ACTION_DATE_FIELD; | ||
import static org.folio.util.AuditEventDBConstants.ACTION_FIELD; | ||
import static org.folio.util.AuditEventDBConstants.EVENT_DATE_FIELD; | ||
import static org.folio.util.AuditEventDBConstants.ID_FIELD; | ||
import static org.folio.util.AuditEventDBConstants.MODIFIED_CONTENT_FIELD; | ||
import static org.folio.util.AuditEventDBConstants.ORDER_BY_PATTERN; | ||
import static org.folio.util.AuditEventDBConstants.PIECE_ID_FIELD; | ||
import static org.folio.util.AuditEventDBConstants.USER_ID_FIELD; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneOffset; | ||
import java.util.Date; | ||
import java.util.UUID; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.Promise; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.sqlclient.Row; | ||
import io.vertx.sqlclient.RowSet; | ||
import io.vertx.sqlclient.Tuple; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.folio.dao.acquisition.PieceEventsDao; | ||
import org.folio.rest.jaxrs.model.PieceAuditEvent; | ||
import org.folio.rest.jaxrs.model.PieceAuditEventCollection; | ||
import org.folio.util.PostgresClientFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public class PieceEventsDaoImpl implements PieceEventsDao { | ||
private static final Logger LOGGER = LogManager.getLogger(); | ||
private static final String TABLE_NAME = "acquisition_piece_log"; | ||
private static final String GET_BY_PIECE_ID_SQL = "SELECT id, action, piece_id, user_id, event_date, action_date, modified_content_snapshot" + | ||
" FROM %s WHERE piece_id = $1 %s LIMIT $2 OFFSET $3"; | ||
private static final String GET_UNIQUE_STATUS_BY_PIECE_ID_SQL = "SELECT id, action, piece_id, user_id, event_date, action_date, modified_content_snapshot" + | ||
" FROM %s WHERE piece_id = $1 AND" + | ||
" action IN" + | ||
" (SELECT action FROM %s WHERE piece_id = $1 GROUP BY action HAVING COUNT(action) = 1)" + | ||
" %s LIMIT $2 OFFSET $3"; | ||
private static final String INSERT_SQL = "INSERT INTO %s (id, action, piece_id, user_id, event_date, action_date, modified_content_snapshot)" + | ||
" VALUES ($1, $2, $3, $4, $5, $6, $7)"; | ||
|
||
private final PostgresClientFactory pgClientFactory; | ||
|
||
@Autowired | ||
public PieceEventsDaoImpl(PostgresClientFactory pgClientFactory) { | ||
this.pgClientFactory = pgClientFactory; | ||
} | ||
|
||
@Override | ||
public Future<RowSet<Row>> save(PieceAuditEvent pieceAuditEvent, String tenantId) { | ||
LOGGER.debug("save:: Trying to save Piece AuditEvent with tenant id : {}", tenantId); | ||
Promise<RowSet<Row>> promise = Promise.promise(); | ||
|
||
String logTable = formatDBTableName(tenantId, TABLE_NAME); | ||
String query = format(INSERT_SQL, logTable); | ||
|
||
makeSaveCall(promise, query, pieceAuditEvent, tenantId); | ||
LOGGER.info("save:: Saved Piece AuditEvent for pieceId={} in tenant id={}", pieceAuditEvent.getPieceId(), tenantId); | ||
return promise.future(); | ||
} | ||
|
||
@Override | ||
public Future<PieceAuditEventCollection> getAuditEventsByPieceId(String pieceId, String sortBy, String sortOrder, int limit, int offset, String tenantId) { | ||
LOGGER.debug("getAuditEventsByOrderId:: Trying to retrieve AuditEvent with piece id : {}", pieceId); | ||
Promise<RowSet<Row>> promise = Promise.promise(); | ||
try { | ||
String logTable = formatDBTableName(tenantId, TABLE_NAME); | ||
String query = format(GET_BY_PIECE_ID_SQL, logTable, format(ORDER_BY_PATTERN, sortBy, sortOrder)); | ||
Tuple queryParams = Tuple.of(UUID.fromString(pieceId), limit, offset); | ||
pgClientFactory.createInstance(tenantId).selectRead(query, queryParams, promise); | ||
} catch (Exception e) { | ||
LOGGER.warn("Error getting piece audit events by piece id: {}", pieceId, e); | ||
promise.fail(e); | ||
} | ||
LOGGER.info("getAuditEventsByOrderId:: Retrieved AuditEvent with piece id : {}", pieceId); | ||
return promise.future().map(rowSet -> rowSet.rowCount() == 0 ? | ||
new PieceAuditEventCollection().withTotalItems(0) : | ||
mapRowToListOfPieceEvent(rowSet)); | ||
} | ||
|
||
@Override | ||
public Future<PieceAuditEventCollection> getAuditEventsWithUniqueStatusByPieceId(String pieceId, String sortBy, String sortOrder, int limit, int offset, String tenantId) { | ||
LOGGER.debug("getAuditEventsByOrderId:: Retrieving AuditEvent with piece id : {}", pieceId); | ||
Promise<RowSet<Row>> promise = Promise.promise(); | ||
try { | ||
LOGGER.info("getAuditEventsByOrderId:: Trying to Retrieve AuditEvent with order id : {}", pieceId); | ||
String logTable = formatDBTableName(tenantId, TABLE_NAME); | ||
String query = format(GET_UNIQUE_STATUS_BY_PIECE_ID_SQL, logTable, logTable, format(ORDER_BY_PATTERN, sortBy, sortOrder)); | ||
Tuple queryParams = Tuple.of(UUID.fromString(pieceId), limit, offset); | ||
pgClientFactory.createInstance(tenantId).selectRead(query, queryParams, promise); | ||
} catch (Exception e) { | ||
LOGGER.warn("Error getting order audit events by piece id: {}", pieceId, e); | ||
promise.fail(e); | ||
} | ||
LOGGER.info("getAuditEventsByOrderId:: Retrieved AuditEvent with piece id: {}", pieceId); | ||
return promise.future().map(rowSet -> rowSet.rowCount() == 0 ? new PieceAuditEventCollection().withTotalItems(0) | ||
: mapRowToListOfPieceEvent(rowSet)); | ||
} | ||
|
||
private PieceAuditEventCollection mapRowToListOfPieceEvent(RowSet<Row> rowSet) { | ||
LOGGER.debug("mapRowToListOfOrderEvent:: Mapping row to List of Piece Events"); | ||
PieceAuditEventCollection pieceAuditEventCollection = new PieceAuditEventCollection(); | ||
|
||
// set audit piece change record(s) by mapping rowSet one by one | ||
rowSet.iterator().forEachRemaining(row -> { | ||
pieceAuditEventCollection.getPieceAuditEvents().add(mapRowToPieceEvent(row)); | ||
}); | ||
// set total records | ||
int totalRecords = pieceAuditEventCollection.getPieceAuditEvents().size(); | ||
pieceAuditEventCollection.setTotalItems(totalRecords); | ||
|
||
LOGGER.info("mapRowToListOfOrderEvent:: Mapped row to List of Piece Events"); | ||
SerhiiNosko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return pieceAuditEventCollection; | ||
} | ||
|
||
private PieceAuditEvent mapRowToPieceEvent(Row row) { | ||
LOGGER.debug("mapRowToPieceEvent:: Mapping row to Order Event"); | ||
return new PieceAuditEvent() | ||
.withId(row.getValue(ID_FIELD).toString()) | ||
.withAction(row.get(PieceAuditEvent.Action.class, ACTION_FIELD)) | ||
.withPieceId(row.getValue(PIECE_ID_FIELD).toString()) | ||
.withUserId(row.getValue(USER_ID_FIELD).toString()) | ||
.withEventDate(Date.from(row.getLocalDateTime(EVENT_DATE_FIELD).toInstant(ZoneOffset.UTC))) | ||
.withActionDate(Date.from(row.getLocalDateTime(ACTION_DATE_FIELD).toInstant(ZoneOffset.UTC))) | ||
.withPieceSnapshot(JsonObject.mapFrom(row.getValue(MODIFIED_CONTENT_FIELD))); | ||
} | ||
|
||
private void makeSaveCall(Promise<RowSet<Row>> promise, String query, PieceAuditEvent pieceAuditEvent, String tenantId) { | ||
LOGGER.debug("makeSaveCall:: Making save call with query : {} and tenant id : {}", query, tenantId); | ||
try { | ||
pgClientFactory.createInstance(tenantId).execute(query, Tuple.of(pieceAuditEvent.getId(), | ||
pieceAuditEvent.getActionDate(), | ||
pieceAuditEvent.getPieceId(), | ||
pieceAuditEvent.getUserId(), | ||
LocalDateTime.ofInstant(pieceAuditEvent.getEventDate().toInstant(), ZoneOffset.UTC), | ||
LocalDateTime.ofInstant(pieceAuditEvent.getActionDate().toInstant(), ZoneOffset.UTC), | ||
JsonObject.mapFrom(pieceAuditEvent.getPieceSnapshot())), | ||
promise); | ||
} catch (Exception e) { | ||
LOGGER.error("Failed to save record with id: {} for order id: {} in to table {}", | ||
pieceAuditEvent.getId(), pieceAuditEvent.getPieceId(), TABLE_NAME, e); | ||
promise.fail(e); | ||
} | ||
} | ||
|
||
private String formatDBTableName(String tenantId, String table) { | ||
SerhiiNosko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId); | ||
return format("%s.%s", convertToPsqlStandard(tenantId), table); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doubt
Do we need to push this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it avoid situation where intellij idea asks whether add postgres-conf.json to git or not. For me it is useful