use of org.broadleafcommerce.openadmin.web.form.entity.CodeField 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);
}
}
}
Aggregations