use of org.broadleafcommerce.common.web.JsonResponse in project BroadleafCommerce by BroadleafCommerce.
the class AdminBasicEntityController method removeEntity.
/**
* Attempts to remove the given entity.
*
* @param request
* @param response
* @param model
* @param pathVars
* @param id
* @return the return view path
* @throws Exception
*/
@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
public String removeEntity(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @ModelAttribute(value = "entityForm") EntityForm entityForm, BindingResult result, RedirectAttributes ra) throws Exception {
String sectionKey = getSectionKey(pathVars);
String sectionClassName = getClassNameForSection(sectionKey);
List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, sectionKey, id);
String[] sectionCriteria = customCriteriaService.mergeSectionCustomCriteria(sectionClassName, getSectionCustomCriteria());
Entity entity = service.removeEntity(entityForm, sectionCriteria, sectionCrumbs).getEntity();
// Removal does not normally return an Entity unless there is some validation error
if (entity != null) {
entityFormValidator.validate(entityForm, entity, result);
if (result.hasErrors()) {
// Create a flash attribute for the unsuccessful delete
FlashMap fm = new FlashMap();
fm.put("headerFlash", "delete.unsuccessful");
fm.put("headerFlashAlert", true);
request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, fm);
// Re-look back up the entity so that we can return something populated
PersistencePackageRequest ppr = getSectionPersistencePackageRequest(sectionClassName, sectionCrumbs, pathVars);
ClassMetadata cmd = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
entity = service.getRecord(ppr, id, cmd, false).getDynamicResultSet().getRecords()[0];
Map<String, DynamicResultSet> subRecordsMap = service.getRecordsForAllSubCollections(ppr, entity, sectionCrumbs);
entityForm.clearFieldsMap();
formService.populateEntityForm(cmd, entity, subRecordsMap, entityForm, sectionCrumbs);
modifyEntityForm(entityForm, pathVars);
return populateJsonValidationErrors(entityForm, result, new JsonResponse(response)).done();
}
}
ra.addFlashAttribute("headerFlash", "delete.successful");
ra.addFlashAttribute("headerFlashAlert", true);
if (isAjaxRequest(request)) {
// redirect attributes won't work here since ajaxredirect actually makes a new request
return "ajaxredirect:" + getContextPath(request) + sectionKey + "?headerFlash=delete.successful";
} else {
return "redirect:/" + sectionKey;
}
}
Aggregations