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);
}
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);
}
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);
}
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);
}
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;
}
Aggregations