Search in sources :

Example 81 with Property

use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.

the class FormBuilderServiceImpl method setEntityFormFields.

protected void setEntityFormFields(ClassMetadata cmd, EntityForm ef, List<Property> properties) {
    List<Field> homelessFields = new ArrayList<>();
    List<Field> fieldsWithAssociations = new ArrayList<>();
    for (Property property : properties) {
        if (property.getMetadata() instanceof BasicFieldMetadata) {
            BasicFieldMetadata fmd = (BasicFieldMetadata) property.getMetadata();
            if (!ArrayUtils.contains(getFormHiddenVisibilities(), fmd.getVisibility())) {
                // Depending on visibility, field for the particular property is not created on the form
                String fieldType = fmd.getFieldType() == null ? null : fmd.getFieldType().toString();
                // Create the field and set some basic attributes
                Field f;
                if (fieldType.equals(SupportedFieldType.BROADLEAF_ENUMERATION.toString()) || fieldType.equals(SupportedFieldType.EXPLICIT_ENUMERATION.toString()) || fieldType.equals(SupportedFieldType.DATA_DRIVEN_ENUMERATION.toString()) || fieldType.equals(SupportedFieldType.EMPTY_ENUMERATION.toString())) {
                    // We're dealing with fields that should render as drop-downs, so set their possible values
                    f = new ComboField();
                    ((ComboField) f).setOptions(fmd.getEnumerationValues());
                    if (fmd.getHideEnumerationIfEmpty() != null && fmd.getHideEnumerationIfEmpty().booleanValue() && ((ComboField) f).getOptions().size() == 0) {
                        f.setIsVisible(false);
                    }
                } else if (fieldType.equals(SupportedFieldType.CODE.toString())) {
                    f = new CodeField();
                } else if (fieldType.equals(SupportedFieldType.RULE_SIMPLE.toString()) || fieldType.equals(SupportedFieldType.RULE_SIMPLE_TIME.toString()) || fieldType.equals(SupportedFieldType.RULE_WITH_QUANTITY.toString())) {
                    // We're dealing with rule builders, so we'll create those specialized fields
                    f = new RuleBuilderField();
                    ((RuleBuilderField) f).setJsonFieldName(property.getName() + "Json");
                    ((RuleBuilderField) f).setDataWrapper(new DataWrapper());
                    ((RuleBuilderField) f).setFieldBuilder(fmd.getRuleIdentifier());
                    ((RuleBuilderField) f).setDisplayType(fmd.getDisplayType().toString());
                    String blankJsonString = "{\"data\":[]}";
                    ((RuleBuilderField) f).setJson(blankJsonString);
                    DataWrapper dw = convertJsonToDataWrapper(blankJsonString);
                    if (dw != null) {
                        ((RuleBuilderField) f).setDataWrapper(dw);
                    }
                    if (fieldType.equals(SupportedFieldType.RULE_SIMPLE.toString())) {
                        ((RuleBuilderField) f).setRuleType("rule-builder-simple");
                    } else if (fieldType.equals(SupportedFieldType.RULE_WITH_QUANTITY.toString())) {
                        ((RuleBuilderField) f).setRuleType("rule-builder-with-quantity");
                    } else if (fieldType.equals(SupportedFieldType.RULE_SIMPLE_TIME.toString())) {
                        ((RuleBuilderField) f).setRuleType("rule-builder-simple-time");
                    }
                } else if (LookupType.DROPDOWN.equals(fmd.getLookupType())) {
                    // We're dealing with a to-one field that wants to be rendered as a dropdown instead of in a
                    // modal, so we'll provision the combo field here. Available options will be set as part of a
                    // subsequent operation
                    f = new ComboField();
                } else if (fieldType.equals(SupportedFieldType.MEDIA.toString())) {
                    f = new MediaField();
                } else {
                    // Create a default field since there was no specialized handler
                    f = new Field();
                }
                Boolean required = fmd.getRequiredOverride();
                if (required == null) {
                    required = fmd.getRequired();
                }
                Boolean allowNoValueEnum = fmd.getAllowNoValueEnumOption();
                if (allowNoValueEnum != null) {
                    f.setAllowNoValueEnumOption(allowNoValueEnum);
                }
                f.withName(property.getName()).withFieldType(fieldType).withFieldComponentRenderer(getFieldComponentRenderer(fmd)).withGridFieldComponentRenderer(getGridFieldComponentRenderer(fmd)).withOrder(fmd.getOrder()).withFriendlyName(fmd.getFriendlyName()).withForeignKeyDisplayValueProperty(fmd.getForeignKeyDisplayValueProperty()).withForeignKeyClass(fmd.getForeignKeyClass()).withForeignKeySectionPath(getAdminSectionPath(fmd.getForeignKeyClass())).withOwningEntityClass(fmd.getOwningClass() != null ? fmd.getOwningClass() : fmd.getInheritedFromType()).withRequired(required).withReadOnly(fmd.getReadOnly()).withTranslatable(fmd.getTranslatable()).withAlternateOrdering((Boolean) fmd.getAdditionalMetadata().get(Field.ALTERNATE_ORDERING)).withLargeEntry(fmd.isLargeEntry()).withHint(fmd.getHint()).withTooltip(fmd.getTooltip()).withHelp(fmd.getHelpText()).withTypeaheadEnabled(fmd.getEnableTypeaheadLookup()).withCanLinkToExternalEntity(fmd.getCanLinkToExternalEntity()).withAssociatedFieldName(fmd.getAssociatedFieldName());
                String defaultValue = fmd.getDefaultValue();
                if (defaultValue != null) {
                    defaultValue = extractDefaultValueFromFieldData(fieldType, fmd);
                    f.withValue(defaultValue);
                }
                if (StringUtils.isBlank(f.getFriendlyName())) {
                    f.setFriendlyName(f.getName());
                }
                // If is form hidden, set visible to false
                if (VisibilityEnum.FORM_EXPLICITLY_HIDDEN.equals(fmd.getVisibility())) {
                    f.setIsVisible(false);
                }
                if (VisibilityEnum.VISIBLE_ALL.equals(fmd.getVisibility())) {
                    f.setIsVisible(true);
                }
                // Add the field to the appropriate FieldGroup
                if (fmd.getGroup() == null) {
                    homelessFields.add(f);
                } else {
                    ef.addField(cmd, f, fmd.getGroup(), fmd.getGroupOrder(), fmd.getTab(), fmd.getTabOrder());
                }
                if (StringUtils.isNotEmpty(fmd.getAssociatedFieldName())) {
                    fieldsWithAssociations.add(f);
                }
            }
        }
    }
    for (Field f : homelessFields) {
        ef.addField(cmd, f, null, null, null, null);
    }
    for (Field f : fieldsWithAssociations) {
        Field associatedField = findAssociatedField(ef, f);
        if (associatedField != null) {
            associatedField.setShouldRender(false);
            f.setAssociatedFieldName(associatedField.getName());
        } else {
            f.setAssociatedFieldName(null);
        }
    }
}
Also used : DataWrapper(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper) 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) CodeField(org.broadleafcommerce.openadmin.web.form.entity.CodeField) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) ArrayList(java.util.ArrayList) RuleBuilderField(org.broadleafcommerce.openadmin.web.form.component.RuleBuilderField) MediaField(org.broadleafcommerce.openadmin.web.form.component.MediaField) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 82 with Property

use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.

the class FormBuilderServiceImpl method populateEntityFormFieldValues.

@Override
public void populateEntityFormFieldValues(ClassMetadata cmd, Entity entity, EntityForm ef) {
    // Set the appropriate property values
    for (Property p : cmd.getProperties()) {
        if (p.getMetadata() instanceof BasicFieldMetadata) {
            BasicFieldMetadata basicFM = (BasicFieldMetadata) p.getMetadata();
            Property entityProp = entity.findProperty(p.getName());
            boolean explicitlyShown = VisibilityEnum.FORM_EXPLICITLY_SHOWN.equals(basicFM.getVisibility());
            // always show special map fields
            if (p.getName().equals("key") || p.getName().equals("priorKey")) {
                explicitlyShown = true;
            }
            if (entityProp == null && explicitlyShown) {
                Field field = ef.findField(p.getName());
                if (field != null) {
                    field.setValue(null);
                }
            } else if (entityProp == null && !SupportedFieldType.PASSWORD_CONFIRM.equals(basicFM.getExplicitFieldType())) {
                ef.removeField(p.getName());
            } else {
                Field field = ef.findField(p.getName());
                if (field != null) {
                    if (entityProp != null) {
                        // protect against null - can happen with password confirmation fields (i.e. admin user)
                        field.setDirty(entityProp.getIsDirty());
                    }
                    if (basicFM.getFieldType() == SupportedFieldType.RULE_SIMPLE || basicFM.getFieldType() == SupportedFieldType.RULE_SIMPLE_TIME || basicFM.getFieldType() == SupportedFieldType.RULE_WITH_QUANTITY) {
                        RuleBuilderField rbf = (RuleBuilderField) field;
                        if (entity.getPMap().containsKey(rbf.getJsonFieldName())) {
                            String json = entity.getPMap().get(rbf.getJsonFieldName()).getValue();
                            rbf.setJson(json);
                            DataWrapper dw = convertJsonToDataWrapper(json);
                            if (dw != null) {
                                rbf.setDataWrapper(dw);
                            }
                        }
                    }
                    if (basicFM.getFieldType() == SupportedFieldType.MEDIA) {
                        field.setValue(entityProp.getValue());
                        field.setDisplayValue(entityProp.getDisplayValue());
                        MediaField mf = (MediaField) field;
                        Class<MediaDto> type = entityConfiguration.lookupEntityClass(MediaDto.class.getName(), MediaDto.class);
                        mf.setMedia(mediaBuilderService.convertJsonToMedia(entityProp.getUnHtmlEncodedValue(), type));
                    } else if (!SupportedFieldType.PASSWORD_CONFIRM.equals(basicFM.getExplicitFieldType())) {
                        field.setValue(entityProp.getValue());
                        field.setDisplayValue(entityProp.getDisplayValue());
                    }
                }
            }
        }
    }
}
Also used : DataWrapper(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper) 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) MediaDto(org.broadleafcommerce.common.media.domain.MediaDto) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) RuleBuilderField(org.broadleafcommerce.openadmin.web.form.component.RuleBuilderField) MediaField(org.broadleafcommerce.openadmin.web.form.component.MediaField) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 83 with Property

use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.

the class FormBuilderServiceImpl method populateMapEntityFormFields.

@Override
public void populateMapEntityFormFields(EntityForm ef, Entity entity) {
    Field field = ef.findField("priorKey");
    Property entityProp = entity.findProperty("key");
    if (field != null && entityProp != null) {
        field.setValue(entityProp.getValue());
    }
}
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) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 84 with Property

use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.

the class FormBuilderServiceImpl method shouldHideField.

protected boolean shouldHideField(FieldMetadata fmd, Entity entity) {
    if (fmd == null || fmd.getShowIfFieldEquals() == null) {
        return false;
    }
    for (String property : fmd.getShowIfFieldEquals().keySet()) {
        List<String> values = fmd.getShowIfFieldEquals().get(property);
        Property entityProp = entity.findProperty(property);
        if (entityProp == null || values.contains(entityProp.getValue())) {
            return false;
        }
    }
    return true;
}
Also used : Property(org.broadleafcommerce.openadmin.dto.Property)

Example 85 with Property

use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.

the class MVELToDataWrapperTranslator method createRuleData.

public DataWrapper createRuleData(Entity[] entities, String mvelProperty, String quantityProperty, String idProperty, String containedProperty, RuleBuilderFieldService fieldService) {
    if (entities == null || entities.length == 0 || mvelProperty == null) {
        return null;
    }
    DataWrapper dataWrapper = new DataWrapper();
    String mvel = null;
    try {
        for (Entity e : entities) {
            Integer qty = null;
            Long id = null;
            Long containedId = null;
            for (Property p : e.getProperties()) {
                if (mvelProperty.equals(p.getName())) {
                    mvel = p.getValue();
                }
                if (quantityProperty != null && quantityProperty.equals(p.getName())) {
                    qty = Integer.parseInt(p.getValue());
                }
                if (idProperty != null && idProperty.equals(p.getName())) {
                    id = Long.parseLong(p.getValue());
                }
                if (containedProperty != null && containedProperty.equals(p.getName())) {
                    containedId = Long.parseLong(p.getValue());
                }
            }
            if (mvel != null) {
                Group group = groupingTranslator.createGroups(mvel);
                DataDTO dataDTO = createRuleDataDTO(null, group, fieldService);
                if (dataDTO != null) {
                    dataDTO.setPk(id);
                    dataDTO.setContainedPk(containedId);
                    dataDTO.setQuantity(qty);
                    dataWrapper.getData().add(dataDTO);
                    if (group.getSubGroups().size() > 0) {
                        Boolean invalidSubGroupFound = checkForInvalidSubGroup(dataDTO);
                        if (invalidSubGroupFound) {
                            throw new MVELTranslationException(MVELTranslationException.SUB_GROUP_DETECTED, SUB_GROUP_MESSAGE);
                        }
                    }
                }
            }
        }
    } catch (MVELTranslationException e) {
        LOG.error("Unable to translate rule MVEL", e);
        dataWrapper.getData().clear();
        dataWrapper.setError(e.getLocalizedMessage());
        dataWrapper.setRawMvel(mvel);
    }
    return dataWrapper;
}
Also used : DataWrapper(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper) Entity(org.broadleafcommerce.openadmin.dto.Entity) Group(org.broadleafcommerce.openadmin.web.rulebuilder.grouping.Group) DataDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataDTO) Property(org.broadleafcommerce.openadmin.dto.Property)

Aggregations

Property (org.broadleafcommerce.openadmin.dto.Property)120 Entity (org.broadleafcommerce.openadmin.dto.Entity)62 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)45 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)38 ArrayList (java.util.ArrayList)28 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)26 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)25 DataWrapper (org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper)21 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)19 HashMap (java.util.HashMap)18 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)18 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)16 Map (java.util.Map)15 ServiceException (org.broadleafcommerce.common.exception.ServiceException)15 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)15 Field (org.broadleafcommerce.openadmin.web.form.entity.Field)15 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14 AdornedTargetCollectionMetadata (org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)13 RuleBuilderField (org.broadleafcommerce.openadmin.web.form.component.RuleBuilderField)12 ComboField (org.broadleafcommerce.openadmin.web.form.entity.ComboField)12