Search in sources :

Example 1 with Field

use of org.broadleafcommerce.openadmin.web.form.entity.Field 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 Field

use of org.broadleafcommerce.openadmin.web.form.entity.Field 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 Field

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

the class AdminProductController method modifyAddEntityForm.

@Override
protected void modifyAddEntityForm(EntityForm ef, Map<String, String> pathVars) {
    String defaultCategoryUrlPrefix = null;
    Field defaultCategory = ef.findField("defaultCategory");
    if (defaultCategory != null && StringUtils.isNotBlank(defaultCategory.getValue())) {
        Category cat = catalogService.findCategoryById(Long.parseLong(defaultCategory.getValue()));
        defaultCategoryUrlPrefix = cat.getUrl();
    }
    Field overrideGeneratedUrl = ef.findField("overrideGeneratedUrl");
    if (overrideGeneratedUrl != null) {
        overrideGeneratedUrl.setFieldType(SupportedFieldType.HIDDEN.toString().toLowerCase());
        boolean overriddenUrl = Boolean.parseBoolean(overrideGeneratedUrl.getValue());
        Field fullUrl = ef.findField("url");
        if (fullUrl != null) {
            fullUrl.withAttribute("overriddenUrl", overriddenUrl).withAttribute("sourceField", "defaultSku--name").withAttribute("toggleField", "overrideGeneratedUrl").withAttribute("prefix-selector", "#field-defaultCategory").withAttribute("prefix", defaultCategoryUrlPrefix).withFieldType(SupportedFieldType.GENERATED_URL.toString().toLowerCase());
        }
    }
}
Also used : Field(org.broadleafcommerce.openadmin.web.form.entity.Field) Category(org.broadleafcommerce.core.catalog.domain.Category)

Example 4 with Field

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

the class AdminCategoryController method modifyAddEntityForm.

@Override
protected void modifyAddEntityForm(EntityForm ef, Map<String, String> pathVars) {
    Field overrideGeneratedUrl = ef.findField("overrideGeneratedUrl");
    overrideGeneratedUrl.setFieldType(SupportedFieldType.HIDDEN.toString().toLowerCase());
    boolean overriddenUrl = Boolean.parseBoolean(overrideGeneratedUrl.getValue());
    Field fullUrl = ef.findField("url");
    fullUrl.withAttribute("overriddenUrl", overriddenUrl).withAttribute("sourceField", "name").withAttribute("toggleField", "overrideGeneratedUrl").withFieldType(SupportedFieldType.GENERATED_URL.toString().toLowerCase());
}
Also used : Field(org.broadleafcommerce.openadmin.web.form.entity.Field)

Example 5 with Field

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

the class AdminBasicEntityController method viewEntityList.

// ******************************************
// REQUEST-MAPPING BOUND CONTROLLER METHODS *
// ******************************************
/**
 * Renders the main entity listing for the specified class, which is based on the current sectionKey with some optional
 * criteria.
 *
 * @param request
 * @param response
 * @param model
 * @param pathVars
 * @param requestParams a Map of property name -> list critiera values
 * @return the return view path
 * @throws Exception
 */
@RequestMapping(value = "", method = RequestMethod.GET)
public String viewEntityList(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @RequestParam MultiValueMap<String, String> requestParams) throws Exception {
    String sectionKey = getSectionKey(pathVars);
    String sectionClassName = getClassNameForSection(sectionKey);
    List<SectionCrumb> crumbs = getSectionCrumbs(request, null, null);
    PersistencePackageRequest ppr = getSectionPersistencePackageRequest(sectionClassName, requestParams, crumbs, pathVars);
    ClassMetadata cmd = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
    DynamicResultSet drs = service.getRecords(ppr).getDynamicResultSet();
    ListGrid listGrid = formService.buildMainListGrid(drs, cmd, sectionKey, crumbs);
    listGrid.setSelectType(ListGrid.SelectType.NONE);
    Set<Field> headerFields = listGrid.getHeaderFields();
    if (CollectionUtils.isNotEmpty(headerFields)) {
        Field firstField = headerFields.iterator().next();
        if (requestParams.containsKey(firstField.getName())) {
            model.addAttribute("mainSearchTerm", requestParams.get(firstField.getName()).get(0));
        }
    }
    model.addAttribute("viewType", "entityList");
    setupViewEntityListBasicModel(request, cmd, sectionKey, sectionClassName, model, requestParams);
    model.addAttribute("listGrid", listGrid);
    return "modules/defaultContainer";
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) ListGrid(org.broadleafcommerce.openadmin.web.form.component.ListGrid) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Field (org.broadleafcommerce.openadmin.web.form.entity.Field)36 Property (org.broadleafcommerce.openadmin.dto.Property)15 ComboField (org.broadleafcommerce.openadmin.web.form.entity.ComboField)15 MediaField (org.broadleafcommerce.openadmin.web.form.component.MediaField)14 RuleBuilderField (org.broadleafcommerce.openadmin.web.form.component.RuleBuilderField)14 CodeField (org.broadleafcommerce.openadmin.web.form.entity.CodeField)13 ArrayList (java.util.ArrayList)10 EntityForm (org.broadleafcommerce.openadmin.web.form.entity.EntityForm)10 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)9 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)8 Entity (org.broadleafcommerce.openadmin.dto.Entity)8 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)7 ListGrid (org.broadleafcommerce.openadmin.web.form.component.ListGrid)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 HashMap (java.util.HashMap)5 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)5 Map (java.util.Map)4 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)4 Translation (org.broadleafcommerce.common.i18n.domain.Translation)4 DataWrapper (org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper)4