Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Unterrainer committed Aug 12, 2021
2 parents 25c83d9 + 0ab94e6 commit d3a0b1f
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 38 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<modelVersion>4.0.0</modelVersion>
<artifactId>http-server</artifactId>
<version>0.2.30</version>
<version>0.2.31</version>
<name>HttpServer</name>
<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public class AddonBuilder<P extends BasicJpa, J extends BasicJson, E> {

private final GenericHandlerGroupBuilder<P, J, E> builder;

public GenericHandlerGroupBuilder<P, J, E> postDeleteAsync(final PostDeleteAsync delegate) {
public GenericHandlerGroupBuilder<P, J, E> postDeleteAsync(final PostDeleteAsync<P> delegate) {
builder.extensions.postDeleteAsync().add(delegate);
return builder;
}

public GenericHandlerGroupBuilder<P, J, E> postDeleteSync(final PostDeleteSync<E> delegate) {
public GenericHandlerGroupBuilder<P, J, E> postDeleteSync(final PostDeleteSync<P, E> delegate) {
builder.extensions.postDeleteSync().add(delegate);
return builder;
}
Expand Down Expand Up @@ -75,12 +75,12 @@ public GenericHandlerGroupBuilder<P, J, E> postModifySync(final PostModifySync<P
return builder;
}

public GenericHandlerGroupBuilder<P, J, E> preDeleteAsync(final PreDeleteAsync delegate) {
public GenericHandlerGroupBuilder<P, J, E> preDeleteAsync(final PreDeleteAsync<P> delegate) {
builder.extensions.preDeleteAsync().add(delegate);
return builder;
}

public GenericHandlerGroupBuilder<P, J, E> preDeleteSync(final PreDeleteSync<E> delegate) {
public GenericHandlerGroupBuilder<P, J, E> preDeleteSync(final PreDeleteSync<P, E> delegate) {
builder.extensions.preDeleteSync().add(delegate);
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ExecutorService;

import info.unterrainer.commons.httpserver.daos.CoreDao;
Expand Down Expand Up @@ -211,11 +212,14 @@ private void delete(final Context ctx) {
Long id = hu.checkAndGetId(ctx);
DaoTransaction<E> transaction = dao.getTransactionManager().beginTransaction(ctx);

id = extensions.runPreDelete(ctx, makeAsyncExtensionContextFor(ctx), transaction.getManager(), id,
Set<Long> tenants = hu.getReadTenantIdsFrom(ctx);
P jpa = dao.getById(transaction.getManager(), id, tenants);
id = extensions.runPreDelete(ctx, makeAsyncExtensionContextFor(ctx), transaction.getManager(), id, jpa,
executorService);
dao.delete(transaction.getManager(), id, hu.getReadTenantIdsFrom(ctx));
ctx.status(204);
extensions.runPostDelete(ctx, makeAsyncExtensionContextFor(ctx), transaction.getManager(), id, executorService);
extensions.runPostDelete(ctx, makeAsyncExtensionContextFor(ctx), transaction.getManager(), id, jpa,
executorService);

transaction.end();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
@Accessors(fluent = true)
public class HandlerExtensions<P extends BasicJpa, J extends BasicJson, E> {

private final List<PostDeleteAsync> postDeleteAsync = new ArrayList<>();
private final List<PostDeleteSync<E>> postDeleteSync = new ArrayList<>();
private final List<PostDeleteAsync<P>> postDeleteAsync = new ArrayList<>();
private final List<PostDeleteSync<P, E>> postDeleteSync = new ArrayList<>();
private final List<PostGetListAsync<P, J>> postGetListAsync = new ArrayList<>();
private final List<PostGetListSync<P, J, E>> postGetListSync = new ArrayList<>();
private final List<PostGetSingleAsync<P, J>> postGetSingleAsync = new ArrayList<>();
Expand All @@ -46,8 +46,8 @@ public class HandlerExtensions<P extends BasicJpa, J extends BasicJson, E> {
private final List<PostInsertSync<P, J, E>> postInsertSync = new ArrayList<>();
private final List<PostModifyAsync<P, J>> postModifyAsync = new ArrayList<>();
private final List<PostModifySync<P, J, E>> postModifySync = new ArrayList<>();
private final List<PreDeleteAsync> preDeleteAsync = new ArrayList<>();
private final List<PreDeleteSync<E>> preDeleteSync = new ArrayList<>();
private final List<PreDeleteAsync<P>> preDeleteAsync = new ArrayList<>();
private final List<PreDeleteSync<P, E>> preDeleteSync = new ArrayList<>();
private final List<PreInsertAsync<P, J>> preInsertAsync = new ArrayList<>();
private final List<PreInsertSync<P, J, E>> preInsertSync = new ArrayList<>();
private final List<PreModifyAsync<P, J>> preModifyAsync = new ArrayList<>();
Expand Down Expand Up @@ -143,26 +143,26 @@ public J runPostModify(final Context ctx, final AsyncExtensionContext asyncCtx,
}

public Long runPreDelete(final Context ctx, final AsyncExtensionContext asyncCtx, final E entityManager,
final Long receivedId, final ExecutorService executorService) {
for (PreDeleteAsync h : preDeleteAsync())
executorService.execute(() -> h.handle(asyncCtx, receivedId));
final Long receivedId, final P jpaToDelete, final ExecutorService executorService) {
for (PreDeleteAsync<P> h : preDeleteAsync())
executorService.execute(() -> h.handle(asyncCtx, receivedId, jpaToDelete));

Long result = receivedId;
for (PreDeleteSync<E> h : preDeleteSync()) {
result = h.handle(ctx, entityManager, receivedId);
for (PreDeleteSync<P, E> h : preDeleteSync()) {
result = h.handle(ctx, entityManager, receivedId, jpaToDelete);
if (result == null)
throw new GracefulCancelationException();
}
return result;
}

public void runPostDelete(final Context ctx, final AsyncExtensionContext asyncCtx, final E entityManager,
final Long receivedId, final ExecutorService executorService) {
for (PostDeleteAsync h : postDeleteAsync())
executorService.execute(() -> h.handle(asyncCtx, receivedId));
final Long receivedId, final P deletedJpa, final ExecutorService executorService) {
for (PostDeleteAsync<P> h : postDeleteAsync())
executorService.execute(() -> h.handle(asyncCtx, receivedId, deletedJpa));

for (PostDeleteSync<E> h : postDeleteSync())
if (!h.handle(ctx, entityManager, receivedId))
for (PostDeleteSync<P, E> h : postDeleteSync())
if (!h.handle(ctx, entityManager, receivedId, deletedJpa))
throw new GracefulCancelationException();
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package info.unterrainer.commons.httpserver.extensions.delegates;

import info.unterrainer.commons.httpserver.extensions.AsyncExtensionContext;
import info.unterrainer.commons.rdbutils.entities.BasicJpa;

public interface PostDeleteAsync {
public interface PostDeleteAsync<P extends BasicJpa> {

/**
* Allows you to execute code after the deletion of an item.
* <p>
* Since this is asynchronous changing the DTOs will do nothing (runs in
* parallel and the action probably already happened when your code is
* executed).
*
*
* @param asyncCtx a context containing values that have been mapped using
* AsyncExtensionContextMappers
* @param receivedId the ID of the item that was deleted
* @param deletedJpa the JPA that was deleted
*/
void handle(AsyncExtensionContext asyncCtx, Long receivedId);
void handle(AsyncExtensionContext asyncCtx, Long receivedId, P deletedJpa);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package info.unterrainer.commons.httpserver.extensions.delegates;

import info.unterrainer.commons.rdbutils.entities.BasicJpa;
import io.javalin.http.Context;

public interface PostDeleteSync<E> {
public interface PostDeleteSync<P extends BasicJpa, E> {

/**
* Allows you to execute code after the deletion of an item.<br>
Expand All @@ -15,8 +16,9 @@ public interface PostDeleteSync<E> {
* @param entityManager the entity-manager you can use to get the active
* transaction, if any
* @param receivedId the ID of the item that was deleted
* @param deletedJpa the JPA that was deleted
* @return true, if the handler-chain should be continued to be processed, false
* otherwise.
*/
boolean handle(Context ctx, E entityManager, Long receivedId);
boolean handle(Context ctx, E entityManager, Long receivedId, P deletedJpa);
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package info.unterrainer.commons.httpserver.extensions.delegates;

import info.unterrainer.commons.httpserver.extensions.AsyncExtensionContext;
import info.unterrainer.commons.rdbutils.entities.BasicJpa;

public interface PreDeleteAsync {
public interface PreDeleteAsync<P extends BasicJpa> {

/**
* Allows you to execute code before deletion of an item.
* <p>
* Since this is asynchronous changing the DTOs will do nothing (runs in
* parallel and the action probably already happened when your code is
* executed).
*
* @param asyncCtx a context containing values that have been mapped using
* AsyncExtensionContextMappers
* @param receivedId the ID of the item that is about to get deleted
*
* @param asyncCtx a context containing values that have been mapped using
* AsyncExtensionContextMappers
* @param receivedId the ID of the item that is about to get deleted
* @param jpaToDelete the JPA to delete
*/
void handle(AsyncExtensionContext asyncCtx, Long receivedId);
void handle(AsyncExtensionContext asyncCtx, Long receivedId, P jpaToDelete);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package info.unterrainer.commons.httpserver.extensions.delegates;

import info.unterrainer.commons.rdbutils.entities.BasicJpa;
import io.javalin.http.Context;

public interface PreDeleteSync<E> {
public interface PreDeleteSync<P extends BasicJpa, E> {

/**
* Allows you to execute code before the deletion of an item.<br>
Expand All @@ -15,7 +16,8 @@ public interface PreDeleteSync<E> {
* @param entityManager the entity-manager you can use to get the active
* transaction, if any
* @param receivedId the ID of the item that is about to get deleted
* @param jpaToDelete the JPA to delete
* @return the ID of the item that is about to get deleted
*/
Long handle(Context ctx, E entityManager, Long receivedId);
Long handle(Context ctx, E entityManager, Long receivedId, P jpaToDelete);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static void startServer() {
return resultJpa;
})
.extension()
.preDeleteSync((ctx, em, receivedId) -> {
.preDeleteSync((ctx, em, receivedId, deletedJpa) -> {
log.info("before delete id:[{}]", receivedId);
return receivedId;
})
Expand All @@ -77,7 +77,7 @@ private static void startServer() {
return responseList;
})
.extension()
.postDeleteSync((ctx, em, receivedId) -> {
.postDeleteSync((ctx, em, receivedId, deletedJpa) -> {
log.info("after delete");
return true;
})
Expand Down Expand Up @@ -105,7 +105,7 @@ private static void startServer() {
return resultJpa;
})
.extension()
.preDeleteSync((ctx, em, receivedId) -> {
.preDeleteSync((ctx, em, receivedId, deletedJpa) -> {
log.info("before delete id:[{}]", receivedId);
return receivedId;
})
Expand All @@ -125,7 +125,7 @@ private static void startServer() {
return responseList;
})
.extension()
.postDeleteSync((ctx, em, receivedId) -> {
.postDeleteSync((ctx, em, receivedId, deletedJpa) -> {
log.info("after delete");
return true;
})
Expand Down

0 comments on commit d3a0b1f

Please sign in to comment.