FormsApiController.java
@RequestMapping(value = "/api/form/saveForm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public Map saveForm(HttpServletRequest request, HttpServletResponse response, @RequestBody String payload) { SessionManager sessionManager = new SessionManager(request, response); Map result = null; FormBL formBL = new FormBL(request, response); result = formBL.saveForm(payload); return result; }
FormBL.java
public Map<String, Object> saveForm(String payload) { Map<String, Object> result = new HashMap<>(); try { Gson gson = new Gson(); PojoForm pojoForm = gson.fromJson(payload, PojoForm.class); Form form = new Form(); if (pojoForm.formId > 0) { form.id = pojoForm.formId; } Machinery machinery = null; if (pojoForm.machineryId > 0) { machinery = Statics.machineryRepository.findById(pojoForm.machineryId); } form.name = pojoForm.formName; form.machinery = machinery; Form dbForm = Statics.formRepository.save(form); pojoForm.formId = dbForm.id; if (pojoForm.formRows.size() > 0) { for (PojoFormRow row : pojoForm.formRows) { FormRow formRow = new FormRow(); if (row.getId() > 0) { formRow.id = row.getId(); } formRow.rowOrder = row.row_order; formRow.rowType = row.getRowType(); formRow.title = row.getTitle(); formRow.form = dbForm; FormRow dbFormRow = Statics.formRowRepository.save(formRow); row.id = dbFormRow.id; if (row.getRow_values().size() > 0) { for (PojoRowValue rowValue : row.getRow_values()) { RowSelectValue rowSelectValue = new RowSelectValue(); if (rowValue.getId() > 0) { rowSelectValue.id = rowValue.getId(); } rowSelectValue.title = rowValue.title; rowSelectValue.titleOrder = rowValue.title_order; rowSelectValue.formRow = dbFormRow; RowSelectValue dbRowSelectValue = Statics.rowSelectValueRepository.save(rowSelectValue); rowValue.id = dbRowSelectValue.id; } } } } result.put("form", ""); result.put("error", 0); } catch (Exception ex) { // exception result.put("error", 1); ex.printStackTrace(); } return result; }