use of com.emc.metalnx.core.domain.entity.DataGridTemplate in project metalnx-web by irods-contrib.
the class TemplateController method modifyTemplate.
@RequestMapping(value = "modify/action/")
public String modifyTemplate(final Model model, @ModelAttribute final MetadataTemplateForm templateForm, final RedirectAttributes redirectAttributes) {
DataGridTemplate template = null;
try {
template = templateService.findById(templateForm.getId());
if (template == null) {
throw new Exception("Cannot modify a non-existent template");
}
DataGridUser loggedUser = loggedUserUtils.getLoggedDataGridUser();
if (!template.getOwner().equalsIgnoreCase(loggedUser.getUsername())) {
throw new Exception("Cannot modify a template beloging to another user");
}
template.setTemplateName(templateForm.getTemplateName());
template.setDescription(templateForm.getDescription());
template.setUsageInformation(templateForm.getUsageInformation());
template.setAccessType(templateForm.getAccessType().toString());
List<String> positions = templateForm.getAvuPositions();
List<String> attributes = templateForm.getAvuAttributes();
List<String> values = templateForm.getAvuValues();
List<String> units = templateForm.getAvuUnits();
if (attributes != null) {
for (int i = 0; i < attributes.size(); i++) {
for (int j = i + 1; j < attributes.size(); j++) {
if (attributes.get(i).equals(attributes.get(j)) && !values.get(i).isEmpty() && values.get(i).equals(values.get(j)) && units.get(i).equals(units.get(j))) {
redirectAttributes.addFlashAttribute("repeatedAVU", true);
return "redirect:/templates/modify/";
}
}
}
}
if (positions != null) {
for (int i = 0; i < positions.size(); i++) {
String position = positions.get(i);
if (position.contains("mod_pos_")) {
addTemplateFields.get(Integer.parseInt(position.replace("mod_pos_", ""))).setAttribute(attributes.get(i));
addTemplateFields.get(Integer.parseInt(position.replace("mod_pos_", ""))).setValue(values.get(i));
addTemplateFields.get(Integer.parseInt(position.replace("mod_pos_", ""))).setUnit(units.get(i));
} else if (position.contains("mod_id_")) {
templateFieldService.modifyTemplateField(Long.parseLong(position.replace("mod_id_", "")), attributes.get(i), values.get(i), units.isEmpty() ? "" : units.get(i));
template.setModified(true);
}
}
}
// adding all fields to the template
for (TemplateFieldForm tempFieldForm : addTemplateFields) {
DataGridTemplateField dataGridTempField = mapTempFieldFormToDataGridTemp(tempFieldForm);
dataGridTempField.setTemplate(template);
templateFieldService.createTemplateField(dataGridTempField);
template.setModified(true);
}
Set<DataGridTemplateField> currentTempFields = template.getFields();
// removing fields from the template
for (TemplateFieldForm tempFieldForm : removeTemplateFields) {
DataGridTemplateField dataGridTempField = mapTempFieldFormToDataGridTemp(tempFieldForm);
dataGridTempField.setTemplate(template);
template.setModified(true);
// removing template fields from template
if (currentTempFields.remove(dataGridTempField)) {
templateFieldService.deleteTemplateField(dataGridTempField);
logger.debug("Template removed from memory and database");
} else // template field wasn't removed
{
throw new Exception("Could not removed template field from memory.");
}
}
template.setFields(currentTempFields);
templateService.modifyTemplate(template);
// reseting the temporary fields to be added and removed from a template
addTemplateFields = new ArrayList<TemplateFieldForm>();
removeTemplateFields = new ArrayList<TemplateFieldForm>();
redirectAttributes.addFlashAttribute("templateModifiedSuccessfully", template.getTemplateName());
selectedTemplates.clear();
} catch (Exception e) {
logger.error("Could not modify template {}.", templateForm.getTemplateName());
redirectAttributes.addFlashAttribute("templateNotModifiedSuccessfully", templateForm.getTemplateName());
}
return "redirect:/templates/";
}
use of com.emc.metalnx.core.domain.entity.DataGridTemplate in project metalnx-web by irods-contrib.
the class TemplateController method addNewTemplate.
@RequestMapping(value = "add/action/")
public String addNewTemplate(final Model model, @ModelAttribute final MetadataTemplateForm templateForm, final RedirectAttributes redirectAttributes) {
DataGridTemplate newTemplate = null;
try {
newTemplate = new DataGridTemplate();
newTemplate.setTemplateName(templateForm.getTemplateName());
newTemplate.setDescription(templateForm.getDescription());
newTemplate.setUsageInformation(templateForm.getUsageInformation());
newTemplate.setOwner(loggedUserUtils.getLoggedDataGridUser().getUsername());
newTemplate.setAccessType(templateForm.getAccessType().toString());
long templateID = templateService.createTemplate(newTemplate);
if (templateID > 0) {
redirectAttributes.addFlashAttribute("templateAddedSuccessfully", newTemplate.getTemplateName());
newTemplate.setId(templateID);
// adding all fields to the template
for (TemplateFieldForm tempFieldForm : addTemplateFields) {
DataGridTemplateField dataGridTempField = mapTempFieldFormToDataGridTemp(tempFieldForm);
dataGridTempField.setTemplate(newTemplate);
templateFieldService.createTemplateField(dataGridTempField);
}
// reseting the temporary fields to be added and removed from a
// template
addTemplateFields = new ArrayList<TemplateFieldForm>();
removeTemplateFields = new ArrayList<TemplateFieldForm>();
return "redirect:/templates/";
}
} catch (DataGridTooLongTemplateNameException e) {
redirectAttributes.addFlashAttribute("templateNotAddedSuccessfully", true);
redirectAttributes.addFlashAttribute("tooLongTemplateName", true);
} catch (Exception e) {
redirectAttributes.addFlashAttribute("templateNotAddedSuccessfully", true);
}
return "redirect:/templates/add/";
}
use of com.emc.metalnx.core.domain.entity.DataGridTemplate in project metalnx-web by irods-contrib.
the class TemplateDaoImpl method findByName.
@Override
public DataGridTemplate findByName(String templateName) {
Query q = this.sessionFactory.getCurrentSession().createQuery("from DataGridTemplate where template_name = :templateName");
q.setString("templateName", templateName);
return (DataGridTemplate) q.uniqueResult();
}
use of com.emc.metalnx.core.domain.entity.DataGridTemplate in project metalnx-web by irods-contrib.
the class TemplateDaoImpl method deleteById.
@Override
public boolean deleteById(long id) {
DataGridTemplate dataGridTemplate = this.findById(id);
if (dataGridTemplate == null) {
return false;
}
this.delete(dataGridTemplate);
return true;
}
Aggregations