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

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

import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.github.jmchilton.blend4j.galaxy.LibrariesClient;
import com.github.jmchilton.blend4j.galaxy.beans.LibraryContent;
import com.sun.jersey.api.client.UniformInterfaceException;

import ca.corefacility.bioinformatics.irida.exceptions.ExecutionManagerObjectNotFoundException;
import ca.corefacility.bioinformatics.irida.exceptions.galaxy.NoGalaxyContentFoundException;
import ca.corefacility.bioinformatics.irida.model.upload.galaxy.LibraryContentId;

public class GalaxyLibraryContentSearch extends GalaxySearch<LibraryContent, LibraryContentId> {

	private LibrariesClient librariesClient;
	private URL galaxyURL;

	/**
	 * Builds a new GalaxyLibrarySearch object around the passed LibrariesClient.
	 * 
	 * @param librariesClient
	 *            The LibrariesClient to use.
	 *  @param galaxyURL
	 *            The URL to the Galaxy instance.
	 */
	public GalaxyLibraryContentSearch(LibrariesClient librariesClient, URL galaxyURL) {
		checkNotNull(librariesClient, "librariesClient is null");
		checkNotNull(galaxyURL, "galaxyURL is null");

		this.librariesClient = librariesClient;
		this.galaxyURL = galaxyURL;
	}
	
	/**
	 * Gets a list of library contents given a library id.
	 * @param libraryId  The library id to search for a list of library contents.
	 * @return  A list of library contents.
	 * @throws NoGalaxyContentFoundException  If no library contents could be found.
	 */
	private List<LibraryContent> getLibraryContents(String libraryId)
			throws NoGalaxyContentFoundException {
		
		try {
			List<LibraryContent> libraryContents = librariesClient.getLibraryContents(libraryId);
			
			if (libraryContents != null) {
				return libraryContents;
			}
			
		} catch (UniformInterfaceException e) {
			throw new NoGalaxyContentFoundException("Could not find library content for id " + libraryId + " in Galaxy "
					+ galaxyURL, e);
		}
		
		throw new NoGalaxyContentFoundException("Could not find library content for id " + libraryId + " in Galaxy "
				+ galaxyURL);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public LibraryContent findById(LibraryContentId id)
			throws ExecutionManagerObjectNotFoundException {
		checkNotNull(id, "id is null");

		List<LibraryContent> libraryContents = getLibraryContents(id.getLibraryId());

		Optional<LibraryContent> content = libraryContents.stream()
				.filter(c -> c.getType().equals("folder") && 
						c.getName().equals(id.getFolderPath().getName())).findFirst();
		if (content.isPresent()) {
			return content.get();
		}
		
		throw new NoGalaxyContentFoundException("Could not find library content for id " + id + " in Galaxy "
				+ galaxyURL);
	}
	

	/**<commit_msg>Added catching of another Galaxy exception<commit_after>package ca.corefacility.bioinformatics.irida.pipeline.upload.galaxy;

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

import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.github.jmchilton.blend4j.galaxy.GalaxyResponseException;
import com.github.jmchilton.blend4j.galaxy.LibrariesClient;
import com.github.jmchilton.blend4j.galaxy.beans.LibraryContent;
import com.sun.jersey.api.client.UniformInterfaceException;

import ca.corefacility.bioinformatics.irida.exceptions.ExecutionManagerObjectNotFoundException;
import ca.corefacility.bioinformatics.irida.exceptions.galaxy.NoGalaxyContentFoundException;
import ca.corefacility.bioinformatics.irida.model.upload.galaxy.LibraryContentId;

public class GalaxyLibraryContentSearch extends GalaxySearch<LibraryContent, LibraryContentId> {

	private LibrariesClient librariesClient;
	private URL galaxyURL;

	/**
	 * Builds a new GalaxyLibrarySearch object around the passed LibrariesClient.
	 * 
	 * @param librariesClient
	 *            The LibrariesClient to use.
	 *  @param galaxyURL
	 *            The URL to the Galaxy instance.
	 */
	public GalaxyLibraryContentSearch(LibrariesClient librariesClient, URL galaxyURL) {
		checkNotNull(librariesClient, "librariesClient is null");
		checkNotNull(galaxyURL, "galaxyURL is null");

		this.librariesClient = librariesClient;
		this.galaxyURL = galaxyURL;
	}
	
	/**
	 * Gets a list of library contents given a library id.
	 * @param libraryId  The library id to search for a list of library contents.
	 * @return  A list of library contents.
	 * @throws NoGalaxyContentFoundException  If no library contents could be found.
	 */
	private List<LibraryContent> getLibraryContents(String libraryId)
			throws NoGalaxyContentFoundException {
		
		try {
			List<LibraryContent> libraryContents = librariesClient.getLibraryContents(libraryId);
			
			if (libraryContents != null) {
				return libraryContents;
			}
			
		} catch (GalaxyResponseException | UniformInterfaceException e) {
			throw new NoGalaxyContentFoundException("Could not find library content for id " + libraryId + " in Galaxy "
					+ galaxyURL, e);
		}
		
		throw new NoGalaxyContentFoundException("Could not find library content for id " + libraryId + " in Galaxy "
				+ galaxyURL);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public LibraryContent findById(LibraryContentId id)
			throws ExecutionManagerObjectNotFoundException {
		checkNotNull(id, "id is null");

		List<LibraryContent> libraryContents = getLibraryContents(id.getLibraryId());

		Optional<LibraryContent> content = libraryContents.stream()
				.filter(c -> c.getType().equals("folder") && 
						c.getName().equals(id.getFolderPath().getName())).findFirst();
		if (content.isPresent()) {
			return content.get();
		}
		
		throw new NoGalaxyContentFoundException("Could not find library content for id " + id + " in Galaxy "
				+ galaxyURL);
	}
	

	/**
{'fraction_non_alphanumeric': 0.09187220658831319, 'fraction_numerical': 0.0008276775368316504, 'mean_word_length': 4.641456582633054, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '1799378', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1659, 'n_words': 412}