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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import ca.corefacility.bioinformatics.irida.model.sample.Sample;
import ca.corefacility.bioinformatics.irida.ria.config.GalaxySessionInterceptor;
import ca.corefacility.bioinformatics.irida.ria.web.ajax.dto.CartSample;
import ca.corefacility.bioinformatics.irida.ria.web.cart.components.Cart;
import ca.corefacility.bioinformatics.irida.ria.web.cart.dto.GalaxyExportSample;
import ca.corefacility.bioinformatics.irida.service.sample.SampleService;

/**
 * Controller to handle all ajax requests made to the cart that have to do with Galaxy.
 */
@RestController
@Scope("session")
@RequestMapping("/ajax/galaxy-export")
public class CartGalaxyController {
	private final SampleService sampleService;
	private final Cart cart;

	@Autowired
	public CartGalaxyController(SampleService sampleService, Cart cart) {
		this.sampleService = sampleService;
		this.cart = cart;
	}

	/**
	 * Get a list of links for all {@link Sample} to be exported to the Galaxy Client.
	 * @return {@link List} of {@link Sample} links.
	 */
	@RequestMapping("/samples")
	public List<GalaxyExportSample> getGalaxyExportForm() {
		Map<Long, Map<Long, CartSample>> contents = cart.get();
		List<GalaxyExportSample> result = new ArrayList<>();
		for (Long projectId : contents.keySet()) {
			Iterable<Sample> samples = sampleService.readMultiple(contents.get(projectId)
					.keySet());
			samples.forEach(s -> result.add(new GalaxyExportSample(s, projectId)));
		}
		return result;
	}

	/**
	 * Remove the Galaxy attributes from the session.
	 *
	 * @param request - the current {@link HttpServletRequest}
	 */
	@RequestMapping("remove")
	public void removeGalaxySession(HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.removeAttribute(GalaxySessionInterceptor.GALAXY_CALLBACK_URL);
		session.removeAttribute(GalaxySessionInterceptor.GALAXY_CLIENT_ID);
	}
}

@@ -70,21 +70,16 @@
 va.util.
-Array
 List;%0Aim
@@ -85,36 +85,71 @@
 mport java.util.
-List
+Map;%0Aimport java.util.stream.Collectors
 ;%0Aimport java.ut
@@ -151,19 +151,29 @@
 va.util.
-Map
+stream.Stream
 ;%0A%0Aimpor
@@ -496,16 +496,83 @@
 oller;%0A%0A
+import ca.corefacility.bioinformatics.irida.model.project.Project;%0A
 import c
@@ -713,155 +713,8 @@
 or;%0A
-import ca.corefacility.bioinformatics.irida.ria.web.ajax.dto.CartSample;%0Aimport ca.corefacility.bioinformatics.irida.ria.web.cart.components.Cart;%0A
 impo
@@ -782,32 +782,108 @@
 xyExportSample;%0A
+import ca.corefacility.bioinformatics.irida.ria.web.services.UICartService;%0A
 import ca.corefa
@@ -1205,25 +1205,41 @@
 e final 
+UI
 Cart
- cart
+Service cartService
 ;%0A%0A%09@Aut
@@ -1307,17 +1307,33 @@
 ce, 
+UI
 Cart
- cart
+Service cartService
 ) %7B%0A
@@ -1381,23 +1381,37 @@
 his.cart
- = cart
+Service = cartService
 ;%0A%09%7D%0A%0A%09/
@@ -1648,28 +1648,22 @@
 Map%3C
-Long, Map%3CLong, Cart
+Project, List%3C
 Samp
@@ -1686,94 +1686,39 @@
 cart
-.get();%0A%09%09List%3CGalaxyExportSample%3E result = new ArrayList%3C%3E();%0A%09%09for (Long projectId :
+Service.getFullCart();%0A%09%09return
  con
@@ -1727,212 +1727,197 @@
 nts.
-k
 e
+ntr
 ySet()
-) %7B%0A%09%09%09Iterable%3CSample%3E samples = sampleService.readMultiple(contents.get(projectId)%0A%09%09%09%09%09.keySet());%0A%09%09%09samples.forEach(s -%3E result.add(new GalaxyExportSample(s, projec
+.stream().map(entry -%3E entry.getValue().stream().map(sample -%3E new GalaxyExportSample(sample, entry.getKey().ge
 tId
+()
 )))
-;%0A%09%09%7D%0A%09%09return result
+.flatMap(%0A%09%09%09%09Stream::distinct).collect(Collectors.toList())
 ;%0A%09%7D

Fix galaxy code