package to.etc.domui.util.asyncdialog;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import to.etc.parallelrunner.IAsyncRunnable;
import to.etc.domui.logic.ILogicContext;
import to.etc.domui.logic.LogicContextImpl;
import to.etc.util.FileTool;
import to.etc.util.Progress;
import to.etc.webapp.query.QContextManager;
import to.etc.webapp.query.QDataContext;

/**
 * @author <a href="mailto:jal@etc.to">Frits Jalvingh</a>
 * Created on 31-10-17.
 */
abstract public class AbstractAsyncDialogTask implements IAsyncRunnable {
	@Nullable
	private QDataContext m_dc;

	@Nullable
	private LogicContextImpl m_lc;

	private volatile boolean m_done;

	abstract protected void execute(@NonNull Progress p) throws Exception;

	@Override
	public final void run(@NonNull Progress progress) throws Exception {
		try {
			execute(progress);
		} finally {
			m_done = true;
			FileTool.closeAll(m_dc);
			m_dc = null;
			m_lc = null;
		}
	}

	@NonNull
	protected QDataContext	dc() throws Exception {
		QDataContext dc = m_dc;
		if(null == dc) {
			if(isDone())
				throw new IllegalStateException("You cannot use the task context after completion. Use the page's instead");
			dc = m_dc = QContextManager.createUnmanagedContext();
		}
		return dc;
	}

	@NonNull
	protected ILogicContext lc() throws Exception {
		ILogicContext lc = m_lc;
		if(null == lc) {
			lc = m_lc = new LogicContextImpl(dc());
		}
		return lc;
	}

	private boolean isDone() {
		return m_done;
	}
}

@@ -555,16 +555,31 @@
 Runnable
+, AutoCloseable
  %7B%0A%09@Nul
@@ -1455,16 +1455,151 @@
 lc;%0A%09%7D%0A%0A
+%09@Override%0A%09public void close() throws Exception %7B%0A%09%09QDataContext dc = m_dc;%0A%09%09if(dc != null) %7B%0A%09%09%09m_dc = null;%0A%09%09%09dc.close();%0A%09%09%7D%0A%09%7D%0A%0A
 %09private

Make AbstractAsyncDialogTask closeable