package at.medevit.elexis.impfplan.ui.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import at.medevit.elexis.impfplan.model.po.Vaccination;
import at.medevit.elexis.impfplan.ui.dialogs.SupplementVaccinationDialog;
import ch.elexis.core.data.events.ElexisEventDispatcher;
import ch.elexis.core.ui.UiDesk;
import ch.elexis.data.Artikel;
import ch.elexis.data.Patient;
import ch.elexis.data.PersistentObjectFactory;
import ch.rgw.tools.TimeTool;

public class SupplementVaccinationHandler extends AbstractHandler {
	private static Logger logger = LoggerFactory.getLogger(SupplementVaccinationHandler.class);
	
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException{
		Patient sp = ElexisEventDispatcher.getSelectedPatient();
		if(sp==null) {
			return null;
		}
		SupplementVaccinationDialog svd = new SupplementVaccinationDialog(UiDesk.getTopShell(), sp);
		int retVal = svd.open();
		if (retVal == TitleAreaDialog.OK) {
			String patientId = sp.getId();
			String administratorString = svd.getAdministratorString();
			String lotNo = svd.getLotNo();
			TimeTool doa = svd.getDateOfAdministration();
			String articleString = svd.getArticleString();
			Artikel art = (Artikel) new PersistentObjectFactory().createFromString(articleString);
			
			if (art != null) {
				new Vaccination(patientId, art, doa.getTime(), lotNo, administratorString);
			} else {
				Vaccination v =
					new Vaccination(patientId, null, articleString, null, null, doa.getTime(),
						lotNo, administratorString);
				v.setVaccAgainst(svd.getVaccAgainst());
			}
			logger.debug("Supplement vaccination: " + articleString + " added");
		}
		return null;
	}
}

@@ -515,17 +515,22 @@
 ore.
-ui.UiDesk
+model.IArticle
 ;%0Aim
@@ -548,20 +548,97 @@
 xis.
-data.Artikel
+core.model.Identifiable;%0Aimport ch.elexis.core.services.holder.StoreToStringServiceHolder
 ;%0Aim
@@ -656,20 +656,22 @@
 xis.
-data.Patient
+core.ui.UiDesk
 ;%0Aim
@@ -691,38 +691,22 @@
 s.data.P
-ersistentObjectFactory
+atient
 ;%0Aimport
@@ -1468,68 +1468,68 @@
 %0A%09%09%09
-Artikel art = (Artikel) new PersistentObjectFactory().create
+Identifiable art =%0A%09%09%09%09StoreToStringServiceHolder.get().load
 From
@@ -1545,24 +1545,37 @@
 ticleString)
+.orElse(null)
 ;%0A%09%09%09%0A%09%09%09if 
@@ -1583,53 +1583,180 @@
 art 
-!= null) %7B%0A%09%09%09%09new Vaccination(patientId, art
+instanceof IArticle) %7B%0A%09%09%09%09IArticle article = (IArticle) art;%0A%09%09%09%09new Vaccination(patientId, articleString, article.getLabel(), article.getGtin(),%0A%09%09%09%09%09article.getAtcCode()
 , do

use store to string service to load selected article