/*******************************************************************************
 * Copyright (c) 2019 IT-Med AG <info@it-med-ag.ch>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IT-Med AG <info@it-med-ag.ch> - initial implementation
 ******************************************************************************/

package ch.itmed.fop.printing.handlers;

import javax.inject.Inject;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.e4.core.commands.ECommandService;
import org.eclipse.e4.core.commands.EHandlerService;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

import ch.elexis.core.data.events.ElexisEventDispatcher;
import ch.elexis.core.data.util.NoPoUtil;
import ch.elexis.core.model.Identifiable;
import ch.elexis.core.ui.util.CoreUiUtil;
import ch.elexis.data.PersistentObject;

@SuppressWarnings("restriction")
public final class SelectedAppointmentCardHandler extends AbstractHandler {
	
	@Inject
	private ECommandService commandService;
	
	@Inject
	private EHandlerService handlerService;
	
	public SelectedAppointmentCardHandler(){
		CoreUiUtil.injectServices(this);
	}
	
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		// transfer selection to elexis event dispatcher
		IStructuredSelection currentSelection = HandlerUtil.getCurrentStructuredSelection(event);
		if (!currentSelection.isEmpty()) {
			ElexisEventDispatcher.fireSelectionEvent(getAsPersistentObject(currentSelection));
		}
		// call default command
		ParameterizedCommand cmd =
			commandService.createCommand("ch.itmed.fop.printing.command.AppointmentCardPrint",
				null);
		if (cmd != null) {
			handlerService.executeHandler(cmd);
		}
		return null;
	}
	
	private PersistentObject getAsPersistentObject(IStructuredSelection currentSelection){
		PersistentObject ret = null;
		if (currentSelection.getFirstElement() instanceof PersistentObject) {
			ret = (PersistentObject) currentSelection.getFirstElement();
		} else if (currentSelection.getFirstElement() instanceof Identifiable) {
			ret =
				NoPoUtil.loadAsPersistentObject((Identifiable) currentSelection.getFirstElement());
		}
		return ret;
	}
}
@@ -582,37 +582,8 @@
 s;%0A%0A
-import javax.inject.Inject;%0A%0A
 impo
@@ -763,36 +763,35 @@
 ommands.
-ParameterizedCommand
+NotEnabledException
 ;%0Aimport
@@ -795,35 +795,32 @@
 ort org.eclipse.
-e4.
 core.commands.EC
@@ -817,31 +817,35 @@
 ommands.
-ECommandService
+NotHandledException
 ;%0Aimport
@@ -861,11 +861,8 @@
 pse.
-e4.
 core
@@ -871,31 +871,42 @@
 ommands.
-EHandlerService
+common.NotDefinedException
 ;%0Aimport
@@ -997,16 +997,96 @@
 lerUtil;
+%0Aimport org.eclipse.ui.handlers.IHandlerService;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -1250,26 +1250,25 @@
 ui.util.
-CoreUiUtil
+SWTHelper
 ;%0Aimport
@@ -1305,41 +1305,57 @@
 ct;%0A
-%0A@SuppressWarnings(%22restriction%22)
+import ch.itmed.fop.printing.resources.Messages;%0A
 %0Apub
@@ -1435,275 +1435,213 @@
 %09%0A%09@
-Inject%0A%09private ECommandService commandService;%0A%09%0A%09@Inject%0A%09private EHandlerService handlerService;%0A%09%0A%09public SelectedAppointmentCardHandler()%7B%0A%09%09CoreUiUtil.injectServices(this);%0A%09%7D%0A%09%0A%09@Override%0A%09public Object execute(ExecutionEvent event) throws ExecutionException %7B
+Override%0A%09public Object execute(ExecutionEvent event) throws ExecutionException %7B%0A%09%09IHandlerService handlerService =%0A%09%09%09(IHandlerService) HandlerUtil.getActiveSite(event).getService(IHandlerService.class);
 %0A%09%09/
@@ -1939,53 +1939,32 @@
 d%0A%09%09
-ParameterizedCommand cmd =%0A%09%09%09command
+try %7B%0A%09%09%09handler
 Service.
 crea
@@ -1963,12 +1963,13 @@
 ice.
-crea
+execu
 teCo
@@ -2045,63 +2045,266 @@
 ;%0A%09%09
-if (cmd != null) %7B%0A%09%09%09handlerService.executeHandler(cmd
+%7D catch (ExecutionException %7C NotDefinedException %7C NotEnabledException%0A%09%09%09%09%7C NotHandledException e) %7B%0A%09%09%09SWTHelper.showError(Messages.DefaultError_Title, Messages.DefaultError_Message);%0A%09%09%09LoggerFactory.getLogger(getClass()).error(e.getLocalizedMessage(), e
 );%0A%09

use e3 handler service to execute default appointment card print