Search in sources :

Example 86 with MetaProperty

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;
}
Also used : MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 87 with MetaProperty

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;
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) java.util(java.util) Logger(org.slf4j.Logger) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Metadata(io.jmix.core.Metadata) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) Pattern(java.util.regex.Pattern) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) MetadataTools(io.jmix.core.MetadataTools) Pattern(java.util.regex.Pattern) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 88 with MetaProperty

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;
}
Also used : DataManager(io.jmix.core.DataManager) MetaClass(io.jmix.core.metamodel.model.MetaClass) Logger(org.slf4j.Logger) TransactionDefinition(org.springframework.transaction.TransactionDefinition) ValueLoadContext(io.jmix.core.ValueLoadContext) KeyValueEntity(io.jmix.core.entity.KeyValueEntity) EntityIdsLoader(io.jmix.search.index.queue.EntityIdsLoader) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Metadata(io.jmix.core.Metadata) EntityManager(javax.persistence.EntityManager) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ArrayList(java.util.ArrayList) EnqueueingSession(io.jmix.search.index.queue.entity.EnqueueingSession) Query(javax.persistence.Query) List(java.util.List) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) MetadataTools(io.jmix.core.MetadataTools) Collections(java.util.Collections) Nullable(javax.annotation.Nullable) StoreAwareLocator(io.jmix.data.StoreAwareLocator) MetaClass(io.jmix.core.metamodel.model.MetaClass) ValueLoadContext(io.jmix.core.ValueLoadContext) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) KeyValueEntity(io.jmix.core.entity.KeyValueEntity)

Example 89 with MetaProperty

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);
        }
    }
}
Also used : Entity(io.jmix.core.Entity) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) Range(io.jmix.core.metamodel.model.Range) Datatype(io.jmix.core.metamodel.datatype.Datatype) ParseException(java.text.ParseException) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 90 with MetaProperty

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);
        }
    }
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) KeyValueMetaClass(io.jmix.core.impl.keyvalue.KeyValueMetaClass) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Aggregations

MetaProperty (io.jmix.core.metamodel.model.MetaProperty)267 MetaClass (io.jmix.core.metamodel.model.MetaClass)162 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)53 Nullable (javax.annotation.Nullable)36 Range (io.jmix.core.metamodel.model.Range)23 Autowired (org.springframework.beans.factory.annotation.Autowired)23 Collectors (java.util.stream.Collectors)22 java.util (java.util)20 Component (org.springframework.stereotype.Component)18 Entity (io.jmix.core.Entity)17 EntityValues (io.jmix.core.entity.EntityValues)17 io.jmix.core (io.jmix.core)16 Logger (org.slf4j.Logger)15 LoggerFactory (org.slf4j.LoggerFactory)15 ArrayList (java.util.ArrayList)12 Collection (java.util.Collection)12 EntityValueSource (io.jmix.ui.component.data.meta.EntityValueSource)10 Metadata (io.jmix.core.Metadata)9 KeyValueMetaClass (io.jmix.core.impl.keyvalue.KeyValueMetaClass)9 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)8