use of org.broadleafcommerce.common.web.JsonResponse in project BroadleafCommerce by BroadleafCommerce.
the class AdminBasicEntityController method removeCollectionItem.
/**
* Removes the requested collection item
*
* Note that the request must contain a parameter called "key" when attempting to remove a collection item from a
* map collection.
*
* @param request
* @param response
* @param model
* @param pathVars
* @param id
* @param collectionField
* @param collectionItemId
* @return the return view path
* @throws Exception
*/
@RequestMapping(value = "/{id}/{collectionField:.*}/{collectionItemId}/{alternateId}/delete", method = RequestMethod.POST)
public String removeCollectionItem(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @PathVariable(value = "collectionField") String collectionField, @PathVariable(value = "collectionItemId") String collectionItemId, @PathVariable(value = "alternateId") String alternateId) throws Exception {
String sectionKey = getSectionKey(pathVars);
String mainClassName = getClassNameForSection(sectionKey);
List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, sectionKey, id);
ClassMetadata mainMetadata = service.getClassMetadata(getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars)).getDynamicResultSet().getClassMetaData();
Property collectionProperty = mainMetadata.getPMap().get(collectionField);
String priorKey = request.getParameter("key");
PersistencePackageRequest ppr = getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars);
declareShouldIgnoreAdditionStatusFilter();
Entity entity = service.getRecord(ppr, id, mainMetadata, false).getDynamicResultSet().getRecords()[0];
// First, we must remove the collection entity
PersistenceResponse persistenceResponse = service.removeSubCollectionEntity(mainMetadata, collectionProperty, entity, collectionItemId, alternateId, priorKey, sectionCrumbs);
if (persistenceResponse.getEntity() != null && persistenceResponse.getEntity().isValidationFailure()) {
String error = "There was an error removing the whatever";
if (MapUtils.isNotEmpty(persistenceResponse.getEntity().getPropertyValidationErrors())) {
// If we failed, we'll return some JSON with the first error
error = persistenceResponse.getEntity().getPropertyValidationErrors().values().iterator().next().get(0);
} else if (CollectionUtils.isNotEmpty(persistenceResponse.getEntity().getGlobalValidationErrors())) {
error = persistenceResponse.getEntity().getGlobalValidationErrors().get(0);
}
return new JsonResponse(response).with("status", "error").with("message", BLCMessageUtils.getMessage(error)).done();
}
// Next, we must get the new list grid that represents this collection
// We return the new list grid so that it can replace the currently visible one
ListGrid listGrid = getCollectionListGrid(mainMetadata, entity, collectionProperty, null, sectionKey, persistenceResponse, sectionCrumbs);
model.addAttribute("listGrid", listGrid);
model.addAttribute("currentUrl", request.getRequestURL().toString());
setModelAttributes(model, sectionKey);
return "views/standaloneListGrid";
}
use of org.broadleafcommerce.common.web.JsonResponse in project BroadleafCommerce by BroadleafCommerce.
the class AdminOfferController method getErrorDuplicatingResponse.
protected String getErrorDuplicatingResponse(HttpServletResponse response, String code) {
List<Map<String, Object>> errors = new ArrayList<>();
String message;
BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
if (context != null && context.getMessageSource() != null) {
message = context.getMessageSource().getMessage(code, null, code, context.getJavaLocale());
} else {
LOG.warn("Could not find the MessageSource on the current request, not translating the message key");
message = "Duplication_Failure";
}
Map<String, Object> errorMap = new HashMap<>();
errorMap.put("errorType", "global");
errorMap.put("code", code);
errorMap.put("message", message);
errors.add(errorMap);
return new JsonResponse(response).with("errors", errors).done();
}
use of org.broadleafcommerce.common.web.JsonResponse in project BroadleafCommerce by BroadleafCommerce.
the class AdminBasicEntityController method saveEntityJson.
/**
* Builds JSON that looks like this:
*
* {"errors":
* [{"message":"This field is Required",
* "code": "requiredValidationFailure"
* "field":"defaultSku--name",
* "errorType", "field",
* "tab": "General"
* },
* {"message":"This field is Required",
* "code": "requiredValidationFailure"
* "field":"defaultSku--name",
* "errorType", "field",
* "tab": "General"
* }]
* }
*/
@RequestMapping(value = "/{id}", method = RequestMethod.POST, produces = "application/json")
public String saveEntityJson(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 {
saveEntity(request, response, model, pathVars, id, entityForm, result, ra);
JsonResponse json = new JsonResponse(response);
if (result.hasErrors()) {
populateJsonValidationErrors(entityForm, result, json);
}
List<String> dirtyList = buildDirtyList(pathVars, request, id);
if (CollectionUtils.isNotEmpty(dirtyList)) {
json.with("dirty", dirtyList);
}
ExtensionResultHolder<String> resultHolder = new ExtensionResultHolder<>();
ExtensionResultStatusType resultStatusType = extensionManager.getProxy().overrideSaveEntityJsonResponse(response, result.hasErrors(), getSectionKey(pathVars), id, resultHolder);
if (resultStatusType.equals(ExtensionResultStatusType.HANDLED)) {
return resultHolder.getResult();
}
return json.done();
}
use of org.broadleafcommerce.common.web.JsonResponse in project BroadleafCommerce by BroadleafCommerce.
the class AdminBasicOperationsController method logJavaScriptError.
@RequestMapping(value = "/logJavaScriptError", method = RequestMethod.POST)
@ResponseBody
public String logJavaScriptError(HttpServletRequest request, HttpServletResponse response, @RequestParam MultiValueMap<String, String> requestParams) throws Exception {
// Grab the error information from the request params
String url = requestParams.getFirst("url");
String lineNumber = requestParams.getFirst("lineNumber");
String message = requestParams.getFirst("message");
// Log the error
LOG.error("[JS] - (" + StringUtil.sanitize(url) + ":" + StringUtil.sanitize(lineNumber) + ") - " + StringUtil.sanitize(message));
// Return an errorLogged message to the client
return (new JsonResponse(response)).with("errorLogged", true).done();
}
use of org.broadleafcommerce.common.web.JsonResponse in project BroadleafCommerce by BroadleafCommerce.
the class AdminBasicEntityController method addEmptyCollectionItem.
@RequestMapping(value = "/{id}/{collectionField:.*}/addEmpty", method = RequestMethod.POST)
@ResponseBody
public String addEmptyCollectionItem(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @PathVariable(value = "collectionField") String collectionField, @ModelAttribute(value = "entityForm") EntityForm entityForm, BindingResult result) throws Exception {
String sectionKey = getSectionKey(pathVars);
String mainClassName = getClassNameForSection(sectionKey);
List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, sectionKey, id);
ClassMetadata mainMetadata = service.getClassMetadata(getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars)).getDynamicResultSet().getClassMetaData();
Property collectionProperty = mainMetadata.getPMap().get(collectionField);
if (StringUtils.isBlank(entityForm.getEntityType())) {
FieldMetadata fmd = collectionProperty.getMetadata();
if (fmd instanceof BasicCollectionMetadata) {
entityForm.setEntityType(((BasicCollectionMetadata) fmd).getCollectionCeilingEntity());
}
}
PersistencePackageRequest ppr = getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars);
Entity entity = service.getRecord(ppr, id, mainMetadata, false).getDynamicResultSet().getRecords()[0];
entity.setIsPreAdd(true);
// First, we must save the collection entity
PersistenceResponse persistenceResponse = service.addSubCollectionEntity(entityForm, mainMetadata, collectionProperty, entity, sectionCrumbs);
Entity savedEntity = persistenceResponse.getEntity();
return new JsonResponse(response).with("status", "complete").with("id", savedEntity.findProperty(entityForm.getIdProperty()).getValue()).done();
}
Aggregations