Search in sources :

Example 1 with AttributeType

use of io.jmix.dynattr.AttributeType in project jmix by jmix-framework.

the class CategoryAttrsEdit method getDataTypeOptions.

protected Map<String, AttributeType> getDataTypeOptions() {
    Map<String, AttributeType> options = new TreeMap<>();
    AttributeType[] types = AttributeType.values();
    for (AttributeType attributeType : types) {
        String key = AttributeType.class.getSimpleName() + "." + attributeType.toString();
        options.put(messages.getMessage(AttributeType.class, key), attributeType);
    }
    return options;
}
Also used : AttributeType(io.jmix.dynattr.AttributeType)

Example 2 with AttributeType

use of io.jmix.dynattr.AttributeType in project jmix by jmix-framework.

the class CategoryAttrsFragment method categoryAttrsTableDefaultValueColumnGenerator.

@Install(to = "categoryAttrsTable.defaultValue", subject = "columnGenerator")
protected Label<String> categoryAttrsTableDefaultValueColumnGenerator(CategoryAttribute attribute) {
    String defaultValue = "";
    AttributeType dataType = attribute.getDataType();
    switch(dataType) {
        case BOOLEAN:
            Boolean b = attribute.getDefaultBoolean();
            if (b != null)
                defaultValue = BooleanUtils.isTrue(b) ? messages.getMessage("trueString") : messages.getMessage("falseString");
            break;
        case DATE:
            Date dateTime = attribute.getDefaultDate();
            if (dateTime != null) {
                String dateTimeFormat = formatStringsRegistry.getFormatStrings(currentAuthentication.getLocale()).getDateTimeFormat();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateTimeFormat);
                defaultValue = simpleDateFormat.format(dateTime);
            } else if (BooleanUtils.isTrue(attribute.getDefaultDateIsCurrent())) {
                defaultValue = messages.getMessage(CategoryAttrsFragment.class, "categoryAttrsTable.currentDate");
            }
            break;
        case DATE_WITHOUT_TIME:
            LocalDate dateWoTime = attribute.getDefaultDateWithoutTime();
            if (dateWoTime != null) {
                String dateWoTimeFormat = formatStringsRegistry.getFormatStrings(currentAuthentication.getLocale()).getDateFormat();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateWoTimeFormat);
                defaultValue = dateWoTime.format(formatter);
            } else if (BooleanUtils.isTrue(attribute.getDefaultDateIsCurrent())) {
                defaultValue = messages.getMessage(CategoryAttrsFragment.class, "categoryAttrsTable.currentDate");
            }
            break;
        case DECIMAL:
            BigDecimal defaultDecimal = attribute.getDefaultDecimal();
            if (defaultDecimal != null) {
                defaultValue = defaultDecimal.toString();
            }
            break;
        case DOUBLE:
            Double defaultDouble = attribute.getDefaultDouble();
            if (defaultDouble != null) {
                defaultValue = defaultDouble.toString();
            }
            break;
        case ENTITY:
            Class<?> entityClass = attribute.getJavaType();
            if (entityClass != null) {
                defaultValue = "";
                if (attribute.getObjectDefaultEntityId() != null) {
                    MetaClass metaClass = metadata.getClass(entityClass);
                    LoadContext<?> lc = new LoadContext<>(metadata.getClass(attribute.getJavaType()));
                    FetchPlan fetchPlan = fetchPlanRepository.getFetchPlan(metaClass, FetchPlan.INSTANCE_NAME);
                    lc.setFetchPlan(fetchPlan);
                    String pkName = referenceToEntitySupport.getPrimaryKeyForLoadingEntity(metaClass);
                    lc.setQueryString(String.format("select e from %s e where e.%s = :entityId", metaClass.getName(), pkName)).setParameter("entityId", attribute.getObjectDefaultEntityId());
                    Object entity = dataManager.load(lc);
                    if (entity != null) {
                        defaultValue = metadataTools.getInstanceName(entity);
                    }
                }
            } else {
                defaultValue = messages.getMessage(CategoryAttrsFragment.class, "categoryAttrsTable.entityNotFound");
            }
            break;
        case ENUMERATION:
        case STRING:
            defaultValue = attribute.getDefaultString();
            break;
        case INTEGER:
            Integer defaultInt = attribute.getDefaultInt();
            if (defaultInt != null) {
                defaultValue = defaultInt.toString();
            }
            break;
    }
    Label<String> defaultValueLabel = uiComponents.create(Label.TYPE_STRING);
    defaultValueLabel.setValue(defaultValue);
    return defaultValueLabel;
}
Also used : LocalDate(java.time.LocalDate) Date(java.util.Date) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) MetaClass(io.jmix.core.metamodel.model.MetaClass) AttributeType(io.jmix.dynattr.AttributeType) SimpleDateFormat(java.text.SimpleDateFormat) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 3 with AttributeType

use of io.jmix.dynattr.AttributeType in project jmix by jmix-framework.

the class DynAttrComponentGenerationStrategy method initValuesSelectActionByAttribute.

protected void initValuesSelectActionByAttribute(ValuesSelectAction selectAction, AttributeDefinition attribute) {
    AttributeType attributeType = attribute.getDataType();
    switch(attributeType) {
        case DATE:
            selectAction.setJavaClass(Date.class);
            selectAction.setResolution(DateField.Resolution.MIN);
            break;
        case DATE_WITHOUT_TIME:
            selectAction.setJavaClass(LocalDate.class);
            selectAction.setResolution(DateField.Resolution.DAY);
            break;
        case STRING:
            selectAction.setJavaClass(String.class);
            break;
        case ENUMERATION:
            selectAction.setJavaClass(String.class);
            selectAction.setOptions(new MapOptions(getLocalizedEnumerationMap(attribute)));
            break;
        case DOUBLE:
            selectAction.setJavaClass(Double.class);
            break;
        case DECIMAL:
            selectAction.setJavaClass(BigDecimal.class);
            break;
        case INTEGER:
            selectAction.setJavaClass(Integer.class);
            break;
        case BOOLEAN:
            selectAction.setJavaClass(Boolean.class);
            break;
        case ENTITY:
            Class<?> javaType = attribute.getJavaType();
            if (javaType != null) {
                MetaClass metaClass = metadata.getClass(javaType);
                selectAction.setEntityName(metaClass.getName());
            }
            selectAction.setLookupScreenId(attribute.getConfiguration().getLookupScreen());
            selectAction.setUseComboBox(attribute.getConfiguration().isLookup());
            break;
    }
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) MapOptions(io.jmix.ui.component.data.options.MapOptions) AttributeType(io.jmix.dynattr.AttributeType)

Example 4 with AttributeType

use of io.jmix.dynattr.AttributeType in project jmix by jmix-framework.

the class CategoryAttrsEdit method refreshAttributesValues.

protected void refreshAttributesValues() {
    AttributeType attributeType = dataTypeField.getValue();
    CategoryAttribute categoryAttribute = getEditedEntity();
    CategoryAttributeConfiguration configuration = categoryAttribute.getConfiguration();
    if (ENTITY.equals(attributeType)) {
        if (!Strings.isNullOrEmpty(categoryAttribute.getEntityClass())) {
            Map<String, String> options = ((MapOptions<String>) screenField.getOptions()).getItemsCollection();
            categoryAttribute.setScreen(options.containsValue(categoryAttribute.getScreen()) ? categoryAttribute.getScreen() : null);
        }
        if (configuration.getOptionsLoaderType() == SQL) {
            configuration.setOptionsLoaderType(JPQL);
        }
    } else if (configuration.getOptionsLoaderType() == JPQL) {
        configuration.setOptionsLoaderType(null);
    }
    if (DATE.equals(attributeType)) {
        if (Boolean.TRUE.equals(categoryAttribute.getDefaultDateIsCurrent())) {
            categoryAttribute.setDefaultDate(null);
        }
    }
    if (DATE_WITHOUT_TIME.equals(attributeType)) {
        if (Boolean.TRUE.equals(categoryAttribute.getDefaultDateIsCurrent())) {
            categoryAttribute.setDefaultDateWithoutTime(null);
        }
    }
    if (BOOLEAN.equals(attributeType)) {
        categoryAttribute.setIsCollection(null);
    }
    if (categoryAttribute.getDataType() == null || !SUPPORTED_OPTIONS_TYPES.contains(categoryAttribute.getDataType())) {
        categoryAttribute.setLookup(false);
    }
    if (!Boolean.TRUE.equals(categoryAttribute.getLookup())) {
        configuration.setOptionsLoaderType(null);
        configuration.setOptionsLoaderScript(null);
        categoryAttribute.setWhereClause(null);
        categoryAttribute.setJoinClause(null);
    } else {
        OptionsLoaderType optionsType = configuration.getOptionsLoaderType();
        if (optionsType == JPQL) {
            configuration.setOptionsLoaderScript(null);
        } else if (optionsType == GROOVY || optionsType == SQL) {
            categoryAttribute.setWhereClause(null);
            categoryAttribute.setJoinClause(null);
        } else if (optionsType == null) {
            configuration.setOptionsLoaderScript(null);
            categoryAttribute.setWhereClause(null);
            categoryAttribute.setJoinClause(null);
            if (categoryAttribute.getDataType() == ENTITY) {
                configuration.setOptionsLoaderType(JPQL);
            }
        }
    }
}
Also used : CategoryAttribute(io.jmix.dynattr.model.CategoryAttribute) CategoryAttributeConfiguration(io.jmix.dynattr.model.CategoryAttributeConfiguration) MapOptions(io.jmix.ui.component.data.options.MapOptions) AttributeType(io.jmix.dynattr.AttributeType) OptionsLoaderType(io.jmix.dynattr.OptionsLoaderType)

Example 5 with AttributeType

use of io.jmix.dynattr.AttributeType in project jmix by jmix-framework.

the class CategoryAttrsEdit method onValidation.

@Subscribe
protected void onValidation(ValidationEvent event) {
    ValidationErrors validationErrors = new ValidationErrors();
    CategoryAttribute attribute = getEditedEntity();
    AttributeType dataType = attribute.getDataType();
    CategoryAttributeConfiguration configuration = attribute.getConfiguration();
    if (INTEGER.equals(dataType)) {
        ValidationErrors errors = validateNumbers(INTEGER, configuration.getMinInt(), configuration.getMaxInt(), attribute.getDefaultInt());
        validationErrors.addAll(errors);
    } else if (DOUBLE.equals(dataType)) {
        ValidationErrors errors = validateNumbers(DOUBLE, configuration.getMinDouble(), configuration.getMaxDouble(), attribute.getDefaultDouble());
        validationErrors.addAll(errors);
    } else if (DECIMAL.equals(dataType)) {
        ValidationErrors errors = validateNumbers(DECIMAL, configuration.getMinDecimal(), configuration.getMaxDecimal(), attribute.getDefaultDecimal());
        validationErrors.addAll(errors);
    } else if (ENUMERATION.equals(dataType)) {
        ValidationErrors errors = validateEnumeration(attribute.getEnumeration(), attribute.getDefaultString());
        validationErrors.addAll(errors);
    }
    Category category = getEditedEntity().getCategory();
    if (category != null && category.getCategoryAttrs() != null) {
        for (CategoryAttribute categoryAttribute : category.getCategoryAttrs()) {
            if (!categoryAttribute.equals(attribute)) {
                if (categoryAttribute.getName().equals(attribute.getName())) {
                    validationErrors.add(messages.getMessage(CategoryAttrsEdit.class, "uniqueName"));
                    return;
                } else if (categoryAttribute.getCode().equals(attribute.getCode())) {
                    validationErrors.add(messages.getMessage(CategoryAttrsEdit.class, "uniqueCode"));
                    return;
                }
            }
        }
    }
    event.addErrors(validationErrors);
}
Also used : CategoryAttribute(io.jmix.dynattr.model.CategoryAttribute) Category(io.jmix.dynattr.model.Category) CategoryAttributeConfiguration(io.jmix.dynattr.model.CategoryAttributeConfiguration) AttributeType(io.jmix.dynattr.AttributeType)

Aggregations

AttributeType (io.jmix.dynattr.AttributeType)7 MetaClass (io.jmix.core.metamodel.model.MetaClass)3 CategoryAttribute (io.jmix.dynattr.model.CategoryAttribute)3 CategoryAttributeConfiguration (io.jmix.dynattr.model.CategoryAttributeConfiguration)3 MapOptions (io.jmix.ui.component.data.options.MapOptions)3 OptionsLoaderType (io.jmix.dynattr.OptionsLoaderType)2 Category (io.jmix.dynattr.model.Category)2 BigDecimal (java.math.BigDecimal)2 LocalDate (java.time.LocalDate)2 Strings (com.google.common.base.Strings)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Multimap (com.google.common.collect.Multimap)1 io.jmix.core (io.jmix.core)1 CrudEntityContext (io.jmix.core.accesscontext.CrudEntityContext)1 Datatype (io.jmix.core.metamodel.datatype.Datatype)1 DatatypeRegistry (io.jmix.core.metamodel.datatype.DatatypeRegistry)1 FormatStringsRegistry (io.jmix.core.metamodel.datatype.FormatStringsRegistry)1 AdaptiveNumberDatatype (io.jmix.core.metamodel.datatype.impl.AdaptiveNumberDatatype)1 AccessDeniedException (io.jmix.core.security.AccessDeniedException)1