Search in sources :

Example 1 with EntityKey

use of com.haulmont.cuba.testmodel.primary_keys.EntityKey in project cuba by cuba-platform.

the class CompositeKeyTest method testOperations.

@Test
public void testOperations() throws Exception {
    CompositeKeyEntity foo = metadata.create(CompositeKeyEntity.class);
    EntityKey entityKey = metadata.create(EntityKey.class);
    entityKey.setTenant(1);
    entityKey.setEntityId(10L);
    foo.setId(entityKey);
    foo.setName("foo");
    foo.setEmail("foo@mail.com");
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().persist(foo);
        tx.commit();
    }
    CompositeKeyEntity loadedFoo;
    try (Transaction tx = persistence.createTransaction()) {
        loadedFoo = persistence.getEntityManager().find(CompositeKeyEntity.class, entityKey);
        tx.commit();
    }
    assertNotNull(loadedFoo);
    assertEquals(foo, loadedFoo);
    loadedFoo.setName("bar");
    CompositeKeyEntity bar;
    try (Transaction tx = persistence.createTransaction()) {
        bar = persistence.getEntityManager().merge(loadedFoo);
        tx.commit();
    }
    assertEquals(foo, bar);
    CompositeKeyEntity loadedBar;
    try (Transaction tx = persistence.createTransaction()) {
        loadedBar = persistence.getEntityManager().find(CompositeKeyEntity.class, entityKey);
        tx.commit();
    }
    assertNotNull(loadedBar);
    assertEquals("bar", loadedBar.getName());
    try (Transaction tx = persistence.createTransaction()) {
        loadedBar = persistence.getEntityManager().find(CompositeKeyEntity.class, entityKey);
        persistence.getEntityManager().remove(loadedBar);
        tx.commit();
    }
    assertTrue(BaseEntityInternalAccess.isRemoved(loadedBar));
    try (Transaction tx = persistence.createTransaction()) {
        loadedBar = persistence.getEntityManager().find(CompositeKeyEntity.class, entityKey);
        assertNull(loadedBar);
        tx.commit();
    }
}
Also used : EntityKey(com.haulmont.cuba.testmodel.primary_keys.EntityKey) Transaction(com.haulmont.cuba.core.Transaction) CompositeKeyEntity(com.haulmont.cuba.testmodel.primary_keys.CompositeKeyEntity) Test(org.junit.jupiter.api.Test)

Example 2 with EntityKey

use of com.haulmont.cuba.testmodel.primary_keys.EntityKey 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 EntityKey

use of com.haulmont.cuba.testmodel.primary_keys.EntityKey in project cuba by cuba-platform.

the class CompositeKeyTest method testListParameter.

@Test
public void testListParameter() throws Exception {
    CompositeKeyEntity foo1 = metadata.create(CompositeKeyEntity.class);
    EntityKey entityKey1 = metadata.create(EntityKey.class);
    entityKey1.setTenant(1);
    entityKey1.setEntityId(10L);
    foo1.setId(entityKey1);
    foo1.setName("foo1");
    foo1.setEmail("foo1@mail.com");
    CompositeKeyEntity foo2 = metadata.create(CompositeKeyEntity.class);
    EntityKey entityKey2 = metadata.create(EntityKey.class);
    entityKey2.setTenant(2);
    entityKey2.setEntityId(20L);
    foo2.setId(entityKey2);
    foo2.setName("foo2");
    foo2.setEmail("foo2@mail.com");
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().persist(foo1);
        persistence.getEntityManager().persist(foo2);
        tx.commit();
    }
    try (Transaction tx = persistence.createTransaction()) {
        Query query = persistence.getEntityManager().createQuery("select e from test$CompositeKeyEntity e " + "where e.id.tenant in :list1 and e.id.entityId in :list2");
        query.setParameter("list1", Arrays.asList(entityKey1.getTenant(), entityKey2.getTenant()));
        query.setParameter("list2", Arrays.asList(entityKey1.getEntityId(), entityKey2.getEntityId()));
        List resultList = query.getResultList();
        assertTrue(resultList.contains(foo1) && resultList.contains(foo2));
        tx.commit();
    }
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().remove(foo1);
        persistence.getEntityManager().remove(foo2);
        tx.commit();
    }
}
Also used : EntityKey(com.haulmont.cuba.testmodel.primary_keys.EntityKey) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) List(java.util.List) CompositeKeyEntity(com.haulmont.cuba.testmodel.primary_keys.CompositeKeyEntity) Test(org.junit.jupiter.api.Test)

Aggregations

CompositeKeyEntity (com.haulmont.cuba.testmodel.primary_keys.CompositeKeyEntity)3 EntityKey (com.haulmont.cuba.testmodel.primary_keys.EntityKey)3 Transaction (com.haulmont.cuba.core.Transaction)2 Test (org.junit.jupiter.api.Test)2 Gson (com.google.gson.Gson)1 QueryRunner (com.haulmont.bali.db.QueryRunner)1 Query (com.haulmont.cuba.core.Query)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 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)1 CategoryAttributeConfiguration (com.haulmont.cuba.core.entity.CategoryAttributeConfiguration)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 List (java.util.List)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1