Skip to content
Snippets Groups Projects
Commit f563b5d0 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-5322 refactor bescheid event listener

- init security context at one point
- don't use 'storeAsDocument' setting
- add listener for CREATE_BESCHEID_DOCUMENT command
parent 9f8fdada
No related branches found
No related tags found
No related merge requests found
......@@ -25,6 +25,7 @@ package de.ozgcloud.bescheid;
import java.time.LocalDate;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Predicate;
import org.apache.commons.lang3.StringUtils;
......@@ -56,25 +57,27 @@ class BescheidEventListener {
public static final String CREATE_BESCHEID_ORDER = "CREATE_BESCHEID";
public static final String DELETE_BESCHEID_ORDER = "DELETE_BESCHEID";
public static final String UPDATE_BESCHEID_ORDER = "UPDATE_BESCHEID";
public static final String CREATE_BESCHEID_DOCUMENT_ORDER = "CREATE_BESCHEID_DOCUMENT";
public static final Predicate<Command> IS_CREATE_BESCHEID_COMMAND = command -> command.getOrder().equals(CREATE_BESCHEID_ORDER);
public static final Predicate<Command> IS_CREATE_BESCHEID_COMMAND = command -> CREATE_BESCHEID_ORDER.equals(command.getOrder());
private static final String IS_CREATE_BESCHEID = "{T(de.ozgcloud.bescheid.BescheidEventListener).IS_CREATE_BESCHEID_COMMAND.test(event.getSource())}";
public static final Predicate<Command> IS_DELETE_BESCHEID_COMMAND = command -> command.getOrder().equals(DELETE_BESCHEID_ORDER);
public static final Predicate<Command> IS_DELETE_BESCHEID_COMMAND = command -> DELETE_BESCHEID_ORDER.equals(command.getOrder());
private static final String IS_DELETE_BESCHEID = "{T(de.ozgcloud.bescheid.BescheidEventListener).IS_DELETE_BESCHEID_COMMAND.test(event.getSource())}";
public static final Predicate<Command> IS_UPDATE_BESCHEID_COMMAND = command -> command.getOrder().equals(UPDATE_BESCHEID_ORDER);
public static final Predicate<Command> IS_UPDATE_BESCHEID_COMMAND = command -> UPDATE_BESCHEID_ORDER.equals(command.getOrder());
private static final String IS_UPDATE_BESCHEID = "{T(de.ozgcloud.bescheid.BescheidEventListener).IS_UPDATE_BESCHEID_COMMAND.test(event.getSource())}";
public static final Predicate<Command> IS_CREATE_BESCHEID_DOCUMENT_COMMAND = command -> CREATE_BESCHEID_DOCUMENT_ORDER.equals(command.getOrder());
private static final String IS_CREATE_BESCHEID_DOCUMENT = "{T(de.ozgcloud.bescheid.BescheidEventListener).IS_CREATE_BESCHEID_DOCUMENT_COMMAND.test(event.getSource())}";
private static final String TEMPLATE_GROUP_KIEL = "Kiel";
static final String VORGANG_ID_BODYKEY = "vorgangId";
static final String BESCHEID_VOM_BODYKEY = "bescheidVom";
static final String GENEHMIGT_BODYKEY = "genehmigt";
private static final String LOG_MESSAGE_TEMPLATE = "{}. Command failed.";
private static final String CREATE_BESCHEID_ERROR_MESSAGE = "Error on executing Create Bescheid Command.";
private static final String DELETE_BESCHEID_ERROR_MESSAGE = "Error on executing Delete Bescheid Command.";
private static final String UPDATE_BESCHEID_ERROR_MESSAGE = "Error on executing Update Bescheid Command.";
private static final String ERROR_MESSAGE_TEMPLATE = "Error on executing %s Command.";
private final BescheidService service;
private final BinaryFileService fileService;
......@@ -90,29 +93,13 @@ class BescheidEventListener {
@EventListener(condition = IS_CREATE_BESCHEID)
public void onCreateBescheidCommand(CommandCreatedEvent event) {
Command command = event.getSource();
doCreateBescheid(command);
}
private void doCreateBescheid(Command command) {
SecurityContext prevContext = null;
try {
prevContext = userService.startSecurityContext(command);
execute(command);
} catch (Exception e) {
LOG.error(LOG_MESSAGE_TEMPLATE, CREATE_BESCHEID_ERROR_MESSAGE, e);
eventPublisher.publishEvent(new CommandFailedEvent(command.getId(), buildErrorMessage(CREATE_BESCHEID_ERROR_MESSAGE, e)));
} finally {
userService.resetSecurityContext(prevContext);
}
runWithSecurityContext(event.getSource(), this::doCreateBescheid);
}
void execute(Command command) {
void doCreateBescheid(Command command) {
if (isKielEnvironment()) {
doCreateBescheidBiz(command).ifPresentOrElse(
documentId -> eventPublisher.publishEvent(new BescheidCreatedEvent(command, documentId)),
() -> eventPublisher.publishEvent(new BescheidCreatedEvent(command)));
doCreateBescheidBiz(command);
eventPublisher.publishEvent(new BescheidCreatedEvent(command));
return;
}
var createdItemId = attachedItemService.createBescheidDraft(command);
......@@ -124,15 +111,10 @@ class BescheidEventListener {
return smartDocumentsProperties.filter(configuredForKiel).isPresent();
}
public Optional<String> doCreateBescheidBiz(@NonNull Command command) {
void doCreateBescheidBiz(@NonNull Command command) {
var bescheid = service.createBescheid(createRequest(command));
bescheid = fileService.uploadBescheidFile(bescheid);
if (featureProperties.isStoreAsDocument()) {
return Optional.of(documentService.createBescheidDocument(command, bescheid));
}
nachrichtService.createNachrichtDraft(bescheid);
return Optional.empty();
}
BescheidRequest createRequest(Command command) {
......@@ -152,34 +134,46 @@ class BescheidEventListener {
@EventListener(condition = IS_DELETE_BESCHEID)
public void onDeleteBescheid(CommandCreatedEvent event) {
SecurityContext prevContext = null;
Command command = event.getSource();
try {
prevContext = userService.startSecurityContext(command);
attachedItemService.deleteBescheidDraft(command);
eventPublisher.publishEvent(new BescheidDeletedEvent(command));
} catch (Exception e) {
LOG.error(LOG_MESSAGE_TEMPLATE, DELETE_BESCHEID_ERROR_MESSAGE, e);
eventPublisher.publishEvent(new CommandFailedEvent(command.getId(), buildErrorMessage(DELETE_BESCHEID_ERROR_MESSAGE, e)));
} finally {
userService.resetSecurityContext(prevContext);
}
runWithSecurityContext(event.getSource(), this::doDeleteBescheid);
}
void doDeleteBescheid(Command command) {
attachedItemService.deleteBescheidDraft(command);
eventPublisher.publishEvent(new BescheidDeletedEvent(command));
}
@EventListener(condition = IS_UPDATE_BESCHEID)
public void onUpdateBescheidCommand(CommandCreatedEvent event) {
Command command = event.getSource();
runWithSecurityContext(event.getSource(), this::doUpdateBescheid);
}
void doUpdateBescheid(Command command) {
attachedItemService.updateBescheidDraft(command);
eventPublisher.publishEvent(new BescheidUpdatedEvent(command));
}
@EventListener(condition = IS_CREATE_BESCHEID_DOCUMENT)
public void onCreatedBescheidDocument(CommandCreatedEvent event) {
runWithSecurityContext(event.getSource(), this::doCreateBescheidDocument);
}
void doCreateBescheidDocument(Command command) {
}
void runWithSecurityContext(Command command, Consumer<Command> commandExecutor) {
SecurityContext prevContext = null;
try {
prevContext = userService.startSecurityContext(command);
attachedItemService.updateBescheidDraft(command);
eventPublisher.publishEvent(new BescheidUpdatedEvent(command));
commandExecutor.accept(command);
} catch (Exception e) {
LOG.error(LOG_MESSAGE_TEMPLATE, UPDATE_BESCHEID_ERROR_MESSAGE, e);
eventPublisher.publishEvent(new CommandFailedEvent(command.getId(), buildErrorMessage(UPDATE_BESCHEID_ERROR_MESSAGE, e)));
var errorMessage = ERROR_MESSAGE_TEMPLATE.formatted(command.getOrder());
LOG.error(LOG_MESSAGE_TEMPLATE, errorMessage, e);
eventPublisher.publishEvent(new CommandFailedEvent(command.getId(), buildErrorMessage(errorMessage, e)));
} finally {
userService.resetSecurityContext(prevContext);
}
}
private String buildErrorMessage(String message, Exception cause) {
try {
StringBuilder sb = new StringBuilder(message);
......@@ -191,7 +185,7 @@ class BescheidEventListener {
return sb.toString();
} catch (Exception e2) {
LOG.error("Error in building Error Message (sick).", e2);
return CREATE_BESCHEID_ERROR_MESSAGE;
return message;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment