<commit_before>package com.specmate.migration.internal.services;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.emf.cdo.common.model.EMFUtil;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EPackage.Registry;
import org.eclipse.emf.ecore.impl.EPackageRegistryImpl;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;

import com.specmate.common.SpecmateException;
import com.specmate.migration.api.IMigrator;
import com.specmate.migration.api.IMigratorService;
import com.specmate.persistency.IPackageProvider;
import com.specmate.persistency.cdo.config.CDOPersistenceConfig;

@Component
public class MigratorService implements IMigratorService {

	private static final int MIGRATOR_TIMEOUT = 1000;
	private static final String TABLE_PACKAGE_UNITS = "CDO_PACKAGE_UNITS";
	private static final String TABLE_PACKAGE_INFOS = "CDO_PACKAGE_INFOS";
	private static final String TABLE_EXTERNAL_REFS = "CDO_EXTERNAL_REFS";

	private Connection connection;
	private ConfigurationAdmin configurationAdmin;
	private LogService logService;

	private Pattern versionPattern = Pattern.compile("http://specmate.com/(\\d+)/.*");
	private Pattern databaseNotFoundPattern = Pattern.compile(".*Database \\\".*\\\" not found.*", Pattern.DOTALL);

	private IPackageProvider packageProvider;
	private BundleContext context;

	@Activate
	public void activate(BundleContext context) throws SpecmateException {
		this.context = context;
	}

	private void initiateDBConnection() throws SpecmateException {
		try {
			DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String jdbcConnection = "";

		try {
			Dictionary<String, Object> properties = configurationAdmin.getConfiguration(CDOPersistenceConfig.PID)
					.getProperties();

			jdbcConnection = (String) properties.get(CDOPersistenceConfig.KEY_JDBC_CONNECTION);
			this.connection = DriverManager.getConnection(jdbcConnection, "", "");
		} catch (SQLException e) {
			throw new SpecmateException("Migration: Could not connect to the database using the connection: "
					+ jdbcConnection + ". " + e.getMessage());
		} catch (IOException e) {
			throw new SpecmateException("Migration: Could not obtain database configuration.", e);
		}
	}

	private void closeConnection() throws SpecmateException {
		if (connection != null) {
			try {
				connection.close();
				connection = null;
			} catch (SQLException e) {
				throw new SpecmateException("Migration: Could not close connection.");
			}
		}
	}

	@Override
	public boolean needsMigration() throws SpecmateException {
		return false;

		// try {
		// initiateDBConnection();
		// } catch (SpecmateException e) {
		// // In development, when specmate or the tests are run for the first
		// // time, no database exists (neither on the
		// // file system, nor in memory). There is no sane way to check if a
		// // database exists, except by connecting
		// // to it. In case it does not exist, an SQL exception is thrown.
		// // While in all other possible error cases
		// // we want the client to handle the error, in the situation that the
		// // database does not exist, we want
		// // specmate to continue, without performing a migration, because the
		// // next step CDO performs is to create
		// // the database.
		// Matcher matcher = databaseNotFoundPattern.matcher(e.getMessage());
		// if (matcher.matches()) {
		// return false;
		// } else {
		// throw e;
		// }
		// }
		//
		// try {
		// String currentVersion = getCurrentModelVersion();
		// if (currentVersion == null) {
		// throw new SpecmateException("Migration: Could not determine currently
		// deployed model version");
		// }
		// String targetVersion = getTargetModelVersion();
		// if (targetVersion == null) {
		// throw new SpecmateException("Migration: Could not determine target
		// model version");
		// }
		//
		// boolean needsMigration = !currentVersion.equals(targetVersion);
		//
		// if (needsMigration) {
		// logService.log(LogService.LOG_INFO,
		// "Migration needed. Current version: " + currentVersion + " / Target
		// version: " + targetVersion);
		// } else {
		// logService.log(LogService.LOG_INFO, "No migration needed. Current
		// version: " + currentVersion);
		// }
		//
		// return needsMigration;
		// } finally {
		// closeConnection();
		// }
	}

	@Override<commit_msg>Integrated Oracle DB in migrator service<commit_after>package com.specmate.migration.internal.services;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.emf.cdo.common.model.EMFUtil;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EPackage.Registry;
import org.eclipse.emf.ecore.impl.EPackageRegistryImpl;
import org.h2.Driver;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;

import com.specmate.common.SpecmateException;
import com.specmate.migration.api.IMigrator;
import com.specmate.migration.api.IMigratorService;
import com.specmate.persistency.IPackageProvider;
import com.specmate.persistency.cdo.config.CDOPersistenceConfig;

@Component
public class MigratorService implements IMigratorService {

	private static final int MIGRATOR_TIMEOUT = 1000;
	private static final String TABLE_PACKAGE_UNITS = "CDO_PACKAGE_UNITS";
	private static final String TABLE_PACKAGE_INFOS = "CDO_PACKAGE_INFOS";
	private static final String TABLE_EXTERNAL_REFS = "CDO_EXTERNAL_REFS";

	private Connection connection;
	private ConfigurationAdmin configurationAdmin;
	private LogService logService;

	private Pattern versionPattern = Pattern.compile("http://specmate.com/(\\d+)/.*");
	private Pattern databaseNotFoundPattern = Pattern.compile(".*Database \\\".*\\\" not found.*", Pattern.DOTALL);

	private IPackageProvider packageProvider;
	private BundleContext context;
	private Dictionary<String, Object> configProperties;
	private boolean isVirginDB;

	@Activate
	public void activate(BundleContext context) throws SpecmateException {
		this.context = context;
		this.isVirginDB = false;
		try {
			configProperties = configurationAdmin.getConfiguration(CDOPersistenceConfig.PID).getProperties();
		} catch (IOException e) {
			throw new SpecmateException("Migration: Could not obtain database configuration.", e);
		}
	}

	private void initiateDBConnection() throws SpecmateException {
		String dbType = (String) configProperties.get(CDOPersistenceConfig.KEY_DB_TYPE);
		if (dbType.equals(CDOPersistenceConfig.DB_H2)) {
			try {
				initiateDBConnectionH2();
				// In development, when specmate or the tests are run for the
				// first time, no database exists (neither on the
				// file system, nor in memory). There is no sane way to check if
				// a database exists, except by connecting
				// to it. In case it does not exist, an SQL exception is thrown.
				// While in all other possible error cases
				// we want the client to handle the error, in the situation that
				// the database does not exist, we want
				// specmate to continue, without performing a migration, because
				// the next step CDO performs is to create the database.
			} catch (SpecmateException e) {
				Matcher matcher = databaseNotFoundPattern.matcher(e.getMessage());
				if (matcher.matches()) {
					this.isVirginDB = true;
				} else {
					throw e;
				}
			}
		} else if (dbType.equals(CDOPersistenceConfig.DB_ORACLE)) {
			initiateDBConnectionOracle();
			try {
				PreparedStatement stmt = this.connection.prepareStatement("select * from CDO_PACKAGE_INFOS");
				stmt.execute();
			} catch (SQLException e) {
				isVirginDB = true;
			}
		}
	}

	private void initiateDBConnectionH2() throws SpecmateException {
		Class<Driver> h2driver = org.h2.Driver.class;

		String jdbcConnection = "";

		try {
			jdbcConnection = (String) configProperties.get(CDOPersistenceConfig.KEY_JDBC_CONNECTION);
			this.connection = DriverManager.getConnection(jdbcConnection + ";IFEXISTS=TRUE", "", "");
		} catch (SQLException e) {
			throw new SpecmateException("Migration: Could not connect to the H2 database using the connection: "
					+ jdbcConnection + ". " + e.getMessage());
		}
	}

	private void initiateDBConnectionOracle() throws SpecmateException {
		try {
			DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
		} catch (SQLException e) {
			throw new SpecmateException("Migration: Could not register Oracle driver. " + e.getMessage());
		}

		String jdbcConnection = "";

		try {
			jdbcConnection = (String) configProperties.get(CDOPersistenceConfig.KEY_JDBC_CONNECTION);
			String username = (String) configProperties.get(CDOPersistenceConfig.KEY_ORACLE_USERNAME);
			String password = (String) configProperties.get(CDOPersistenceConfig.KEY_ORACLE_PASSWORD);
			this.connection = DriverManager.getConnection(jdbcConnection, username, password);
		} catch (SQLException e) {
			throw new SpecmateException("Migration: Could not connect to the Oracle database using the connection: "
					+ jdbcConnection + ". " + e.getMessage());
		}
	}

	private void closeConnection() throws SpecmateException {
		if (connection != null) {
			try {
				connection.close();
				connection = null;
			} catch (SQLException e) {
				throw new SpecmateException("Migration: Could not close connection.");
			}
		}
	}

	@Override
	public boolean needsMigration() throws SpecmateException {
		initiateDBConnection();

		if (this.isVirginDB) {
			return false;
		}

		try {
			String currentVersion = getCurrentModelVersion();
			if (currentVersion == null) {
				throw new SpecmateException("Migration: Could not determine currently deployed model version");
			}
			String targetVersion = getTargetModelVersion();
			if (targetVersion == null) {
				throw new SpecmateException("Migration: Could not determine target model version");
			}

			boolean needsMigration = !currentVersion.equals(targetVersion);

			if (needsMigration) {
				logService.log(LogService.LOG_INFO,
						"Migration needed. Current version: " + currentVersion + " / Target version: " + targetVersion);
			} else {
				logService.log(LogService.LOG_INFO, "No migration needed. Current version: " + currentVersion);
			}

			return needsMigration;
		} finally {
			closeConnection();
		}
	}

	@Override
{'fraction_non_alphanumeric': 0.10537207898594464, 'fraction_numerical': 0.0014658963525049581, 'mean_word_length': 4.3128721942281265, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '381302', 'n_tokens_mistral': 3653, 'n_tokens_neox': 3311, 'n_words': 861}