Search in sources :

Example 1 with JsonResponse

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";
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Entity(org.broadleafcommerce.openadmin.dto.Entity) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) PersistenceResponse(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse) Property(org.broadleafcommerce.openadmin.dto.Property) ListGrid(org.broadleafcommerce.openadmin.web.form.component.ListGrid) JsonResponse(org.broadleafcommerce.common.web.JsonResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with JsonResponse

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();
}
Also used : HashMap(java.util.HashMap) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) JsonResponse(org.broadleafcommerce.common.web.JsonResponse)

Example 3 with JsonResponse

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();
}
Also used : ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) JsonResponse(org.broadleafcommerce.common.web.JsonResponse) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with JsonResponse

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();
}
Also used : JsonResponse(org.broadleafcommerce.common.web.JsonResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with JsonResponse

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();
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) PersistenceResponse(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) Property(org.broadleafcommerce.openadmin.dto.Property) JsonResponse(org.broadleafcommerce.common.web.JsonResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

JsonResponse (org.broadleafcommerce.common.web.JsonResponse)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)3 Entity (org.broadleafcommerce.openadmin.dto.Entity)3 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)3 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)3 Property (org.broadleafcommerce.openadmin.dto.Property)2 PersistenceResponse (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)1 BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)1 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)1 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)1 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)1 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)1 ListGrid (org.broadleafcommerce.openadmin.web.form.component.ListGrid)1