package ch.elexis.core.ui.startup;

import java.util.Collections;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchListener;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;

import ch.elexis.core.ui.services.LocalDocumentServiceHolder;

/**
 * Class for code executed on {@link IWorkbench} startup.
 * 
 * @author thomas
 *
 */
@Component(property = EventConstants.EVENT_TOPIC + "=" + UIEvents.UILifeCycle.APP_STARTUP_COMPLETE)
public class StartupHandler implements EventHandler {
	
	@Override
	public void handleEvent(Event event){
		PlatformUI.getWorkbench().addWorkbenchListener(new IWorkbenchListener() {
			@Override
			public boolean preShutdown(IWorkbench workbench, boolean forced){
				if (LocalDocumentServiceHolder.getService() != null
					&& LocalDocumentServiceHolder.getService().isPresent()
					&& !LocalDocumentServiceHolder.getService().get().getAll().isEmpty()) {
					ICommandService commandService = (ICommandService) PlatformUI.getWorkbench()
						.getService(ICommandService.class);
					Command command =
						commandService.getCommand("ch.elexis.core.ui.command.openLocalDocuments"); //$NON-NLS-1$
					
					ExecutionEvent event =
						new ExecutionEvent(command, Collections.EMPTY_MAP, this, null);
					try {
						command.executeWithChecks(event);
						return LocalDocumentServiceHolder.getService().get().getAll().isEmpty();
					} catch (ExecutionException | NotDefinedException | NotEnabledException
							| NotHandledException e) {
						MessageDialog.openError(
							PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
							Messages.UiStartup_errortitle,
							Messages.UiStartup_errormessage);
					}
				}
				return true;
			}
			
			@Override
			public void postShutdown(IWorkbench workbench){
				// nothing to do here
			}
		});
	}
}

@@ -29,16 +29,45 @@
 artup;%0A%0A
+import java.util.Collection;%0A
 import j
@@ -428,29 +428,256 @@
 .e4.
-ui.workbench.UIEvents
+core.contexts.IEclipseContext;%0Aimport org.eclipse.e4.ui.model.application.MApplication;%0Aimport org.eclipse.e4.ui.model.application.ui.basic.MPart;%0Aimport org.eclipse.e4.ui.workbench.UIEvents;%0Aimport org.eclipse.e4.ui.workbench.modeling.EPartService
 ;%0Aim
@@ -1380,56 +1380,341 @@
 %0A%09%0A%09
-@Override%0A%09public void handleEvent(Event event)%7B
+private static IEclipseContext applicationContext;%0A%09%0A%09@Override%0A%09public void handleEvent(Event event)%7B%0A%09%09Object property = event.getProperty(%22org.eclipse.e4.data%22);%0A%09%09if (property instanceof MApplication) %7B%0A%09%09%09MApplication application = (MApplication) property;%0A%09%09%09StartupHandler.applicationContext = application.getContext();%0A%09%09%7D%0A%09%09
 %0A%09%09P
@@ -2839,24 +2839,686 @@
 %09%09%09%09%7D%0A%09%09%09%09%7D%0A
+%09%09%09%09// reset dirty of closed compatibility parts that would open save resources dialog (redmine #20724)%0A%09%09%09%09if (StartupHandler.applicationContext != null%0A%09%09%09%09%09&& StartupHandler.applicationContext.get(EPartService.class) != null) %7B%0A%09%09%09%09%09EPartService partService =%0A%09%09%09%09%09%09StartupHandler.applicationContext.get(EPartService.class);%0A%09%09%09%09%09Collection%3CMPart%3E dirtyParts = partService.getDirtyParts();%0A%09%09%09%09%09if (!dirtyParts.isEmpty()) %7B%0A%09%09%09%09%09%09for (MPart mPart : dirtyParts) %7B%0A%09%09%09%09%09%09%09if (mPart.getObject() == null && mPart.getContributionURI()%0A%09%09%09%09%09%09%09%09.endsWith(%22internal.e4.compatibility.CompatibilityView%22)) %7B%0A%09%09%09%09%09%09%09%09mPart.setDirty(false);%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A
 %09%09%09%09return t

reset dirty flag of closed compatibility view on pre shutdown