Search in sources :

Example 1 with DataDrivenEnumerationValue

use of org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumerationValue in project BroadleafCommerce by BroadleafCommerce.

the class DynamicFieldPersistenceHandlerHelper method buildDynamicProperty.

public Property buildDynamicProperty(FieldDefinition definition, Class<?> inheritedType) {
    Property property = new Property();
    property.setName(definition.getName());
    BasicFieldMetadata fieldMetadata = new BasicFieldMetadata();
    property.setMetadata(fieldMetadata);
    fieldMetadata.setFieldType(definition.getFieldType());
    fieldMetadata.setMutable(true);
    fieldMetadata.setInheritedFromType(inheritedType.getName());
    fieldMetadata.setAvailableToTypes(new String[] { inheritedType.getName() });
    fieldMetadata.setForeignKeyCollection(false);
    fieldMetadata.setMergedPropertyType(MergedPropertyType.PRIMARY);
    fieldMetadata.setLength(definition.getMaxLength());
    if (definition.getDataDrivenEnumeration() != null && !CollectionUtils.isEmpty(definition.getDataDrivenEnumeration().getEnumValues())) {
        int count = definition.getDataDrivenEnumeration().getEnumValues().size();
        String[][] enumItems = new String[count][2];
        for (int j = 0; j < count; j++) {
            DataDrivenEnumerationValue item = definition.getDataDrivenEnumeration().getEnumValues().get(j);
            enumItems[j][0] = item.getKey();
            enumItems[j][1] = item.getDisplay();
        }
        fieldMetadata.setEnumerationValues(enumItems);
    }
    fieldMetadata.setName(definition.getName());
    fieldMetadata.setFriendlyName(definition.getFriendlyName());
    fieldMetadata.setSecurityLevel(definition.getSecurityLevel() == null ? "" : definition.getSecurityLevel());
    fieldMetadata.setVisibility(definition.getHiddenFlag() ? VisibilityEnum.HIDDEN_ALL : VisibilityEnum.VISIBLE_ALL);
    fieldMetadata.setTab("General");
    fieldMetadata.setTabOrder(100);
    fieldMetadata.setExplicitFieldType(SupportedFieldType.UNKNOWN);
    fieldMetadata.setLargeEntry(definition.getTextAreaFlag());
    fieldMetadata.setProminent(false);
    fieldMetadata.setColumnWidth(String.valueOf(definition.getColumnWidth()));
    fieldMetadata.setBroadleafEnumeration("");
    fieldMetadata.setReadOnly(false);
    fieldMetadata.setRequiredOverride(definition.getRequiredFlag());
    fieldMetadata.setHint(definition.getHint());
    fieldMetadata.setHelpText(definition.getHelpText());
    fieldMetadata.setTooltip(definition.getTooltip());
    fieldMetadata.setTranslatable(true);
    if (definition.getValidationRegEx() != null) {
        Map<String, String> itemMap = new HashMap<String, String>();
        itemMap.put("regularExpression", definition.getValidationRegEx());
        itemMap.put(ConfigurationItem.ERROR_MESSAGE, definition.getValidationErrorMesageKey());
        List<Map<String, String>> configurationItems = new ArrayList<Map<String, String>>();
        configurationItems.add(itemMap);
        fieldMetadata.getValidationConfigurations().put(RegexPropertyValidator.class.getName(), configurationItems);
    }
    if (definition.getFieldType().equals(SupportedFieldType.ADDITIONAL_FOREIGN_KEY)) {
        fieldMetadata.setForeignKeyClass(definition.getAdditionalForeignKeyClass());
        fieldMetadata.setOwningClass(definition.getAdditionalForeignKeyClass());
        fieldMetadata.setForeignKeyDisplayValueProperty("__adminMainEntity");
    }
    return property;
}
Also used : HashMap(java.util.HashMap) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) DataDrivenEnumerationValue(org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumerationValue) ArrayList(java.util.ArrayList) RegexPropertyValidator(org.broadleafcommerce.openadmin.server.service.persistence.validation.RegexPropertyValidator) Property(org.broadleafcommerce.openadmin.dto.Property) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with DataDrivenEnumerationValue

use of org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumerationValue in project BroadleafCommerce by BroadleafCommerce.

the class DataDrivenEnumVariableExpression method getEnumValues.

/**
 * Looks up a list of {@link DataDrivenEnumerationValue} by the {@link DataDrivenEnumeration#getKey()} specified by <b>key</b>
 * @param key lookup for the {@link DataDrivenEnumeration}
 * @param sort optional, either 'ASCENDING' or 'DESCENDING' depending on how you want the result list sorted
 * @return the list of {@link DataDrivenEnumerationValue} for the given <b>key</b>
 */
public List<DataDrivenEnumerationValue> getEnumValues(String key, final String sort) {
    if (StringUtils.isEmpty(key)) {
        throw new IllegalArgumentException("No 'key' parameter was passed to find enumeration values");
    }
    DataDrivenEnumeration ddEnum = enumService.findEnumByKey(key);
    if (ddEnum == null) {
        throw new IllegalArgumentException("Could not find a data driven enumeration keyed by " + key);
    }
    List<DataDrivenEnumerationValue> enumValues = new ArrayList<>(ddEnum.getEnumValues());
    if (StringUtils.isNotEmpty(sort)) {
        Collections.sort(enumValues, new Comparator<DataDrivenEnumerationValue>() {

            @Override
            public int compare(DataDrivenEnumerationValue arg0, DataDrivenEnumerationValue arg1) {
                if (sort.equals("ASCENDING")) {
                    return arg0.getDisplay().compareTo(arg1.getDisplay());
                } else {
                    return arg1.getDisplay().compareTo(arg0.getDisplay());
                }
            }
        });
    }
    return enumValues;
}
Also used : DataDrivenEnumerationValue(org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumerationValue) ArrayList(java.util.ArrayList) DataDrivenEnumeration(org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumeration)

Aggregations

ArrayList (java.util.ArrayList)2 DataDrivenEnumerationValue (org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumerationValue)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DataDrivenEnumeration (org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumeration)1 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)1 Property (org.broadleafcommerce.openadmin.dto.Property)1 RegexPropertyValidator (org.broadleafcommerce.openadmin.server.service.persistence.validation.RegexPropertyValidator)1