use of com.haulmont.chile.core.model.MetaPropertyPath 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;
}
use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.
the class GroupDelegate method groupItems.
protected GroupInfo<MetaPropertyPath> groupItems(int propertyIndex, GroupInfo parent, List<GroupInfo> children, T item, final LinkedMap groupValues) {
final MetaPropertyPath property = (MetaPropertyPath) groupProperties[propertyIndex++];
Object itemValue = getValueByProperty(item, property);
groupValues.put(property, itemValue);
GroupInfo<MetaPropertyPath> groupInfo = new GroupInfo<>(groupValues);
itemGroups.put(item.getId(), groupInfo);
if (!parents.containsKey(groupInfo)) {
parents.put(groupInfo, parent);
}
if (!children.contains(groupInfo)) {
children.add(groupInfo);
}
List<GroupInfo> groupChildren = this.children.computeIfAbsent(groupInfo, k -> new ArrayList<>());
if (propertyIndex < groupProperties.length) {
groupInfo = groupItems(propertyIndex, groupInfo, groupChildren, item, groupValues);
}
return groupInfo;
}
use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.
the class DynamicAttributeCustomFieldGenerator method generateField.
@Override
public Component generateField(Datasource datasource, String propertyId) {
ComponentsFactory componentsFactory = AppBeans.get(ComponentsFactory.class);
ListEditor listEditor = componentsFactory.createComponent(ListEditor.class);
MetaPropertyPath metaPropertyPath = DynamicAttributesUtils.getMetaPropertyPath(datasource.getMetaClass(), propertyId);
if (metaPropertyPath == null) {
log.error("MetaPropertyPath for dynamic attribute {} not found", propertyId);
return null;
}
CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaPropertyPath.getMetaProperty());
if (categoryAttribute == null) {
log.error("Dynamic attribute {} not found", propertyId);
return null;
}
listEditor.setEntityJoinClause(categoryAttribute.getJoinClause());
listEditor.setEntityWhereClause(categoryAttribute.getWhereClause());
ListEditor.ItemType itemType = listEditorItemTypeFromDynamicAttrType(categoryAttribute.getDataType());
listEditor.setItemType(itemType);
Metadata metadata = AppBeans.get(Metadata.class);
Scripting scripting = AppBeans.get(Scripting.class);
if (!Strings.isNullOrEmpty(categoryAttribute.getEntityClass())) {
Class<?> clazz = scripting.loadClass(categoryAttribute.getEntityClass());
if (clazz == null) {
log.error("Unable to find class of entity {} for dynamic attribute {}", categoryAttribute.getEntityClass(), categoryAttribute.getCode());
return null;
}
MetaClass metaClass = metadata.getClassNN(clazz);
listEditor.setEntityName(metaClass.getName());
listEditor.setUseLookupField(BooleanUtils.isTrue(categoryAttribute.getLookup()));
}
// noinspection unchecked
datasource.addStateChangeListener(e -> {
if (e.getState() == Datasource.State.VALID) {
Object value = datasource.getItem().getValue(propertyId);
if (value != null && value instanceof Collection) {
listEditor.setValue(value);
}
}
});
listEditor.addValueChangeListener(e -> {
datasource.getItem().setValue(propertyId, e.getValue());
});
listEditor.setWidthFull();
return listEditor;
}
use of com.haulmont.chile.core.model.MetaPropertyPath 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;
}
use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.
the class EntityDiffManager method getPropertyDiffs.
/**
* Get diffs for entity properties
*
* @param diffView View
* @param firstEntity First entity
* @param secondEntity Second entity
* @param diffBranch Diff branch
* @return Diff list
*/
protected List<EntityPropertyDiff> getPropertyDiffs(View diffView, Entity firstEntity, Entity secondEntity, Stack<Object> diffBranch) {
List<EntityPropertyDiff> propertyDiffs = new LinkedList<>();
MetaClass viewMetaClass = metadata.getSession().getClass(diffView.getEntityClass());
MetaClass metaClass = extendedEntities.getEffectiveMetaClass(viewMetaClass);
Collection<MetaPropertyPath> metaProperties = metadataTools.getViewPropertyPaths(diffView, metaClass);
for (MetaPropertyPath metaPropertyPath : metaProperties) {
MetaProperty metaProperty = metaPropertyPath.getMetaProperty();
if (!metadataTools.isNotPersistent(metaProperty) && !metadataTools.isSystem(metaProperty)) {
ViewProperty viewProperty = diffView.getProperty(metaProperty.getName());
Object firstValue = firstEntity != null ? getPropertyValue(firstEntity, metaPropertyPath) : null;
Object secondValue = secondEntity != null ? getPropertyValue(secondEntity, metaPropertyPath) : null;
EntityPropertyDiff diff = getPropertyDifference(firstValue, secondValue, metaProperty, viewProperty, diffBranch);
if (diff != null)
propertyDiffs.add(diff);
}
}
Collection<CategoryAttribute> categoryAttributes = dynamicAttributesManagerAPI.getAttributesForMetaClass(metaClass);
if (categoryAttributes != null) {
for (CategoryAttribute categoryAttribute : categoryAttributes) {
MetaPropertyPath metaPropertyPath = DynamicAttributesUtils.getMetaPropertyPath(metaClass, categoryAttribute);
MetaProperty metaProperty = metaPropertyPath.getMetaProperty();
Object firstValue = firstEntity != null ? getPropertyValue(firstEntity, metaPropertyPath) : null;
Object secondValue = secondEntity != null ? getPropertyValue(secondEntity, metaPropertyPath) : null;
EntityPropertyDiff diff = getDynamicAttributeDifference(firstValue, secondValue, metaProperty, categoryAttribute);
if (diff != null)
propertyDiffs.add(diff);
}
}
Comparator<EntityPropertyDiff> comparator = Comparator.comparing(EntityPropertyDiff::getName);
Collections.sort(propertyDiffs, comparator);
return propertyDiffs;
}
Aggregations