<commit_before>package ca.corefacility.bioinformatics.irida.pipeline.upload.galaxy;

import static com.google.common.base.Preconditions.*;

import java.util.LinkedList;
import java.util.List;

import javax.validation.ConstraintViolationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.jersey.api.client.ClientHandlerException;

import ca.corefacility.bioinformatics.irida.exceptions.UploadConnectionException;
import ca.corefacility.bioinformatics.irida.exceptions.UploadException;
import ca.corefacility.bioinformatics.irida.model.upload.UploadResult;
import ca.corefacility.bioinformatics.irida.model.upload.UploadSample;
import ca.corefacility.bioinformatics.irida.model.upload.galaxy.GalaxyAccountEmail;
import ca.corefacility.bioinformatics.irida.model.upload.galaxy.GalaxyProjectName;
import ca.corefacility.bioinformatics.irida.pipeline.upload.UploadWorker;

/**
 * Class for performing the actual work of uploading files to Galaxy.
 * @author <NAME> <<EMAIL>>
 *
 */
public class GalaxyUploadWorker implements UploadWorker {
	
	private static final Logger logger = LoggerFactory
			.getLogger(GalaxyUploadWorker.class);
	
	private List<UploadEventListener> eventListeners;

	private GalaxyAPI galaxyAPI;
	
	private List<UploadSample> samples;
	private GalaxyProjectName dataLocation;
	private GalaxyAccountEmail userName;
	
	private UploadResult uploadResult = null;
	private UploadException uploadException = null;
	
	/**
	 * Constructs a new GalaxyUploadWorker for performing the upload of files to Galaxy within a new Thread.
	 * @param galaxyAPI  The GalaxyAPI to connect to an instance of Galaxy.
	 * @param samples  The list of samples to upload.
	 * @param dataLocation  The location of the Galaxy project to upload into.
	 * @param userName  The user to upload the data as.
	 */
	public GalaxyUploadWorker(GalaxyAPI galaxyAPI, List<UploadSample> samples,
			GalaxyProjectName dataLocation, GalaxyAccountEmail userName) {
		checkNotNull(galaxyAPI, "galaxyAPI is null");
		checkNotNull(samples, "samples is null");
		checkNotNull(dataLocation, "dataLocation is null");
		checkNotNull(userName, "userName is null");
		
		this.galaxyAPI = galaxyAPI;
		this.samples = samples;
		this.dataLocation = dataLocation;
		this.userName = userName;
		
		eventListeners = new LinkedList<UploadEventListener>();
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void run() {
		try {
			uploadResult = uploadSamples(samples, dataLocation, userName);
			finish(uploadResult);
		} catch (ConstraintViolationException e) {
			this.uploadException = new UploadException(e);
			exception(uploadException);
		} catch (UploadException e) {
			this.uploadException = e;
			exception(uploadException);
		} catch (RuntimeException e) {
			this.uploadException = new UploadException(e);
			exception(uploadException);
		} catch (Exception e) {
			// handle any remaining exceptions
			this.uploadException = new UploadException(e);
			exception(uploadException);
		}
	}
	
	/**
	 * Gets a copy of the event listeners list.
	 * @return  A copy of the event listeners list
	 */
	private synchronized List<UploadEventListener> getEventListenersCopy() {
		List<UploadEventListener> eventListenersList = new LinkedList<UploadEventListener>(eventListeners);
		for (UploadEventListener eventListener : eventListeners) {
			eventListenersList.add(eventListener);
		}
		
		return eventListenersList;
	}
	
	/**
	 * Runs the finish() method on all event listeners.
	 * @param uploadResult  The result of the upload.
	 */
	private void finish(UploadResult uploadResult) {
		getEventListenersCopy().forEach(listener -> listener.finish(uploadResult));
	}
	
	/**<commit_msg>Don't duplicate the contents of the list.<commit_after>package ca.corefacility.bioinformatics.irida.pipeline.upload.galaxy;

import static com.google.common.base.Preconditions.*;

import java.util.LinkedList;
import java.util.List;

import javax.validation.ConstraintViolationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.jersey.api.client.ClientHandlerException;

import ca.corefacility.bioinformatics.irida.exceptions.UploadConnectionException;
import ca.corefacility.bioinformatics.irida.exceptions.UploadException;
import ca.corefacility.bioinformatics.irida.model.upload.UploadResult;
import ca.corefacility.bioinformatics.irida.model.upload.UploadSample;
import ca.corefacility.bioinformatics.irida.model.upload.galaxy.GalaxyAccountEmail;
import ca.corefacility.bioinformatics.irida.model.upload.galaxy.GalaxyProjectName;
import ca.corefacility.bioinformatics.irida.pipeline.upload.UploadWorker;

/**
 * Class for performing the actual work of uploading files to Galaxy.
 * @author <NAME> <<EMAIL>>
 *
 */
public class GalaxyUploadWorker implements UploadWorker {
	
	private static final Logger logger = LoggerFactory
			.getLogger(GalaxyUploadWorker.class);
	
	private List<UploadEventListener> eventListeners;

	private GalaxyAPI galaxyAPI;
	
	private List<UploadSample> samples;
	private GalaxyProjectName dataLocation;
	private GalaxyAccountEmail userName;
	
	private UploadResult uploadResult = null;
	private UploadException uploadException = null;
	
	/**
	 * Constructs a new GalaxyUploadWorker for performing the upload of files to Galaxy within a new Thread.
	 * @param galaxyAPI  The GalaxyAPI to connect to an instance of Galaxy.
	 * @param samples  The list of samples to upload.
	 * @param dataLocation  The location of the Galaxy project to upload into.
	 * @param userName  The user to upload the data as.
	 */
	public GalaxyUploadWorker(GalaxyAPI galaxyAPI, List<UploadSample> samples,
			GalaxyProjectName dataLocation, GalaxyAccountEmail userName) {
		checkNotNull(galaxyAPI, "galaxyAPI is null");
		checkNotNull(samples, "samples is null");
		checkNotNull(dataLocation, "dataLocation is null");
		checkNotNull(userName, "userName is null");
		
		this.galaxyAPI = galaxyAPI;
		this.samples = samples;
		this.dataLocation = dataLocation;
		this.userName = userName;
		
		eventListeners = new LinkedList<UploadEventListener>();
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void run() {
		try {
			uploadResult = uploadSamples(samples, dataLocation, userName);
			finish(uploadResult);
		} catch (ConstraintViolationException e) {
			this.uploadException = new UploadException(e);
			exception(uploadException);
		} catch (UploadException e) {
			this.uploadException = e;
			exception(uploadException);
		} catch (RuntimeException e) {
			this.uploadException = new UploadException(e);
			exception(uploadException);
		} catch (Exception e) {
			// handle any remaining exceptions
			this.uploadException = new UploadException(e);
			exception(uploadException);
		}
	}
	
	/**
	 * Gets a copy of the event listeners list.
	 * @return  A copy of the event listeners list
	 */
	private synchronized List<UploadEventListener> getEventListenersCopy() {
		List<UploadEventListener> eventListenersList = new LinkedList<UploadEventListener>(eventListeners);
		return eventListenersList;
	}
	
	/**
	 * Runs the finish() method on all event listeners.
	 * @param uploadResult  The result of the upload.
	 */
	private void finish(UploadResult uploadResult) {
		getEventListenersCopy().forEach(listener -> listener.finish(uploadResult));
	}
	
	/**
{'fraction_non_alphanumeric': 0.08932491767288694, 'fraction_numerical': 0.0005488474204171241, 'mean_word_length': 5.059019118869493, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, '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': '988390', 'n_tokens_mistral': 2187, 'n_tokens_neox': 1990, 'n_words': 531}