use of ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField in project irida by phac-nml.
the class AnalysisController method getMetadataTemplateFields.
/**
* Generates a list of metadata fields for a five template.
*
* @param templateId {@link Long} id for the {@link MetadataTemplate} that the fields are required.
* @return {@link Map}
*/
@RequestMapping("/ajax/{submissionId}/metadata-template-fields")
@ResponseBody
public Map<String, Object> getMetadataTemplateFields(@RequestParam Long templateId) {
MetadataTemplate template = metadataTemplateService.read(templateId);
List<MetadataTemplateField> metadataFields = template.getFields();
List<String> fields = new ArrayList<>();
for (MetadataTemplateField metadataField : metadataFields) {
fields.add(metadataField.getLabel());
}
return ImmutableMap.of("fields", fields);
}
use of ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField in project irida by phac-nml.
the class ProjectLineListController method saveMetadataTemplate.
/**
* Save a list a {@link MetadataTemplateField} as a {@link MetadataTemplate}
*
* @param projectId identifier for the current {@link Project}
* @param fields {@link List} of {@link String} names of {@link MetadataTemplateField}
* @param name {@link String} name for the new template.
* @param templateId ID of the template to update. Will create new template if null
* @param locale Locale of teh logged in user
* @return the saved {@link MetadataTemplate} and a response message
*/
@RequestMapping(value = "/templates", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> saveMetadataTemplate(@PathVariable long projectId, @RequestParam String name, @RequestParam(value = "fields[]") List<String> fields, @RequestParam(required = false) Long templateId, Locale locale) {
Project project = projectService.read(projectId);
List<MetadataTemplateField> metadataFields = new ArrayList<>();
for (String label : fields) {
// Check to see if this field already exists.
MetadataTemplateField metadataField = metadataTemplateService.readMetadataFieldByLabel(label);
// If it does not exist, create a new field.
if (metadataField == null) {
metadataField = new MetadataTemplateField(label, "text");
metadataTemplateService.saveMetadataField(metadataField);
}
metadataFields.add(metadataField);
}
MetadataTemplate template;
String message;
// If the template already has an ID, it is an existing template, so just update it.
if (templateId != null) {
template = metadataTemplateService.read(templateId);
template.setFields(metadataFields);
template.setName(name);
metadataTemplateService.updateMetadataTemplateInProject(template);
message = messageSource.getMessage("linelist.create-template.update-success", new Object[] { name }, locale);
} else {
template = new MetadataTemplate(name, metadataFields);
ProjectMetadataTemplateJoin join = metadataTemplateService.createMetadataTemplateInProject(template, project);
template = join.getObject();
message = messageSource.getMessage("linelist.create-template.success", new Object[] { name }, locale);
}
return ImmutableMap.of("template", template, "message", message);
}
use of ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField in project irida by phac-nml.
the class ProjectLineListController method getAllProjectMetadataFields.
/**
* Get the template the the line list table. This becomes the table headers.
*
* @param projectId
* {@link Long} identifier of the current {@link Project}
*
* @return {@link Set} containing unique metadata fields
*/
private List<String> getAllProjectMetadataFields(Long projectId, Locale locale) {
Project project = projectService.read(projectId);
Set<String> fields = new HashSet<>();
List<MetadataTemplateField> metadataFieldsForProject = metadataTemplateService.getMetadataFieldsForProject(project);
fields.addAll(metadataFieldsForProject.stream().map(MetadataTemplateField::getLabel).collect(Collectors.toSet()));
// Get all the fields from the templates.
List<ProjectMetadataTemplateJoin> metadataTemplateJoins = metadataTemplateService.getMetadataTemplatesForProject(project);
for (ProjectMetadataTemplateJoin join : metadataTemplateJoins) {
MetadataTemplate template = join.getObject();
fields.addAll(template.getFields().stream().map(MetadataTemplateField::getLabel).collect(Collectors.toSet()));
}
return new ArrayList<>(fields);
}
use of ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField in project irida by phac-nml.
the class ProjectSampleMetadataController method saveProjectSampleMetadata.
/**
* Save uploaded metadata to the
*
* @param locale
* {@link Locale} of the current user.
* @param session
* {@link HttpSession}
* @param projectId
* {@link Long} identifier for the current project
*
* @return {@link Map} of potential errors.
*/
@RequestMapping(value = "/upload/save", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> saveProjectSampleMetadata(Locale locale, HttpSession session, @PathVariable long projectId) {
Map<String, Object> errors = new HashMap<>();
Project project = projectService.read(projectId);
SampleMetadataStorage stored = (SampleMetadataStorage) session.getAttribute("pm-" + projectId);
if (stored == null) {
errors.put("stored-error", true);
}
List<Sample> samplesToUpdate = new ArrayList<>();
List<Map<String, String>> found = stored.getFound();
if (found != null) {
// Lets try to get a sample
String sampleNameColumn = stored.getSampleNameColumn();
List<String> errorList = new ArrayList<>();
try {
for (Map<String, String> row : found) {
String name = row.get(sampleNameColumn);
Sample sample = sampleService.getSampleBySampleName(project, name);
row.remove(sampleNameColumn);
Map<MetadataTemplateField, MetadataEntry> newData = new HashMap<>();
// Need to overwrite duplicate keys
for (Entry<String, String> entry : row.entrySet()) {
MetadataTemplateField key = metadataTemplateService.readMetadataFieldByLabel(entry.getKey());
if (key == null) {
key = metadataTemplateService.saveMetadataField(new MetadataTemplateField(entry.getKey(), "text"));
}
newData.put(key, new MetadataEntry(entry.getValue(), "text"));
}
sample.mergeMetadata(newData);
// Save metadata back to the sample
samplesToUpdate.add(sample);
}
sampleService.updateMultiple(samplesToUpdate);
} catch (EntityNotFoundException e) {
// This really should not happen, but hey, you never know!
errorList.add(messageSource.getMessage("metadata.results.save.sample-not-found", new Object[] { e.getMessage() }, locale));
}
if (errorList.size() > 0) {
errors.put("save-errors", errorList);
}
} else {
errors.put("found-error", messageSource.getMessage("metadata.results.save.found-error", new Object[] {}, locale));
}
if (errors.size() == 0) {
return ImmutableMap.of("success", messageSource.getMessage("metadata.results.save.success", new Object[] { found.size() }, locale));
}
return errors;
}
use of ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField in project irida by phac-nml.
the class ProjectSamplesMetadataTemplateController method saveMetadataTemplate.
/**
* 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);
List<MetadataTemplateField> metadataFields = new ArrayList<>();
for (String field : template.getFields()) {
MetadataTemplateField metadataTemplateField = metadataTemplateService.readMetadataFieldByLabel(field);
if (metadataTemplateField == null) {
metadataTemplateField = new MetadataTemplateField(field, "text");
metadataTemplateService.saveMetadataField(metadataTemplateField);
}
metadataFields.add(metadataTemplateField);
}
MetadataTemplate metadataTemplate;
if (template.getId() != null) {
metadataTemplate = metadataTemplateService.read(template.getId());
metadataTemplate.setName(template.getName());
metadataTemplate.setFields(metadataFields);
metadataTemplateService.updateMetadataTemplateInProject(metadataTemplate);
} else {
ProjectMetadataTemplateJoin projectMetadataTemplateJoin = metadataTemplateService.createMetadataTemplateInProject(new MetadataTemplate(template.getName(), metadataFields), project);
metadataTemplate = projectMetadataTemplateJoin.getObject();
}
return "redirect:/projects/" + projectId + "/metadata-templates/" + metadataTemplate.getId();
}
Aggregations