Search in sources :

Example 26 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class ViewHelper method intersectViews.

public static View intersectViews(View first, View second) {
    if (first == null)
        throw new IllegalArgumentException("View is null");
    if (second == null)
        throw new IllegalArgumentException("View is null");
    View resultView = new View(first.getEntityClass());
    Collection<ViewProperty> firstProps = first.getProperties();
    for (ViewProperty firstProperty : firstProps) {
        if (second.containsProperty(firstProperty.getName())) {
            View resultPropView = null;
            ViewProperty secondProperty = second.getProperty(firstProperty.getName());
            if ((firstProperty.getView() != null) && (secondProperty.getView() != null)) {
                resultPropView = intersectViews(firstProperty.getView(), secondProperty.getView());
            }
            resultView.addProperty(firstProperty.getName(), resultPropView);
        }
    }
    return resultView;
}
Also used : ViewProperty(com.haulmont.cuba.core.global.ViewProperty) View(com.haulmont.cuba.core.global.View)

Example 27 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class EntityFactory method build.

@Override
public Object build(String string) {
    if (StringUtils.isBlank(string)) {
        return null;
    }
    EntityLoadInfo info = EntityLoadInfo.parse(string);
    if (info == null) {
        throw new IllegalArgumentException("Invalid entity info: " + string);
    }
    Entity entity;
    String property = AppContext.getProperty("cuba.useCurrentTxForConfigEntityLoad");
    Transaction tx = Boolean.valueOf(property) ? persistence.getTransaction() : persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        View view = null;
        if (info.getViewName() != null) {
            view = metadata.getViewRepository().getView(info.getMetaClass(), info.getViewName());
        }
        Class javaClass = info.getMetaClass().getJavaClass();
        if (view != null) {
            entity = em.find(javaClass, info.getId(), view);
        } else {
            entity = em.find(javaClass, info.getId());
        }
        tx.commit();
    } finally {
        tx.end();
    }
    return entity;
}
Also used : EntityLoadInfo(com.haulmont.cuba.core.global.EntityLoadInfo) Entity(com.haulmont.cuba.core.entity.Entity) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View)

Example 28 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class EntityManagerTest method testReloadSameTx.

@Test
public void testReloadSameTx() throws Exception {
    User originalUser, reloadedUser;
    View view = new View(User.class, false).addProperty("name");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        originalUser = em.find(User.class, userId, view);
        assertNotNull(originalUser);
        reloadedUser = em.reload(originalUser, "user.edit");
        tx.commit();
    } finally {
        tx.end();
    }
    assertTrue(originalUser == reloadedUser);
    reloadedUser = reserialize(reloadedUser);
    assertNotNull(reloadedUser);
    assertNotNull(reloadedUser.getLogin());
    assertTrue(PersistenceHelper.isLoaded(reloadedUser, "userRoles"));
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 29 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class EntityManagerTest method testFindCombinedView.

@Test
public void testFindCombinedView() throws Exception {
    View view1 = new View(User.class, false).addProperty("name").addProperty("login");
    View view2 = new View(User.class, false).addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"));
    User user1;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        user1 = em.find(User.class, userId, view1, view2);
        tx.commit();
    }
    user1 = reserialize(user1);
    assertNotNull(user1);
    assertNotNull(user1.getCreatedBy());
    assertNotNull(user1.getGroup());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 30 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class SerializationTest method testManagedSerialization.

@Test
public void testManagedSerialization() throws Exception {
    View view = getView();
    User user;
    User kryoUser;
    User standardUser;
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        user = em.find(User.class, userId, view);
        assertNotNull(user);
        KryoSerialization kryoSerialization = new KryoSerialization();
        kryoUser = (User) kryoSerialization.deserialize(kryoSerialization.serialize(user));
        tx.commit();
    } finally {
        tx.end();
    }
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        user = em.find(User.class, userId, view);
        assertNotNull(user);
        StandardSerialization standardSerialization = new StandardSerialization();
        standardUser = (User) standardSerialization.deserialize(standardSerialization.serialize(user));
        tx.commit();
    } finally {
        tx.end();
    }
    assertEquals(standardUser, kryoUser);
    assertEquals(standardUser.getLogin(), kryoUser.getLogin());
    assertEquals(standardUser.getName(), kryoUser.getName());
    assertEquals(standardUser.getPosition(), kryoUser.getPosition());
    assertEquals(standardUser.getGroup(), kryoUser.getGroup());
    assertEquals(standardUser.getGroup().getName(), kryoUser.getGroup().getName());
    assertEquals(standardUser.getUserRoles().get(0), kryoUser.getUserRoles().get(0));
    assertEquals(standardUser.getUserRoles().get(0).getRole(), kryoUser.getUserRoles().get(0).getRole());
    assertEquals(standardUser.getUserRoles().get(0).getRole().getName(), kryoUser.getUserRoles().get(0).getRole().getName());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View)

Aggregations

View (com.haulmont.cuba.core.global.View)55 Test (org.junit.Test)24 EntityManager (com.haulmont.cuba.core.EntityManager)22 Transaction (com.haulmont.cuba.core.Transaction)21 User (com.haulmont.cuba.security.entity.User)19 Group (com.haulmont.cuba.security.entity.Group)10 MetaClass (com.haulmont.chile.core.model.MetaClass)5 ViewProperty (com.haulmont.cuba.core.global.ViewProperty)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)3 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 Entity (com.haulmont.cuba.core.entity.Entity)2 ViewRepository (com.haulmont.cuba.core.global.ViewRepository)2 Role (com.haulmont.cuba.security.entity.Role)2 SoftDeleteOneToOneB (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneB)2 QueryRunner (com.haulmont.bali.db.QueryRunner)1 MetadataObject (com.haulmont.chile.core.model.MetadataObject)1 DynamicAttributesMetaProperty (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty)1 QueryResult (com.haulmont.cuba.core.entity.QueryResult)1 Server (com.haulmont.cuba.core.entity.Server)1