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

import java.io.IOException;
import java.security.Principal;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplate;
import ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField;
import ca.corefacility.bioinformatics.irida.ria.web.models.UIMetadataTemplate;
import ca.corefacility.bioinformatics.irida.ria.web.projects.ProjectControllerUtils;
import ca.corefacility.bioinformatics.irida.service.ProjectService;
import ca.corefacility.bioinformatics.irida.service.sample.MetadataTemplateService;

/**
 * Handles requests for {@link MetadataTemplate}s in a {@link Project}
 */
@Controller
@RequestMapping("/projects/{projectId}/metadata-templates")
public class ProjectSamplesMetadataTemplateController {
	private ProjectService projectService;
	private MetadataTemplateService metadataTemplateService;
	private ProjectControllerUtils projectControllerUtils;

	@Autowired
	public ProjectSamplesMetadataTemplateController(ProjectService projectService,
			ProjectControllerUtils projectControllerUtils, MetadataTemplateService metadataTemplateService) {
		this.projectService = projectService;
		this.projectControllerUtils = projectControllerUtils;
		this.metadataTemplateService = metadataTemplateService;
	}

	/**
	 * Get the page to create a new {@link MetadataTemplate}
	 *
	 * @param projectId
	 * 		{@link Long} identifier for a {@link Project}
	 * @param model
	 * 		{@link Model} spring page model
	 * @param principal
	 * 		{@link Principal} currently logged in user
	 *
	 * @return {@link String} path to the new template page
	 */
	@RequestMapping("/new")
	public String getMetadataTemplateListPage(@PathVariable Long projectId, Model model, Principal principal) {
		Project project = projectService.read(projectId);

		// Add an empty MetadataTemplate. This facilitates code reuse for this page
		// since it is used for both creation and updating a template, and the html
		// is looking for a template object.
		model.addAttribute("template", new UIMetadataTemplate());
		projectControllerUtils.getProjectTemplateDetails(model, principal, project);
		return "projects/project_samples_metadata_template";
	}

	/**
	 * Get a the page for a specific {@link MetadataTemplate}
	 *
	 * @param projectId
	 * 		{@link Long} identifier for a {@link Project}
	 * @param templateId
	 * 		{@link Long} identifier for a {@link MetadataTemplate}
	 * @param principal
	 * 		{@link Principal} currently logged in user
	 * @param model
	 * 		{@link Model} spring page model
	 *
	 * @return {@link String} path to template page
	 */
	@RequestMapping("/{templateId}")
	public String getMetadataTemplatePage(@PathVariable Long projectId, @PathVariable Long templateId,
			Principal principal, Model model) {
		Project project = projectService.read(projectId);
		projectControllerUtils.getProjectTemplateDetails(model, principal, project);
		MetadataTemplate metadataTemplate = metadataTemplateService.read(templateId);
		UIMetadataTemplate template = new UIMetadataTemplate(metadataTemplate);
		model.addAttribute("template", template);
		return "projects/project_samples_metadata_template";
	}

	/**
	 * Delete a {@link MetadataTemplate} within a {@link Project}
	 *
	 * @param projectId<commit_msg>This should not have been removed in this merge request.<commit_after>package ca.corefacility.bioinformatics.irida.ria.web.projects.metadata;

import java.io.IOException;
import java.security.Principal;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectMetadataTemplateJoin;
import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplate;
import ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField;
import ca.corefacility.bioinformatics.irida.ria.web.models.UIMetadataTemplate;
import ca.corefacility.bioinformatics.irida.ria.web.projects.ProjectControllerUtils;
import ca.corefacility.bioinformatics.irida.service.ProjectService;
import ca.corefacility.bioinformatics.irida.service.sample.MetadataTemplateService;

/**
 * Handles requests for {@link MetadataTemplate}s in a {@link Project}
 */
@Controller
@RequestMapping("/projects/{projectId}/metadata-templates")
public class ProjectSamplesMetadataTemplateController {
	private ProjectService projectService;
	private MetadataTemplateService metadataTemplateService;
	private ProjectControllerUtils projectControllerUtils;

	@Autowired
	public ProjectSamplesMetadataTemplateController(ProjectService projectService,
			ProjectControllerUtils projectControllerUtils, MetadataTemplateService metadataTemplateService) {
		this.projectService = projectService;
		this.projectControllerUtils = projectControllerUtils;
		this.metadataTemplateService = metadataTemplateService;
	}

	/**
	 * Get the page to create a new {@link MetadataTemplate}
	 *
	 * @param projectId
	 * 		{@link Long} identifier for a {@link Project}
	 * @param model
	 * 		{@link Model} spring page model
	 * @param principal
	 * 		{@link Principal} currently logged in user
	 *
	 * @return {@link String} path to the new template page
	 */
	@RequestMapping("/new")
	public String getMetadataTemplateListPage(@PathVariable Long projectId, Model model, Principal principal) {
		Project project = projectService.read(projectId);

		// Add an empty MetadataTemplate. This facilitates code reuse for this page
		// since it is used for both creation and updating a template, and the html
		// is looking for a template object.
		model.addAttribute("template", new UIMetadataTemplate());
		projectControllerUtils.getProjectTemplateDetails(model, principal, project);
		return "projects/project_samples_metadata_template";
	}

	/**
	 * Get a the page for a specific {@link MetadataTemplate}
	 *
	 * @param projectId
	 * 		{@link Long} identifier for a {@link Project}
	 * @param templateId
	 * 		{@link Long} identifier for a {@link MetadataTemplate}
	 * @param principal
	 * 		{@link Principal} currently logged in user
	 * @param model
	 * 		{@link Model} spring page model
	 *
	 * @return {@link String} path to template page
	 */
	@RequestMapping("/{templateId}")
	public String getMetadataTemplatePage(@PathVariable Long projectId, @PathVariable Long templateId,
			Principal principal, Model model) {
		Project project = projectService.read(projectId);
		projectControllerUtils.getProjectTemplateDetails(model, principal, project);
		MetadataTemplate metadataTemplate = metadataTemplateService.read(templateId);
		UIMetadataTemplate template = new UIMetadataTemplate(metadataTemplate);
		model.addAttribute("template", template);
		return "projects/project_samples_metadata_template";
	}

	/**
	 * Save or update a {@link MetadataTemplate} within a {@link Project}
	 *
	 * @param projectId {@link Long} identifier for a {@link Project}
	 * @param template  A {@link UIMetadataTemplate} to save to a {@link Project}
	 * @return {@link String} redirects to the template page.
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String saveMetadataTemplate(@PathVariable Long projectId, UIMetadataTemplate template) {
		Project project = projectService.read(projectId);

		MetadataTemplate metadataTemplate;
		if (template.getId() != null) {
			metadataTemplate = metadataTemplateService.read(template.getId());
			metadataTemplate.setName(template.getName());
			metadataTemplate.setFields(template.getFields());
			metadataTemplateService.updateMetadataTemplateInProject(metadataTemplate);
		} else {
			ProjectMetadataTemplateJoin projectMetadataTemplateJoin = metadataTemplateService
					.createMetadataTemplateInProject(new MetadataTemplate(template.getName(), template.getFields()), project);
			metadataTemplate = projectMetadataTemplateJoin.getObject();
		}
		return "redirect:/projects/" + projectId + "/metadata-templates/" + metadataTemplate.getId();
	}


	/**
	 * Delete a {@link MetadataTemplate} within a {@link Project}
	 *
	 * @param projectId
{'fraction_non_alphanumeric': 0.09363091997822537, 'fraction_numerical': 0.0, 'mean_word_length': 5.7693441414885775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6005959', 'n_tokens_mistral': 2627, 'n_tokens_neox': 2429, 'n_words': 615}