<commit_before>/*
 * Copyright (C) 2003 EBI, GRL
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ensembl.healthcheck;

import java.sql.Connection;
import java.util.logging.Logger;

import org.ensembl.healthcheck.util.DBUtils;

/**
 * Container for information about a database that can be stored in a DatabaseRegistry.
 * 
 */
public class DatabaseRegistryEntry implements Comparable {

	private String name;

	private Species species;

	private String schemaVersion, geneBuildVersion;

	private DatabaseType type;

	private Connection con;

	/** The logger to use */
	private static Logger logger = Logger.getLogger("HealthCheckLogger");

	// -----------------------------------------------------------------
	/**
   * Create a new DatabaseRegistryEntry. A connection to the named database is also created if required.
   * 
   * @param name The name of the database.
   * @param species The species that this database represents. If null, guess it from name.
   * @param type The type of this databse. If null, guess it from name.
   * @param connect If true, open a connection to the database on the primary database server.
   */
	public DatabaseRegistryEntry(String name, Species species, DatabaseType type, boolean connect) {

		this.name = name;
		if (species != null) {
			this.species = species;
		} else {
			this.species = setSpeciesFromName(name);
		}
		if (type != null) {
			this.type = type;
		} else {
			this.type = setTypeFromName(name);
		}
		if (connect) {
			this.con = DBUtils.openConnection(System.getProperty("driver"),
																				System.getProperty("databaseURL") + name,
																				System.getProperty("user"),
																				System.getProperty("password"));
		}

	}

	// -----------------------------------------------------------------
	/**
   * Attempt to figure out species from database name. Also set schema and genebuild versions as appropriate.
   * 
   * @param name The name to use.
   * @return The species corresponding to name, or Species.UNKNOWN.
   */
	public final Species setSpeciesFromName(final String name) {

		Species result = Species.UNKNOWN;
		String[] bits = name.split("_");
		String alias;
		
		// there are many different possibilities for database naming; the most
		// likely are catered for here

		// homo_sapiens_core_20_34a
		if (bits.length >= 2) {
			alias = bits[0] + "_" + bits[1];
			if (bits.length == 5) {
				this.schemaVersion = bits[3];
				this.geneBuildVersion = bits[4];
			}
			if (Species.resolveAlias(alias) != Species.UNKNOWN) {
				return Species.resolveAlias(alias);
			}

		}

		// human_core_20, hsapiens_XXX
		if (bits.length > 1) {
			alias = bits[0];
			if (bits.length == 3) {
				this.schemaVersion = bits[2];
			}
			if (Species.resolveAlias(alias) != Species.UNKNOWN) {
				return Species.resolveAlias(alias);
			}

		}

		// compara, mart, go doesn't really have a species
		if (bits.length >= 2 && (bits[1].equalsIgnoreCase("compara") || bits[1].equalsIgnoreCase("go") || bits[1].equalsIgnoreCase("mart"))) {
			return Species.UNKNOWN;
		}

		// Vega naming convention e.g. vega_homo_sapiens_ext_20040821_v19
		if (bits.length > 3 && bits[0].equalsIgnoreCase("vega")) {
			alias = bits[1] + "_" + bits[2];
			if (Species.resolveAlias(alias) != Species.UNKNOWN) {
				return Species.resolveAlias(alias);
			}
		}

		// username_species_type_version_release
		if (bits.length > 3) {
			alias = bits[1] + "_" + bits[2];
			if (bits.length == 5) {
				this.schemaVersion = bits[3];
				this.geneBuildVersion = bits[4];
			}
			if (Species.resolveAlias(alias) != Species.UNKNOWN) {
				return Species.resolveAlias(alias);
			}

		}

		// ensembl help databases
		if (name.startsWith("ensembl_help_")) {

			return Species.HELP;

		}

		// system/reserved databases
		if (name.equals("mysql")) {

			return Species.SYSTEM;

		}

		// other permutations?

		if (result.equals(Species.UNKNOWN)) {
			logger.warning("Can't deduce species from database name " + name);
		}

		return result;

	}

	// -----------------------------------------------------------------
	/**
   * Attempt to figure out database type from database name.
   * 
   * @param name The database name to use.
   * @return The database type corresponding to name, or DatabaseType.UNKNOWN.
   */
	public final DatabaseType setTypeFromName(final String name) {

		DatabaseType result = DatabaseType.UNKNOWN;
		String[] bits = name.split("_");
		String alias;

		// there are many different possibilities for database naming; the most
		// likely are catered for here

		// homo_sapiens_core_20_34a
		if (bits.length >= 4) {
			alias = bits[2];
			if (DatabaseType.resolveAlias(alias) != DatabaseType.UNKNOWN) {
				return DatabaseType.resolveAlias(alias);
			}
		}

		// human_core_20, ensembl_compara_20_1
		if (bits.length >= 3) {
			alias = bits[1];
			if (DatabaseType.resolveAlias(alias) != DatabaseType.UNKNOWN) {
				return DatabaseType.resolveAlias(alias);
			}
		}

		// Vega naming convention e.g. vega_homo_sapiens_ext_20040821_v19
		if (bits.length > 3 && bits[0].equalsIgnoreCase("vega")) {
			return DatabaseType.VEGA;
		}

		// username_species_type_version_release
		if (bits.length > 3) {
			alias = bits[3];
			if (DatabaseType.resolveAlias(alias) != DatabaseType.UNKNOWN) {
				return DatabaseType.resolveAlias(alias);
			}<commit_msg>Fix bug when trying to the type for databases like homo_sapiens_core_expression_est_35_35h<commit_after>/*
 * Copyright (C) 2003 EBI, GRL
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ensembl.healthcheck;

import java.sql.Connection;
import java.util.logging.Logger;

import org.ensembl.healthcheck.util.DBUtils;

/**
 * Container for information about a database that can be stored in a DatabaseRegistry.
 * 
 */
public class DatabaseRegistryEntry implements Comparable {

	private String name;

	private Species species;

	private String schemaVersion, geneBuildVersion;

	private DatabaseType type;

	private Connection con;

	/** The logger to use */
	private static Logger logger = Logger.getLogger("HealthCheckLogger");

	// -----------------------------------------------------------------
	/**
   * Create a new DatabaseRegistryEntry. A connection to the named database is also created if required.
   * 
   * @param name The name of the database.
   * @param species The species that this database represents. If null, guess it from name.
   * @param type The type of this databse. If null, guess it from name.
   * @param connect If true, open a connection to the database on the primary database server.
   */
	public DatabaseRegistryEntry(String name, Species species, DatabaseType type, boolean connect) {

		this.name = name;
		if (species != null) {
			this.species = species;
		} else {
			this.species = setSpeciesFromName(name);
		}
		if (type != null) {
			this.type = type;
		} else {
			this.type = setTypeFromName(name);
		}
		if (connect) {
			this.con = DBUtils.openConnection(System.getProperty("driver"),
																				System.getProperty("databaseURL") + name,
																				System.getProperty("user"),
																				System.getProperty("password"));
		}

	}

	// -----------------------------------------------------------------
	/**
   * Attempt to figure out species from database name. Also set schema and genebuild versions as appropriate.
   * 
   * @param name The name to use.
   * @return The species corresponding to name, or Species.UNKNOWN.
   */
	public final Species setSpeciesFromName(final String name) {

		Species result = Species.UNKNOWN;
		String[] bits = name.split("_");
		String alias;
		
		// there are many different possibilities for database naming; the most
		// likely are catered for here

		// homo_sapiens_core_20_34a
		if (bits.length >= 2) {
			alias = bits[0] + "_" + bits[1];
			if (bits.length == 5) {
				this.schemaVersion = bits[3];
				this.geneBuildVersion = bits[4];
			}
			if (Species.resolveAlias(alias) != Species.UNKNOWN) {
				return Species.resolveAlias(alias);
			}

		}

		// human_core_20, hsapiens_XXX
		if (bits.length > 1) {
			alias = bits[0];
			if (bits.length == 3) {
				this.schemaVersion = bits[2];
			}
			if (Species.resolveAlias(alias) != Species.UNKNOWN) {
				return Species.resolveAlias(alias);
			}

		}

		// compara, mart, go doesn't really have a species
		if (bits.length >= 2 && (bits[1].equalsIgnoreCase("compara") || bits[1].equalsIgnoreCase("go") || bits[1].equalsIgnoreCase("mart"))) {
			return Species.UNKNOWN;
		}

		// Vega naming convention e.g. vega_homo_sapiens_ext_20040821_v19
		if (bits.length > 3 && bits[0].equalsIgnoreCase("vega")) {
			alias = bits[1] + "_" + bits[2];
			if (Species.resolveAlias(alias) != Species.UNKNOWN) {
				return Species.resolveAlias(alias);
			}
		}

		// username_species_type_version_release
		if (bits.length > 3) {
			alias = bits[1] + "_" + bits[2];
			if (bits.length == 5) {
				this.schemaVersion = bits[3];
				this.geneBuildVersion = bits[4];
			}
			if (Species.resolveAlias(alias) != Species.UNKNOWN) {
				return Species.resolveAlias(alias);
			}

		}

		// ensembl help databases
		if (name.startsWith("ensembl_help_")) {

			return Species.HELP;

		}

		// system/reserved databases
		if (name.equals("mysql")) {

			return Species.SYSTEM;

		}

		// other permutations?

		if (result.equals(Species.UNKNOWN)) {
			logger.warning("Can't deduce species from database name " + name);
		}

		return result;

	}

	// -----------------------------------------------------------------
	/**
   * Attempt to figure out database type from database name.
   * 
   * @param name The database name to use.
   * @return The database type corresponding to name, or DatabaseType.UNKNOWN.
   */
	public final DatabaseType setTypeFromName(final String name) {

		DatabaseType result = DatabaseType.UNKNOWN;
		String[] bits = name.split("_");
		String alias;

		// there are many different possibilities for database naming; the most
		// likely are catered for here

		// homo_sapiens_core_20_34a
		if (bits.length >= 4) {
			// Try to match homo_sapiens_core_expression_est_35_35h before
			alias = bits[3];
			if (DatabaseType.resolveAlias(alias) != DatabaseType.UNKNOWN) {
				return DatabaseType.resolveAlias(alias);
			}
			alias = bits[2];
			if (DatabaseType.resolveAlias(alias) != DatabaseType.UNKNOWN) {
				return DatabaseType.resolveAlias(alias);
			}
		}

		// human_core_20, ensembl_compara_20_1
		if (bits.length >= 3) {
			alias = bits[1];
			if (DatabaseType.resolveAlias(alias) != DatabaseType.UNKNOWN) {
				return DatabaseType.resolveAlias(alias);
			}
		}

		// Vega naming convention e.g. vega_homo_sapiens_ext_20040821_v19
		if (bits.length > 3 && bits[0].equalsIgnoreCase("vega")) {
			return DatabaseType.VEGA;
		}

		// username_species_type_version_release
		if (bits.length > 3) {
			alias = bits[3];
			if (DatabaseType.resolveAlias(alias) != DatabaseType.UNKNOWN) {
				return DatabaseType.resolveAlias(alias);
			}
{'fraction_non_alphanumeric': 0.1370797466298522, 'fraction_numerical': 0.01486113366899464, 'mean_word_length': 2.9433237271853985, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '5136315', 'n_tokens_mistral': 4152, 'n_tokens_neox': 3577, 'n_words': 1110}