<commit_before>package info.elexis.server.core.connector.elexis.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Optional;

import javax.xml.bind.JAXBException;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.elexis.core.common.DBConnection;
import ch.elexis.core.services.IElexisDataSource;
import ch.elexis.core.services.IModelService;
import ch.elexis.core.status.StatusUtil;
import info.elexis.server.core.common.util.CoreUtil;
import info.elexis.server.core.connector.elexis.internal.Activator;

public class ElexisDBConnectionUtil {

	private static DBConnection connection;
	private static Path connectionConfigPath;

	private static Logger log = LoggerFactory.getLogger(ElexisDBConnectionUtil.class);

	static {
		connectionConfigPath = CoreUtil.getHomeDirectory().resolve("elexis-connection.xml");
		if (connectionConfigPath.toFile().exists()) {
			try (InputStream is = Files.newInputStream(connectionConfigPath, StandardOpenOption.READ)) {
				connection = DBConnection.unmarshall(is);
				log.info("Initialized elexis connection from " + connectionConfigPath.toAbsolutePath());
				StatusUtil.logStatus(log, verifyConnection(connection));
			} catch (IOException | JAXBException e) {
				log.warn("Error opening " + connectionConfigPath.toAbsolutePath(), e);
			}
		}
	}

	public static Optional<DBConnection> getConnection() {
		return Optional.ofNullable(connection);
	}

	public static IStatus setConnection(IElexisDataSource elexisDataSource, DBConnection connection) {
		IStatus status = verifyConnection(connection);
		if (status.isOK()) {
			try {
				status = doSetConnection(elexisDataSource, connection);
			} catch (IOException | JAXBException | InterruptedException e) {
				return new Status(Status.ERROR, Activator.BUNDLE_ID, "Error setting database connection", e);<commit_msg>[15150] Log error on setDbConnection problem<commit_after>package info.elexis.server.core.connector.elexis.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Optional;

import javax.xml.bind.JAXBException;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.elexis.core.common.DBConnection;
import ch.elexis.core.services.IElexisDataSource;
import ch.elexis.core.services.IModelService;
import ch.elexis.core.status.StatusUtil;
import info.elexis.server.core.common.util.CoreUtil;
import info.elexis.server.core.connector.elexis.internal.Activator;

public class ElexisDBConnectionUtil {

	private static DBConnection connection;
	private static Path connectionConfigPath;

	private static Logger log = LoggerFactory.getLogger(ElexisDBConnectionUtil.class);

	static {
		connectionConfigPath = CoreUtil.getHomeDirectory().resolve("elexis-connection.xml");
		if (connectionConfigPath.toFile().exists()) {
			try (InputStream is = Files.newInputStream(connectionConfigPath, StandardOpenOption.READ)) {
				connection = DBConnection.unmarshall(is);
				log.info("Initialized elexis connection from " + connectionConfigPath.toAbsolutePath());
				StatusUtil.logStatus(log, verifyConnection(connection));
			} catch (IOException | JAXBException e) {
				log.warn("Error opening " + connectionConfigPath.toAbsolutePath(), e);
			}
		}
	}

	public static Optional<DBConnection> getConnection() {
		return Optional.ofNullable(connection);
	}

	public static IStatus setConnection(IElexisDataSource elexisDataSource, DBConnection connection) {
		IStatus status = verifyConnection(connection);
		if (status.isOK()) {
			try {
				status = doSetConnection(elexisDataSource, connection);
			} catch (IOException | JAXBException | InterruptedException e) {
				log.error("Error setting database connection", e);
				return new Status(Status.ERROR, Activator.BUNDLE_ID, "Error setting database connection", e);
{'fraction_non_alphanumeric': 0.10464078536367694, 'fraction_numerical': 0.002008032128514056, 'mean_word_length': 5.8547400611620795, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '1095689', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1312, 'n_words': 259}