@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(ModelMap model,
@Valid @ModelAttribute("addForm") UnitForm saveForm,
BindingResult result) {
String code = saveForm.getSuperUnitCode();
Unit unit = saveForm.getUnit();
if (saveForm.getChannelWithDeletePressed() != null) {
saveForm.removeChannel(saveForm.getChannelWithDeletePressed());
return "add";
} else if (saveForm.getAddChannelButton() != null) {
saveForm.getChannels().add(new Channel());
return "add";
} else {
Unit units = saveForm.getUnit();
units.addChannels(saveForm.getChannels());
if (code != null && !code.isEmpty()) {
Long superUnit = dao.getUnitByCode(code).getId();
unit.setSuper_unit_id(superUnit);
}
units.setChannels(saveForm.getChannels());
if (result.hasErrors()) {
return "add";
}
dao.save(units);
return "redirect:/search";