Skip to content
Snippets Groups Projects

Ozg 7037 vorgang an dms

Merged Felix Reichenbach requested to merge OZG-7037-Vorgang-an-DMS into main
Files
36
@@ -21,7 +21,7 @@
* Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen.
*/
package de.ozgcloud.archive.vorgang;
package de.ozgcloud.archive.archivierung;
import java.util.function.Consumer;
import java.util.function.Predicate;
@@ -34,48 +34,81 @@ import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Component;
import de.ozgcloud.archive.ArchiveManagerConfiguration;
import de.ozgcloud.archive.attributes.ClientAttributeService;
import de.ozgcloud.archive.common.callcontext.CallContextUser;
import de.ozgcloud.archive.common.callcontext.CurrentUserService;
import de.ozgcloud.archive.common.command.CommandService;
import de.ozgcloud.archive.common.errorhandling.TimeoutException;
import de.ozgcloud.archive.vorgang.VorgangService;
import de.ozgcloud.command.Command;
import de.ozgcloud.command.CommandCreatedEvent;
import de.ozgcloud.command.CommandFailedEvent;
import de.ozgcloud.command.VorgangLockedEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
@Component()
@Component
@RequiredArgsConstructor
@Log4j2
class ArchiveEventListener {
public class ArchiveEventListener {
static final int MAXIMUM_CHECKS_FOR_PENDING_COMMANDS = 3;
static final int WAIT_INTERVAL = 30 * 1000;
private static final String LOG_MESSAGE_TEMPLATE = "{}. Command failed.";
private static final String ERROR_MESSAGE_TEMPLATE = "Error on executing %s Command (id: %s).";
static final String ARCHIVE_VORGANG_ORDER = "ARCHIVE_VORGANG";
private static final String IS_ARCHIVE_VORGANG_EVENT =
"{T(de.ozgcloud.archive.vorgang.ArchiveEventListener).IS_ARCHIVE_VORGANG_COMMAND.test(event.getSource())}";
private static final String IS_ARCHIVE_VORGANG_EVENT = "{T(de.ozgcloud.archive.archivierung.ArchiveEventListener).IS_ARCHIVE_VORGANG_COMMAND.test(event.getSource())}";
public static final Predicate<Command> IS_ARCHIVE_VORGANG_COMMAND = command -> ARCHIVE_VORGANG_ORDER.equals(command.getOrder());
private static final String IS_LOCKED_BY_ARCHIVE_MANAGER_EVENT = "{T(de.ozgcloud.archive.archivierung.ArchiveEventListener)."
+ "IS_LOCK_BY_ARCHIVE_MANAGER_COMMAND.test(event.getCommand())}";
public static final Predicate<Command> IS_LOCK_BY_ARCHIVE_MANAGER_COMMAND = command -> CallContextUser.ARCHIVE_MANAGER_CLIENT_NAME
.equals(command.getCreatedByClientName());
@Qualifier(ArchiveManagerConfiguration.CURRENT_USER_SERVICE_NAME) // NOSONAR
private final CurrentUserService currentUserService;
@Qualifier(ArchiveManagerConfiguration.CLIENT_ATTRIBUTE_SERVICE_NAME) // NOSONAR
private final ClientAttributeService clientAttributeService;
@Qualifier(ArchiveManagerConfiguration.VORGANG_SERVICE_NAME) // NOSONAR
private final VorgangService vorgangService;
private final ApplicationEventPublisher eventPublisher;
@Qualifier(ArchiveManagerConfiguration.COMMAND_SERVICE_NAME) // NOSONAR
private final CommandService commandService;
private final ArchiveService archiveService;
@EventListener(condition = IS_ARCHIVE_VORGANG_EVENT)
void onArchiveVorgangEvent(CommandCreatedEvent event) {
runWithSecurityContext(event.getSource(), this::doLockVorgang);
}
void doLockVorgang(Command command) {
clientAttributeService.setVorgangArchiving(command.getVorgangId());
archiveService.setVorgangArchiving(command.getVorgangId());
vorgangService.lockVorgang(command);
}
@EventListener(condition = IS_LOCKED_BY_ARCHIVE_MANAGER_EVENT)
public void onVorgangLockedEvent(VorgangLockedEvent event) {
waitForPendingCommandsToFinish(event.getCommand().getVorgangId(), WAIT_INTERVAL);
runWithSecurityContext(event.getCommand(), archiveService::archiveVorgang);
}
void waitForPendingCommandsToFinish(String vorgangId, long waitIntervalInMillis) {
var numberOfAttempts = 0;
while (commandService.hasPendingCommandsExceptWithOrder(vorgangId, ARCHIVE_VORGANG_ORDER)) {
numberOfAttempts++;
if (numberOfAttempts >= MAXIMUM_CHECKS_FOR_PENDING_COMMANDS) {
throw new TimeoutException("Waiting for pending commands");
}
try {
Thread.sleep(waitIntervalInMillis);
} catch (InterruptedException e) {
LOG.error("Error while waiting for commands to finish.", e);
Thread.currentThread().interrupt();
}
}
}
void runWithSecurityContext(Command command, Consumer<Command> commandExecutor) {
SecurityContext prevContext = null;
try {
Loading