Search in sources :

Example 66 with MetaPropertyPath

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

the class WebTree method tryToAssignCaptionProperty.

protected void tryToAssignCaptionProperty() {
    if (datasource != null && captionProperty != null && captionMode == CaptionMode.PROPERTY) {
        MetaPropertyPath propertyPath = datasource.getMetaClass().getPropertyPath(captionProperty);
        if (propertyPath != null && component.getContainerDataSource() != null) {
            ((HierarchicalDsWrapper) component.getContainerDataSource()).addProperty(propertyPath);
            component.setItemCaptionPropertyId(propertyPath);
        } else {
            throw new IllegalArgumentException(String.format("Can't find property for given caption property: %s", captionProperty));
        }
    }
}
Also used : MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) HierarchicalDsWrapper(com.haulmont.cuba.web.gui.data.HierarchicalDsWrapper)

Example 67 with MetaPropertyPath

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

the class SortableCollectionDsWrapper method sort.

@Override
public void sort(Object[] propertyId, boolean[] ascending) {
    List<SortInfo> infos = new LinkedList<>();
    for (int i = 0; i < propertyId.length; i++) {
        final MetaPropertyPath propertyPath = (MetaPropertyPath) propertyId[i];
        final SortInfo<MetaPropertyPath> info = new SortInfo<>();
        info.setPropertyPath(propertyPath);
        info.setOrder(ascending[i] ? Order.ASC : Order.DESC);
        infos.add(info);
    }
    SortInfo[] sortInfos = infos.toArray(new SortInfo[infos.size()]);
    ((CollectionDatasource.Sortable) datasource).sort(sortInfos);
}
Also used : MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) LinkedList(java.util.LinkedList) SortInfo(com.haulmont.cuba.gui.data.CollectionDatasource.Sortable.SortInfo)

Example 68 with MetaPropertyPath

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

the class SortableDataGridIndexedCollectionDsWrapper method sort.

@Override
public void sort(Object[] propertyId, boolean[] ascending) {
    List<SortInfo> infos = new LinkedList<>();
    for (int i = 0; i < propertyId.length; i++) {
        final MetaPropertyPath propertyPath = (MetaPropertyPath) propertyId[i];
        final SortInfo<MetaPropertyPath> info = new SortInfo<>();
        info.setPropertyPath(propertyPath);
        info.setOrder(ascending[i] ? Order.ASC : Order.DESC);
        infos.add(info);
    }
    SortInfo[] sortInfos = infos.toArray(new SortInfo[infos.size()]);
    ((CollectionDatasource.Sortable) datasource).sort(sortInfos);
}
Also used : MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) LinkedList(java.util.LinkedList) SortInfo(com.haulmont.cuba.gui.data.CollectionDatasource.Sortable.SortInfo)

Example 69 with MetaPropertyPath

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

the class RestFilterParser method parsePropertyCondition.

protected RestFilterPropertyCondition parsePropertyCondition(JsonObject conditionJsonObject, MetaClass metaClass) throws RestFilterParseException {
    RestFilterPropertyCondition condition = new RestFilterPropertyCondition();
    JsonElement propertyJsonElem = conditionJsonObject.get("property");
    if (propertyJsonElem == null) {
        throw new RestFilterParseException("Field 'property' is not defined for filter condition");
    }
    String propertyName = propertyJsonElem.getAsString();
    JsonElement operatorJsonElem = conditionJsonObject.get("operator");
    if (operatorJsonElem == null) {
        throw new RestFilterParseException("Field 'operator' is not defined for filter condition");
    }
    String operator = operatorJsonElem.getAsString();
    Op op = findOperator(operator);
    boolean isValueRequired = op != Op.NOT_EMPTY;
    JsonElement valueJsonElem = conditionJsonObject.get("value");
    if (valueJsonElem == null && isValueRequired) {
        throw new RestFilterParseException("Field 'value' is not defined for filter condition");
    }
    MetaPropertyPath propertyPath = metaClass.getPropertyPath(propertyName);
    if (propertyPath == null) {
        throw new RestFilterParseException("Property for " + metaClass.getName() + " not found: " + propertyName);
    }
    MetaProperty metaProperty = propertyPath.getMetaProperty();
    EnumSet<Op> opsAvailableForJavaType = opManager.availableOps(metaProperty.getJavaType());
    if (!opsAvailableForJavaType.contains(op)) {
        throw new RestFilterParseException("Operator " + operator + " is not available for java type " + metaProperty.getJavaType().getCanonicalName());
    }
    if (metaProperty.getRange().isClass()) {
        if (Entity.class.isAssignableFrom(metaProperty.getJavaType())) {
            MetaClass _metaClass = metadata.getClass(metaProperty.getJavaType());
            MetaProperty primaryKeyProperty = metadata.getTools().getPrimaryKeyProperty(_metaClass);
            String pkName = primaryKeyProperty.getName();
            propertyName += "." + pkName;
            propertyPath = metaClass.getPropertyPath(propertyName);
            if (propertyPath == null) {
                throw new RestFilterParseException("Property " + propertyName + " for " + metaClass.getName() + " not found");
            }
            metaProperty = propertyPath.getMetaProperty();
        }
    }
    if (isValueRequired) {
        Object value = null;
        if (op == Op.IN || op == Op.NOT_IN) {
            if (!valueJsonElem.isJsonArray()) {
                throw new RestFilterParseException("JSON array was expected as a value for condition with operator " + operator);
            }
            List<Object> parsedArrayValues = new ArrayList<>();
            for (JsonElement arrayItemElem : valueJsonElem.getAsJsonArray()) {
                parsedArrayValues.add(parseValue(metaProperty, arrayItemElem.getAsString()));
            }
            value = parsedArrayValues;
        } else {
            value = parseValue(metaProperty, valueJsonElem.getAsString());
        }
        condition.setValue(transformValue(value, op));
        condition.setQueryParamName(generateQueryParamName());
    }
    condition.setPropertyName(propertyName);
    condition.setOperator(op);
    return condition;
}
Also used : Op(com.haulmont.cuba.core.global.filter.Op) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 70 with MetaPropertyPath

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

the class XMLConverter2 method parseEntity.

/**
 * Converts a content of XML element to an entity.
 *
 * @param instanceEl    element that contains entity description
 * @param entity        if this parameter is not null then its fields will be filled,
 *                      if it is null then new entity will be created.
 * @param commitRequest must not be null if method is called when parsing a {@code CommitRequest}.
 *                      Security permissions checks are performed based on existing/absence of this
 *                      parameter.
 */
protected Entity parseEntity(Element instanceEl, @Nullable Entity entity, @Nullable CommitRequest commitRequest) {
    try {
        if (entity == null) {
            String id = instanceEl.attributeValue("id");
            EntityLoadInfo loadInfo = EntityLoadInfo.parse(id);
            if (loadInfo == null)
                throw new IllegalArgumentException("XML description of entity doesn't contain valid 'id' attribute");
            entity = createEmptyInstance(loadInfo);
            entity.setValue("id", loadInfo.getId());
        }
        MetaClass metaClass = entity.getMetaClass();
        List propertyEls = instanceEl.elements();
        for (Object el : propertyEls) {
            Element propertyEl = (Element) el;
            if (entity instanceof BaseGenericIdEntity && "__securityToken".equals(propertyEl.getName())) {
                byte[] securityToken = Base64.getDecoder().decode(propertyEl.getText());
                SecurityState securityState = BaseEntityInternalAccess.getOrCreateSecurityState((BaseGenericIdEntity) entity);
                BaseEntityInternalAccess.setSecurityToken(securityState, securityToken);
                continue;
            }
            String propertyName = propertyEl.attributeValue("name");
            MetaPropertyPath metaPropertyPath = metadata.getTools().resolveMetaPropertyPath(metaClass, propertyName);
            Preconditions.checkNotNullArgument(metaPropertyPath, "Could not resolve property '%s' in '%s'", propertyName, metaClass);
            MetaProperty property = metaPropertyPath.getMetaProperty();
            if (commitRequest != null && !attrModifyPermitted(metaClass, propertyName))
                continue;
            if (commitRequest != null && metadataTools.isNotPersistent(property) && !DynamicAttributesUtils.isDynamicAttribute(propertyName))
                continue;
            if (Boolean.parseBoolean(propertyEl.attributeValue("null"))) {
                entity.setValue(propertyName, null);
                continue;
            }
            if (entity instanceof BaseGenericIdEntity && DynamicAttributesUtils.isDynamicAttribute(propertyName) && ((BaseGenericIdEntity) entity).getDynamicAttributes() == null) {
                ConverterHelper.fetchDynamicAttributes(entity);
            }
            String stringValue = propertyEl.getText();
            Object value;
            switch(property.getType()) {
                case DATATYPE:
                    value = property.getRange().asDatatype().parse(stringValue);
                    entity.setValue(propertyName, value);
                    break;
                case ENUM:
                    value = property.getRange().asEnumeration().parse(stringValue);
                    entity.setValue(propertyName, value);
                    break;
                case COMPOSITION:
                case ASSOCIATION:
                    MetaClass propertyMetaClass = propertyMetaClass(property);
                    // checks if the user permitted to read and update a property
                    if (commitRequest != null && !updatePermitted(propertyMetaClass) && !readPermitted(propertyMetaClass))
                        break;
                    if (!property.getRange().getCardinality().isMany()) {
                        Element refInstanceEl = propertyEl.element("instance");
                        if (metadataTools.isEmbedded(property)) {
                            MetaClass embeddedMetaClass = property.getRange().asClass();
                            Entity embeddedEntity = metadata.create(embeddedMetaClass);
                            value = parseEntity(refInstanceEl, embeddedEntity, commitRequest);
                        } else {
                            String id = refInstanceEl.attributeValue("id");
                            // will be registered later
                            if (commitRequest != null && commitRequest.getCommitIds().contains(id)) {
                                EntityLoadInfo loadInfo = EntityLoadInfo.parse(id);
                                Entity ref = metadata.create(loadInfo.getMetaClass());
                                ref.setValue("id", loadInfo.getId());
                                entity.setValue(propertyName, ref);
                                break;
                            }
                            value = parseEntity(refInstanceEl, null, commitRequest);
                        }
                        entity.setValue(propertyName, value);
                    } else {
                        Class<?> propertyJavaType = property.getJavaType();
                        Collection<Object> coll;
                        if (List.class.isAssignableFrom(propertyJavaType))
                            coll = new ArrayList<>();
                        else if (Set.class.isAssignableFrom(propertyJavaType))
                            coll = new HashSet<>();
                        else
                            throw new RuntimeException("Datatype " + propertyJavaType.getName() + " of " + metaClass.getName() + "#" + property.getName() + " is not supported");
                        entity.setValue(propertyName, coll);
                        for (Object childInstenceEl : propertyEl.elements("instance")) {
                            Entity childEntity = parseEntity((Element) childInstenceEl, null, commitRequest);
                            coll.add(childEntity);
                        }
                    }
                    break;
                default:
                    throw new IllegalStateException("Unknown property type");
            }
        }
        return entity;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Element(org.dom4j.Element) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) SecurityState(com.haulmont.cuba.core.entity.SecurityState) ParseException(java.text.ParseException) MetaClass(com.haulmont.chile.core.model.MetaClass) 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