Search in sources :

Example 1 with ComboField

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

the class FormBuilderServiceImpl method populateDropdownToOneFields.

protected void populateDropdownToOneFields(EntityForm ef, ClassMetadata cmd) throws ServiceException {
    for (Property p : cmd.getProperties()) {
        if (p.getMetadata() instanceof BasicFieldMetadata) {
            BasicFieldMetadata fmd = (BasicFieldMetadata) p.getMetadata();
            if (LookupType.DROPDOWN.equals(fmd.getLookupType()) && !ArrayUtils.contains(getFormHiddenVisibilities(), fmd.getVisibility())) {
                // Get the records
                PersistencePackageRequest toOnePpr = PersistencePackageRequest.standard().withCeilingEntityClassname(fmd.getForeignKeyClass());
                Entity[] rows = adminEntityService.getRecords(toOnePpr).getDynamicResultSet().getRecords();
                // Determine the id field
                String idProp = null;
                ClassMetadata foreignClassMd = adminEntityService.getClassMetadata(toOnePpr).getDynamicResultSet().getClassMetaData();
                for (Property foreignP : foreignClassMd.getProperties()) {
                    if (foreignP.getMetadata() instanceof BasicFieldMetadata) {
                        BasicFieldMetadata foreignFmd = (BasicFieldMetadata) foreignP.getMetadata();
                        if (SupportedFieldType.ID.equals(foreignFmd.getFieldType())) {
                            idProp = foreignP.getName();
                        }
                    }
                }
                if (idProp == null) {
                    throw new RuntimeException("Could not determine ID property for " + fmd.getForeignKeyClass());
                }
                // Determine the display field
                String displayProp = fmd.getLookupDisplayProperty();
                // Build the options map
                Map<String, String> options = new HashMap<>();
                for (Entity row : rows) {
                    Property prop = row.findProperty(displayProp);
                    if (prop == null) {
                        LOG.warn("Could not find displayProp [" + StringUtil.sanitize(displayProp) + "] on entity [" + ef.getCeilingEntityClassname() + "]");
                    } else {
                        String displayValue = prop.getDisplayValue();
                        if (StringUtils.isBlank(displayValue)) {
                            displayValue = prop.getValue();
                        }
                        options.put(row.findProperty(idProp).getValue(), displayValue);
                    }
                }
                // Set the options on the entity field
                ComboField cf = (ComboField) ef.findField(p.getName());
                cf.setOptions(options);
            }
        }
    }
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) Entity(org.broadleafcommerce.openadmin.dto.Entity) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) HashMap(java.util.HashMap) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 2 with ComboField

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

the class FormBuilderServiceImpl method buildMapForm.

@Override
public EntityForm buildMapForm(MapMetadata mapMd, final MapStructure mapStructure, ClassMetadata cmd, String parentId, EntityForm ef) throws ServiceException {
    ForeignKey foreignKey = (ForeignKey) mapMd.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
    ef.setEntityType(foreignKey.getForeignKeyClass());
    Field keyField;
    if (!mapMd.getForceFreeFormKeys()) {
        // We will use a combo field to render the key choices
        ComboField temp = new ComboField();
        temp.withName("key").withFieldType("combo_field").withFriendlyName(mapStructure.getKeyPropertyFriendlyName());
        if (mapMd.getKeys() != null) {
            // The keys can be explicitly set in the annotation...
            temp.setOptions(mapMd.getKeys());
        } else {
            // Or they could be based on a different entity
            PersistencePackageRequest ppr = PersistencePackageRequest.standard().withCeilingEntityClassname(mapMd.getMapKeyOptionEntityClass());
            DynamicResultSet drs = adminEntityService.getRecords(ppr).getDynamicResultSet();
            for (Entity entity : drs.getRecords()) {
                String keyValue = entity.getPMap().get(mapMd.getMapKeyOptionEntityValueField()).getValue();
                String keyDisplayValue = entity.getPMap().get(mapMd.getMapKeyOptionEntityDisplayField()).getValue();
                temp.putOption(keyValue, keyDisplayValue);
            }
        }
        keyField = temp;
    } else {
        keyField = new Field().withName("key").withFieldType(SupportedFieldType.STRING.toString()).withFriendlyName(mapStructure.getKeyPropertyFriendlyName());
    }
    keyField.setRequired(true);
    ef.addMapKeyField(cmd, keyField);
    // Set the fields for this form
    List<Property> mapFormProperties;
    if (mapMd.isSimpleValue()) {
        ef.setIdProperty("key");
        mapFormProperties = new ArrayList<>();
        Property valueProp = cmd.getPMap().get("value");
        mapFormProperties.add(valueProp);
    } else {
        String valueClassName = mapStructure.getValueClassName();
        List<String> classNames = getValueClassNames(valueClassName);
        mapFormProperties = new ArrayList<>(Arrays.asList(cmd.getProperties()));
        filterMapFormProperties(mapFormProperties, classNames);
    }
    setEntityFormFields(cmd, ef, mapFormProperties);
    Field f = new Field().withName("priorKey").withFieldType(SupportedFieldType.HIDDEN.toString());
    ef.addHiddenField(cmd, f);
    ef.setParentId(parentId);
    return ef;
}
Also used : CodeField(org.broadleafcommerce.openadmin.web.form.entity.CodeField) RuleBuilderField(org.broadleafcommerce.openadmin.web.form.component.RuleBuilderField) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) MediaField(org.broadleafcommerce.openadmin.web.form.component.MediaField) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) Entity(org.broadleafcommerce.openadmin.dto.Entity) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 3 with ComboField

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

the class FormBuilderServiceImpl method createListGrid.

/**
 * Populate a ListGrid with ListGridRecords.
 *
 * @param className
 * @param headerFields
 * @param type
 * @param drs
 * @param sectionKey
 * @param order
 * @param idProperty
 * @param sectionCrumbs
 * @param sortPropery
 * @return
 */
protected ListGrid createListGrid(String className, List<Field> headerFields, ListGrid.Type type, DynamicResultSet drs, String sectionKey, Integer order, String idProperty, List<SectionCrumb> sectionCrumbs, String sortPropery) {
    // Create the list grid and set some basic attributes
    ListGrid listGrid = new ListGrid();
    listGrid.setClassName(className);
    listGrid.getHeaderFields().addAll(headerFields);
    listGrid.setListGridType(type);
    listGrid.setSectionCrumbs(sectionCrumbs);
    listGrid.setSectionKey(sectionKey);
    listGrid.setOrder(order);
    listGrid.setIdProperty(idProperty);
    listGrid.setStartIndex(drs.getStartIndex());
    listGrid.setTotalRecords(drs.getTotalRecords());
    listGrid.setPageSize(drs.getPageSize());
    String sectionIdentifier = extractSectionIdentifierFromCrumb(sectionCrumbs);
    AdminSection section = navigationService.findAdminSectionByClassAndSectionId(className, sectionIdentifier);
    if (section != null) {
        listGrid.setExternalEntitySectionKey(section.getUrl());
    }
    // format date list grid cells
    SimpleDateFormat formatter = new SimpleDateFormat("MMM d, Y @ hh:mma");
    DateFormatSymbols symbols = new DateFormatSymbols(Locale.getDefault());
    symbols.setAmPmStrings(new String[] { "am", "pm" });
    formatter.setDateFormatSymbols(symbols);
    // that are used for the header fields.
    for (Entity e : drs.getRecords()) {
        ListGridRecord record = new ListGridRecord();
        record.setListGrid(listGrid);
        record.setDirty(e.isDirty());
        record.setEntity(e);
        if (StringUtils.isNotBlank(sortPropery) && e.findProperty(sortPropery) != null) {
            Property sort = e.findProperty(sortPropery);
            record.setDisplayOrder(sort.getValue());
        }
        if (e.findProperty("hasError") != null) {
            Boolean hasError = Boolean.parseBoolean(e.findProperty("hasError").getValue());
            record.setIsError(hasError);
            if (hasError) {
                ExtensionResultStatusType messageResultStatus = listGridErrorExtensionManager.getProxy().determineErrorMessageForEntity(e, record);
                if (ExtensionResultStatusType.NOT_HANDLED.equals(messageResultStatus)) {
                    record.setErrorKey("listgrid.record.error");
                }
            }
        }
        if (e.findProperty("progressStatus") != null) {
            ExtensionResultStatusType messageResultStatus = listGridErrorExtensionManager.getProxy().determineStatusMessageForEntity(e, record);
            if (ExtensionResultStatusType.NOT_HANDLED.equals(messageResultStatus)) {
                record.setStatus(e.findProperty("progressStatus").getValue());
                record.setStatusCssClass("listgrid.record.status");
            }
        }
        if (e.findProperty(idProperty) != null) {
            record.setId(e.findProperty(idProperty).getValue());
        }
        for (Field headerField : headerFields) {
            Property p = e.findProperty(headerField.getName());
            if (p != null) {
                Field recordField = new Field().withName(headerField.getName()).withFriendlyName(headerField.getFriendlyName()).withOrder(p.getMetadata().getOrder());
                if (headerField instanceof ComboField) {
                    recordField.setValue(((ComboField) headerField).getOption(p.getValue()));
                    recordField.setDisplayValue(p.getDisplayValue());
                } else {
                    if (headerField.getFieldType().equals("DATE")) {
                        try {
                            Date date = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(p.getValue());
                            String newValue = formatter.format(date);
                            recordField.setValue(newValue);
                        } catch (Exception ex) {
                            recordField.setValue(p.getValue());
                        }
                    } else {
                        recordField.setValue(p.getValue());
                    }
                    recordField.setDisplayValue(p.getDisplayValue());
                }
                recordField.setDerived(isDerivedField(headerField, recordField, p));
                record.getFields().add(recordField);
            }
        }
        if (e.findProperty(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY) != null) {
            Field hiddenField = new Field().withName(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY);
            hiddenField.setValue(e.findProperty(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY).getValue());
            record.getHiddenFields().add(hiddenField);
        }
        if (e.findProperty(BasicPersistenceModule.ALTERNATE_ID_PROPERTY) != null) {
            record.setAltId(e.findProperty(BasicPersistenceModule.ALTERNATE_ID_PROPERTY).getValue());
        }
        extensionManager.getProxy().modifyListGridRecord(className, record, e);
        listGrid.getRecords().add(record);
    }
    if (drs.getFirstId() != null) {
        listGrid.setFirstId(drs.getFirstId());
    }
    if (drs.getLastId() != null) {
        listGrid.setLastId(drs.getLastId());
    }
    if (drs.getUpperCount() != null) {
        listGrid.setUpperCount(drs.getUpperCount());
    }
    if (drs.getLowerCount() != null) {
        listGrid.setLowerCount(drs.getLowerCount());
    }
    if (drs.getFetchType() != null) {
        listGrid.setFetchType(drs.getFetchType().toString());
    }
    if (drs.getTotalCountLessThanPageSize() != null) {
        listGrid.setTotalCountLessThanPageSize(drs.getTotalCountLessThanPageSize());
    }
    if (drs.getPromptSearch() != null) {
        listGrid.setPromptSearch(drs.getPromptSearch());
    }
    return listGrid;
}
Also used : AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) Entity(org.broadleafcommerce.openadmin.dto.Entity) ListGridRecord(org.broadleafcommerce.openadmin.web.form.component.ListGridRecord) AdminSection(org.broadleafcommerce.openadmin.server.security.domain.AdminSection) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ListGrid(org.broadleafcommerce.openadmin.web.form.component.ListGrid) Date(java.util.Date) ServiceException(org.broadleafcommerce.common.exception.ServiceException) IOException(java.io.IOException) NoSuchMessageException(org.springframework.context.NoSuchMessageException) ParseException(java.text.ParseException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) CodeField(org.broadleafcommerce.openadmin.web.form.entity.CodeField) RuleBuilderField(org.broadleafcommerce.openadmin.web.form.component.RuleBuilderField) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) MediaField(org.broadleafcommerce.openadmin.web.form.component.MediaField) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) DateFormatSymbols(java.text.DateFormatSymbols) SimpleDateFormat(java.text.SimpleDateFormat) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 4 with ComboField

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

the class TranslationFormBuilderServiceImpl method getLocaleField.

protected ComboField getLocaleField(String value) {
    ComboField f = new ComboField();
    f.setName("localeCode");
    f.setFriendlyName("Translation_localeCode");
    f.setFieldType(SupportedFieldType.EXPLICIT_ENUMERATION.toString());
    f.setOrder(0);
    if (StringUtils.isNotBlank(value)) {
        f.setValue(value);
    }
    List<Locale> locales = localeService.findAllLocales();
    Map<String, String> localeMap = new HashMap<String, String>();
    for (Locale l : locales) {
        localeMap.put(l.getLocaleCode(), l.getFriendlyName());
    }
    f.setOptions(localeMap);
    return f;
}
Also used : Locale(org.broadleafcommerce.common.locale.domain.Locale) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) HashMap(java.util.HashMap)

Example 5 with ComboField

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

the class FormBuilderServiceImpl method createHeaderField.

protected Field createHeaderField(Property p, BasicFieldMetadata fmd) {
    Field hf;
    if (fmd.getFieldType().equals(SupportedFieldType.EXPLICIT_ENUMERATION) || fmd.getFieldType().equals(SupportedFieldType.BROADLEAF_ENUMERATION) || fmd.getFieldType().equals(SupportedFieldType.DATA_DRIVEN_ENUMERATION) || fmd.getFieldType().equals(SupportedFieldType.EMPTY_ENUMERATION)) {
        hf = new ComboField();
        ((ComboField) hf).setOptions(fmd.getEnumerationValues());
    } else {
        hf = new Field();
    }
    hf.withName(p.getName()).withFriendlyName(StringUtils.isNotEmpty(fmd.getFriendlyName()) ? fmd.getFriendlyName() : p.getName()).withOrder(fmd.getGridOrder()).withColumnWidth(fmd.getColumnWidth()).withForeignKeyDisplayValueProperty(fmd.getForeignKeyDisplayValueProperty()).withForeignKeyClass(fmd.getForeignKeyClass()).withForeignKeySectionPath(getAdminSectionPath(fmd.getForeignKeyClass())).withOwningEntityClass(fmd.getOwningClass() != null ? fmd.getOwningClass() : fmd.getTargetClass()).withCanLinkToExternalEntity(fmd.getCanLinkToExternalEntity());
    String fieldType = fmd.getFieldType() == null ? null : fmd.getFieldType().toString();
    hf.setFieldType(fieldType);
    return hf;
}
Also used : CodeField(org.broadleafcommerce.openadmin.web.form.entity.CodeField) RuleBuilderField(org.broadleafcommerce.openadmin.web.form.component.RuleBuilderField) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) MediaField(org.broadleafcommerce.openadmin.web.form.component.MediaField) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField)

Aggregations

ComboField (org.broadleafcommerce.openadmin.web.form.entity.ComboField)7 Field (org.broadleafcommerce.openadmin.web.form.entity.Field)5 Property (org.broadleafcommerce.openadmin.dto.Property)4 MediaField (org.broadleafcommerce.openadmin.web.form.component.MediaField)4 RuleBuilderField (org.broadleafcommerce.openadmin.web.form.component.RuleBuilderField)4 CodeField (org.broadleafcommerce.openadmin.web.form.entity.CodeField)4 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)3 Entity (org.broadleafcommerce.openadmin.dto.Entity)3 HashMap (java.util.HashMap)2 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)2 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)2 IOException (java.io.IOException)1 DateFormatSymbols (java.text.DateFormatSymbols)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)1