package ch.elexis.core.ui.e4.locks;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Display;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.elexis.core.lock.types.LockResponse;
import ch.elexis.core.model.Identifiable;
import ch.elexis.core.services.holder.LocalLockServiceHolder;

public class AcquireLockBlockingUi {
	private static Logger logger = LoggerFactory.getLogger(AcquireLockBlockingUi.class);
	
	public static void aquireAndRun(Identifiable identifiable, ILockHandler handler){
		Display display = Display.getDefault();
		display.syncExec(new Runnable() {
			
			@Override
			public void run(){
				ProgressMonitorDialog progress =
					new ProgressMonitorDialog(display.getActiveShell());
				try {
					progress.run(true, true, new AcquireLockRunnable(identifiable, handler));
				} catch (InvocationTargetException | InterruptedException e) {
					logger.warn("Exception during acquire lock.", e);
				}
			}
		});
	}
	
	private static class AcquireLockRunnable implements IRunnableWithProgress {
		private Identifiable lockIdentifiable;
		
		private ILockHandler lockhander;
		private LockResponse result;
		
		public AcquireLockRunnable(Identifiable lockIdentifiable, ILockHandler lockhander){
			this.lockIdentifiable = lockIdentifiable;
			this.lockhander = lockhander;
		}
		
		@Override
		public void run(IProgressMonitor monitor)
			throws InvocationTargetException, InterruptedException{
				result =
					LocalLockServiceHolder.get().acquireLockBlocking(lockIdentifiable, 30, monitor);
			if (result != null) {
				Display display = Display.getDefault();
				if (result.isOk()) {
					display.syncExec(new Runnable() {
						@Override
						public void run(){
							lockhander.lockAcquired();
						}
					});
					monitor.beginTask("Releasing lock ...", IProgressMonitor.UNKNOWN);
					monitor.done();
				} else {
					display.syncExec(new Runnable() {
						@Override
						public void run(){
							lockhander.lockFailed();
							LockResponseHelper.showInfo(result, lockIdentifiable, logger);
						}
					});
				}
			}
		}
	}
}

@@ -434,16 +434,151 @@
 fiable;%0A
+import ch.elexis.core.services.IElexisServerService.ConnectionStatus;%0Aimport ch.elexis.core.services.holder.ElexisServerServiceHolder;%0A
 import c
@@ -840,16 +840,155 @@
 ndler)%7B%0A
+%09%09if (ElexisServerServiceHolder.get().getConnectionStatus() == ConnectionStatus.STANDALONE) %7B%0A%09%09%09handler.lockAcquired();%0A%09%09%09return;%0A%09%09%7D%0A%09%09%0A
 %09%09Displa
@@ -2296,16 +2296,85 @@
 KNOWN);%0A
+%09%09%09%09%09LocalLockServiceHolder.get().releaseLock(result.getLockInfo());%0A
 %09%09%09%09%09mon

Add missing lock release to e4.AcquireLockBlockingUi