package ca.corefacility.bioinformatics.irida.ria.web.sessionAttrs;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import ca.corefacility.bioinformatics.irida.model.sample.Sample;

import com.google.common.collect.Sets;

/**
 * Session object to hold samples that are currently in the cart.
 * HashMap<Sample ID, Project ID>
 */
public class Cart extends HashMap<Long, Long> {
	/*
	Sample Id --> Sample Name
	 */
	private final HashMap<Long, String> sampleNames = new HashMap<>();

	public int addSample(Sample sample, Long projectId) {
		this.put(sample.getId(), projectId);
		this.sampleNames.put(sample.getId(), sample.getLabel());
		return this.size();
	}

	public int removeSample(Long id) {
		this.remove(id);
		sampleNames.remove(id);
		return this.size();
	}

	public int removeProject(Long projectId) {
		Iterator<Entry<Long, Long>> iter = this.entrySet()
				.iterator();
		while (iter.hasNext()) {
			Entry<Long, Long> entry = iter.next();
			if (entry.getValue().equals(projectId)) {
				iter.remove();
				sampleNames.remove(entry.getKey());
			}
		}


		this.forEach((key, value) -> {
			if (value.equals(projectId)) {
				this.remove(key);
				sampleNames.remove(key);
			}
		});
		return this.size();
	}

	public Set<String> getSampleNamesInCart() {
		return Sets.newHashSet(sampleNames.values());
	}

	public Set<Long> getProjectsIdsInCart() {
		return Sets.newHashSet(this.values());
	}

}

@@ -509,292 +509,797 @@
 ;%0A%0A%09
-public int addSample(Sample sample, Long projectId) %7B%0A%09%09this.put(sample.getId(), projectId);%0A%09%09this.sampleNames.put(sample.getId(), sample.getLabel());%0A%09%09return this.size();%0A%09%7D%0A%0A%09public int removeSample(Long id) %7B%0A%09%09this.remove(id);%0A%09%09sampleNames.remove(id);%0A%09%09return this.size();%0A%09%7D%0A
+/**%0A%09 * Add a sample to the cart%0A%09 *%0A%09 * @param sample    to add to the cart%0A%09 * @param projectId identifier for the project from which the sample was added.%0A%09 * @return the number of samples in the cart%0A%09 */%0A%09public int addSample(Sample sample, Long projectId) %7B%0A%09%09this.put(sample.getId(), projectId);%0A%09%09this.sampleNames.put(sample.getId(), sample.getLabel());%0A%09%09return this.size();%0A%09%7D%0A%0A%09/**%0A%09 * Remove a sample from the cart.%0A%09 *%0A%09 * @param id for the sample to remove%0A%09 * @return the number of samples in the cart%0A%09 */%0A%09public int removeSample(Long id) %7B%0A%09%09this.remove(id);%0A%09%09sampleNames.remove(id);%0A%09%09return this.size();%0A%09%7D%0A%0A%09/**%0A%09 * Remove an entire project from the cart%0A%09 *%0A%09 * @param projectId identifier for the project to remove%0A%09 * @return the number of samples in the cart%0A%09 */
 %0A%09pu
@@ -1501,16 +1501,22 @@
 tValue()
+%0A%09%09%09%09%09
 .equals(
@@ -1598,17 +1598,16 @@
 %09%7D%0A%09%09%7D%0A%0A
-%0A
 %09%09this.f
@@ -1758,103 +1758,420 @@
 %7D%0A%0A%09
-public Set%3CString%3E getSampleNamesInCart() %7B%0A%09%09return Sets.newHashSet(sampleNames.values());%0A%09%7D%0A
+/**%0A%09 * Get the names of all the samples in the cart.%0A%09 * This is needed because pipelines cannot have samples with the same name.%0A%09 *%0A%09 * @return List of names of samples in the cart%0A%09 */%0A%09public Set%3CString%3E getSampleNamesInCart() %7B%0A%09%09return Sets.newHashSet(sampleNames.values());%0A%09%7D%0A%0A%09/**%0A%09 * Get all the identifiers for projects that have samples in the cart.%0A%09 *%0A%09 * @return List of project identifiers.%0A%09 */
 %0A%09pu

Update JavaDoc