Search in sources :

Example 1 with Category

use of com.haulmont.cuba.core.entity.Category 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 2 with Category

use of com.haulmont.cuba.core.entity.Category in project cuba by cuba-platform.

the class DynamicAttributesConditionFrame method commit.

@Override
public boolean commit() {
    if (!super.commit())
        return false;
    String error = checkCondition();
    if (error != null) {
        showNotification(messages.getMainMessage(error), Frame.NotificationType.TRAY);
        return false;
    }
    CategoryAttribute attribute = attributeLookup.getValue();
    String alias = condition.getEntityAlias();
    String cavAlias = "cav" + RandomStringUtils.randomNumeric(5);
    String paramName;
    String operation = operationLookup.<Op>getValue().forJpql();
    Op op = operationLookup.getValue();
    Class javaClass = DynamicAttributesUtils.getAttributeClass(attribute);
    String propertyPath = Strings.isNullOrEmpty(condition.getPropertyPath()) ? "" : "." + condition.getPropertyPath();
    ConditionParamBuilder paramBuilder = AppBeans.get(ConditionParamBuilder.class);
    paramName = paramBuilder.createParamName(condition);
    String cavEntityId = referenceToEntitySupport.getReferenceIdPropertyName(condition.getDatasource().getMetaClass());
    String where;
    if (op == Op.NOT_EMPTY) {
        where = "(exists (select " + cavAlias + " from sys$CategoryAttributeValue " + cavAlias + " where " + cavAlias + ".entity." + cavEntityId + "=" + "{E}" + propertyPath + ".id and " + cavAlias + ".categoryAttribute.id='" + attributeLookup.<CategoryAttribute>getValue().getId() + "'))";
    } else {
        String valueFieldName = "stringValue";
        if (Entity.class.isAssignableFrom(javaClass))
            valueFieldName = "entityValue." + referenceToEntitySupport.getReferenceIdPropertyName(metadata.getClassNN(javaClass));
        else if (String.class.isAssignableFrom(javaClass))
            valueFieldName = "stringValue";
        else if (Integer.class.isAssignableFrom(javaClass))
            valueFieldName = "intValue";
        else if (Double.class.isAssignableFrom(javaClass))
            valueFieldName = "doubleValue";
        else if (Boolean.class.isAssignableFrom(javaClass))
            valueFieldName = "booleanValue";
        else if (Date.class.isAssignableFrom(javaClass))
            valueFieldName = "dateValue";
        if (!attribute.getIsCollection()) {
            condition.setJoin(", sys$CategoryAttributeValue " + cavAlias + " ");
            String paramStr = " ? ";
            where = cavAlias + ".entity." + cavEntityId + "=" + "{E}" + propertyPath + ".id and " + cavAlias + "." + valueFieldName + " " + operation + (op.isUnary() ? " " : paramStr) + "and " + cavAlias + ".categoryAttribute.id='" + attributeLookup.<CategoryAttribute>getValue().getId() + "'";
            where = where.replace("?", ":" + paramName);
        } else {
            where = "(exists (select " + cavAlias + " from sys$CategoryAttributeValue " + cavAlias + " where " + cavAlias + ".entity." + cavEntityId + "=" + "{E}" + propertyPath + ".id and " + cavAlias + "." + valueFieldName + " = :" + paramName + " and " + cavAlias + ".categoryAttribute.id='" + attributeLookup.<CategoryAttribute>getValue().getId() + "'))";
        }
    }
    condition.setWhere(where);
    condition.setUnary(op.isUnary());
    condition.setEntityParamView(null);
    condition.setEntityParamWhere(null);
    condition.setInExpr(Op.IN.equals(op) || Op.NOT_IN.equals(op));
    condition.setOperator(operationLookup.<Op>getValue());
    Class paramJavaClass = op.isUnary() ? Boolean.class : javaClass;
    condition.setJavaClass(javaClass);
    Param param = Param.Builder.getInstance().setName(paramName).setJavaClass(paramJavaClass).setDataSource(condition.getDatasource()).setProperty(DynamicAttributesUtils.getMetaPropertyPath(null, attribute).getMetaProperty()).setInExpr(condition.getInExpr()).setRequired(condition.getRequired()).setCategoryAttrId(attribute.getId()).build();
    Object defaultValue = condition.getParam().getDefaultValue();
    param.setDefaultValue(defaultValue);
    condition.setParam(param);
    condition.setCategoryId(categoryLookup.<Category>getValue().getId());
    condition.setCategoryAttributeId(attributeLookup.<CategoryAttribute>getValue().getId());
    condition.setIsCollection(BooleanUtils.isTrue(attributeLookup.<CategoryAttribute>getValue().getIsCollection()));
    condition.setLocCaption(attribute.getLocaleName());
    condition.setCaption(caption.getValue());
    return true;
}
Also used : Op(com.haulmont.cuba.core.global.filter.Op) ConditionParamBuilder(com.haulmont.cuba.gui.components.filter.ConditionParamBuilder) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) Category(com.haulmont.cuba.core.entity.Category) Param(com.haulmont.cuba.gui.components.filter.Param) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 3 with Category

use of com.haulmont.cuba.core.entity.Category in project cuba by cuba-platform.

the class DynamicAttributesCache method getAttributesForMetaClass.

public Collection<CategoryAttribute> getAttributesForMetaClass(MetaClass metaClass) {
    MetaClass targetMetaClass = resolveTargetMetaClass(metaClass);
    Collection<Category> categories = categoriesCache.get(targetMetaClass.getName());
    List<CategoryAttribute> categoryAttributes = new ArrayList<>();
    for (Category category : categories) {
        categoryAttributes.addAll(Collections2.filter(category.getCategoryAttrs(), new Predicate<CategoryAttribute>() {

            @Override
            public boolean apply(@Nullable CategoryAttribute input) {
                return input != null && StringUtils.isNotBlank(input.getCode());
            }
        }));
    }
    return categoryAttributes;
}
Also used : Category(com.haulmont.cuba.core.entity.Category) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) Nullable(javax.annotation.Nullable) Predicate(com.google.common.base.Predicate)

Example 4 with Category

use of com.haulmont.cuba.core.entity.Category in project cuba by cuba-platform.

the class CategoryEditor method initIsDefaultCheckbox.

protected void initIsDefaultCheckbox() {
    isDefault.setValue(BooleanUtils.isTrue(category.getIsDefault()));
    isDefault.addValueChangeListener(e -> {
        if (Boolean.TRUE.equals(e.getValue())) {
            LoadContext<Category> lc = new LoadContext<>(Category.class).setView("category.defaultEdit");
            lc.setQueryString("select c from sys$Category c where c.entityType = :entityType and not c.id = :id").setParameter("entityType", category.getEntityType()).setParameter("id", category.getId());
            List<Category> result = dataManager.loadList(lc);
            for (Category cat : result) {
                cat.setIsDefault(false);
            }
            CommitContext commitCtx = new CommitContext(result);
            dataManager.commit(commitCtx);
            category.setIsDefault(true);
        } else if (Boolean.FALSE.equals(e.getValue())) {
            category.setIsDefault(false);
        }
    });
}
Also used : Category(com.haulmont.cuba.core.entity.Category)

Aggregations

Category (com.haulmont.cuba.core.entity.Category)4 MetaClass (com.haulmont.chile.core.model.MetaClass)3 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)2 Predicate (com.google.common.base.Predicate)1 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 DynamicAttributes (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributes)1 Op (com.haulmont.cuba.core.global.filter.Op)1 ConditionParamBuilder (com.haulmont.cuba.gui.components.filter.ConditionParamBuilder)1 Param (com.haulmont.cuba.gui.components.filter.Param)1 Nullable (javax.annotation.Nullable)1