Search in sources :

Example 1 with EntityForm

use of org.broadleafcommerce.openadmin.web.form.entity.EntityForm in project BroadleafCommerce by BroadleafCommerce.

the class ErrorsProcessor method populateModelVariables.

@Override
public Map<String, Object> populateModelVariables(String tagName, Map<String, String> tagAttributes, String attributeName, String attributeValue, BroadleafTemplateContext context) {
    BindStatus bindStatus = context.getBindStatus(attributeValue);
    Map<String, Object> newLocalVars = new HashMap<>();
    if (bindStatus.isError()) {
        EntityForm form = (EntityForm) ((BindingResult) bindStatus.getErrors()).getTarget();
        // Map of tab name -> (Map field Name -> list of error messages)
        Map<String, Map<String, List<String>>> result = new HashMap<>();
        if (!hideTopLevelFieldErrors) {
            for (FieldError err : bindStatus.getErrors().getFieldErrors()) {
                // attempt to look up which tab the field error is on. If it can't be found, just use
                // the default tab for the group
                String tabName = EntityForm.DEFAULT_TAB_NAME;
                Tab tab = form.findTabForField(err.getField());
                if (tab != null) {
                    tabName = tab.getTitle();
                }
                Map<String, List<String>> tabErrors = result.get(tabName);
                if (tabErrors == null) {
                    tabErrors = new HashMap<>();
                    result.put(tabName, tabErrors);
                }
                if (err.getField().contains(DynamicEntityFormInfo.FIELD_SEPARATOR)) {
                    // at this point the field name actually occurs within some array syntax
                    String fieldName = extractFieldName(err);
                    String[] fieldInfo = fieldName.split("\\" + DynamicEntityFormInfo.FIELD_SEPARATOR);
                    Field formField = form.getDynamicForm(fieldInfo[0]).findField(fieldName);
                    if (formField != null) {
                        addFieldError(formField.getFriendlyName(), err.getCode(), tabErrors);
                    } else {
                        LOG.warn("Could not find field " + fieldName + " within the dynamic form " + fieldInfo[0]);
                        addFieldError(fieldName, err.getCode(), tabErrors);
                    }
                } else {
                    if (form.getTabs().size() > 0) {
                        Field formField = form.findField(err.getField());
                        if (formField != null) {
                            addFieldError(formField.getFriendlyName(), err.getCode(), tabErrors);
                        } else {
                            LOG.warn("Could not find field " + err.getField() + " within the main form");
                            addFieldError(err.getField(), err.getCode(), tabErrors);
                        }
                    } else {
                        // this is the code that is executed when a Translations add action contains errors
                        // this branch of the code just puts a placeholder "tabErrors", to avoid errprProcessor parsing errors, and
                        // avoids checking on tabs, fieldGroups or fields (which for translations are empty), thus skipping any warning
                        newLocalVars.put("tabErrors", tabErrors);
                        return newLocalVars;
                    }
                }
            }
        }
        String translatedGeneralTab = GENERAL_ERRORS_TAB_KEY;
        BroadleafRequestContext blcContext = BroadleafRequestContext.getBroadleafRequestContext();
        if (blcContext != null && blcContext.getMessageSource() != null) {
            translatedGeneralTab = blcContext.getMessageSource().getMessage(translatedGeneralTab, null, translatedGeneralTab, blcContext.getJavaLocale());
        }
        for (ObjectError err : bindStatus.getErrors().getGlobalErrors()) {
            Map<String, List<String>> tabErrors = result.get(GENERAL_ERRORS_TAB_KEY);
            if (tabErrors == null) {
                tabErrors = new HashMap<>();
                result.put(translatedGeneralTab, tabErrors);
            }
            addFieldError(GENERAL_ERROR_FIELD_KEY, err.getCode(), tabErrors);
        }
        newLocalVars.put("tabErrors", result);
    }
    return newLocalVars;
}
Also used : EntityForm(org.broadleafcommerce.openadmin.web.form.entity.EntityForm) HashMap(java.util.HashMap) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) FieldError(org.springframework.validation.FieldError) BindStatus(org.springframework.web.servlet.support.BindStatus) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) ObjectError(org.springframework.validation.ObjectError) Tab(org.broadleafcommerce.openadmin.web.form.entity.Tab) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with EntityForm

use of org.broadleafcommerce.openadmin.web.form.entity.EntityForm in project BroadleafCommerce by BroadleafCommerce.

the class AdminPageController method saveEntity.

@Override
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String saveEntity(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 {
    // Attach the dynamic form info so that the update service will know how to split up the fields
    DynamicEntityFormInfo info = new DynamicEntityFormInfo().withCeilingClassName(PageTemplate.class.getName()).withSecurityCeilingClassName(Page.class.getName()).withCriteriaName("constructForm").withPropertyName("pageTemplate");
    entityForm.putDynamicFormInfo("pageTemplate", info);
    String returnPath = super.saveEntity(request, response, model, pathVars, id, entityForm, result, ra);
    if (result.hasErrors()) {
        info = entityForm.getDynamicFormInfo("pageTemplate");
        if (entityForm.getFields().containsKey("pageTemplate")) {
            info.setPropertyValue(entityForm.findField("pageTemplate").getValue());
        }
        // grab back the dynamic form that was actually put in
        EntityForm inputDynamicForm = entityForm.getDynamicForm("pageTemplate");
        if (inputDynamicForm != null) {
            List<Field> fieldsToChange = new ArrayList<Field>();
            String prefix = "pageTemplate" + DynamicEntityFormInfo.FIELD_SEPARATOR;
            for (Entry<String, Field> entry : inputDynamicForm.getFields().entrySet()) {
                if (entry.getKey().startsWith(prefix)) {
                    fieldsToChange.add(entry.getValue());
                }
            }
            for (Field f : fieldsToChange) {
                inputDynamicForm.getFields().remove(f.getName());
                f.setName(f.getName().substring(prefix.length()));
                inputDynamicForm.getFields().put(f.getName(), f);
            }
        }
        EntityForm dynamicForm;
        if (info.getPropertyValue() != null) {
            dynamicForm = getDynamicFieldTemplateForm(info, id, inputDynamicForm);
        } else {
            dynamicForm = getEntityForm(info, inputDynamicForm);
        }
        entityForm.putDynamicForm("pageTemplate", dynamicForm);
        entityForm.removeListGrid("additionalAttributes");
    }
    return returnPath;
}
Also used : EntityForm(org.broadleafcommerce.openadmin.web.form.entity.EntityForm) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) ArrayList(java.util.ArrayList) Page(org.broadleafcommerce.cms.page.domain.Page) DynamicEntityFormInfo(org.broadleafcommerce.openadmin.web.form.entity.DynamicEntityFormInfo) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with EntityForm

use of org.broadleafcommerce.openadmin.web.form.entity.EntityForm in project BroadleafCommerce by BroadleafCommerce.

the class AdminOfferController method addDuplicateOption.

protected void addDuplicateOption(Model model, String id) {
    if (duplicator.validate(OfferImpl.class, Long.parseLong(id))) {
        EntityForm form = (EntityForm) model.asMap().get("entityForm");
        EntityFormAction duplicate = new EntityFormAction("duplicate").withButtonClass("duplicate-button").withDisplayText("Duplicate");
        form.addAction(duplicate);
    }
}
Also used : EntityForm(org.broadleafcommerce.openadmin.web.form.entity.EntityForm) EntityFormAction(org.broadleafcommerce.openadmin.web.form.entity.EntityFormAction)

Example 4 with EntityForm

use of org.broadleafcommerce.openadmin.web.form.entity.EntityForm in project BroadleafCommerce by BroadleafCommerce.

the class AdminProductController method viewEntityForm.

@Override
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String viewEntityForm(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id) throws Exception {
    String view = super.viewEntityForm(request, response, model, pathVars, id);
    // Skus have a specific toolbar action to generate Skus based on permutations
    EntityForm form = (EntityForm) model.asMap().get("entityForm");
    ListGridAction generateSkusAction = new ListGridAction(ListGridAction.GEN_SKUS).withDisplayText("Generate_Skus").withIconClass("icon-fighter-jet").withButtonClass("generate-skus").withUrlPostfix("/generate-skus").withActionUrlOverride("/product/" + id + "/additionalSkus/generate-skus");
    ListGrid skusGrid = form.findListGrid("additionalSkus");
    if (skusGrid != null) {
        skusGrid.setCanFilterAndSort(false);
    }
    ListGrid productOptionsGrid = form.findListGrid("productOptions");
    if (productOptionsGrid != null) {
        productOptionsGrid.addToolbarAction(generateSkusAction);
    }
    // list grids. Remove them from the form.
    if (ProductBundle.class.isAssignableFrom(Class.forName(form.getEntityType()))) {
        form.removeListGrid("additionalSkus");
        form.removeListGrid("productOptions");
        form.removeField("canSellWithoutOptions");
    }
    form.removeListGrid("defaultSku.skuAttributes");
    return view;
}
Also used : ListGridAction(org.broadleafcommerce.openadmin.web.form.component.ListGridAction) EntityForm(org.broadleafcommerce.openadmin.web.form.entity.EntityForm) ListGrid(org.broadleafcommerce.openadmin.web.form.component.ListGrid) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with EntityForm

use of org.broadleafcommerce.openadmin.web.form.entity.EntityForm in project BroadleafCommerce by BroadleafCommerce.

the class AdminStructuredContentController method saveEntity.

@Override
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String saveEntity(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 {
    // Attach the dynamic form info so that the update service will know how to split up the fields
    DynamicEntityFormInfo info = new DynamicEntityFormInfo().withCeilingClassName(StructuredContentType.class.getName()).withSecurityCeilingClassName(StructuredContent.class.getName()).withCriteriaName("constructForm").withPropertyName("structuredContentType");
    entityForm.putDynamicFormInfo("structuredContentType", info);
    String returnPath = super.saveEntity(request, response, model, pathVars, id, entityForm, result, ra);
    if (result.hasErrors()) {
        info = entityForm.getDynamicFormInfo("structuredContentType");
        info.setPropertyValue(entityForm.findField("structuredContentType").getValue());
        // grab back the dynamic form that was actually put in
        EntityForm inputDynamicForm = entityForm.getDynamicForm("structuredContentType");
        EntityForm dynamicForm = getDynamicFieldTemplateForm(info, id, inputDynamicForm);
        entityForm.putDynamicForm("structuredContentType", dynamicForm);
    }
    return returnPath;
}
Also used : EntityForm(org.broadleafcommerce.openadmin.web.form.entity.EntityForm) DynamicEntityFormInfo(org.broadleafcommerce.openadmin.web.form.entity.DynamicEntityFormInfo) StructuredContent(org.broadleafcommerce.cms.structure.domain.StructuredContent) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

EntityForm (org.broadleafcommerce.openadmin.web.form.entity.EntityForm)33 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)12 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)10 Field (org.broadleafcommerce.openadmin.web.form.entity.Field)10 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)8 DynamicEntityFormInfo (org.broadleafcommerce.openadmin.web.form.entity.DynamicEntityFormInfo)7 ArrayList (java.util.ArrayList)6 Entity (org.broadleafcommerce.openadmin.dto.Entity)5 HashMap (java.util.HashMap)3 Map (java.util.Map)3 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)3 Property (org.broadleafcommerce.openadmin.dto.Property)3 Page (org.broadleafcommerce.cms.page.domain.Page)2 Translation (org.broadleafcommerce.common.i18n.domain.Translation)2 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)2 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)2 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)2 ListGrid (org.broadleafcommerce.openadmin.web.form.component.ListGrid)2 EntityFormAction (org.broadleafcommerce.openadmin.web.form.entity.EntityFormAction)2