package info.elexis.server.findings.fhir.jpa.model.service;

import java.time.LocalDate;

import javax.persistence.EntityManager;

import ch.rgw.tools.VersionedResource;
import info.elexis.server.core.connector.elexis.jpa.model.annotated.Behandlung;
import info.elexis.server.core.connector.elexis.jpa.model.annotated.Fall;
import info.elexis.server.core.connector.elexis.jpa.model.annotated.Kontakt;
import info.elexis.server.findings.fhir.jpa.model.annotated.Encounter;
import info.elexis.server.findings.fhir.jpa.model.service.internal.FindingsEntityManager;
import info.elexis.server.findings.fhir.jpa.service.FindingsFactory;

public class EncounterService extends AbstractService<Encounter> {

	public EncounterService() {
		super(Encounter.class);
	}

	@Override
	protected EntityManager getEntityManager() {
		return FindingsEntityManager.getEntityManager();
	}

	public EncounterModelAdapter createEncounter(Behandlung behandlung) {
		FindingsFactory factory = new FindingsFactory();
		EncounterModelAdapter encounter = (EncounterModelAdapter) factory.createEncounter();
		return updateEncounter(encounter, behandlung);
	}

	public EncounterModelAdapter updateEncounter(EncounterModelAdapter encounter, Behandlung behandlung) {
		encounter.setConsultationId(behandlung.getId());
		encounter.setServiceProviderId(behandlung.getMandant().getId());

		LocalDate encounterDate = behandlung.getDatum();
		if (encounterDate != null) {
			encounter.setEffectiveTime(encounterDate.atStartOfDay());
		}
		Fall fall = behandlung.getFall();
		if (fall != null) {
			Kontakt patient = fall.getPatientKontakt();
			if (patient != null) {
				encounter.setPatientId(patient.getId());
			}
		}

		VersionedResource vr = behandlung.getEintrag();
		if (vr != null) {
			String text = vr.getHead();
			encounter.setText(text);
		}

		write(encounter.getModel());
		return encounter;
	}
}

@@ -126,24 +126,66 @@
 Manager;%0D%0A%0D%0A
+import ch.elexis.core.text.model.Samdas;%0D%0A
 import ch.rg
@@ -1851,21 +1851,34 @@
 %09%09%09S
-tring text = 
+amdas samdas = new Samdas(
 vr.g
@@ -1885,16 +1885,17 @@
 etHead()
+)
 ;%0D%0A%09%09%09en
@@ -1910,20 +1910,38 @@
 setText(
-t
+samdas.getRecordT
 ext
+()
 );%0D%0A%09%09%7D%0D

use Samdas to parse generate text for narrative text