}
@Override
public User setCurrentWorkspace(String userId, String workspaceId) {
if (userId == null) {
throw new LumifyException("UserId cannot be null");
}
Session session = sessionManager.getSession();
Transaction transaction = null;
SqlUser sqlUser = null;
try {
transaction = session.beginTransaction();
sqlUser = (SqlUser) findById(userId);
if (sqlUser == null) {
throw new LumifyException("User does not exist");
}
List<SqlWorkspace> workspaces = session.createQuery
("select workspace from " + SqlWorkspace.class.getSimpleName() + " as workspace where workspace.workspaceId=:id")
.setParameter("id", workspaceId)
.list();
if (workspaces.size() == 0) {
throw new LumifyException("Could not find workspace with id: " + workspaceId);
}
sqlUser.setCurrentWorkspace(workspaces.get(0));
session.merge(sqlUser);
transaction.commit();
} catch (HibernateException e) {
if (transaction != null) {
transaction.rollback();
}
throw new LumifyException("HibernateException while setting current workspace", e);
}
return sqlUser;
}