<commit_before>package ca.corefacility.bioinformatics.irida.ria.web.analysis;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.ImmutableMap;

import ca.corefacility.bioinformatics.irida.model.joins.Join;
import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.model.sample.Sample;
import ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin;
import ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile;
import ca.corefacility.bioinformatics.irida.service.ProjectService;
import ca.corefacility.bioinformatics.irida.service.SequencingObjectService;
import ca.corefacility.bioinformatics.irida.service.sample.SampleService;
import ca.corefacility.bioinformatics.irida.service.user.UserService;
import ca.corefacility.bioinformatics.irida.web.controller.api.projects.RESTProjectSamplesController;
import ca.corefacility.bioinformatics.irida.web.controller.api.samples.RESTSampleSequenceFilesController;

/**
 * Controller managing interactions with the selected sequences
 * 
 *
 */
@Controller
@Scope("session")
@RequestMapping("/cart")
public class CartController {
	private Map<Project, Set<Sample>> selected;
	
	private final SampleService sampleService;
	private final UserService userService;
	private final ProjectService projectService;
	private final SequencingObjectService sequencingObjectService;
	
	private final MessageSource messageSource;

	@Autowired
	public CartController(SampleService sampleService, UserService userService, ProjectService projectService,
			SequencingObjectService sequencingObjectService, MessageSource messageSource) {
		this.sampleService = sampleService;
		this.projectService = projectService;
		this.userService = userService;
		this.sequencingObjectService = sequencingObjectService;
		this.messageSource = messageSource;
		selected = new HashMap<>();
	}

	/**
	 * Get a modal dialog in order to export sample files to Galaxy
	 * @param model
	 *            The model to add attributes to for the template
	 * @param principal
	 *            A reference to the logged in user.
	 * @param projectId
	 *            The {@link Project} ID
	 * @return the name of the galaxy export modal dialog page
	 */
	@RequestMapping("/template/galaxy/project/{projectId}")
	public String getGalaxyModal(Model model, Principal principal,@PathVariable Long projectId ) {
		model.addAttribute("email", userService.getUserByUsername(principal.getName()).getEmail());
		model.addAttribute("name", projectService.read(projectId).getName() + "-" + principal.getName());
		String orgName = projectService.read(projectId).getOrganism() + "-" + principal.getName();
		model.addAttribute("orgName", orgName);
		return "templates/galaxy.tmpl";
	}

	/**
	 * Get a Json representation of what's in the cart. Format: {@code 
	 * 'projects' : [ { 'id': '5', 'label': 'project', 'samples': [ { 'id': '6',
	 * 'label': 'a sample', 'sequenceFiles': [ { 'selfRef' : 
	 * 'http://localhost/projects/1/samples/1/sequenceFiles/1' } ] } ] } ] }
	 * @return a {@code Map<String,Object>} containing the cart information.
	 */
	@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> getCartMap() {<commit_msg>added content type for galaxy template<commit_after>package ca.corefacility.bioinformatics.irida.ria.web.analysis;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.ImmutableMap;

import ca.corefacility.bioinformatics.irida.model.joins.Join;
import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.model.sample.Sample;
import ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin;
import ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile;
import ca.corefacility.bioinformatics.irida.service.ProjectService;
import ca.corefacility.bioinformatics.irida.service.SequencingObjectService;
import ca.corefacility.bioinformatics.irida.service.sample.SampleService;
import ca.corefacility.bioinformatics.irida.service.user.UserService;
import ca.corefacility.bioinformatics.irida.web.controller.api.projects.RESTProjectSamplesController;
import ca.corefacility.bioinformatics.irida.web.controller.api.samples.RESTSampleSequenceFilesController;

/**
 * Controller managing interactions with the selected sequences
 * 
 *
 */
@Controller
@Scope("session")
@RequestMapping("/cart")
public class CartController {
	private Map<Project, Set<Sample>> selected;
	
	private final SampleService sampleService;
	private final UserService userService;
	private final ProjectService projectService;
	private final SequencingObjectService sequencingObjectService;
	
	private final MessageSource messageSource;

	@Autowired
	public CartController(SampleService sampleService, UserService userService, ProjectService projectService,
			SequencingObjectService sequencingObjectService, MessageSource messageSource) {
		this.sampleService = sampleService;
		this.projectService = projectService;
		this.userService = userService;
		this.sequencingObjectService = sequencingObjectService;
		this.messageSource = messageSource;
		selected = new HashMap<>();
	}

	/**
	 * Get a modal dialog in order to export sample files to Galaxy
	 * @param model
	 *            The model to add attributes to for the template
	 * @param principal
	 *            A reference to the logged in user.
	 * @param projectId
	 *            The {@link Project} ID
	 * @return the name of the galaxy export modal dialog page
	 */
	@RequestMapping(value = "/template/galaxy/project/{projectId}", produces = MediaType.TEXT_HTML_VALUE)
	public String getGalaxyModal(Model model, Principal principal,@PathVariable Long projectId ) {
		model.addAttribute("email", userService.getUserByUsername(principal.getName()).getEmail());
		model.addAttribute("name", projectService.read(projectId).getName() + "-" + principal.getName());
		String orgName = projectService.read(projectId).getOrganism() + "-" + principal.getName();
		model.addAttribute("orgName", orgName);
		return "templates/galaxy.tmpl";
	}

	/**
	 * Get a Json representation of what's in the cart. Format: {@code 
	 * 'projects' : [ { 'id': '5', 'label': 'project', 'samples': [ { 'id': '6',
	 * 'label': 'a sample', 'sequenceFiles': [ { 'selfRef' : 
	 * 'http://localhost/projects/1/samples/1/sequenceFiles/1' } ] } ] } ] }
	 * @return a {@code Map<String,Object>} containing the cart information.
	 */
	@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> getCartMap() {
{'fraction_non_alphanumeric': 0.10611408402595925, 'fraction_numerical': 0.001138563133325743, 'mean_word_length': 5.819875776397516, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '2281494', 'n_tokens_mistral': 2508, 'n_tokens_neox': 2372, 'n_words': 508}