Search in sources :

Example 1 with MetadataTools

use of com.haulmont.cuba.core.global.MetadataTools in project cuba by cuba-platform.

the class DynamicAttributesUtils method getDynamicAttributeValueAsString.

/**
 * For collection dynamic attributes the method returns a list of formatted collection items joined with the comma,
 * for non-collection dynamic attribute a formatted value is returned
 */
public static String getDynamicAttributeValueAsString(MetaProperty metaProperty, Object value) {
    CategoryAttribute categoryAttribute = getCategoryAttribute(metaProperty);
    MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
    if (categoryAttribute.getIsCollection()) {
        if (value instanceof Collection) {
            List<String> valuesList = ((Collection<Object>) value).stream().map(item -> metadataTools.format(item, metaProperty)).collect(Collectors.toList());
            return Joiner.on(", ").join(valuesList);
        }
    }
    return metadataTools.format(value, metaProperty);
}
Also used : Date(java.util.Date) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Collection(java.util.Collection) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) UUID(java.util.UUID) AppBeans(com.haulmont.cuba.core.global.AppBeans) Collectors(java.util.stream.Collectors) MetaClass(com.haulmont.chile.core.model.MetaClass) List(java.util.List) MetadataTools(com.haulmont.cuba.core.global.MetadataTools) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) Nullable(javax.annotation.Nullable) Joiner(com.google.common.base.Joiner) MetadataTools(com.haulmont.cuba.core.global.MetadataTools) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) Collection(java.util.Collection)

Example 2 with MetadataTools

use of com.haulmont.cuba.core.global.MetadataTools in project cuba by cuba-platform.

the class EntityImportView method addSystemProperties.

public EntityImportView addSystemProperties() {
    Metadata metadata = AppBeans.get(Metadata.class);
    MetaClass metaClass = metadata.getClassNN(entityClass);
    MetadataTools metadataTools = metadata.getTools();
    metaClass.getProperties().stream().filter(metadataTools::isSystem).forEach(metaProperty -> addLocalProperty(metaProperty.getName()));
    return this;
}
Also used : MetadataTools(com.haulmont.cuba.core.global.MetadataTools) MetaClass(com.haulmont.chile.core.model.MetaClass) Metadata(com.haulmont.cuba.core.global.Metadata)

Example 3 with MetadataTools

use of com.haulmont.cuba.core.global.MetadataTools in project cuba by cuba-platform.

the class MetadataTest method testPersistentAndTransientProperties.

@Test
public void testPersistentAndTransientProperties() throws Exception {
    MetadataTools tools = cont.metadata().getTools();
    // User
    MetaClass metaClass = cont.metadata().getSession().getClassNN(User.class);
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("id")));
    assertTrue(tools.isPersistent(metaClass, metaClass.getPropertyNN("id")));
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("createTs")));
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("login")));
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("group")));
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("userRoles")));
    // EntityLogItem
    metaClass = cont.metadata().getSession().getClassNN(EntityLogItem.class);
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("user")));
    assertFalse(tools.isPersistent(metaClass.getPropertyNN("attributes")));
    assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("attributes")));
    // Folder
    metaClass = cont.metadata().getSession().getClassNN(Folder.class);
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("name")));
    assertTrue(tools.isNotPersistent(new Folder(), "itemStyle"));
    // UserSessionEntity
    metaClass = cont.metadata().getSession().getClassNN(UserSessionEntity.class);
    // see JavaDocs on isPersistent() for the reason
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("id")));
    assertTrue(tools.isNotPersistent(metaClass, metaClass.getPropertyNN("id")));
    assertTrue(tools.isNotPersistent(metadata.create(metaClass), "id"));
    assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("login")));
    assertTrue(tools.isNotPersistent(metaClass, metaClass.getPropertyNN("login")));
    // TestTransientEntity
    metaClass = cont.metadata().getSession().getClassNN(TestNotPersistentEntity.class);
    // see JavaDocs on isPersistent() for the reason
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("id")));
    assertTrue(tools.isNotPersistent(metaClass, metaClass.getPropertyNN("id")));
    assertTrue(tools.isNotPersistent(metadata.create(metaClass), "id"));
    assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("name")));
    assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("info")));
    assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("embeddedRef")));
    assertFalse(tools.isEmbedded(metaClass.getPropertyNN("embeddedRef")));
}
Also used : MetadataTools(com.haulmont.cuba.core.global.MetadataTools) MetaClass(com.haulmont.chile.core.model.MetaClass) Folder(com.haulmont.cuba.core.entity.Folder) TestNotPersistentEntity(com.haulmont.cuba.testmodel.TestNotPersistentEntity) Test(org.junit.Test)

Example 4 with MetadataTools

use of com.haulmont.cuba.core.global.MetadataTools in project cuba by cuba-platform.

the class WebAbstractField method initBeanValidator.

protected void initBeanValidator() {
    MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
    MetaClass propertyEnclosingMetaClass = metadataTools.getPropertyEnclosingMetaClass(metaPropertyPath);
    Class enclosingJavaClass = propertyEnclosingMetaClass.getJavaClass();
    if (enclosingJavaClass != KeyValueEntity.class && !DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
        BeanValidation beanValidation = AppBeans.get(BeanValidation.NAME);
        javax.validation.Validator validator = beanValidation.getValidator();
        BeanDescriptor beanDescriptor = validator.getConstraintsForClass(enclosingJavaClass);
        if (beanDescriptor.isBeanConstrained()) {
            addValidator(new BeanValidator(enclosingJavaClass, metaProperty.getName()));
        }
    }
}
Also used : MetadataTools(com.haulmont.cuba.core.global.MetadataTools) BeanValidator(com.haulmont.cuba.gui.components.validators.BeanValidator) MetaClass(com.haulmont.chile.core.model.MetaClass) BeanValidation(com.haulmont.cuba.core.global.BeanValidation) BeanDescriptor(javax.validation.metadata.BeanDescriptor) MetaClass(com.haulmont.chile.core.model.MetaClass) KeyValueEntity(com.haulmont.cuba.core.entity.KeyValueEntity)

Example 5 with MetadataTools

use of com.haulmont.cuba.core.global.MetadataTools in project cuba by cuba-platform.

the class WebAbstractTextField method setValue.

@Override
public void setValue(Object value) {
    if (!(value instanceof String)) {
        String formattedValue;
        Datatype<String> stringDatatype = Datatypes.getNN(String.class);
        Datatype datatype = getActualDatatype();
        if (datatype != null && stringDatatype != datatype) {
            formattedValue = datatype.format(value, locale);
        } else {
            MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
            formattedValue = metadataTools.format(value);
        }
        super.setValue(formattedValue);
    } else {
        super.setValue(value);
    }
}
Also used : MetadataTools(com.haulmont.cuba.core.global.MetadataTools) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Aggregations

MetadataTools (com.haulmont.cuba.core.global.MetadataTools)11 MetaClass (com.haulmont.chile.core.model.MetaClass)9 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)3 Test (org.junit.Test)3 KeyValueEntity (com.haulmont.cuba.core.entity.KeyValueEntity)2 AppBeans (com.haulmont.cuba.core.global.AppBeans)2 BeanValidation (com.haulmont.cuba.core.global.BeanValidation)2 Metadata (com.haulmont.cuba.core.global.Metadata)2 Op (com.haulmont.cuba.core.global.filter.Op)2 OpManager (com.haulmont.cuba.core.global.filter.OpManager)2 BeanValidator (com.haulmont.cuba.gui.components.validators.BeanValidator)2 TestNotPersistentEntity (com.haulmont.cuba.testmodel.TestNotPersistentEntity)2 Collection (java.util.Collection)2 BeanDescriptor (javax.validation.metadata.BeanDescriptor)2 Joiner (com.google.common.base.Joiner)1 Datatype (com.haulmont.chile.core.datatypes.Datatype)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)1 Entity (com.haulmont.cuba.core.entity.Entity)1 Folder (com.haulmont.cuba.core.entity.Folder)1