Search in sources :

Example 6 with Range

use of com.haulmont.chile.core.model.Range in project cuba by cuba-platform.

the class DesktopAbstractTextField method validateRawValue.

protected Object validateRawValue(String rawValue) {
    if (trimming && rawValue != null)
        rawValue = rawValue.trim();
    if ((datasource != null) && (metaPropertyPath != null)) {
        Range range = metaProperty.getRange();
        if (range.isDatatype())
            datatype = metaPropertyPath.getRange().asDatatype();
        if (range.isClass())
            return prevValue;
        if (range.isEnum()) {
            try {
                return range.asEnumeration().parse(rawValue, locale);
            } catch (ParseException e) {
                showValidationMessage();
                return prevValue;
            }
        }
    }
    if (datatype != null) {
        try {
            // double conversion to verify type constraints
            // used for properly parsing BigDecimal values
            Object datatypeValue = datatype.parse(rawValue, locale);
            String formattedValue;
            if (valueFormatter.getFormatter() == null) {
                formattedValue = datatype.format(datatypeValue, locale);
            } else {
                formattedValue = valueFormatter.getFormatter().format(datatypeValue);
            }
            return datatype.parse(formattedValue, locale);
        } catch (ParseException ignored) {
            showValidationMessage();
            return prevValue;
        }
    }
    return rawValue;
}
Also used : ParseException(java.text.ParseException) Range(com.haulmont.chile.core.model.Range)

Example 7 with Range

use of com.haulmont.chile.core.model.Range in project cuba by cuba-platform.

the class CollectionDsHelper method createProperties.

public static List<MetaPropertyPath> createProperties(View view, MetaClass metaClass) {
    List<MetaPropertyPath> properties = new ArrayList<>();
    MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
    if (view != null && metadataTools.isPersistent(metaClass)) {
        for (ViewProperty property : view.getProperties()) {
            final String name = property.getName();
            final MetaProperty metaProperty = metaClass.getProperty(name);
            if (metaProperty == null) {
                String message = String.format("Unable to find property %s for entity %s", name, metaClass.getName());
                throw new DevelopmentException(message);
            }
            if (!metadataTools.isPersistent(metaProperty)) {
                String message = String.format("Specified transient property %s in view for datasource with persistent entity %s", name, metaClass.getName());
                LoggerFactory.getLogger(CollectionDsHelper.class).warn(message);
                continue;
            }
            final Range range = metaProperty.getRange();
            if (range == null) {
                continue;
            }
            final Range.Cardinality cardinality = range.getCardinality();
            if (!cardinality.isMany()) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
        // add all non-persistent properties
        for (MetaProperty metaProperty : metaClass.getProperties()) {
            if (metadataTools.isNotPersistent(metaProperty)) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
    } else {
        if (view != null) {
            LoggerFactory.getLogger(CollectionDsHelper.class).warn("Specified view {} for datasource with not persistent entity {}", view.getName(), metaClass.getName());
        }
        for (MetaProperty metaProperty : metaClass.getProperties()) {
            final Range range = metaProperty.getRange();
            if (range == null)
                continue;
            final Range.Cardinality cardinality = range.getCardinality();
            if (!cardinality.isMany()) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
    }
    return properties;
}
Also used : MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) ArrayList(java.util.ArrayList) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Range(com.haulmont.chile.core.model.Range)

Example 8 with Range

use of com.haulmont.chile.core.model.Range in project cuba by cuba-platform.

the class AbstractComponentGenerationStrategy method createComponentInternal.

protected Component createComponentInternal(ComponentGenerationContext context) {
    MetaClass metaClass = context.getMetaClass();
    MetaPropertyPath mpp = resolveMetaPropertyPath(metaClass, context.getProperty());
    Element xmlDescriptor = context.getXmlDescriptor();
    if (mpp != null) {
        Range mppRange = mpp.getRange();
        if (mppRange.isDatatype()) {
            Class type = mppRange.asDatatype().getJavaClass();
            MetaProperty metaProperty = mpp.getMetaProperty();
            if (DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
                CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaProperty);
                if (categoryAttribute != null && categoryAttribute.getDataType() == PropertyType.ENUMERATION) {
                    return createEnumField(context);
                }
            }
            if (xmlDescriptor != null && "true".equalsIgnoreCase(xmlDescriptor.attributeValue("link"))) {
                return createDatatypeLinkField(context);
            } else {
                boolean hasMaskAttribute = xmlDescriptor != null && xmlDescriptor.attribute("mask") != null;
                if (type.equals(String.class)) {
                    if (hasMaskAttribute) {
                        return createMaskedField(context);
                    } else {
                        return createStringField(context, mpp);
                    }
                } else if (type.equals(UUID.class)) {
                    return createUuidField(context);
                } else if (type.equals(Boolean.class)) {
                    return createBooleanField(context);
                } else if (type.equals(java.sql.Date.class) || type.equals(Date.class)) {
                    return createDateField(context);
                } else if (type.equals(Time.class)) {
                    return createTimeField(context);
                } else if (Number.class.isAssignableFrom(type)) {
                    if (hasMaskAttribute) {
                        return createMaskedField(context);
                    } else {
                        Field currencyField = createCurrencyField(context, mpp);
                        if (currencyField != null) {
                            return currencyField;
                        }
                        return createNumberField(context);
                    }
                }
            }
        } else if (mppRange.isClass()) {
            MetaProperty metaProperty = mpp.getMetaProperty();
            Class<?> javaType = metaProperty.getJavaType();
            if (FileDescriptor.class.isAssignableFrom(javaType)) {
                return createFileUploadField(context);
            }
            if (!Collection.class.isAssignableFrom(javaType)) {
                return createEntityField(context, mpp);
            }
        } else if (mppRange.isEnum()) {
            return createEnumField(context);
        }
    }
    return null;
}
Also used : Element(org.dom4j.Element) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) Range(com.haulmont.chile.core.model.Range) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaClass(com.haulmont.chile.core.model.MetaClass) DynamicAttributesMetaProperty(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty) MetaProperty(com.haulmont.chile.core.model.MetaProperty) UUID(java.util.UUID)

Example 9 with Range

use of com.haulmont.chile.core.model.Range in project cuba by cuba-platform.

the class DomainModelBuilder method addProperty.

private void addProperty(EntityBuilder builder, MetaClass metaClass, MetaProperty prop) {
    String name = prop.getName();
    String userFriendlyName = null;
    if (loadCaptions) {
        userFriendlyName = messageTools.getPropertyCaption(metaClass, prop.getName());
    }
    boolean isEmbedded = metadataTools.isEmbedded(prop);
    MetaProperty.Type type = prop.getType();
    Class<?> javaType = prop.getJavaType();
    Range range = prop.getRange();
    switch(type) {
        case COMPOSITION:
        case ASSOCIATION:
            if (range.isClass()) {
                MetaClass rangeClass = range.asClass();
                if (range.getCardinality().isMany()) {
                    builder.addCollectionReferenceAttribute(name, rangeClass.getName(), userFriendlyName);
                } else {
                    builder.addReferenceAttribute(name, rangeClass.getName(), userFriendlyName, isEmbedded);
                }
            } else {
                builder.addSingleValueAttribute(javaType, name, userFriendlyName);
            }
            break;
        case ENUM:
            // todo
            builder.addSingleValueAttribute(javaType, name, userFriendlyName);
            break;
        case DATATYPE:
            builder.addSingleValueAttribute(javaType, name, userFriendlyName);
            break;
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Range(com.haulmont.chile.core.model.Range)

Example 10 with Range

use of com.haulmont.chile.core.model.Range in project cuba by cuba-platform.

the class AttributeSecuritySupport method setNullPropertyValue.

protected void setNullPropertyValue(Entity entity, MetaProperty property) {
    // Using reflective access to field because the attribute can be unfetched if loading not partial entities,
    // which is the case when in-memory constraints exist
    Range range = property.getRange();
    if (range.isClass()) {
        Object nullValue = null;
        if (range.getCardinality().isMany()) {
            Class<?> propertyType = property.getJavaType();
            if (List.class.isAssignableFrom(propertyType)) {
                nullValue = new ArrayList<>();
            } else if (Set.class.isAssignableFrom(propertyType)) {
                nullValue = new LinkedHashSet<>();
            }
        }
        BaseEntityInternalAccess.setValue(entity, property.getName(), nullValue);
        BaseEntityInternalAccess.setValueForHolder(entity, property.getName(), nullValue);
    } else {
        BaseEntityInternalAccess.setValue(entity, property.getName(), null);
    }
}
Also used : Range(com.haulmont.chile.core.model.Range)

Aggregations

Range (com.haulmont.chile.core.model.Range)12 MetaProperty (com.haulmont.chile.core.model.MetaProperty)7 MetaClass (com.haulmont.chile.core.model.MetaClass)6 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)3 Entity (com.haulmont.cuba.core.entity.Entity)3 Element (org.dom4j.Element)3 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)2 AppConfig (com.haulmont.cuba.gui.AppConfig)2 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)2 Formatter (com.haulmont.cuba.gui.components.Formatter)2 DsBuilder (com.haulmont.cuba.gui.data.DsBuilder)2 DsContextImplementation (com.haulmont.cuba.gui.data.impl.DsContextImplementation)2 ComponentsFactory (com.haulmont.cuba.gui.xml.layout.ComponentsFactory)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 java.util (java.util)2 Inject (javax.inject.Inject)2 StringUtils (org.apache.commons.lang.StringUtils)2 Files (com.google.common.io.Files)1 JsonElement (com.google.gson.JsonElement)1