Search in sources :

Example 11 with Datatype

use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.

the class MetadataLoader method loadDatatypesFromClasspathResource.

protected void loadDatatypesFromClasspathResource() {
    SAXReader reader = new SAXReader();
    URL resource = Datatypes.class.getResource(getGetDatatypesResourcePath());
    if (resource != null) {
        log.info("Loading datatypes from " + resource);
        try {
            Document document = reader.read(resource);
            Element element = document.getRootElement();
            List<Element> datatypeElements = Dom4j.elements(element, "datatype");
            for (Element datatypeElement : datatypeElements) {
                String datatypeClassName = datatypeElement.attributeValue("class");
                try {
                    Datatype datatype;
                    Class<Datatype> datatypeClass = ReflectionHelper.getClass(datatypeClassName);
                    try {
                        final Constructor<Datatype> constructor = datatypeClass.getConstructor(Element.class);
                        datatype = constructor.newInstance(datatypeElement);
                    } catch (Throwable e) {
                        datatype = datatypeClass.newInstance();
                    }
                    String id = datatypeElement.attributeValue("id");
                    if (Strings.isNullOrEmpty(id))
                        id = guessDatatypeId(datatype);
                    datatypeRegistry.register(datatype, id, true);
                } catch (Throwable e) {
                    log.error(String.format("Fail to load datatype '%s'", datatypeClassName), e);
                }
            }
        } catch (DocumentException e) {
            log.error("Fail to load datatype settings", e);
        }
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) AnnotatedElement(java.lang.reflect.AnnotatedElement) DocumentException(org.dom4j.DocumentException) Document(org.dom4j.Document) URL(java.net.URL) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Example 12 with Datatype

use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.

the class AppPropertiesLocator method setDataType.

private void setDataType(Method method, AppPropertyEntity entity) {
    Class<?> returnType = method.getReturnType();
    if (returnType.isPrimitive()) {
        if (returnType == boolean.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Boolean.class));
        if (returnType == int.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Integer.class));
        if (returnType == long.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Long.class));
        if (returnType == double.class || returnType == float.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Double.class));
    } else if (returnType.isEnum()) {
        entity.setDataTypeName("enum");
        EnumStore enumStoreAnn = method.getAnnotation(EnumStore.class);
        if (enumStoreAnn != null && enumStoreAnn.value() == EnumStoreMode.ID) {
            // noinspection unchecked
            Class<EnumClass> enumeration = (Class<EnumClass>) returnType;
            entity.setEnumValues(Arrays.asList(enumeration.getEnumConstants()).stream().map(ec -> String.valueOf(ec.getId())).collect(Collectors.joining(",")));
        } else {
            entity.setEnumValues(Arrays.asList(returnType.getEnumConstants()).stream().map(Object::toString).collect(Collectors.joining(",")));
        }
    } else {
        Datatype<?> datatype = datatypes.get(returnType);
        if (datatype != null)
            entity.setDataTypeName(datatypes.getId(datatype));
        else
            entity.setDataTypeName(datatypes.getIdByJavaClass(String.class));
    }
    String dataTypeName = entity.getDataTypeName();
    if (!dataTypeName.equals("enum")) {
        Datatype datatype = Datatypes.get(dataTypeName);
        String v = null;
        try {
            v = entity.getDefaultValue();
            datatype.parse(v);
            v = entity.getCurrentValue();
            datatype.parse(v);
        } catch (ParseException e) {
            log.debug("Cannot parse '{}' with {} datatype, using StringDatatype for property {}", v, datatype, entity.getName());
            entity.setDataTypeName(datatypes.getIdByJavaClass(String.class));
        }
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) ReflectionHelper(com.haulmont.bali.util.ReflectionHelper) LoggerFactory(org.slf4j.LoggerFactory) ClassMetadata(org.springframework.core.type.ClassMetadata) DatatypeRegistry(com.haulmont.chile.core.datatypes.DatatypeRegistry) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) Inject(javax.inject.Inject) Datatype(com.haulmont.chile.core.datatypes.Datatype) MetadataReader(org.springframework.core.type.classreading.MetadataReader) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) ParseException(java.text.ParseException) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) Resource(org.springframework.core.io.Resource) com.haulmont.cuba.core.config.type(com.haulmont.cuba.core.config.type) Logger(org.slf4j.Logger) Datatypes(com.haulmont.chile.core.datatypes.Datatypes) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Component(org.springframework.stereotype.Component) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) com.haulmont.cuba.core.config.defaults(com.haulmont.cuba.core.config.defaults) AppContext(com.haulmont.cuba.core.sys.AppContext) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) Entity(com.haulmont.cuba.core.entity.Entity) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) ParseException(java.text.ParseException) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Example 13 with Datatype

use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.

the class EntitySnapshot method getLabel.

@MetaProperty(related = { "snapshotDate,author" })
public String getLabel() {
    String name = "";
    if (author != null && StringUtils.isNotEmpty(this.author.getCaption())) {
        name += this.author.getCaption() + " ";
    }
    Datatype datatype = Datatypes.getNN(Date.class);
    UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
    if (userSessionSource != null && userSessionSource.checkCurrentUserSession()) {
        name += datatype.format(snapshotDate, userSessionSource.getLocale());
    }
    return StringUtils.trim(name);
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) Datatype(com.haulmont.chile.core.datatypes.Datatype) MetaProperty(com.haulmont.chile.core.annotations.MetaProperty)

Example 14 with Datatype

use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.

the class MetadataTools method format.

/**
 * Formats a value according to the property type.
 *
 * @param value    object to format
 * @param property metadata
 * @return formatted value as string
 */
public String format(@Nullable Object value, MetaProperty property) {
    if (value == null)
        return "";
    Objects.requireNonNull(property, "property is null");
    Range range = property.getRange();
    if (DynamicAttributesUtils.isDynamicAttribute(property)) {
        CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(property);
        if (categoryAttribute.getDataType().equals(PropertyType.ENUMERATION)) {
            return LocaleHelper.getEnumLocalizedValue((String) value, categoryAttribute.getEnumerationLocales());
        }
        if (categoryAttribute.getIsCollection() && value instanceof Collection) {
            return DynamicAttributesUtils.getDynamicAttributeValueAsString(property, value);
        }
    }
    if (range.isDatatype()) {
        Datatype datatype = range.asDatatype();
        if (value instanceof Date && datatype.getJavaClass().equals(Date.class)) {
            Boolean ignoreUserTimeZone = getMetaAnnotationValue(property, IgnoreUserTimeZone.class);
            if (!Boolean.TRUE.equals(ignoreUserTimeZone)) {
                return datatypeFormatter.formatDateTime((Date) value);
            }
        }
        return datatype.format(value, userSessionSource.getLocale());
    } else if (range.isEnum()) {
        return messages.getMessage((Enum) value);
    } else if (value instanceof Instance) {
        return ((Instance) value).getInstanceName();
    } else {
        return value.toString();
    }
}
Also used : Datatype(com.haulmont.chile.core.datatypes.Datatype)

Example 15 with Datatype

use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.

the class SessionAttributeEditor method commitAndClose.

@Override
public void commitAndClose() {
    SessionAttribute item = datasource.getItem();
    if (item.getStringValue() != null) {
        Datatype dt = Datatypes.get(item.getDatatype());
        try {
            Object object = dt.parse(item.getStringValue());
            item.setStringValue(object == null ? "" : object.toString());
        } catch (IllegalArgumentException | ParseException e) {
            showNotification(getMessage("unableToParseValue"), NotificationType.ERROR);
            return;
        }
    }
    super.commitAndClose();
}
Also used : ParseException(java.text.ParseException) SessionAttribute(com.haulmont.cuba.security.entity.SessionAttribute) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Aggregations

Datatype (com.haulmont.chile.core.datatypes.Datatype)27 ParseException (java.text.ParseException)9 Entity (com.haulmont.cuba.core.entity.Entity)6 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)6 Datatypes (com.haulmont.chile.core.datatypes.Datatypes)3 AdaptiveNumberDatatype (com.haulmont.chile.core.datatypes.impl.AdaptiveNumberDatatype)3 MetaClass (com.haulmont.chile.core.model.MetaClass)3 Element (org.dom4j.Element)3 Logger (org.slf4j.Logger)3 JsonObject (com.google.gson.JsonObject)2 EnumClass (com.haulmont.chile.core.datatypes.impl.EnumClass)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)2 AppBeans (com.haulmont.cuba.core.global.AppBeans)2 Metadata (com.haulmont.cuba.core.global.Metadata)2 ComponentsFactory (com.haulmont.cuba.gui.xml.layout.ComponentsFactory)2 RestAPIException (com.haulmont.restapi.exception.RestAPIException)2 Map (java.util.Map)2 Inject (javax.inject.Inject)2 Document (org.dom4j.Document)2