Search in sources :

Example 16 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class ConditionDescriptorsTreeBuilder method build.

@Override
public Tree<AbstractConditionDescriptor> build() {
    Messages messages = AppBeans.get(Messages.class);
    String messagesPack = filter.getFrame().getMessagesPack();
    CollectionDatasource datasource = filter.getDatasource();
    Tree<AbstractConditionDescriptor> tree = new Tree<>();
    List<AbstractConditionDescriptor> propertyDescriptors = new ArrayList<>();
    List<AbstractConditionDescriptor> customDescriptors = new ArrayList<>();
    boolean propertiesExplicitlyDefined = false;
    if (filter.getXmlDescriptor() != null) {
        for (Element element : Dom4j.elements(filter.getXmlDescriptor())) {
            AbstractConditionDescriptor conditionDescriptor;
            if ("properties".equals(element.getName())) {
                addMultiplePropertyDescriptors(element, propertyDescriptors, filter);
                propertiesExplicitlyDefined = true;
            } else if ("property".equals(element.getName())) {
                conditionDescriptor = new PropertyConditionDescriptor(element, messagesPack, filterComponentName, datasource);
                propertyDescriptors.add(conditionDescriptor);
                propertiesExplicitlyDefined = true;
            } else if ("custom".equals(element.getName())) {
                conditionDescriptor = new CustomConditionDescriptor(element, messagesPack, filterComponentName, datasource);
                customDescriptors.add(conditionDescriptor);
                propertiesExplicitlyDefined = true;
            } else {
                throw new UnsupportedOperationException("Element not supported: " + element.getName());
            }
        }
    }
    if (!propertiesExplicitlyDefined) {
        addMultiplePropertyDescriptors(".*", "", propertyDescriptors, filter);
    }
    propertyDescriptors.sort(new ConditionDescriptorComparator());
    customDescriptors.sort(new ConditionDescriptorComparator());
    HeaderConditionDescriptor propertyHeaderDescriptor = new HeaderConditionDescriptor("propertyConditions", messages.getMainMessage("filter.addCondition.propertyConditions"), filterComponentName, datasource);
    HeaderConditionDescriptor customHeaderDescriptor = new HeaderConditionDescriptor("customConditions", messages.getMainMessage("filter.addCondition.customConditions"), filterComponentName, datasource);
    Node<AbstractConditionDescriptor> propertyHeaderNode = new Node<>(propertyHeaderDescriptor);
    Node<AbstractConditionDescriptor> customHeaderNode = new Node<>(customHeaderDescriptor);
    int currentDepth = 0;
    for (AbstractConditionDescriptor propertyDescriptor : propertyDescriptors) {
        MetaClass propertyDsMetaClass = propertyDescriptor.getDatasourceMetaClass();
        MetaPropertyPath propertyPath = propertyDsMetaClass.getPropertyPath(propertyDescriptor.getName());
        if (propertyPath == null) {
            log.error("Property path for {} of metaClass {} not found", propertyDescriptor.getName(), propertyDsMetaClass.getName());
            continue;
        }
        MetaProperty metaProperty = propertyPath.getMetaProperty();
        MetaClass propertyEnclosingMetaClass = metadataTools.getPropertyEnclosingMetaClass(propertyPath);
        if (isPropertyAllowed(propertyEnclosingMetaClass, metaProperty) && !excludedProperties.contains(metaProperty.getName())) {
            Node<AbstractConditionDescriptor> node = new Node<>(propertyDescriptor);
            propertyHeaderNode.addChild(node);
            if (currentDepth < hierarchyDepth) {
                recursivelyFillPropertyDescriptors(node, currentDepth);
            }
        }
    }
    for (AbstractConditionDescriptor customDescriptor : customDescriptors) {
        Node<AbstractConditionDescriptor> node = new Node<>(customDescriptor);
        customHeaderNode.addChild(node);
    }
    List<Node<AbstractConditionDescriptor>> rootNodes = new ArrayList<>();
    rootNodes.add(propertyHeaderNode);
    if (!customDescriptors.isEmpty())
        rootNodes.add(customHeaderNode);
    if (!hideCustomConditions && security.isSpecificPermitted(CUSTOM_CONDITIONS_PERMISSION)) {
        rootNodes.add(new Node<>(new CustomConditionCreator(filterComponentName, datasource)));
    }
    if (!hideDynamicAttributes && !dynamicAttributes.getAttributesForMetaClass(datasource.getMetaClass()).isEmpty()) {
        rootNodes.add(new Node<>(new DynamicAttributesConditionCreator(filterComponentName, datasource, "")));
    }
    if (FtsConfigHelper.getEnabled()) {
        rootNodes.add(new Node<>(new FtsConditionDescriptor(filterComponentName, datasource)));
    }
    tree.setRootNodes(rootNodes);
    return tree;
}
Also used : CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Element(org.dom4j.Element) Node(com.haulmont.bali.datastruct.Node) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) ConditionsTree(com.haulmont.cuba.gui.components.filter.ConditionsTree) Tree(com.haulmont.bali.datastruct.Tree) MetaProperty(com.haulmont.chile.core.model.MetaProperty) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 17 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class ConditionDescriptorsTreeBuilder method recursivelyFillPropertyDescriptors.

protected void recursivelyFillPropertyDescriptors(Node<AbstractConditionDescriptor> parentNode, int currentDepth) {
    currentDepth++;
    List<AbstractConditionDescriptor> descriptors = new ArrayList<>();
    MetaClass filterMetaClass = filter.getDatasource().getMetaClass();
    String propertyId = parentNode.getData().getName();
    MetaPropertyPath mpp = filterMetaClass.getPropertyPath(propertyId);
    if (mpp == null) {
        throw new RuntimeException("Unable to find property " + propertyId);
    }
    MetaProperty metaProperty = mpp.getMetaProperty();
    if (metaProperty.getRange().isClass() && (metadataTools.getCrossDataStoreReferenceIdProperty(storeName, metaProperty) == null)) {
        MetaClass childMetaClass = metaProperty.getRange().asClass();
        for (MetaProperty property : childMetaClass.getProperties()) {
            if (isPropertyAllowed(childMetaClass, property)) {
                String propertyPath = mpp.toString() + "." + property.getName();
                if (excludedProperties.contains(propertyPath))
                    continue;
                PropertyConditionDescriptor childPropertyConditionDescriptor = new PropertyConditionDescriptor(propertyPath, null, filter.getFrame().getMessagesPack(), filterComponentName, filter.getDatasource());
                descriptors.add(childPropertyConditionDescriptor);
            }
        }
    }
    descriptors.sort(new ConditionDescriptorComparator());
    for (AbstractConditionDescriptor descriptor : descriptors) {
        Node<AbstractConditionDescriptor> newNode = new Node<>(descriptor);
        parentNode.addChild(newNode);
        if (currentDepth < hierarchyDepth) {
            recursivelyFillPropertyDescriptors(newNode, currentDepth);
        }
    }
    if (metaProperty.getRange().isClass()) {
        MetaClass childMetaClass = metaProperty.getRange().asClass();
        if (!dynamicAttributes.getAttributesForMetaClass(childMetaClass).isEmpty()) {
            DynamicAttributesConditionCreator descriptor = new DynamicAttributesConditionCreator(filterComponentName, filter.getDatasource(), propertyId);
            Node<AbstractConditionDescriptor> newNode = new Node<>(descriptor);
            parentNode.addChild(newNode);
        }
    }
}
Also used : Node(com.haulmont.bali.datastruct.Node) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 18 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class CustomConditionFrame method fillEntitySelect.

protected void fillEntitySelect(Param param) {
    if (!entitySelect.isEnabled()) {
        entitySelect.setValue(null);
        return;
    }
    MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
    MessageTools messageTools = AppBeans.get(MessageTools.NAME);
    Map<String, Object> items = new TreeMap<>();
    Object selectedItem = null;
    if (ParamType.ENTITY.equals(typeSelect.getValue())) {
        for (MetaClass metaClass : metadataTools.getAllPersistentMetaClasses()) {
            if (!metadataTools.isSystemLevel(metaClass)) {
                items.put(messageTools.getEntityCaption(metaClass) + " (" + metaClass.getName() + ")", metaClass);
            }
        }
        if (param != null && Param.Type.ENTITY.equals(param.getType())) {
            Class javaClass = param.getJavaClass();
            Metadata metadata = AppBeans.get(Metadata.NAME);
            selectedItem = metadata.getClass(javaClass);
        }
        entitySelect.setOptionsMap(items);
        entitySelect.setValue(selectedItem);
    } else if (ParamType.ENUM.equals(typeSelect.getValue())) {
        if (param != null && Param.Type.ENUM.equals(param.getType())) {
            selectedItem = param.getJavaClass();
        }
        boolean selectedItemFound = false;
        for (Class enumClass : metadataTools.getAllEnums()) {
            items.put(getEnumClassName(enumClass), enumClass);
            if (selectedItem == null || selectedItem.equals(enumClass))
                selectedItemFound = true;
        }
        // the metamodel, hence not in MetadataHelper.getAllEnums(). So we just add it here.
        if (selectedItem != null && !selectedItemFound) {
            items.put(getEnumClassName((Class) selectedItem), selectedItem);
        }
        entitySelect.setOptionsMap(items);
        entitySelect.setValue(selectedItem);
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 19 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class DynamicAttributesConditionFrame method fillCategorySelect.

protected void fillCategorySelect() {
    DynamicAttributes dynamicAttributes = AppBeans.get(DynamicAttributes.NAME);
    MetaClass metaClass = condition.getDatasource().getMetaClass();
    if (!Strings.isNullOrEmpty(condition.getPropertyPath())) {
        MetaPropertyPath propertyPath = metaClass.getPropertyPath(condition.getPropertyPath());
        if (propertyPath == null) {
            throw new RuntimeException("Property path " + condition.getPropertyPath() + " doesn't exist");
        }
        metaClass = propertyPath.getRange().asClass();
    }
    Collection<Category> categories = dynamicAttributes.getCategoriesForMetaClass(metaClass);
    UUID catId = condition.getCategoryId();
    Category selectedCategory = null;
    Map<String, Object> categoriesMap = new TreeMap<>();
    if (categories.size() == 1 && (catId == null || Objects.equals(catId, categories.iterator().next().getId()))) {
        Category category = categories.iterator().next();
        categoryLookup.setVisible(false);
        categoryLabel.setVisible(false);
        attributeLookup.requestFocus();
        categoriesMap.put(category.getName(), category);
        categoryLookup.setOptionsMap(categoriesMap);
        categoryLookup.setValue(category);
        fillAttributeSelect(category);
    } else {
        categoryLookup.setVisible(true);
        categoryLabel.setVisible(true);
        for (Category category : categories) {
            categoriesMap.put(category.getName(), category);
            if (category.getId().equals(catId)) {
                selectedCategory = category;
            }
        }
        categoryLookup.setOptionsMap(categoriesMap);
        categoryLookup.setValue(selectedCategory);
    }
}
Also used : Category(com.haulmont.cuba.core.entity.Category) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) DynamicAttributes(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributes)

Example 20 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class EntitiesControllerManager method createEntity.

public CreatedEntityInfo createEntity(String entityJson, String entityName, String modelVersion) {
    String transformedEntityName = restControllerUtils.transformEntityNameIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION);
    MetaClass metaClass = restControllerUtils.getMetaClass(transformedEntityName);
    checkCanCreateEntity(metaClass);
    entityJson = restControllerUtils.transformJsonIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION, entityJson);
    Entity entity;
    try {
        entity = entitySerializationAPI.entityFromJson(entityJson, metaClass);
    } catch (Exception e) {
        throw new RestAPIException("Cannot deserialize an entity from JSON", "", HttpStatus.BAD_REQUEST, e);
    }
    EntityImportView entityImportView = entityImportViewBuilderAPI.buildFromJson(entityJson, metaClass);
    Collection<Entity> importedEntities;
    try {
        importedEntities = entityImportExportService.importEntities(Collections.singletonList(entity), entityImportView, true);
    } catch (EntityImportException e) {
        throw new RestAPIException("Entity creation failed", e.getMessage(), HttpStatus.BAD_REQUEST, e);
    }
    // if many entities were created (because of @Composition references) we must find the main entity
    return getMainEntityInfo(importedEntities, metaClass, modelVersion);
}
Also used : EntityImportView(com.haulmont.cuba.core.app.importexport.EntityImportView) MetaClass(com.haulmont.chile.core.model.MetaClass) EntityImportException(com.haulmont.cuba.core.app.importexport.EntityImportException) RestAPIException(com.haulmont.restapi.exception.RestAPIException) RestFilterParseException(com.haulmont.restapi.service.filter.RestFilterParseException) EntityImportException(com.haulmont.cuba.core.app.importexport.EntityImportException) RestAPIException(com.haulmont.restapi.exception.RestAPIException)

Aggregations

MetaClass (com.haulmont.chile.core.model.MetaClass)302 MetaProperty (com.haulmont.chile.core.model.MetaProperty)103 Entity (com.haulmont.cuba.core.entity.Entity)54 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)36 Nullable (javax.annotation.Nullable)25 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)24 Element (org.dom4j.Element)21 java.util (java.util)18 Inject (javax.inject.Inject)17 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)16 Test (org.junit.Test)15 EntityManager (com.haulmont.cuba.core.EntityManager)14 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)14 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)13 Metadata (com.haulmont.cuba.core.global.Metadata)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)12 Collectors (java.util.stream.Collectors)11 Range (com.haulmont.chile.core.model.Range)10