Search in sources :

Example 46 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty 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 47 with MetaProperty

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

the class EntityLoadInfoBuilder method parse.

/**
 * Parse an info from the string.
 *
 * @param str string representation of the info. See {@link EntityLoadInfo} for formats.
 * @return info instance or null if the string can not be parsed. Any exception is silently swallowed.
 */
@Nullable
public EntityLoadInfo parse(String str) {
    boolean isNew = false;
    if (str.startsWith(EntityLoadInfo.NEW_PREFIX)) {
        str = str.substring("NEW-".length());
        isNew = true;
    }
    int idDashPos = str.indexOf('-');
    if (idDashPos == -1) {
        if (isNew) {
            MetaClass metaClass = metadata.getSession().getClass(str);
            if (metaClass == null) {
                return null;
            }
            Entity entity = metadata.create(metaClass);
            MetaProperty primaryKeyProp = metadata.getTools().getPrimaryKeyProperty(metaClass);
            boolean stringKey = primaryKeyProp != null && primaryKeyProp.getJavaType().equals(String.class);
            return new EntityLoadInfo(entity.getId(), metaClass, null, stringKey, true);
        }
        return null;
    }
    String entityName = str.substring(0, idDashPos);
    MetaClass metaClass = metadata.getSession().getClass(entityName);
    if (metaClass == null) {
        return null;
    }
    Object id;
    String viewName;
    boolean stringKey = false;
    MetaProperty primaryKeyProp = metadata.getTools().getPrimaryKeyProperty(metaClass);
    if (primaryKeyProp == null)
        return null;
    if (primaryKeyProp.getJavaType().equals(UUID.class)) {
        int viewDashPos = -1;
        int dashCount = StringUtils.countMatches(str, "-");
        if (dashCount < 5) {
            return null;
        }
        if (dashCount >= 6) {
            int i = 0;
            while (i < 6) {
                viewDashPos = str.indexOf('-', viewDashPos + 1);
                i++;
            }
            viewName = str.substring(viewDashPos + 1);
        } else {
            viewDashPos = str.length();
            viewName = null;
        }
        String entityIdStr = str.substring(idDashPos + 1, viewDashPos);
        try {
            id = UuidProvider.fromString(entityIdStr);
        } catch (Exception e) {
            return null;
        }
    } else {
        String entityIdStr;
        if (primaryKeyProp.getJavaType().equals(String.class)) {
            stringKey = true;
            int viewDashPos = str.indexOf("}-", idDashPos + 2);
            if (viewDashPos > -1) {
                viewName = str.substring(viewDashPos + 2);
            } else {
                viewDashPos = str.length() - 1;
                viewName = null;
            }
            entityIdStr = str.substring(idDashPos + 2, viewDashPos);
        } else {
            int viewDashPos = str.indexOf('-', idDashPos + 1);
            if (viewDashPos > -1) {
                viewName = str.substring(viewDashPos + 1);
            } else {
                viewDashPos = str.length();
                viewName = null;
            }
            entityIdStr = str.substring(idDashPos + 1, viewDashPos);
        }
        try {
            if (primaryKeyProp.getJavaType().equals(Long.class)) {
                id = Long.valueOf(entityIdStr);
            } else if (primaryKeyProp.getJavaType().equals(Integer.class)) {
                id = Integer.valueOf(entityIdStr);
            } else {
                id = entityIdStr;
            }
        } catch (Exception e) {
            return null;
        }
    }
    return new EntityLoadInfo(id, metaClass, viewName, stringKey, isNew);
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 48 with MetaProperty

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

the class EntityStates method checkLoadedWithView.

protected void checkLoadedWithView(Entity entity, View view, Set<Entity> visited) {
    if (visited.contains(entity)) {
        return;
    }
    visited.add(entity);
    for (ViewProperty property : view.getProperties()) {
        MetaClass metaClass = entity.getMetaClass();
        MetaProperty metaProperty = metaClass.getPropertyNN(property.getName());
        if (!isLoaded(entity, property.getName())) {
            String errorMessage = String.format("%s.%s is not loaded", entity.getClass().getSimpleName(), property.getName());
            throw new IllegalArgumentException(errorMessage);
        }
        if (metaProperty.getRange().isClass()) {
            View propertyView = property.getView();
            if (propertyView != null && metadataTools.isPersistent(metaProperty)) {
                Object value = entity.getValue(metaProperty.getName());
                if (value != null) {
                    if (!metaProperty.getRange().getCardinality().isMany()) {
                        checkLoadedWithView((Entity) value, propertyView, visited);
                    } else {
                        @SuppressWarnings("unchecked") Collection<Entity> collection = (Collection) value;
                        for (Entity item : collection) {
                            checkLoadedWithView(item, propertyView, visited);
                        }
                    }
                }
            }
        }
    }
    // after check we remove item from visited because different subtrees may have different view for one instance
    visited.remove(entity);
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) Collection(java.util.Collection) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 49 with MetaProperty

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

the class GlobalPersistentAttributesLoadChecker method isLoaded.

@Override
public boolean isLoaded(Object entity, String property) {
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    if (isDynamicAttribute(property)) {
        @SuppressWarnings("unchecked") Map<String, CategoryAttributeValue> dynamicAttributes = ((BaseGenericIdEntity) entity).getDynamicAttributes();
        if (dynamicAttributes == null) {
            return false;
        }
        String attributeCode = decodeAttributeCode(property);
        return dynamicAttributes.containsKey(attributeCode);
    }
    MetaProperty metaProperty = metaClass.getPropertyNN(property);
    if (!metadataTools.isPersistent(metaProperty)) {
        List<String> relatedProperties = metadataTools.getRelatedProperties(metaProperty);
        if (relatedProperties.isEmpty()) {
            return true;
        } else {
            for (String relatedProperty : relatedProperties) {
                if (!isLoaded(entity, relatedProperty))
                    return false;
            }
            return true;
        }
    }
    PropertyLoadedState isLoaded = isLoadedCommonCheck(entity, property);
    if (isLoaded != PropertyLoadedState.UNKNOWN) {
        return isLoaded == PropertyLoadedState.YES;
    }
    return isLoadedSpecificCheck(entity, property, metaClass, metaProperty);
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) MetaProperty(com.haulmont.chile.core.model.MetaProperty) CategoryAttributeValue(com.haulmont.cuba.core.entity.CategoryAttributeValue)

Example 50 with MetaProperty

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

the class MessageTools method inferMessagePack.

/**
 * Returns message pack inferred from {@link com.haulmont.cuba.core.entity.annotation.LocalizedValue} annotation.
 * @param attribute attribute name
 * @param instance  entity instance
 * @return          inferred message pack or null
 */
@Nullable
public String inferMessagePack(String attribute, Instance instance) {
    Objects.requireNonNull(attribute, "attribute is null");
    Objects.requireNonNull(instance, "instance is null");
    MetaClass metaClass = metadata.getSession().getClassNN(instance.getClass());
    MetaProperty property = metaClass.getPropertyNN(attribute);
    Map<String, Object> attributes = metadata.getTools().getMetaAnnotationAttributes(property.getAnnotations(), LocalizedValue.class);
    String messagePack = (String) attributes.get("messagePack");
    if (!StringUtils.isBlank(messagePack))
        return messagePack;
    else {
        String messagePackExpr = (String) attributes.get("messagePackExpr");
        if (!StringUtils.isBlank(messagePackExpr)) {
            try {
                return instance.getValueEx(messagePackExpr);
            } catch (Exception e) {
                log.error("Unable to infer message pack from expression: " + messagePackExpr, e);
            }
        }
    }
    return null;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Nullable(javax.annotation.Nullable)

Aggregations

MetaProperty (com.haulmont.chile.core.model.MetaProperty)157 MetaClass (com.haulmont.chile.core.model.MetaClass)102 Entity (com.haulmont.cuba.core.entity.Entity)44 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)26 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)21 Range (com.haulmont.chile.core.model.Range)13 Nullable (javax.annotation.Nullable)11 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)10 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)9 java.util (java.util)9 Element (org.dom4j.Element)9 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)8 Collectors (java.util.stream.Collectors)6 Inject (javax.inject.Inject)6 Query (com.haulmont.cuba.core.Query)5 SoftDelete (com.haulmont.cuba.core.entity.SoftDelete)5 Collection (java.util.Collection)5 MessageTools (com.haulmont.cuba.core.global.MessageTools)4 PropertyDatasource (com.haulmont.cuba.gui.data.PropertyDatasource)4 Logger (org.slf4j.Logger)4