use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class DataGridLoader method loadColumn.
protected Column loadColumn(DataGrid component, Element element, Datasource ds) {
String id = element.attributeValue("id");
String property = element.attributeValue("property");
if (id == null) {
if (property != null) {
id = property;
} else {
throw new GuiDevelopmentException("A column must have whether id or property specified", context.getCurrentFrameId(), "DataGrid ID", component.getId());
}
}
Column column;
if (property != null) {
MetaPropertyPath metaPropertyPath = AppBeans.get(MetadataTools.NAME, MetadataTools.class).resolveMetaPropertyPath(ds.getMetaClass(), property);
column = component.addColumn(id, metaPropertyPath);
} else {
column = component.addColumn(id, null);
}
String expandRatio = element.attributeValue("expandRatio");
if (StringUtils.isNotEmpty(expandRatio)) {
column.setExpandRatio(Integer.parseInt(expandRatio));
}
String collapsed = element.attributeValue("collapsed");
if (StringUtils.isNotEmpty(collapsed)) {
column.setCollapsed(Boolean.parseBoolean(collapsed));
}
String collapsible = element.attributeValue("collapsible");
if (StringUtils.isNotEmpty(collapsible)) {
column.setCollapsible(Boolean.parseBoolean(collapsible));
}
String collapsingToggleCaption = element.attributeValue("collapsingToggleCaption");
if (StringUtils.isNotEmpty(collapsingToggleCaption)) {
collapsingToggleCaption = loadResourceString(collapsingToggleCaption);
column.setCollapsingToggleCaption(collapsingToggleCaption);
}
String sortable = element.attributeValue("sortable");
if (StringUtils.isNotEmpty(sortable)) {
column.setSortable(Boolean.parseBoolean(sortable));
}
String resizable = element.attributeValue("resizable");
if (StringUtils.isNotEmpty(resizable)) {
column.setResizable(Boolean.parseBoolean(resizable));
}
String editable = element.attributeValue("editable");
if (StringUtils.isNotEmpty(editable)) {
column.setEditable(Boolean.parseBoolean(editable));
}
// Default caption set to columns when it is added to a DataGrid,
// so we need to set caption as null to get caption from
// metaProperty if 'caption' attribute is empty
column.setCaption(null);
String caption = loadCaption(element);
if (caption == null) {
String columnCaption;
if (column.getPropertyPath() != null) {
MetaProperty metaProperty = column.getPropertyPath().getMetaProperty();
String propertyName = metaProperty.getName();
if (DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaProperty);
columnCaption = LocaleHelper.isLocalizedValueDefined(categoryAttribute.getLocaleNames()) ? categoryAttribute.getLocaleName() : StringUtils.capitalize(categoryAttribute.getName());
} else {
MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(column.getPropertyPath());
columnCaption = messageTools.getPropertyCaption(propertyMetaClass, propertyName);
}
} else {
Class<?> declaringClass = ds.getMetaClass().getJavaClass();
String className = declaringClass.getName();
int i = className.lastIndexOf('.');
if (i > -1) {
className = className.substring(i + 1);
}
columnCaption = messages.getMessage(declaringClass, className + "." + id);
}
column.setCaption(columnCaption);
} else {
column.setCaption(caption);
}
column.setXmlDescriptor(element);
Integer width = loadWidth(element, "width");
if (width != null) {
column.setWidth(width);
}
Integer minimumWidth = loadWidth(element, "minimumWidth");
if (minimumWidth != null) {
column.setMinimumWidth(minimumWidth);
}
Integer maximumWidth = loadWidth(element, "maximumWidth");
if (maximumWidth != null) {
column.setMaximumWidth(maximumWidth);
}
column.setFormatter(loadFormatter(element));
return column;
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class DsBuilder method buildDatasource.
public Datasource buildDatasource() {
init();
Datasource datasource;
try {
if (master == null && property == null) {
if (dsClass == null) {
datasource = createDatasource();
} else {
datasource = (Datasource) dsClass.newInstance();
}
datasource.setup(dsContext, dataSupplier, id, metaClass, view);
} else {
boolean isEmbedded = false;
if (master != null) {
MetaClass metaClass = master.getMetaClass();
MetaProperty metaProperty = metaClass.getProperty(property);
MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
isEmbedded = metadataTools.isEmbedded(metaProperty);
}
if (dsClass == null) {
datasource = isEmbedded ? createEmbeddedDatasource() : createPropertyDatasource();
} else {
datasource = (Datasource) dsClass.newInstance();
}
((NestedDatasource) datasource).setup(id, master, property);
}
} catch (IllegalAccessException | InstantiationException e) {
throw new RuntimeException(e);
}
datasource.setAllowCommit(allowCommit);
registerDatasource(datasource);
return datasource;
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class AbstractComparator method compareAsc.
protected int compareAsc(Object o1, Object o2) {
int c;
if (o1 instanceof String && o2 instanceof String) {
c = ((String) o1).compareToIgnoreCase((String) o2);
} else if (o1 instanceof Comparable && o2 instanceof Comparable) {
c = ((Comparable) o1).compareTo(o2);
} else if (o1 instanceof Instance && o2 instanceof Instance) {
MetaClass metaClass = metadata.getClassNN(o1.getClass());
Collection<MetaProperty> namePatternProperties = metadata.getTools().getNamePatternProperties(metaClass, true);
if (namePatternProperties.isEmpty()) {
c = ((Instance) o1).getInstanceName().compareToIgnoreCase(((Instance) o2).getInstanceName());
} else {
c = 0;
for (MetaProperty property : namePatternProperties) {
Object v1 = ((Instance) o1).getValue(property.getName());
Object v2 = ((Instance) o2).getValue(property.getName());
c = compareAsc(v1, v2);
if (c != 0)
break;
}
}
} else if (Objects.equals(o1, o2)) {
c = 0;
} else if (o1 == null && o2 != null) {
c = nullsLast;
} else {
c = -nullsLast;
}
return c;
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class EntityCopyUtils method copyCompositionsBack.
public static void copyCompositionsBack(Entity source, Entity dest) {
Preconditions.checkNotNullArgument(source, "source is null");
Preconditions.checkNotNullArgument(dest, "dest is null");
for (MetaProperty srcProperty : source.getMetaClass().getProperties()) {
String name = srcProperty.getName();
MetaProperty dstProperty = dest.getMetaClass().getProperty(name);
if (dstProperty != null && !dstProperty.isReadOnly()) {
try {
Object value = source.getValue(name);
if (value != null && srcProperty.getRange().getCardinality().isMany() && srcProperty.getType() == MetaProperty.Type.COMPOSITION) {
((AbstractInstance) dest).setValue(name, source.getValue(name), false);
} else {
dest.setValue(name, source.getValue(name));
}
} catch (RuntimeException e) {
Throwable cause = ExceptionUtils.getRootCause(e);
if (cause == null)
cause = e;
// ignore exception on copy for not loaded fields
if (!isNotLoadedAttributeException(cause))
throw e;
}
}
}
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class EntityCopyUtils method copyCompositions.
public static void copyCompositions(Entity source, Entity dest) {
Preconditions.checkNotNullArgument(source, "source is null");
Preconditions.checkNotNullArgument(dest, "dest is null");
if (source instanceof BaseDbGeneratedIdEntity && dest instanceof BaseDbGeneratedIdEntity) {
((BaseDbGeneratedIdEntity) dest).setId(((BaseDbGeneratedIdEntity) source).getId());
}
for (MetaProperty srcProperty : source.getMetaClass().getProperties()) {
String name = srcProperty.getName();
MetaProperty dstProperty = dest.getMetaClass().getProperty(name);
if (dstProperty != null && !dstProperty.isReadOnly()) {
try {
Object value = source.getValue(name);
if (value != null && srcProperty.getRange().getCardinality().isMany() && srcProperty.getType() == MetaProperty.Type.COMPOSITION) {
// noinspection unchecked
Collection<Entity> srcCollection = (Collection) value;
// Copy first to a Set to remove duplicates that could be created on repeated editing newly
// added items
Collection<Entity> tmpCollection = new LinkedHashSet<>();
for (Entity item : srcCollection) {
Entity copy = copyCompositions(item);
tmpCollection.add(copy);
}
Collection<Entity> dstCollection;
if (value instanceof List)
dstCollection = new ArrayList<>(tmpCollection);
else
dstCollection = tmpCollection;
dest.setValue(name, dstCollection);
} else {
dest.setValue(name, source.getValue(name));
}
} catch (RuntimeException e) {
Throwable cause = ExceptionUtils.getRootCause(e);
if (cause == null)
cause = e;
// ignore exception on copy for not loaded fields
if (!isNotLoadedAttributeException(cause))
throw e;
}
}
}
if (source instanceof BaseGenericIdEntity && dest instanceof BaseGenericIdEntity) {
BaseGenericIdEntity destGenericEntity = (BaseGenericIdEntity) dest;
BaseGenericIdEntity<?> sourceGenericEntity = (BaseGenericIdEntity<?>) source;
BaseEntityInternalAccess.setDetached(destGenericEntity, BaseEntityInternalAccess.isDetached(sourceGenericEntity));
BaseEntityInternalAccess.setNew(destGenericEntity, BaseEntityInternalAccess.isNew(sourceGenericEntity));
destGenericEntity.setDynamicAttributes(sourceGenericEntity.getDynamicAttributes());
}
}
Aggregations