info.unterrainer.commons.httpserver.HandlerExtensions Maven / Gradle / Ivy
package info.unterrainer.commons.httpserver;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import info.unterrainer.commons.httpserver.exceptions.GracefulCancelationException;
import info.unterrainer.commons.httpserver.extensions.AsyncExtensionContext;
import info.unterrainer.commons.httpserver.extensions.delegates.PostDeleteAsync;
import info.unterrainer.commons.httpserver.extensions.delegates.PostDeleteSync;
import info.unterrainer.commons.httpserver.extensions.delegates.PostGetListAsync;
import info.unterrainer.commons.httpserver.extensions.delegates.PostGetListSync;
import info.unterrainer.commons.httpserver.extensions.delegates.PostGetSingleAsync;
import info.unterrainer.commons.httpserver.extensions.delegates.PostGetSingleSync;
import info.unterrainer.commons.httpserver.extensions.delegates.PostInsertAsync;
import info.unterrainer.commons.httpserver.extensions.delegates.PostInsertSync;
import info.unterrainer.commons.httpserver.extensions.delegates.PostModifyAsync;
import info.unterrainer.commons.httpserver.extensions.delegates.PostModifySync;
import info.unterrainer.commons.httpserver.extensions.delegates.PreDeleteAsync;
import info.unterrainer.commons.httpserver.extensions.delegates.PreDeleteSync;
import info.unterrainer.commons.httpserver.extensions.delegates.PreInsertAsync;
import info.unterrainer.commons.httpserver.extensions.delegates.PreInsertSync;
import info.unterrainer.commons.httpserver.extensions.delegates.PreModifyAsync;
import info.unterrainer.commons.httpserver.extensions.delegates.PreModifySync;
import info.unterrainer.commons.httpserver.jsons.ListJson;
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
import info.unterrainer.commons.serialization.jsons.BasicJson;
import io.javalin.http.Context;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Accessors(fluent = true)
public class HandlerExtensions {
private final List> postDeleteAsync = new ArrayList<>();
private final List> postDeleteSync = new ArrayList<>();
private final List> postGetListAsync = new ArrayList<>();
private final List> postGetListSync = new ArrayList<>();
private final List> postGetSingleAsync = new ArrayList<>();
private final List> postGetSingleSync = new ArrayList<>();
private final List> postInsertAsync = new ArrayList<>();
private final List> postInsertSync = new ArrayList<>();
private final List> postModifyAsync = new ArrayList<>();
private final List> postModifySync = new ArrayList<>();
private final List> preDeleteAsync = new ArrayList<>();
private final List> preDeleteSync = new ArrayList<>();
private final List> preInsertAsync = new ArrayList<>();
private final List> preInsertSync = new ArrayList<>();
private final List> preModifyAsync = new ArrayList<>();
private final List> preModifySync = new ArrayList<>();
public J runPostGetSingle(final Context ctx, final AsyncExtensionContext asyncCtx, final E entityManager,
final Long receivedId, final P readJpa, final J response, final ExecutorService executorService) {
for (PostGetSingleAsync h : postGetSingleAsync())
executorService.execute(() -> h.handle(asyncCtx, receivedId, readJpa, response));
J result = response;
for (PostGetSingleSync
h : postGetSingleSync()) {
result = h.handle(ctx, entityManager, receivedId, readJpa, result);
if (result == null)
throw new GracefulCancelationException();
}
return result;
}
public ListJson runPostGetList(final Context ctx, final AsyncExtensionContext asyncCtx, final E entityManager,
final Long size, final Long offset, final ListJson readList, final ListJson response,
final ExecutorService executorService) {
for (PostGetListAsync h : postGetListAsync())
executorService.execute(() -> h.handle(asyncCtx, size, offset, readList, response));
ListJson result = response;
for (PostGetListSync h : postGetListSync()) {
result = h.handle(ctx, entityManager, size, offset, readList, result);
if (result == null)
throw new GracefulCancelationException();
}
return result;
}
public P runPreInsert(final Context ctx, final AsyncExtensionContext asyncCtx, final E entityManager,
final J receivedJson, final P resultJpa, final ExecutorService executorService) {
for (PreInsertAsync
h : preInsertAsync())
executorService.execute(() -> h.handle(asyncCtx, receivedJson, resultJpa));
P result = resultJpa;
for (PreInsertSync
h : preInsertSync()) {
result = h.handle(ctx, entityManager, receivedJson, resultJpa);
if (result == null)
throw new GracefulCancelationException();
}
return result;
}
public J runPostInsert(final Context ctx, final AsyncExtensionContext asyncCtx, final E entityManager,
final J receivedJson, final P mappedJpa, final P createdJpa, final J response,
final ExecutorService executorService) {
for (PostInsertAsync
h : postInsertAsync())
executorService.execute(() -> h.handle(asyncCtx, receivedJson, mappedJpa, createdJpa, response));
J result = response;
for (PostInsertSync
h : postInsertSync()) {
result = h.handle(ctx, entityManager, receivedJson, mappedJpa, createdJpa, response);
if (result == null)
throw new GracefulCancelationException();
}
return result;
}
public P runPreModify(final Context ctx, final AsyncExtensionContext asyncCtx, final E entityManager,
final Long receivedId, final J receivedJson, final P readJpa, final P resultJpa,
final ExecutorService executorService) {
for (PreModifyAsync
h : preModifyAsync())
executorService.execute(() -> h.handle(asyncCtx, receivedId, receivedJson, readJpa, resultJpa));
P result = resultJpa;
for (PreModifySync
h : preModifySync()) {
result = h.handle(ctx, entityManager, receivedId, receivedJson, readJpa, resultJpa);
if (result == null)
throw new GracefulCancelationException();
}
return result;
}
public J runPostModify(final Context ctx, final AsyncExtensionContext asyncCtx, final E entityManager,
final Long receivedId, final J receivedJson, final P readJpa, final P mappedJpa, final P persistedJpa,
final J response, final ExecutorService executorService) {
for (PostModifyAsync
h : postModifyAsync())
executorService.execute(
() -> h.handle(asyncCtx, receivedId, receivedJson, readJpa, mappedJpa, persistedJpa, response));
J result = response;
for (PostModifySync
h : postModifySync()) {
result = h.handle(ctx, entityManager, receivedId, receivedJson, readJpa, mappedJpa, persistedJpa, response);
if (result == null)
throw new GracefulCancelationException();
}
return result;
}
public Long runPreDelete(final Context ctx, final AsyncExtensionContext asyncCtx, final E entityManager,
final Long receivedId, final P jpaToDelete, final ExecutorService executorService) {
for (PreDeleteAsync
h : preDeleteAsync())
executorService.execute(() -> h.handle(asyncCtx, receivedId, jpaToDelete));
Long result = receivedId;
for (PreDeleteSync
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 P deletedJpa, final ExecutorService executorService) {
for (PostDeleteAsync
h : postDeleteAsync())
executorService.execute(() -> h.handle(asyncCtx, receivedId, deletedJpa));
for (PostDeleteSync
h : postDeleteSync())
if (!h.handle(ctx, entityManager, receivedId, deletedJpa))
throw new GracefulCancelationException();
}
}