Search in sources :

Example 1 with CategoryAttributeConfiguration

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

the class AttributesLocationFrame method initTargetGrids.

protected void initTargetGrids(int columnsCount) {
    for (int i = 0; i < columnsCount; i++) {
        if (i < rowsCounts.length) {
            addColumn(i, rowsCounts[i]);
        } else {
            addColumn(i, 0);
        }
    }
    List<CategoryAttribute> removeFromSource = new ArrayList<>();
    for (CategoryAttribute entity : sourceDataContainer) {
        CategoryAttributeConfiguration conf = entity.getConfiguration();
        if (conf.getXCoordinate() != null && conf.getYCoordinate() != null) {
            dataContainers.get(conf.getXCoordinate()).set(conf.getYCoordinate(), entity);
            removeFromSource.add(entity);
        }
    }
    sourceDataContainer.removeAll(removeFromSource);
}
Also used : CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) CategoryAttributeConfiguration(com.haulmont.cuba.core.entity.CategoryAttributeConfiguration)

Example 2 with CategoryAttributeConfiguration

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

the class DynamicAttributesTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    dataManager = AppBeans.get(DataManager.class);
    metadata = AppBeans.get(Metadata.class);
    dynamicAttributesManagerAPI = AppBeans.get(DynamicAttributesManagerAPI.class);
    recalculationTools = AppBeans.get(DynamicAttributesRecalculationTools.class);
    dynamicAttributesCacheService = AppBeans.get(DynamicAttributesCacheService.NAME);
    entityImportExport = AppBeans.get(EntityImportExportAPI.class);
    Gson gson = new Gson();
    CategoryAttributeConfiguration configuration;
    QueryRunner runner = new QueryRunner(cont.persistence().getDataSource());
    runner.update("delete from TEST_COMPOSITE_KEY");
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        userCategory = metadata.create(Category.class);
        userCategory.setName("user");
        userCategory.setEntityType("sec$User");
        em.persist(userCategory);
        userAttribute = metadata.create(CategoryAttribute.class);
        userAttribute.setName("userAttribute");
        userAttribute.setCode("userAttribute");
        userAttribute.setCategory(userCategory);
        userAttribute.setCategoryEntityType("sec$User");
        userAttribute.setDataType(PropertyType.STRING);
        em.persist(userAttribute);
        userRoleCategory = metadata.create(Category.class);
        userRoleCategory.setName("userRole");
        userRoleCategory.setEntityType("sec$UserRole");
        em.persist(userRoleCategory);
        userRoleAttribute = metadata.create(CategoryAttribute.class);
        userRoleAttribute.setName("userRoleAttribute");
        userRoleAttribute.setCode("userRoleAttribute");
        userRoleAttribute.setCategory(userRoleCategory);
        userRoleAttribute.setCategoryEntityType("sec$UserRole");
        userRoleAttribute.setDataType(PropertyType.STRING);
        em.persist(userRoleAttribute);
        roleCategory = metadata.create(Category.class);
        roleCategory.setName("role");
        roleCategory.setEntityType("sec$Role");
        em.persist(roleCategory);
        roleAttribute = metadata.create(CategoryAttribute.class);
        roleAttribute.setName("roleAttribute");
        roleAttribute.setCode("roleAttribute");
        roleAttribute.setCategory(roleCategory);
        roleAttribute.setCategoryEntityType("sec$Role");
        roleAttribute.setDataType(PropertyType.STRING);
        em.persist(roleAttribute);
        group = metadata.create(Group.class);
        group.setName("group");
        em.persist(group);
        group2 = metadata.create(Group.class);
        group2.setName("group2");
        em.persist(group2);
        userGroupAttribute = metadata.create(CategoryAttribute.class);
        userGroupAttribute.setName("userGroupAttribute");
        userGroupAttribute.setCode("userGroupAttribute");
        userGroupAttribute.setCategory(userCategory);
        userGroupAttribute.setCategoryEntityType("sec$User");
        userGroupAttribute.setDataType(PropertyType.ENTITY);
        userGroupAttribute.setEntityClass("com.haulmont.cuba.security.entity.Group");
        em.persist(userGroupAttribute);
        userGroupCollectionAttribute = metadata.create(CategoryAttribute.class);
        userGroupCollectionAttribute.setName("userGroupCollectionAttribute");
        userGroupCollectionAttribute.setCode("userGroupCollectionAttribute");
        userGroupCollectionAttribute.setCategory(userCategory);
        userGroupCollectionAttribute.setCategoryEntityType("sec$User");
        userGroupCollectionAttribute.setDataType(PropertyType.ENTITY);
        userGroupCollectionAttribute.setEntityClass("com.haulmont.cuba.security.entity.Group");
        userGroupCollectionAttribute.setIsCollection(true);
        em.persist(userGroupCollectionAttribute);
        userIntCollectionAttribute = metadata.create(CategoryAttribute.class);
        userIntCollectionAttribute.setName("userIntCollectionAttribute");
        userIntCollectionAttribute.setCode("userIntCollectionAttribute");
        userIntCollectionAttribute.setCategory(userCategory);
        userIntCollectionAttribute.setCategoryEntityType("sec$User");
        userIntCollectionAttribute.setDataType(PropertyType.INTEGER);
        userIntCollectionAttribute.setIsCollection(true);
        em.persist(userIntCollectionAttribute);
        userEnumAttribute = metadata.create(CategoryAttribute.class);
        userEnumAttribute.setName("userEnumAttribute");
        userEnumAttribute.setCode("userEnumAttribute");
        userEnumAttribute.setCategory(userCategory);
        userEnumAttribute.setCategoryEntityType("sec$User");
        userEnumAttribute.setDataType(PropertyType.ENUMERATION);
        userEnumAttribute.setEnumeration("option1,option2,option3");
        em.persist(userEnumAttribute);
        userEnumCollectionAttribute = metadata.create(CategoryAttribute.class);
        userEnumCollectionAttribute.setName("userEnumCollectionAttribute");
        userEnumCollectionAttribute.setCode("userEnumCollectionAttribute");
        userEnumCollectionAttribute.setCategory(userCategory);
        userEnumCollectionAttribute.setCategoryEntityType("sec$User");
        userEnumCollectionAttribute.setDataType(PropertyType.ENUMERATION);
        userEnumCollectionAttribute.setEnumeration("option1,option2,option3");
        userEnumCollectionAttribute.setIsCollection(true);
        em.persist(userEnumCollectionAttribute);
        user = metadata.create(User.class);
        user.setName("user");
        user.setLogin("user");
        user.setGroup(group);
        em.persist(user);
        user2 = metadata.create(User.class);
        user2.setName("user2");
        user2.setLogin("user2");
        user2.setGroup(group);
        em.persist(user2);
        role = metadata.create(Role.class);
        role.setName("role");
        em.persist(role);
        userRole = metadata.create(UserRole.class);
        userRole.setUser(user);
        userRole.setRole(role);
        em.persist(userRole);
        compositeKeyEntity = metadata.create(CompositeKeyEntity.class);
        EntityKey entityKey = metadata.create(EntityKey.class);
        entityKey.setTenant(1);
        entityKey.setEntityId(10L);
        compositeKeyEntity.setId(entityKey);
        compositeKeyEntity.setName("foo");
        compositeKeyEntity.setEmail("foo@mail.com");
        em.persist(compositeKeyEntity);
        userCategoryWithLoop = metadata.create(Category.class);
        userCategoryWithLoop.setName("userCategoryWithLoop");
        userCategoryWithLoop.setEntityType("sec$User");
        em.persist(userCategoryWithLoop);
        userCategoryWithoutLoop = metadata.create(Category.class);
        userCategoryWithoutLoop.setName("userCategoryWithoutLoop");
        userCategoryWithoutLoop.setEntityType("sec$User");
        em.persist(userCategoryWithoutLoop);
        recalcAttr1 = metadata.create(CategoryAttribute.class);
        recalcAttr1.setName("recalcAttr1");
        recalcAttr1.setCode("recalcAttr1");
        recalcAttr1.setCategory(userCategoryWithoutLoop);
        recalcAttr1.setCategoryEntityType("sec$User");
        recalcAttr1.setDataType(PropertyType.STRING);
        em.persist(recalcAttr1);
        recalcAttr2 = metadata.create(CategoryAttribute.class);
        recalcAttr2.setName("recalcAttr2");
        recalcAttr2.setCode("recalcAttr2");
        recalcAttr2.setCategory(userCategoryWithoutLoop);
        recalcAttr2.setCategoryEntityType("sec$User");
        recalcAttr2.setDataType(PropertyType.STRING);
        recalcAttr3 = metadata.create(CategoryAttribute.class);
        recalcAttr3.setName("recalcAttr3");
        recalcAttr3.setCode("recalcAttr3");
        recalcAttr3.setCategory(userCategoryWithoutLoop);
        recalcAttr3.setCategoryEntityType("sec$User");
        recalcAttr3.setDataType(PropertyType.STRING);
        recalcAttr4 = metadata.create(CategoryAttribute.class);
        recalcAttr4.setName("recalcAttr4");
        recalcAttr4.setCode("recalcAttr4");
        recalcAttr4.setCategory(userCategoryWithLoop);
        recalcAttr4.setCategoryEntityType("sec$User");
        recalcAttr4.setDataType(PropertyType.STRING);
        recalcAttr5 = metadata.create(CategoryAttribute.class);
        recalcAttr5.setName("recalcAttr5");
        recalcAttr5.setCode("recalcAttr5");
        recalcAttr5.setCategory(userCategoryWithLoop);
        recalcAttr5.setCategoryEntityType("sec$User");
        recalcAttr5.setDataType(PropertyType.STRING);
        configuration = new CategoryAttributeConfiguration();
        configuration.setDependsOnAttributes(Collections.singletonList(recalcAttr1));
        configuration.setRecalculationScript("entity.getValue(\"+recalcAttr1\") + \"Test\"");
        recalcAttr2.setAttributeConfigurationJson(gson.toJson(configuration));
        em.persist(recalcAttr2);
        configuration = new CategoryAttributeConfiguration();
        configuration.setDependsOnAttributes(Collections.singletonList(recalcAttr2));
        configuration.setRecalculationScript("entity.getValue(\"+recalcAttr2\") + \"Test\"");
        recalcAttr3.setAttributeConfigurationJson(gson.toJson(configuration));
        em.persist(recalcAttr3);
        userCategoryWithoutLoop.setCategoryAttrs(Arrays.asList(recalcAttr1, recalcAttr2, recalcAttr3));
        em.persist(userCategoryWithoutLoop);
        configuration = new CategoryAttributeConfiguration();
        configuration.setDependsOnAttributes(Collections.singletonList(recalcAttr5));
        configuration.setRecalculationScript("entity.getValue(\"+recalcAttr5\") + \"Test\"");
        recalcAttr4.setAttributeConfigurationJson(gson.toJson(configuration));
        em.persist(recalcAttr4);
        configuration = new CategoryAttributeConfiguration();
        configuration.setDependsOnAttributes(Collections.singletonList(recalcAttr4));
        configuration.setRecalculationScript("entity.getValue(\"+recalcAttr4\") + \"Test\"");
        recalcAttr5.setAttributeConfigurationJson(gson.toJson(configuration));
        em.persist(recalcAttr5);
        userCategoryWithLoop.setCategoryAttrs(Arrays.asList(recalcAttr4, recalcAttr5));
        em.persist(userCategoryWithLoop);
        tx.commit();
    }
    dynamicAttributesManagerAPI.loadCache();
    user = dataManager.load(LoadContext.create(User.class).setId(user.getId()).setLoadDynamicAttributes(true));
    user.setValue("+userAttribute", "userName");
    user.setValue("+userGroupAttribute", group);
    user.setValue("+userGroupCollectionAttribute", Lists.newArrayList(group, group2));
    user.setValue("+userIntCollectionAttribute", Lists.newArrayList(1, 2));
    user.setValue("+userEnumAttribute", "option1");
    user.setValue("+userEnumCollectionAttribute", Lists.newArrayList("option1", "option3"));
    dataManager.commit(user);
    user2 = dataManager.load(LoadContext.create(User.class).setId(user2.getId()).setLoadDynamicAttributes(true));
    user2.setValue("+userAttribute", "userName");
    user2.setValue("+userGroupAttribute", group);
    dataManager.commit(user2);
    userRole = dataManager.load(LoadContext.create(UserRole.class).setId(userRole.getId()).setLoadDynamicAttributes(true));
    userRole.setValue("+userRoleAttribute", "userRole");
    dataManager.commit(userRole);
    role = dataManager.load(LoadContext.create(Role.class).setId(role.getId()).setLoadDynamicAttributes(true));
    role.setValue("+roleAttribute", "role");
    dataManager.commit(role);
}
Also used : Group(com.haulmont.cuba.security.entity.Group) Category(com.haulmont.cuba.core.entity.Category) User(com.haulmont.cuba.security.entity.User) Gson(com.google.gson.Gson) DynamicAttributesManagerAPI(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesManagerAPI) EntityImportExportAPI(com.haulmont.cuba.core.app.importexport.EntityImportExportAPI) QueryRunner(com.haulmont.bali.db.QueryRunner) UserRole(com.haulmont.cuba.security.entity.UserRole) Role(com.haulmont.cuba.security.entity.Role) EntityKey(com.haulmont.cuba.testmodel.primary_keys.EntityKey) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) CategoryAttributeConfiguration(com.haulmont.cuba.core.entity.CategoryAttributeConfiguration) UserRole(com.haulmont.cuba.security.entity.UserRole) CompositeKeyEntity(com.haulmont.cuba.testmodel.primary_keys.CompositeKeyEntity) DynamicAttributesRecalculationTools(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesRecalculationTools) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with CategoryAttributeConfiguration

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

the class AttributesLocationFrame method init.

@SuppressWarnings("unchecked")
@Override
public void init(Map<String, Object> params) {
    super.init(params);
    companion = getCompanion();
    Collection<CategoryAttribute> attributes = (Collection<CategoryAttribute>) params.get("attributes");
    sourceDataContainer.addAll(attributes);
    sourceDataContainer.add(createEmptyAttribute());
    if (companion != null) {
        companion.initGridDragAndDrop(sourceDataGrid, sourceDataContainer, true);
    }
    int maxColumnIndex = sourceDataContainer.stream().filter(e -> e.getConfiguration().getXCoordinate() != null).mapToInt(e -> e.getConfiguration().getXCoordinate()).max().orElse(0);
    rowsCounts = new int[maxColumnIndex + 1];
    for (CategoryAttribute entity : sourceDataContainer) {
        CategoryAttributeConfiguration conf = entity.getConfiguration();
        if (conf.getXCoordinate() != null && conf.getYCoordinate() != null) {
            if (rowsCounts[conf.getXCoordinate()] <= conf.getYCoordinate()) {
                rowsCounts[conf.getXCoordinate()] = conf.getYCoordinate() + 1;
            }
        }
    }
    columnsCountLookupField.setOptionsList(getLookupOptionsList());
    columnsCountLookupField.setValue(maxColumnIndex + 1);
    columnsCountLookupField.addValueChangeListener(e -> {
        if (e.getPrevValue() == null || e.getValue() == null) {
            return;
        }
        int range = e.getPrevValue() - e.getValue();
        if (range > 0) {
            for (int i = 1; i <= range; i++) {
                removeColumn(e.getPrevValue() - i);
            }
        } else {
            for (int i = 0; i < -range; i++) {
                addColumn(e.getPrevValue() + i, 0);
            }
        }
    });
    initTargetGrids(maxColumnIndex + 1);
}
Also used : IntStream(java.util.stream.IntStream) GlobalConfig(com.haulmont.cuba.core.global.GlobalConfig) java.util(java.util) Collectors(java.util.stream.Collectors) Metadata(com.haulmont.cuba.core.global.Metadata) Inject(javax.inject.Inject) CategoryAttributeConfiguration(com.haulmont.cuba.core.entity.CategoryAttributeConfiguration) MetadataTools(com.haulmont.cuba.core.global.MetadataTools) Gson(com.google.gson.Gson) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) LookupField(com.haulmont.cuba.gui.components.LookupField) DataGrid(com.haulmont.cuba.gui.components.DataGrid) UiComponents(com.haulmont.cuba.gui.UiComponents) HBoxLayout(com.haulmont.cuba.gui.components.HBoxLayout) AbstractFrame(com.haulmont.cuba.gui.components.AbstractFrame) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) CategoryAttributeConfiguration(com.haulmont.cuba.core.entity.CategoryAttributeConfiguration)

Aggregations

CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)3 CategoryAttributeConfiguration (com.haulmont.cuba.core.entity.CategoryAttributeConfiguration)3 Gson (com.google.gson.Gson)2 QueryRunner (com.haulmont.bali.db.QueryRunner)1 DynamicAttributesManagerAPI (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesManagerAPI)1 DynamicAttributesRecalculationTools (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesRecalculationTools)1 EntityImportExportAPI (com.haulmont.cuba.core.app.importexport.EntityImportExportAPI)1 Category (com.haulmont.cuba.core.entity.Category)1 GlobalConfig (com.haulmont.cuba.core.global.GlobalConfig)1 Metadata (com.haulmont.cuba.core.global.Metadata)1 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)1 UiComponents (com.haulmont.cuba.gui.UiComponents)1 AbstractFrame (com.haulmont.cuba.gui.components.AbstractFrame)1 DataGrid (com.haulmont.cuba.gui.components.DataGrid)1 HBoxLayout (com.haulmont.cuba.gui.components.HBoxLayout)1 LookupField (com.haulmont.cuba.gui.components.LookupField)1 Group (com.haulmont.cuba.security.entity.Group)1 Role (com.haulmont.cuba.security.entity.Role)1 User (com.haulmont.cuba.security.entity.User)1 UserRole (com.haulmont.cuba.security.entity.UserRole)1