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());
}
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);
}
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
}
}
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());
}
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();
}
}
Aggregations