<commit_before>package net.medshare.connector.viollier.handlers;

import static ch.elexis.core.constants.XidConstants.DOMAIN_AHV;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.elexis.core.constants.Elexis;
import ch.elexis.core.data.events.ElexisEventDispatcher;
import ch.elexis.core.exceptions.ElexisException;
import ch.elexis.data.Anschrift;
import ch.elexis.data.Fall;
import ch.elexis.data.Mandant;
import ch.elexis.data.Patient;
import ch.elexis.data.Query;
import ch.elexis.data.Xid;
import net.medshare.connector.viollier.Messages;
import net.medshare.connector.viollier.data.ViollierConnectorSettings;
import net.medshare.connector.viollier.ses.PortalCookieService;

public class CreateOrderHandler extends AbstractHandler {
	private static Logger log = LoggerFactory.getLogger(CreateOrderHandler.class);
	private static String DOMAIN_VIONR = "viollier.ch/vioNumber";
	private ViollierConnectorSettings mySettings;
	private String httpsUrl;
	
	/**
	 * Starte Labor Befundabfrage : <br>
	 * <ul>
	 * <li>HL7 Datei mit Patientendaten erstellen</li>
	 * <li>i/med Seite starten</li>
	 * </ul>
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException{
		String cookie = "";
		String vioNumber = "";
		String vorname = "";
		String name = "";
		String geburtsdatum = "";
		String gender = "";
		String address = "";
		String plz = "";
		String ort = "";
		String land = "";
		String socialSecurityNumber = ""; // AHV-Nummer
		String insuranceCardNumber = ""; // Versicherungskarte
		boolean hasVioNumber = true;
		
		Patient patient = ElexisEventDispatcher.getSelectedPatient();
		if (patient == null) {
			log.warn("No patient selected - exit CreateOrderHandler execution");
			MessageDialog.openError(new Shell(), Messages.Handler_errorTitleNoPatientSelected,
				Messages.Handler_errorMessageNoPatientSelected);
			return null;
		}
		vioNumber = getVioNr(patient);
		// wenn der Kunde keine VioNummer hat, Bestellung mit Patientendaten
		if (vioNumber.isEmpty()) {
			hasVioNumber = false;
			vorname = patient.getVorname();
			name = patient.getName();
			geburtsdatum = convertDate(patient.getGeburtsdatum());
			gender = parseGender(patient);
			Anschrift tempAdr = patient.getAnschrift();
			address = tempAdr.getStrasse();
			plz = tempAdr.getPlz();
			ort = tempAdr.getOrt();
			land = tempAdr.getLand();
			
			socialSecurityNumber = getSocialSecuritNumber(patient);
			insuranceCardNumber = getInsuranceCardNumber(patient);
		}
		
		// URL zum Direktaufruf von OrderIT
		mySettings =
			new ViollierConnectorSettings(
				(Mandant) ElexisEventDispatcher.getSelected(Mandant.class));
		httpsUrl = mySettings.getGlobalLoginUrl();
		// Cookie holen
		try {
			cookie = new PortalCookieService().getCookie();
			
		} catch (IOException e) {
			log.error("Error getting cookie", e);
			MessageDialog.openError(new Shell(), Messages.Handler_errorTitleGetCookie,
				Messages.Handler_errorMessageGetCookie + e.getMessage());
		} catch (ElexisException e) {
			log.error("No password/user defined", e);
			MessageDialog.openError(new Shell(), Messages.Handler_errorTitleGetCookie,
				e.getMessage());
		}
		httpsUrl += "&RCSession=" + cookie;
		try {
			httpsUrl += "&appPath=" + URLEncoder.encode("/orderit/createOrderFromGP?", "UTF-8");
			// falls VioNummer bekannt
			if (hasVioNumber) {
				httpsUrl += URLEncoder.encode("vioNumber=" + vioNumber, "UTF-8");
			}
			// sonst Patientendaten mitgeben
			else {
				httpsUrl += URLEncoder.encode("firstname=" + vorname, "UTF-8");
				httpsUrl += URLEncoder.encode("&surname=" + name, "UTF-8");
				httpsUrl += URLEncoder.encode("&dateOfBirth=" + geburtsdatum, "UTF-8");
				httpsUrl += URLEncoder.encode("&gender=" + gender, "UTF-8");
				httpsUrl += URLEncoder.encode("&address=" + address, "UTF-8");
				httpsUrl += URLEncoder.encode("&zip=" + plz, "UTF-8");
				httpsUrl += URLEncoder.encode("&city=" + ort, "UTF-8");
				httpsUrl += URLEncoder.encode("&country=" + land, "UTF-8");
				httpsUrl += URLEncoder.encode("&patientReference=" + patient.getPatCode(), "UTF-8");
				if (StringUtils.isNotBlank(socialSecurityNumber)) {
					httpsUrl +=
						URLEncoder.encode("&socialSecurityNumber=" + socialSecurityNumber, "UTF-8");
				}<commit_msg>[21184] add mobile number if present to viollier connector url<commit_after>package net.medshare.connector.viollier.handlers;

import static ch.elexis.core.constants.XidConstants.DOMAIN_AHV;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.elexis.core.constants.Elexis;
import ch.elexis.core.data.events.ElexisEventDispatcher;
import ch.elexis.core.exceptions.ElexisException;
import ch.elexis.data.Anschrift;
import ch.elexis.data.Fall;
import ch.elexis.data.Mandant;
import ch.elexis.data.Patient;
import ch.elexis.data.Query;
import ch.elexis.data.Xid;
import net.medshare.connector.viollier.Messages;
import net.medshare.connector.viollier.data.ViollierConnectorSettings;
import net.medshare.connector.viollier.ses.PortalCookieService;

public class CreateOrderHandler extends AbstractHandler {
	private static Logger log = LoggerFactory.getLogger(CreateOrderHandler.class);
	private static String DOMAIN_VIONR = "viollier.ch/vioNumber";
	private ViollierConnectorSettings mySettings;
	private String httpsUrl;
	
	/**
	 * Starte Labor Befundabfrage : <br>
	 * <ul>
	 * <li>HL7 Datei mit Patientendaten erstellen</li>
	 * <li>i/med Seite starten</li>
	 * </ul>
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException{
		String cookie = "";
		String vioNumber = "";
		String vorname = "";
		String name = "";
		String geburtsdatum = "";
		String gender = "";
		String address = "";
		String plz = "";
		String ort = "";
		String land = "";
		String socialSecurityNumber = ""; // AHV-Nummer
		String insuranceCardNumber = ""; // Versicherungskarte
		boolean hasVioNumber = true;
		
		Patient patient = ElexisEventDispatcher.getSelectedPatient();
		if (patient == null) {
			log.warn("No patient selected - exit CreateOrderHandler execution");
			MessageDialog.openError(new Shell(), Messages.Handler_errorTitleNoPatientSelected,
				Messages.Handler_errorMessageNoPatientSelected);
			return null;
		}
		vioNumber = getVioNr(patient);
		// wenn der Kunde keine VioNummer hat, Bestellung mit Patientendaten
		if (vioNumber.isEmpty()) {
			hasVioNumber = false;
			vorname = patient.getVorname();
			name = patient.getName();
			geburtsdatum = convertDate(patient.getGeburtsdatum());
			gender = parseGender(patient);
			Anschrift tempAdr = patient.getAnschrift();
			address = tempAdr.getStrasse();
			plz = tempAdr.getPlz();
			ort = tempAdr.getOrt();
			land = tempAdr.getLand();
			
			socialSecurityNumber = getSocialSecuritNumber(patient);
			insuranceCardNumber = getInsuranceCardNumber(patient);
		}
		
		// URL zum Direktaufruf von OrderIT
		mySettings =
			new ViollierConnectorSettings(
				(Mandant) ElexisEventDispatcher.getSelected(Mandant.class));
		httpsUrl = mySettings.getGlobalLoginUrl();
		// Cookie holen
		try {
			cookie = new PortalCookieService().getCookie();
			
		} catch (IOException e) {
			log.error("Error getting cookie", e);
			MessageDialog.openError(new Shell(), Messages.Handler_errorTitleGetCookie,
				Messages.Handler_errorMessageGetCookie + e.getMessage());
		} catch (ElexisException e) {
			log.error("No password/user defined", e);
			MessageDialog.openError(new Shell(), Messages.Handler_errorTitleGetCookie,
				e.getMessage());
		}
		httpsUrl += "&RCSession=" + cookie;
		try {
			httpsUrl += "&appPath=" + URLEncoder.encode("/orderit/createOrderFromGP?", "UTF-8");
			// falls VioNummer bekannt
			if (hasVioNumber) {
				httpsUrl += URLEncoder.encode("vioNumber=" + vioNumber, "UTF-8");
			}
			// sonst Patientendaten mitgeben
			else {
				httpsUrl += URLEncoder.encode("firstname=" + vorname, "UTF-8");
				httpsUrl += URLEncoder.encode("&surname=" + name, "UTF-8");
				httpsUrl += URLEncoder.encode("&dateOfBirth=" + geburtsdatum, "UTF-8");
				httpsUrl += URLEncoder.encode("&gender=" + gender, "UTF-8");
				httpsUrl += URLEncoder.encode("&address=" + address, "UTF-8");
				httpsUrl += URLEncoder.encode("&zip=" + plz, "UTF-8");
				httpsUrl += URLEncoder.encode("&city=" + ort, "UTF-8");
				httpsUrl += URLEncoder.encode("&country=" + land, "UTF-8");
				httpsUrl += URLEncoder.encode("&patientReference=" + patient.getPatCode(), "UTF-8");
				if (StringUtils.isNotBlank(patient.get(Patient.FLD_MOBILEPHONE))) {
					httpsUrl += URLEncoder
						.encode("&mobileNumber=" + patient.get(Patient.FLD_MOBILEPHONE), "UTF-8");
				}
				if (StringUtils.isNotBlank(socialSecurityNumber)) {
					httpsUrl +=
						URLEncoder.encode("&socialSecurityNumber=" + socialSecurityNumber, "UTF-8");
				}
{'fraction_non_alphanumeric': 0.12998582709050416, 'fraction_numerical': 0.003644462441789836, 'mean_word_length': 4.74026728646136, 'pattern_counts': {'":': 0, '<': 17, '<?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': '3234498', 'n_tokens_mistral': 3573, 'n_tokens_neox': 3207, 'n_words': 600}