<commit_before>/*
*
* author:  gvenzl
* created: 24 Mar 2016
*
* name: Installer.java
*
*/

package com.dbpm.install;

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.dbpm.Module;
import com.dbpm.config.Config;
import com.dbpm.logger.Logger;
import com.dbpm.repository.Repository;
import com.dbpm.utils.PackageReader;
import com.dbpm.utils.Parameter;

/**
 * The Installer takes care of the installation of packages
 * @author gvenzl
 *
 */
public class Installer implements Module {
	
	private String fileName;
	private String userName;
	private String password;
	private String adminUser;
	private String adminPassword;
	private String host;
	private String port;
	private String dbName;
	
	/**
	 * Constructs an Installer object.
	 * @param args An array with the parameters for the installation
	 * @throws IllegalArgumentException if an illegal argument is passed along
	 */
	public Installer (String[] args) throws IllegalArgumentException {
		// Ignore first parameter as this one was already checked by DBPM in order to call installer
		for (int i=1;i<args.length;i++) {
			if (args[i].equals(Parameter.PACKAGEFILE)) {
				fileName = args[++i];
			}
			else if (args[i].equals(Parameter.USER)) {
				userName = args[++i];
			}
			else if (args[i].equals(Parameter.PASSWORD)) {
				password = args[++i];
			}
			else if (args[i].equals(Parameter.ADMINUSER)) {
				adminUser = args[++i];
			}
			else if (args[i].equals(Parameter.ADMINPASSWORD)) {
				adminPassword = args[++i];
			}
			else if (args[i].equals(Parameter.HOST)) {
				host = args[++i];
			}
			else if (args[i].equals(Parameter.PORT)) {
				port = args[++i];
			}
			else if (args[i].equals(Parameter.DBNAME)) {
				dbName = args[++i];
			}
			// Ignore verbose as already set globally by DBPM
			else if (args[i].equals(Parameter.VERBOSE)) { }
			else {
				throw new IllegalArgumentException(args[i] + " is not a valid argument for install");
			}
		}
	}

	@Override
	public void run() {
		Path dir;
		// if no file has been passed on, install all dbpkg files
		if (null == fileName) {
			dir = FileSystems.getDefault().getPath(System.getProperty("user.dir"));
			fileName = "*.dbpgk";
		}
		else {
			dir = FileSystems.getDefault().getPath(fileName.substring(0, fileName.lastIndexOf(File.separator)));
			fileName = fileName.substring(fileName.lastIndexOf(File.separator)+1);
		}
		
		try {
			DirectoryStream<Path> stream = Files.newDirectoryStream(dir, fileName);
			// Install each dbpm package in the current user directory
			// TODO: Build dependency tree instead and call install with right order
			for (Path path : stream) {
				installFile(new File(path.getParent() + File.separator + path.getFileName()));
			}
			stream.close();
		} catch (IOException e) {
			Logger.error("Cannot read package file!");
			Logger.error(e.getMessage());
		}
	}
	
	/**
	 * Installs a package in the database
	 * @param packageFile The package to be installed
	 * @return True if and only if the installation was successful, otherwise false
	 */
	private boolean installFile(File packageFile) {
		if (!packageFile.exists()) {
			Logger.error("Package file " + packageFile.getAbsolutePath() + " does not exist!");
			return false;
		}
		
		try {
			PackageReader pkgReader = new PackageReader(packageFile);
			Repository repo = Config.getRepository();

			// Save package to repository
			if (!repo.savePackage(pkgReader.getPackage(), Files.readAllBytes(Paths.get(packageFile.getAbsolutePath())))) {
				// TODO: Should there be an option to ignore this error?
				return false;
			}

			// Check whether package is already installed
			if (repo.isPackageInstalled(dbName, userName, pkgReader.getPackage())) {
				Logger.log("Package is already installed");
				return true;<commit_msg>Seperate single file installation and directory installation<commit_after>/*
*
* author:  gvenzl
* created: 24 Mar 2016
*
* name: Installer.java
*
*/

package com.dbpm.install;

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.dbpm.Module;
import com.dbpm.config.Config;
import com.dbpm.logger.Logger;
import com.dbpm.repository.Repository;
import com.dbpm.utils.PackageReader;
import com.dbpm.utils.Parameter;

/**
 * The Installer takes care of the installation of packages
 * @author gvenzl
 *
 */
public class Installer implements Module {
	
	private String fileName;
	private String userName;
	private String password;
	private String adminUser;
	private String adminPassword;
	private String host;
	private String port;
	private String dbName;
	
	/**
	 * Constructs an Installer object.
	 * @param args An array with the parameters for the installation
	 * @throws IllegalArgumentException if an illegal argument is passed along
	 */
	public Installer (String[] args) throws IllegalArgumentException {
		// Ignore first parameter as this one was already checked by DBPM in order to call installer
		for (int i=1;i<args.length;i++) {
			if (args[i].equals(Parameter.PACKAGEFILE)) {
				fileName = args[++i];
			}
			else if (args[i].equals(Parameter.USER)) {
				userName = args[++i];
			}
			else if (args[i].equals(Parameter.PASSWORD)) {
				password = args[++i];
			}
			else if (args[i].equals(Parameter.ADMINUSER)) {
				adminUser = args[++i];
			}
			else if (args[i].equals(Parameter.ADMINPASSWORD)) {
				adminPassword = args[++i];
			}
			else if (args[i].equals(Parameter.HOST)) {
				host = args[++i];
			}
			else if (args[i].equals(Parameter.PORT)) {
				port = args[++i];
			}
			else if (args[i].equals(Parameter.DBNAME)) {
				dbName = args[++i];
			}
			// Ignore verbose as already set globally by DBPM
			else if (args[i].equals(Parameter.VERBOSE)) { }
			else {
				throw new IllegalArgumentException(args[i] + " is not a valid argument for install");
			}
		}
	}

	@Override
	public void run() {
		Path dir;
		// if no file has been passed on, install all dbpkg files
		if (null == fileName) {
			dir = FileSystems.getDefault().getPath(System.getProperty("user.dir"));
			fileName = "*.dbpgk";
			try {
				DirectoryStream<Path> stream = Files.newDirectoryStream(dir, fileName);
				// Install each dbpm package in the current user directory
				// TODO: Build dependency tree instead and call install with right order
				for (Path path : stream) {
					installFile(new File(path.getParent() + File.separator + path.getFileName()));
				}
				stream.close();
			} catch (IOException e) {
				Logger.error("Cannot read package file!");
				Logger.error(e.getMessage());
			}
		}
		else {
			installFile(new File(fileName));
		}
		
	}
	
	/**
	 * Installs a package in the database
	 * @param packageFile The package to be installed
	 * @return True if and only if the installation was successful, otherwise false
	 */
	private boolean installFile(File packageFile) {
		if (!packageFile.exists()) {
			Logger.error("Package file " + packageFile.getAbsolutePath() + " does not exist!");
			return false;
		}
		
		try {
			PackageReader pkgReader = new PackageReader(packageFile);
			Repository repo = Config.getRepository();

			// Save package to repository
			if (!repo.savePackage(pkgReader.getPackage(), Files.readAllBytes(Paths.get(packageFile.getAbsolutePath())))) {
				// TODO: Should there be an option to ignore this error?
				return false;
			}

			// Check whether package is already installed
			if (repo.isPackageInstalled(dbName, userName, pkgReader.getPackage())) {
				Logger.log("Package is already installed");
				return true;
{'fraction_non_alphanumeric': 0.12130978471058398, 'fraction_numerical': 0.00206265308753384, 'mean_word_length': 3.727605118829982, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6531788', 'n_tokens_mistral': 2658, 'n_tokens_neox': 2325, 'n_words': 682}