<commit_before>package at.medevit.elexis.corona123.handler;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.format.DateTimeFormatter;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;

import at.medevit.elexis.corona123.preference.PreferenceConstants;
import ch.elexis.core.model.IContact;
import ch.elexis.core.model.ICoverage;
import ch.elexis.core.model.IOrganization;
import ch.elexis.core.model.IPatient;
import ch.elexis.core.model.IPerson;
import ch.elexis.core.services.holder.ConfigServiceHolder;
import ch.elexis.core.services.holder.ContextServiceHolder;
import ch.elexis.core.services.holder.CoreModelServiceHolder;
import ch.elexis.core.types.Gender;

public class UrlBuilder {
	
	public static String getPatientParameters(IPatient patient){
		String parameters = "";
		try {
			parameters += "firstName=" + URLEncoder.encode(patient.getFirstName(), "UTF-8");
			parameters += "&lastName=" + URLEncoder.encode(patient.getLastName(), "UTF-8");
			if (patient.getDateOfBirth() != null) {
				parameters += "&dateOfBirth=" + URLEncoder.encode(
					DateTimeFormatter.ofPattern("yyyy-MM-dd").format(patient.getDateOfBirth()),
					"UTF-8");
			}
			parameters +=
				"&gender=" + URLEncoder.encode(genderAsString(patient.getGender()), "UTF-8");
			
			if (patient.getCountry() != null) {
				parameters +=
					"&nationality=" + URLEncoder.encode(patient.getCountry().toString(), "UTF-8");
			}
			if (StringUtils.isNotBlank(patient.getStreet())) {
				parameters += "&street=" + URLEncoder.encode(patient.getStreet(), "UTF-8");
			}
			if (StringUtils.isNotBlank(patient.getZip())) {
				parameters += "&postalCode=" + URLEncoder.encode(patient.getZip(), "UTF-8");
			}
			if (StringUtils.isNotBlank(patient.getCity())) {
				parameters += "&city=" + URLEncoder.encode(patient.getCity(), "UTF-8");
			}
			
			if (StringUtils.isNotBlank(patient.getPhone1())) {
				parameters += "&phone=" + URLEncoder.encode(patient.getPhone1(), "UTF-8");
			} else if (StringUtils.isNotBlank(patient.getPhone2())) {
				parameters += "&phone=" + URLEncoder.encode(patient.getPhone2(), "UTF-8");
			}
			
			if (StringUtils.isNotBlank(patient.getEmail())) {
				parameters += "&email=" + URLEncoder.encode(patient.getEmail(), "UTF-8");
			}
			
			if (patient.getFamilyDoctor() != null) {
				IContact familyDoctor = patient.getFamilyDoctor();
				parameters += "&generalPractitioner="
					+ URLEncoder.encode(getContactNameWithTitle(familyDoctor),
					"UTF-8");
			} else if (ContextServiceHolder.get().getActiveMandator().isPresent()) {
				parameters +=
					"&generalPractitioner="
						+ URLEncoder.encode(
							getContactNameWithTitle(
								ContextServiceHolder.get().getActiveMandator().get()),
							"UTF-8");
			}
			
			String insuranceCardNumber = getInsuranceCardNumber(patient);
			if (StringUtils.isNotBlank(insuranceCardNumber)) {
				parameters +=
					"&identityNumberInsurance=" + URLEncoder.encode(insuranceCardNumber, "UTF-8");
			}
			
			String insurance = getInsurance(patient);
			if (StringUtils.isNotBlank(insurance)) {
				parameters += "&healthInsurance=" + URLEncoder.encode(insurance, "UTF-8");
			}
			
		} catch (UnsupportedEncodingException e) {
			LoggerFactory.getLogger(UrlBuilder.class).error("Error getting patient parameters", e);
		}
		return parameters;
	}
	
	public static String getVaccinationDefaultParameters(){
		String ret = "";
		try {
			ret += "&takesAnticoagulants=" + URLEncoder.encode("false", "UTF-8");
			ret += "&hasAllergies=" + URLEncoder.encode("false", "UTF-8");
			ret += "&isPregnant=" + URLEncoder.encode("false", "UTF-8");
			ret += "&isCurrentlyBreastfeeding=" + URLEncoder.encode("false", "UTF-8");
			ret += "&worksAsMedicalStaff=" + URLEncoder.encode("false", "UTF-8");
			ret += "&hasContactWithVulnerablePeople=" + URLEncoder.encode("false", "UTF-8");
			ret += "&livesInACommunityFacility=" + URLEncoder.encode("false", "UTF-8");
			ret += "&wantsVaccinationCertificate=" + URLEncoder.encode("false", "UTF-8");
		} catch (UnsupportedEncodingException e) {
			LoggerFactory.getLogger(UrlBuilder.class).error("Error getting patient parameters", e);
		}
		return ret;
	}
	
	private static String getInsurance(IPatient patient){
		String ret = "";
		Optional<ICoverage> activeCoverage = ContextServiceHolder.get().getActiveCoverage();
		if (activeCoverage.isPresent()) {
			ICoverage coverage = activeCoverage.get();
			if (coverage.isOpen()) {
				if (coverage.getCostBearer() != null
					&& coverage.getCostBearer().isOrganization()) {
					IOrganization costBearer = CoreModelServiceHolder.get()
						.load(coverage.getCostBearer().getId(), IOrganization.class).get();<commit_msg>[22554] add additional default parameters for corona123 vaccination form<commit_after>package at.medevit.elexis.corona123.handler;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.format.DateTimeFormatter;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;

import at.medevit.elexis.corona123.preference.PreferenceConstants;
import ch.elexis.core.model.IContact;
import ch.elexis.core.model.ICoverage;
import ch.elexis.core.model.IOrganization;
import ch.elexis.core.model.IPatient;
import ch.elexis.core.model.IPerson;
import ch.elexis.core.services.holder.ConfigServiceHolder;
import ch.elexis.core.services.holder.ContextServiceHolder;
import ch.elexis.core.services.holder.CoreModelServiceHolder;
import ch.elexis.core.types.Gender;

public class UrlBuilder {
	
	public static String getPatientParameters(IPatient patient){
		String parameters = "";
		try {
			parameters += "firstName=" + URLEncoder.encode(patient.getFirstName(), "UTF-8");
			parameters += "&lastName=" + URLEncoder.encode(patient.getLastName(), "UTF-8");
			if (patient.getDateOfBirth() != null) {
				parameters += "&dateOfBirth=" + URLEncoder.encode(
					DateTimeFormatter.ofPattern("yyyy-MM-dd").format(patient.getDateOfBirth()),
					"UTF-8");
			}
			parameters +=
				"&gender=" + URLEncoder.encode(genderAsString(patient.getGender()), "UTF-8");
			
			if (patient.getCountry() != null) {
				parameters +=
					"&nationality=" + URLEncoder.encode(patient.getCountry().toString(), "UTF-8");
			}
			if (StringUtils.isNotBlank(patient.getStreet())) {
				parameters += "&street=" + URLEncoder.encode(patient.getStreet(), "UTF-8");
			}
			if (StringUtils.isNotBlank(patient.getZip())) {
				parameters += "&postalCode=" + URLEncoder.encode(patient.getZip(), "UTF-8");
			}
			if (StringUtils.isNotBlank(patient.getCity())) {
				parameters += "&city=" + URLEncoder.encode(patient.getCity(), "UTF-8");
			}
			
			if (StringUtils.isNotBlank(patient.getPhone1())) {
				parameters += "&phone=" + URLEncoder.encode(patient.getPhone1(), "UTF-8");
			} else if (StringUtils.isNotBlank(patient.getPhone2())) {
				parameters += "&phone=" + URLEncoder.encode(patient.getPhone2(), "UTF-8");
			}
			
			if (StringUtils.isNotBlank(patient.getEmail())) {
				parameters += "&email=" + URLEncoder.encode(patient.getEmail(), "UTF-8");
			}
			
			if (patient.getFamilyDoctor() != null) {
				IContact familyDoctor = patient.getFamilyDoctor();
				parameters += "&generalPractitioner="
					+ URLEncoder.encode(getContactNameWithTitle(familyDoctor),
					"UTF-8");
			} else if (ContextServiceHolder.get().getActiveMandator().isPresent()) {
				parameters +=
					"&generalPractitioner="
						+ URLEncoder.encode(
							getContactNameWithTitle(
								ContextServiceHolder.get().getActiveMandator().get()),
							"UTF-8");
			}
			
			String insuranceCardNumber = getInsuranceCardNumber(patient);
			if (StringUtils.isNotBlank(insuranceCardNumber)) {
				parameters +=
					"&identityNumberInsurance=" + URLEncoder.encode(insuranceCardNumber, "UTF-8");
			}
			
			String insurance = getInsurance(patient);
			if (StringUtils.isNotBlank(insurance)) {
				parameters += "&healthInsurance=" + URLEncoder.encode(insurance, "UTF-8");
			}
			
		} catch (UnsupportedEncodingException e) {
			LoggerFactory.getLogger(UrlBuilder.class).error("Error getting patient parameters", e);
		}
		return parameters;
	}
	
	public static String getVaccinationDefaultParameters(){
		String ret = "";
		try {
			ret += "&takesAnticoagulants=" + URLEncoder.encode("false", "UTF-8");
			ret += "&hasAllergies=" + URLEncoder.encode("false", "UTF-8");
			ret += "&isPregnant=" + URLEncoder.encode("false", "UTF-8");
			ret += "&isCurrentlyBreastfeeding=" + URLEncoder.encode("false", "UTF-8");
			ret += "&worksAsMedicalStaff=" + URLEncoder.encode("false", "UTF-8");
			ret += "&hasContactWithVulnerablePeople=" + URLEncoder.encode("false", "UTF-8");
			ret += "&livesInACommunityFacility=" + URLEncoder.encode("false", "UTF-8");
			ret += "&wantsVaccinationCertificate=" + URLEncoder.encode("false", "UTF-8");
			
			ret += "&hadCovidInTheLast90Days=" + URLEncoder.encode("false", "UTF-8");
			ret += "&hadCovidVaccination=" + URLEncoder.encode("false", "UTF-8");
			ret += "&hasCurrentlyColdSymptoms=" + URLEncoder.encode("false", "UTF-8");
		} catch (UnsupportedEncodingException e) {
			LoggerFactory.getLogger(UrlBuilder.class).error("Error getting patient parameters", e);
		}
		return ret;
	}
	
	private static String getInsurance(IPatient patient){
		String ret = "";
		Optional<ICoverage> activeCoverage = ContextServiceHolder.get().getActiveCoverage();
		if (activeCoverage.isPresent()) {
			ICoverage coverage = activeCoverage.get();
			if (coverage.isOpen()) {
				if (coverage.getCostBearer() != null
					&& coverage.getCostBearer().isOrganization()) {
					IOrganization costBearer = CoreModelServiceHolder.get()
						.load(coverage.getCostBearer().getId(), IOrganization.class).get();
{'fraction_non_alphanumeric': 0.15913034602429316, 'fraction_numerical': 0.00847198121874043, 'mean_word_length': 4.777122641509434, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2148749', 'n_tokens_mistral': 3757, 'n_tokens_neox': 3199, 'n_words': 481}