Search in sources :

Example 1 with MetaPropertyPath

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

the class JSONConverter method asJavaTree.

protected void asJavaTree(CommitRequest commitRequest, Entity entity, MetaClass metaClass, JSONObject json) throws JSONException, InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException, IntrospectionException, ParseException {
    Iterator iter = json.keys();
    while (iter.hasNext()) {
        String propertyName = (String) iter.next();
        if ("id".equals(propertyName)) {
            // id was parsed already
            continue;
        }
        // version is readonly property
        if ("version".equals(propertyName)) {
            continue;
        }
        if (entity instanceof BaseGenericIdEntity && "__securityToken".equals(propertyName)) {
            byte[] securityToken = Base64.getDecoder().decode(json.getString("__securityToken"));
            SecurityState securityState = BaseEntityInternalAccess.getOrCreateSecurityState((BaseGenericIdEntity) entity);
            BaseEntityInternalAccess.setSecurityToken(securityState, securityToken);
            continue;
        }
        MetaPropertyPath metaPropertyPath = metadata.getTools().resolveMetaPropertyPath(metaClass, propertyName);
        checkNotNullArgument(metaPropertyPath, "Could not resolve property '%s' in '%s'", propertyName, metaClass);
        MetaProperty property = metaPropertyPath.getMetaProperty();
        if (!attrModifyPermitted(metaClass, property.getName()))
            continue;
        if (entity instanceof BaseGenericIdEntity && DynamicAttributesUtils.isDynamicAttribute(propertyName) && ((BaseGenericIdEntity) entity).getDynamicAttributes() == null) {
            ConverterHelper.fetchDynamicAttributes(entity);
        }
        if (json.get(propertyName) == null) {
            setField(entity, propertyName, null);
            continue;
        }
        switch(property.getType()) {
            case DATATYPE:
                String value = null;
                if (!json.isNull(propertyName)) {
                    value = json.get(propertyName).toString();
                }
                setField(entity, propertyName, property.getRange().asDatatype().parse(value));
                break;
            case ENUM:
                if (!json.isNull(propertyName)) {
                    setField(entity, propertyName, property.getRange().asEnumeration().parse(json.getString(propertyName)));
                } else {
                    setField(entity, propertyName, null);
                }
                break;
            case COMPOSITION:
            case ASSOCIATION:
                if ("null".equals(json.get(propertyName).toString())) {
                    setField(entity, propertyName, null);
                    break;
                }
                MetaClass propertyMetaClass = propertyMetaClass(property);
                // checks if the user permitted to read and update a property
                if (!updatePermitted(propertyMetaClass) && !readPermitted(propertyMetaClass))
                    break;
                if (!property.getRange().getCardinality().isMany()) {
                    JSONObject jsonChild = json.getJSONObject(propertyName);
                    Entity child;
                    MetaClass childMetaClass;
                    if (jsonChild.has("id")) {
                        String id = jsonChild.getString("id");
                        // will be registered later
                        if (commitRequest.getCommitIds().contains(id)) {
                            EntityLoadInfo loadInfo = EntityLoadInfo.parse(id);
                            if (loadInfo == null)
                                throw new IllegalArgumentException("Unable to parse ID: " + id);
                            Entity ref = metadata.create(loadInfo.getMetaClass());
                            ref.setValue("id", loadInfo.getId());
                            setField(entity, propertyName, ref);
                            break;
                        }
                        InstanceRef ref = commitRequest.parseInstanceRefAndRegister(id);
                        childMetaClass = ref.getMetaClass();
                        child = ref.getInstance();
                    } else {
                        childMetaClass = property.getRange().asClass();
                        child = metadata.create(childMetaClass);
                    }
                    asJavaTree(commitRequest, child, childMetaClass, jsonChild);
                    setField(entity, propertyName, child);
                } else {
                    JSONArray jsonArray = json.getJSONArray(propertyName);
                    Collection<Object> coll = property.getRange().isOrdered() ? new ArrayList<>() : new HashSet<>();
                    setField(entity, propertyName, coll);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        Object arrayValue = jsonArray.get(i);
                        if (arrayValue == null)
                            coll.add(null);
                        else {
                            // assuming no simple type here
                            JSONObject jsonChild = (JSONObject) arrayValue;
                            InstanceRef ref = commitRequest.parseInstanceRefAndRegister(jsonChild.getString("id"));
                            Entity child = ref.getInstance();
                            coll.add(child);
                            asJavaTree(commitRequest, child, ref.getMetaClass(), jsonChild);
                        }
                    }
                }
                break;
            default:
                throw new IllegalStateException("Unknown property type");
        }
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) JSONArray(org.json.JSONArray) SecurityState(com.haulmont.cuba.core.entity.SecurityState) MetaClass(com.haulmont.chile.core.model.MetaClass) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 2 with MetaPropertyPath

use of com.haulmont.chile.core.model.MetaPropertyPath 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 3 with MetaPropertyPath

use of com.haulmont.chile.core.model.MetaPropertyPath 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 4 with MetaPropertyPath

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

the class DynamicAttributesCondition method getLocCaption.

@Override
public String getLocCaption() {
    if (isBlank(caption) && !isBlank(propertyPath)) {
        MessageTools messageTools = AppBeans.get(MessageTools.class);
        String propertyCaption = messageTools.getPropertyCaption(datasource.getMetaClass(), propertyPath);
        if (!isBlank(propertyCaption)) {
            return propertyCaption + "." + locCaption;
        }
    } else if (isNotBlank(caption)) {
        MessageTools messageTools = AppBeans.get(MessageTools.class);
        return messageTools.loadString(messagesPack, caption);
    }
    MetaPropertyPath mpp = DynamicAttributesUtils.getMetaPropertyPath(datasource.getMetaClass(), getCategoryAttributeId());
    if (mpp != null) {
        return DynamicAttributesUtils.getCategoryAttribute(mpp.getMetaProperty()).getLocaleName();
    }
    return super.getLocCaption();
}
Also used : MessageTools(com.haulmont.cuba.core.global.MessageTools) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath)

Example 5 with MetaPropertyPath

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

the class FilterConditionUtils method getPropertyLocCaption.

public static String getPropertyLocCaption(MetaClass metaClass, String propertyPath) {
    MessageTools messageTools = AppBeans.get(MessageTools.class);
    MetaPropertyPath mpp = metaClass.getPropertyPath(propertyPath);
    if (mpp == null) {
        return propertyPath;
    } else {
        MetaProperty[] metaProperties = mpp.getMetaProperties();
        StringBuilder sb = new StringBuilder();
        List<String> metaPropertyNames = new ArrayList<>();
        for (int i = 0; i < metaProperties.length; i++) {
            metaPropertyNames.add(metaProperties[i].getName());
            String currentMetaPropertyPath = StringUtils.join(metaPropertyNames, ".");
            sb.append(messageTools.getPropertyCaption(metaClass, currentMetaPropertyPath));
            if (i < metaProperties.length - 1) {
                sb.append(".");
            }
        }
        return sb.toString();
    }
}
Also used : MessageTools(com.haulmont.cuba.core.global.MessageTools) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) ArrayList(java.util.ArrayList) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)84 MetaClass (com.haulmont.chile.core.model.MetaClass)34 MetaProperty (com.haulmont.chile.core.model.MetaProperty)27 Element (org.dom4j.Element)16 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)11 Entity (com.haulmont.cuba.core.entity.Entity)9 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)9 Datasource (com.haulmont.cuba.gui.data.Datasource)6 Table (com.haulmont.cuba.gui.components.Table)5 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)5 MessageTools (com.haulmont.cuba.core.global.MessageTools)4 Instance (com.haulmont.chile.core.model.Instance)3 Op (com.haulmont.cuba.core.global.filter.Op)3 FocusableTable (com.haulmont.cuba.desktop.sys.vcl.FocusableTable)3 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)3 Formatter (com.haulmont.cuba.gui.components.Formatter)3 Window (com.haulmont.cuba.gui.components.Window)3 CollectionFormatter (com.haulmont.cuba.gui.components.formatters.CollectionFormatter)3 GroupInfo (com.haulmont.cuba.gui.data.GroupInfo)3 java.util (java.util)3