<commit_before>package ch.elexis.core.ui.util;

import java.text.MessageFormat;
import java.time.LocalDate;
import java.util.Collections;
import java.util.Optional;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.slf4j.LoggerFactory;

import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.data.service.CodeElementServiceHolder;
import ch.elexis.core.data.service.ContextServiceHolder;
import ch.elexis.core.data.service.CoreModelServiceHolder;
import ch.elexis.core.data.service.StoreToStringServiceHolder;
import ch.elexis.core.model.IArticle;
import ch.elexis.core.model.IArticleDefaultSignature;
import ch.elexis.core.model.IBilled;
import ch.elexis.core.model.ICodeElement;
import ch.elexis.core.model.IEncounter;
import ch.elexis.core.model.IPrescription;
import ch.elexis.core.model.builder.IPrescriptionBuilder;
import ch.elexis.core.model.localarticle.Constants;
import ch.elexis.core.model.prescription.EntryType;
import ch.elexis.core.services.ICodeElementService.CodeElementTyp;
import ch.elexis.core.services.ICodeElementServiceContribution;
import ch.elexis.core.services.holder.BillingServiceHolder;
import ch.elexis.core.services.holder.MedicationServiceHolder;
import ch.elexis.core.ui.dialogs.PrescriptionSignatureTitleAreaDialog;
import ch.elexis.data.PersistentObject;
import ch.rgw.tools.Result;

public class CreatePrescriptionHelper {
	
	public static final String MEDICATION_SETTINGS_ALWAYS_SHOW_SIGNATURE_DIALOG =
		"medication/settings/alwaysShowSignatureDialog";
	
	public static final String MEDICATION_SETTINGS_SIGNATURE_STD_DISPENSATION =
		"medication/settings/signatureStdDispensation";
	
	public static final String MEDICATION_SETTINGS_DISPENSE_ARTIKELSTAMM_CONVERT =
		"medication/settings/artikelstammConvert";
	
	private IArticle article;
	private Shell parentShell;
	
	private boolean medicationTypeFix = false;
	
	public CreatePrescriptionHelper(IArticle article, Shell parentShell){
		this.article = article;
		this.parentShell = parentShell;
	}
	
	public void setMedicationTypeFix(boolean value){
		this.medicationTypeFix = value;
	}
	
	public void createPrescription(){
		Optional<IArticleDefaultSignature> defaultSignature =
			MedicationServiceHolder.get().getDefaultSignature(article);
		
		Optional<IArticleDefaultSignature> signature = Optional.empty();
		if (defaultSignature.isPresent()) {
			signature = defaultSignature;
			if (CoreHub.userCfg.get(MEDICATION_SETTINGS_ALWAYS_SHOW_SIGNATURE_DIALOG, false)) {
				signature = getSignatureWithDialog(defaultSignature);
			}
		} else {
			signature = getSignatureWithDialog(Optional.empty());
		}
		signature.ifPresent(s -> createPrescriptionFromSignature(s));
	}
	
	private Optional<IArticleDefaultSignature> getSignatureWithDialog(
		Optional<IArticleDefaultSignature> preSelectedSignature){
		PrescriptionSignatureTitleAreaDialog dialog =
			new PrescriptionSignatureTitleAreaDialog(parentShell, article);
		dialog.setMedicationTypeFix(medicationTypeFix);
		preSelectedSignature.ifPresent(s -> dialog.setSignature(s));
		if (dialog.open() != Dialog.OK) {
			return Optional.empty();
		}
		return Optional.of(dialog.getSignature());
	}
	
	public void createPrescriptionFromSignature(IArticleDefaultSignature signature){
		// create medication entry
		IPrescription prescription =
			new IPrescriptionBuilder(CoreModelServiceHolder.get(), ContextServiceHolder.get(),
				article, ContextServiceHolder.get().getActivePatient().get(),
				signature.getSignatureAsDosisString()).build();
		prescription.setDisposalComment(signature.getComment());
		prescription.setEntryType(signature.getMedicationType());
		// a new symptomatic medication can have a stop date
		if (EntryType.SYMPTOMATIC_MEDICATION.equals(signature.getMedicationType())
			&& signature.getEndDate() != null) {
			prescription.setDateTo(signature.getEndDate().atTime(23, 59, 59));
			prescription.setStopReason("Stop geplant");
		}
		// create dispensation entry
		if (signature.getDisposalType() != EntryType.RECIPE) {
			EntryType disposalType = signature.getDisposalType();
			if (disposalType == EntryType.SELF_DISPENSED) {
				selfDispense(prescription);
			}
		}
		CoreModelServiceHolder.get().save(prescription);
	}
	
	public void selfDispense(IPrescription prescription){
		// add article to consultation
		Optional<IEncounter> encounter = ContextServiceHolder.get().getTyped(IEncounter.class);
		if (encounter.isPresent()) {
			boolean isToday = encounter.get().getDate().equals(LocalDate.now());
			if (isToday) {<commit_msg>[17164] fixed setting prescription remark<commit_after>package ch.elexis.core.ui.util;

import java.text.MessageFormat;
import java.time.LocalDate;
import java.util.Collections;
import java.util.Optional;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.slf4j.LoggerFactory;

import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.data.service.CodeElementServiceHolder;
import ch.elexis.core.data.service.ContextServiceHolder;
import ch.elexis.core.data.service.CoreModelServiceHolder;
import ch.elexis.core.data.service.StoreToStringServiceHolder;
import ch.elexis.core.model.IArticle;
import ch.elexis.core.model.IArticleDefaultSignature;
import ch.elexis.core.model.IBilled;
import ch.elexis.core.model.ICodeElement;
import ch.elexis.core.model.IEncounter;
import ch.elexis.core.model.IPrescription;
import ch.elexis.core.model.builder.IPrescriptionBuilder;
import ch.elexis.core.model.localarticle.Constants;
import ch.elexis.core.model.prescription.EntryType;
import ch.elexis.core.services.ICodeElementService.CodeElementTyp;
import ch.elexis.core.services.ICodeElementServiceContribution;
import ch.elexis.core.services.holder.BillingServiceHolder;
import ch.elexis.core.services.holder.MedicationServiceHolder;
import ch.elexis.core.ui.dialogs.PrescriptionSignatureTitleAreaDialog;
import ch.elexis.data.PersistentObject;
import ch.rgw.tools.Result;

public class CreatePrescriptionHelper {
	
	public static final String MEDICATION_SETTINGS_ALWAYS_SHOW_SIGNATURE_DIALOG =
		"medication/settings/alwaysShowSignatureDialog";
	
	public static final String MEDICATION_SETTINGS_SIGNATURE_STD_DISPENSATION =
		"medication/settings/signatureStdDispensation";
	
	public static final String MEDICATION_SETTINGS_DISPENSE_ARTIKELSTAMM_CONVERT =
		"medication/settings/artikelstammConvert";
	
	private IArticle article;
	private Shell parentShell;
	
	private boolean medicationTypeFix = false;
	
	public CreatePrescriptionHelper(IArticle article, Shell parentShell){
		this.article = article;
		this.parentShell = parentShell;
	}
	
	public void setMedicationTypeFix(boolean value){
		this.medicationTypeFix = value;
	}
	
	public void createPrescription(){
		Optional<IArticleDefaultSignature> defaultSignature =
			MedicationServiceHolder.get().getDefaultSignature(article);
		
		Optional<IArticleDefaultSignature> signature = Optional.empty();
		if (defaultSignature.isPresent()) {
			signature = defaultSignature;
			if (CoreHub.userCfg.get(MEDICATION_SETTINGS_ALWAYS_SHOW_SIGNATURE_DIALOG, false)) {
				signature = getSignatureWithDialog(defaultSignature);
			}
		} else {
			signature = getSignatureWithDialog(Optional.empty());
		}
		signature.ifPresent(s -> createPrescriptionFromSignature(s));
	}
	
	private Optional<IArticleDefaultSignature> getSignatureWithDialog(
		Optional<IArticleDefaultSignature> preSelectedSignature){
		PrescriptionSignatureTitleAreaDialog dialog =
			new PrescriptionSignatureTitleAreaDialog(parentShell, article);
		dialog.setMedicationTypeFix(medicationTypeFix);
		preSelectedSignature.ifPresent(s -> dialog.setSignature(s));
		if (dialog.open() != Dialog.OK) {
			return Optional.empty();
		}
		return Optional.of(dialog.getSignature());
	}
	
	public void createPrescriptionFromSignature(IArticleDefaultSignature signature){
		// create medication entry
		IPrescription prescription =
			new IPrescriptionBuilder(CoreModelServiceHolder.get(), ContextServiceHolder.get(),
				article, ContextServiceHolder.get().getActivePatient().get(),
				signature.getSignatureAsDosisString()).build();
		prescription.setRemark(signature.getComment());
		prescription.setEntryType(signature.getMedicationType());
		// a new symptomatic medication can have a stop date
		if (EntryType.SYMPTOMATIC_MEDICATION.equals(signature.getMedicationType())
			&& signature.getEndDate() != null) {
			prescription.setDateTo(signature.getEndDate().atTime(23, 59, 59));
			prescription.setStopReason("Stop geplant");
		}
		// create dispensation entry
		if (signature.getDisposalType() != EntryType.RECIPE) {
			EntryType disposalType = signature.getDisposalType();
			if (disposalType == EntryType.SELF_DISPENSED) {
				selfDispense(prescription);
			}
		}
		CoreModelServiceHolder.get().save(prescription);
	}
	
	public void selfDispense(IPrescription prescription){
		// add article to consultation
		Optional<IEncounter> encounter = ContextServiceHolder.get().getTyped(IEncounter.class);
		if (encounter.isPresent()) {
			boolean isToday = encounter.get().getDate().equals(LocalDate.now());
			if (isToday) {
{'fraction_non_alphanumeric': 0.10020605140440299, 'fraction_numerical': 0.002060514044029932, 'mean_word_length': 6.30166270783848, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2657302', 'n_tokens_mistral': 2936, 'n_tokens_neox': 2741, 'n_words': 436}