use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.
the class PropertyTools method isInverseProperty.
protected boolean isInverseProperty(MetaProperty propertyToCheck, MetaPropertyPath parentPath) {
if (parentPath.length() > 0) {
MetaProperty parentProperty = parentPath.getMetaProperty();
MetaProperty inverseProperty = propertyToCheck.getInverse();
return parentProperty.equals(inverseProperty);
}
return false;
}
use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.
the class PropertyTools method findPropertiesByPathItems.
protected Map<String, MetaPropertyPath> findPropertiesByPathItems(MetaClass metaClass, String[] pathItems, MetaPropertyPath parentPath) {
log.debug("Find properties by path items: entity={}, PathItems={}, parentPath={}", metaClass, Arrays.deepToString(pathItems), parentPath);
if (pathItems.length == 0) {
return Collections.emptyMap();
}
Map<String, MetaPropertyPath> result;
String pathItem = pathItems[0];
log.debug("Path Item = {}", pathItem);
if (pathItems.length == 1) {
log.debug("'{}' is the last level of path", pathItem);
if (hasWildcard(pathItem)) {
Pattern pattern = Pattern.compile(pathItem.replace("*", ".*"));
List<MetaProperty> localPropertiesByPattern = findLocalPropertiesByPattern(metaClass, pattern);
result = localPropertiesByPattern.stream().filter(this::isSearchableProperty).filter(property -> !isInverseProperty(property, parentPath)).map(property -> createPropertyPath(parentPath, property)).collect(Collectors.toMap(MetaPropertyPath::toPathString, Function.identity()));
} else {
MetaProperty property = metaClass.findProperty(pathItem);
if (property != null && isSearchableProperty(property)) {
result = new HashMap<>();
MetaPropertyPath newPath = createPropertyPath(parentPath, property);
result.put(newPath.toPathString(), newPath);
} else {
result = Collections.emptyMap();
}
}
} else {
if (hasWildcard(pathItem)) {
Pattern pattern = Pattern.compile(pathItem.replace("*", ".*"));
List<MetaProperty> localPropertiesByPattern = findLocalPropertiesByPattern(metaClass, pattern);
result = new HashMap<>();
for (MetaProperty property : localPropertiesByPattern) {
if (isReferenceProperty(property) && !isInverseProperty(property, parentPath)) {
MetaClass nextMetaClass = property.getRange().asClass();
MetaPropertyPath nextPath = createPropertyPath(parentPath, property);
result.putAll(findPropertiesByPathItems(nextMetaClass, Arrays.copyOfRange(pathItems, 1, pathItems.length), nextPath));
}
}
} else {
MetaProperty property = metaClass.findProperty(pathItem);
if (property != null && isReferenceProperty(property)) {
MetaClass nextMetaClass = property.getRange().asClass();
MetaPropertyPath nextPath = createPropertyPath(parentPath, property);
result = findPropertiesByPathItems(nextMetaClass, Arrays.copyOfRange(pathItems, 1, pathItems.length), nextPath);
} else {
result = Collections.emptyMap();
}
}
}
return result;
}
use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.
the class OrderBasedEntityIdsLoader method loadNextIds.
@Override
public ResultHolder loadNextIds(EnqueueingSession session, int batchSize) {
String entityName = session.getEntityName();
MetaClass entityClass = metadata.getClass(entityName);
String orderingPropertyName = session.getOrderingProperty();
MetaProperty orderingProperty = entityClass.getProperty(orderingPropertyName);
String lastProcessedRawOrderingValue = session.getLastProcessedValue();
MetaProperty primaryKeyProperty = metadataTools.getPrimaryKeyProperty(entityClass);
if (primaryKeyProperty == null) {
throw new RuntimeException(String.format("Entity '%s' doesn't have primary key", entityName));
}
String primaryKeyPropertyName = primaryKeyProperty.getName();
ResultHolder result;
if (metadataTools.isEmbedded(orderingProperty)) {
log.warn("Sorted loading by embedded property is not supported - perform in-memory loading of all ids");
result = loadAllInMemory(entityClass);
} else {
Object lastProcessedValue = convertRawValue(orderingProperty, lastProcessedRawOrderingValue);
ValueLoadContext valueLoadContext = createValueLoadContext(entityClass, primaryKeyPropertyName, orderingPropertyName, lastProcessedValue, batchSize);
List<KeyValueEntity> loadedValues = loadValues(valueLoadContext);
List<Object> ids = loadedValues.stream().map(v -> v.getValue("objectId")).collect(Collectors.toList());
Object lastLoadedOrderingValue = resolveLastLoadedOrderingValue(loadedValues, primaryKeyPropertyName, orderingPropertyName);
result = new ResultHolder(ids, lastLoadedOrderingValue);
}
return result;
}
use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.
the class ExcelExporter method formatValueCell.
protected void formatValueCell(Cell cell, @Nullable Object cellValue, @Nullable MetaPropertyPath metaPropertyPath, int sizersIndex, int notificationRequired, int level, @Nullable Integer groupChildCount) {
if (cellValue == null) {
if (metaPropertyPath != null && metaPropertyPath.getRange().isDatatype()) {
Class javaClass = metaPropertyPath.getRange().asDatatype().getJavaClass();
if (Boolean.class.equals(javaClass)) {
cellValue = false;
}
} else {
return;
}
}
String childCountValue = "";
if (groupChildCount != null) {
childCountValue = " (" + groupChildCount + ")";
}
if (cellValue instanceof Number) {
Number n = (Number) cellValue;
Datatype datatype = null;
if (metaPropertyPath != null) {
Range range = metaPropertyPath.getMetaProperty().getRange();
if (range.isDatatype()) {
datatype = range.asDatatype();
}
}
datatype = datatype == null ? datatypeRegistry.get(n.getClass()) : datatype;
String str;
// and we should skip it
if (sizersIndex == 0 && level > 0) {
str = createSpaceString(level) + datatype.format(n);
cell.setCellValue(str);
} else {
try {
str = datatype.format(n);
Number result = (Number) datatype.parse(str);
if (result != null) {
if (n instanceof Integer || n instanceof Long || n instanceof Byte || n instanceof Short) {
cell.setCellValue(result.longValue());
cell.setCellStyle(integerFormatCellStyle);
} else {
cell.setCellValue(result.doubleValue());
cell.setCellStyle(doubleFormatCellStyle);
}
}
} catch (ParseException e) {
throw new RuntimeException("Unable to parse numeric value", e);
}
cell.setCellType(CellType.NUMERIC);
}
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof Date) {
Class javaClass = null;
if (metaPropertyPath != null) {
MetaProperty metaProperty = metaPropertyPath.getMetaProperty();
if (metaProperty.getRange().isDatatype()) {
javaClass = metaProperty.getRange().asDatatype().getJavaClass();
}
}
Date date = (Date) cellValue;
cell.setCellValue(date);
if (Objects.equals(java.sql.Time.class, javaClass)) {
cell.setCellStyle(timeFormatCellStyle);
} else if (Objects.equals(java.sql.Date.class, javaClass)) {
cell.setCellStyle(dateFormatCellStyle);
} else {
cell.setCellStyle(dateTimeFormatCellStyle);
}
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
String str = datatypeRegistry.get(Date.class).format(date);
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof Boolean) {
String str = "";
if (sizersIndex == 0) {
str += createSpaceString(level);
}
str += ((Boolean) cellValue) ? getMessage("excelExporter.true") : getMessage("excelExporter.false");
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof Enum) {
String message = (sizersIndex == 0 ? createSpaceString(level) : "") + messages.getMessage((Enum) cellValue);
cell.setCellValue(message + childCountValue);
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(message, stdFont);
}
} else if (cellValue instanceof Entity) {
Object entityVal = cellValue;
String instanceName = metadataTools.getInstanceName(entityVal);
String str = sizersIndex == 0 ? createSpaceString(level) + instanceName : instanceName;
str = str + childCountValue;
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof Collection) {
String str = "";
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof byte[]) {
String str = messages.getMessage("excelExporter.bytes");
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else {
String strValue = cellValue == null ? "" : cellValue.toString();
String str = sizersIndex == 0 ? createSpaceString(level) + strValue : strValue;
str = str + childCountValue;
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
}
}
use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.
the class AbstractDataGrid method setupAutowiredColumns.
protected void setupAutowiredColumns(EntityDataGridItems<E> entityDataGridSource) {
Collection<MetaPropertyPath> paths = getAutowiredProperties(entityDataGridSource);
for (MetaPropertyPath metaPropertyPath : paths) {
MetaProperty property = metaPropertyPath.getMetaProperty();
if (!property.getRange().getCardinality().isMany() && !metadataTools.isSystem(property)) {
String propertyName = property.getName();
ColumnImpl<E> column = createColumn(propertyName, metaPropertyPath, this);
MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(metaPropertyPath);
column.setCaption(messageTools.getPropertyCaption(propertyMetaClass, propertyName));
addColumn(column);
}
}
}
Aggregations