Search in sources :

Example 86 with EntityManager

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

the class LoadSubstitutionsTest method testQuerySubstitutions.

@Test
public void testQuerySubstitutions() throws Exception {
    ViewRepository viewRepository = AppBeans.get(ViewRepository.NAME);
    View userView = new View(new View.ViewParams().src(viewRepository.getView(User.class, View.LOCAL)));
    View substitutedUserView = new View(User.class);
    substitutedUserView.addProperty("login");
    View substitutionsView = new View(UserSubstitution.class);
    substitutionsView.addProperty("substitutedUser", substitutedUserView);
    substitutionsView.addProperty("startDate");
    userView.addProperty("substitutions", substitutionsView);
    User loadedUser;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        loadedUser = em.find(User.class, user.getId(), userView);
        tx.commit();
    }
    assertNotNull(loadedUser);
    assertNotNull(loadedUser.getSubstitutions());
    Assert.assertEquals(1, loadedUser.getSubstitutions().size());
    UserSubstitution loadedSubstitution = loadedUser.getSubstitutions().iterator().next();
    assertEquals(user, loadedSubstitution.getUser());
    assertEquals(substitutedUser, loadedSubstitution.getSubstitutedUser());
}
Also used : ViewRepository(com.haulmont.cuba.core.global.ViewRepository) EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserSubstitution(com.haulmont.cuba.security.entity.UserSubstitution) View(com.haulmont.cuba.core.global.View)

Example 87 with EntityManager

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

the class LoadSubstitutionsTest method setUp.

@Before
public void setUp() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Group group = em.find(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93"));
        user = new User();
        user.setLogin("user-" + user.getId());
        user.setGroup(group);
        em.persist(user);
        substitutedUser = new User();
        substitutedUser.setLogin("substitutedUser");
        substitutedUser.setGroup(group);
        em.persist(substitutedUser);
        userSubstitution = new UserSubstitution();
        userSubstitution.setUser(user);
        userSubstitution.setSubstitutedUser(substitutedUser);
        user.setSubstitutions(new ArrayList<>(ImmutableList.of(userSubstitution)));
        em.persist(userSubstitution);
        tx.commit();
    } finally {
        tx.end();
    }
    cont.setupLogging("com.haulmont.cuba.core.sys.FetchGroupManager", Level.TRACE);
}
Also used : Group(com.haulmont.cuba.security.entity.Group) EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserSubstitution(com.haulmont.cuba.security.entity.UserSubstitution)

Example 88 with EntityManager

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

the class LoginTest method testUserSubstitutionSoftDelete.

@Test
public void testUserSubstitutionSoftDelete() throws Exception {
    // Create a substitution
    cont.persistence().createTransaction().execute(new Transaction.Runnable() {

        @Override
        public void run(EntityManager em) {
            UserSubstitution substitution = new UserSubstitution();
            substitutionId = substitution.getId();
            substitution.setUser(em.getReference(User.class, user1Id));
            substitution.setSubstitutedUser(em.getReference(User.class, user2Id));
            em.persist(substitution);
        }
    });
    // Soft delete it
    cont.persistence().createTransaction().execute(new Transaction.Runnable() {

        @Override
        public void run(EntityManager em) {
            UserSubstitution substitution = em.getReference(UserSubstitution.class, substitutionId);
            em.remove(substitution);
        }
    });
    // Log in
    UserSession session1 = loginWorker.login("user1", passwordEncryption.getPlainHash("1"), Locale.forLanguageTag("en"));
    userSessionSource.setUserSession(session1);
    // Try to substitute - fail
    User user2 = loadUser(user2Id);
    try {
        loginWorker.substituteUser(user2);
        fail();
    } catch (Exception e) {
    // ok
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserSession(com.haulmont.cuba.security.global.UserSession) UserSubstitution(com.haulmont.cuba.security.entity.UserSubstitution) Test(org.junit.Test)

Example 89 with EntityManager

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

the class LoginTest method testUserSubstitution.

@Test
public void testUserSubstitution() throws Exception {
    // Log in
    UserSession session1 = loginWorker.login("user1", passwordEncryption.getPlainHash("1"), Locale.forLanguageTag("en"));
    userSessionSource.setUserSession(session1);
    // Substitute a user that is not in our substitutions list - fail
    User user2 = loadUser(user2Id);
    try {
        loginWorker.substituteUser(user2);
        fail();
    } catch (Exception e) {
    // ok
    }
    // Create a substitution
    cont.persistence().createTransaction().execute(new Transaction.Runnable() {

        @Override
        public void run(EntityManager em) {
            UserSubstitution substitution = new UserSubstitution();
            substitutionId = substitution.getId();
            substitution.setUser(em.getReference(User.class, user1Id));
            substitution.setSubstitutedUser(em.getReference(User.class, user2Id));
            em.persist(substitution);
        }
    });
    // Try again - succeed
    UserSession session2 = loginWorker.substituteUser(user2);
    userSessionSource.setUserSession(session2);
    assertEquals(session1.getId(), session2.getId());
    assertEquals(user1Id, session2.getUser().getId());
    assertEquals(user2Id, session2.getSubstitutedUser().getId());
    // Switch back to the logged in user
    User user1 = loadUser(user1Id);
    UserSession session3 = loginWorker.substituteUser(user1);
    assertEquals(session1.getId(), session3.getId());
    assertEquals(user1Id, session3.getUser().getId());
    assertNull(session3.getSubstitutedUser());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserSession(com.haulmont.cuba.security.global.UserSession) UserSubstitution(com.haulmont.cuba.security.entity.UserSubstitution) Test(org.junit.Test)

Example 90 with EntityManager

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

the class QueryResultsManager method insert.

@Override
public void insert(int queryKey, List idList) {
    if (idList.isEmpty())
        return;
    UUID userSessionId = userSessionSource.getUserSession().getId();
    long start = System.currentTimeMillis();
    String logMsg = "Insert " + idList.size() + " query results for " + userSessionId + " / " + queryKey;
    log.debug(logMsg);
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        DbTypeConverter converter = persistence.getDbTypeConverter();
        Object idFromList = idList.get(0);
        String columnName = null;
        if (idFromList instanceof String) {
            columnName = "STRING_ENTITY_ID";
        } else if (idFromList instanceof Long) {
            columnName = "LONG_ENTITY_ID";
        } else if (idFromList instanceof Integer) {
            columnName = "INT_ENTITY_ID";
        } else {
            columnName = "ENTITY_ID";
        }
        QueryRunner runner = new QueryRunner();
        try {
            // assuming that UUID can be passed to query as string in all databases
            String userSessionIdStr = converter.getSqlObject(userSessionId).toString();
            String sql = String.format("insert into SYS_QUERY_RESULT (SESSION_ID, QUERY_KEY, %s) values ('%s', %s, ?)", columnName, userSessionIdStr, queryKey);
            int[] paramTypes = new int[] { converter.getSqlType(idFromList.getClass()) };
            for (int i = 0; i < idList.size(); i += BATCH_SIZE) {
                List<UUID> sublist = idList.subList(i, Math.min(i + BATCH_SIZE, idList.size()));
                Object[][] params = new Object[sublist.size()][1];
                for (int j = 0; j < sublist.size(); j++) {
                    params[j][0] = converter.getSqlObject(sublist.get(j));
                }
                runner.batch(em.getConnection(), sql, params, paramTypes);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        log.debug("Done in " + (System.currentTimeMillis() - start) + "ms: " + logMsg);
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : SQLException(java.sql.SQLException) DbTypeConverter(com.haulmont.cuba.core.sys.persistence.DbTypeConverter) QueryRunner(com.haulmont.bali.db.QueryRunner) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Aggregations

EntityManager (com.haulmont.cuba.core.EntityManager)167 Transaction (com.haulmont.cuba.core.Transaction)140 Query (com.haulmont.cuba.core.Query)27 User (com.haulmont.cuba.security.entity.User)27 Test (org.junit.Test)25 View (com.haulmont.cuba.core.global.View)22 MetaClass (com.haulmont.chile.core.model.MetaClass)14 Group (com.haulmont.cuba.security.entity.Group)12 Before (org.junit.Before)11 Entity (com.haulmont.cuba.core.entity.Entity)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 UUID (java.util.UUID)7 Nullable (javax.annotation.Nullable)7 TypedQuery (com.haulmont.cuba.core.TypedQuery)6 List (java.util.List)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)5 Role (com.haulmont.cuba.security.entity.Role)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)5 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)4