Commit 66d9af55 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

updating user privileges shouldn't cause exception and takes much less time

parent fb7c8012
......@@ -150,7 +150,12 @@ public class UserService implements IUserService {
@Override
public void setUserPrivilege(User user, BasicPrivilege privilege) {
privilege.setUser(user);
updateUserPrivilegesWithoutDbModification(user, privilege);
updateUser(user);
userDao.flush();
}
private void updateUserPrivilegesWithoutDbModification(User user, BasicPrivilege privilege) {
BasicPrivilege oldPrivilege = null;
for (BasicPrivilege privilegeIter : user.getPrivileges()) {
if (privilegeIter.equalsPrivilege(privilege)) {
......@@ -158,13 +163,13 @@ public class UserService implements IUserService {
}
}
if (oldPrivilege != null) {
user.getPrivileges().remove(oldPrivilege);
oldPrivilege.setUser(null);
privilege.setUser(null);
oldPrivilege.setLevel(privilege.getLevel());
} else {
privilege.setUser(user);
user.getPrivileges().add(privilege);
}
user.getPrivileges().add(privilege);
updateUser(user);
userDao.flush();
}
}
@Override
public void addUser(User user) {
......@@ -248,25 +253,32 @@ public class UserService implements IUserService {
for (PrivilegeView pRow : userRow.getBasicPrivileges()) {
if (pRow.getNumeric()) {
setUserPrivilege(user, new BasicPrivilege(pRow.getLevel(), pRow.getType(), user));
updateUserPrivilegesWithoutDbModification(user, new BasicPrivilege(pRow.getLevel(), pRow.getType(), user));
} else if (pRow.getSelected()) {
setUserPrivilege(user, new BasicPrivilege(1, pRow.getType(), user));
updateUserPrivilegesWithoutDbModification(user, new BasicPrivilege(1, pRow.getType(), user));
} else {
setUserPrivilege(user, new BasicPrivilege(0, pRow.getType(), user));
updateUserPrivilegesWithoutDbModification(user, new BasicPrivilege(0, pRow.getType(), user));
}
}
for (UserProjectPrivilegeView upRow : userRow.getProjectPrivileges()) {
for (PrivilegeView pRow : upRow.getProjectPrivileges()) {
Project project = projectDao.getById(upRow.getIdObject());
int projectId = upRow.getIdObject();
ObjectPrivilege privilege = new ObjectPrivilege();
privilege.setIdObject(projectId);
privilege.setType(pRow.getType());
privilege.setUser(user);
if (pRow.getNumeric()) {
setUserPrivilege(user, new ObjectPrivilege(project, pRow.getLevel(), pRow.getType(), user));
privilege.setLevel(pRow.getLevel());
} else if (pRow.getSelected()) {
setUserPrivilege(user, new ObjectPrivilege(project, 1, pRow.getType(), user));
privilege.setLevel(1);
} else {
setUserPrivilege(user, new ObjectPrivilege(project, 0, pRow.getType(), user));
privilege.setLevel(0);
}
updateUserPrivilegesWithoutDbModification(user, privilege);
}
}
updateUser(user);
}
@Override
......
......@@ -10,6 +10,13 @@ import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.user.BasicPrivilege;
......@@ -22,13 +29,6 @@ import lcsb.mapviewer.services.view.UserView;
import lcsb.mapviewer.services.view.UserView.UserProjectPrivilegeView;
import lcsb.mapviewer.services.view.UserViewFactory;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
@Rollback(true)
public class UserServiceTest extends ServiceTestFunctions {
static Logger logger = Logger.getLogger(UserServiceTest.class);
......@@ -305,6 +305,67 @@ public class UserServiceTest extends ServiceTestFunctions {
}
}
boolean error;
@Test
public void testUpdateUsers() {
try {
error = false;
Thread computations = new Thread(new Runnable() {
@Override
public void run() {
try {
// open transaction for this thread
dbUtils.createSessionForCurrentThread();
Project project = new Project();
projectDao.add(project);
List<UserView> list = userService.getAllUserRows();
userService.updateUsers(list);
// close the transaction for this thread
dbUtils.closeSessionForCurrentThread();
} catch (Exception e) {
e.printStackTrace();
error = true;
}
}
});
Thread computations2 = new Thread(new Runnable() {
@Override
public void run() {
try {
// open transaction for this thread
dbUtils.createSessionForCurrentThread();
Project project = new Project();
projectDao.add(project);
List<UserView> list = userService.getAllUserRows();
userService.updateUsers(list);
// close the transaction for this thread
dbUtils.closeSessionForCurrentThread();
} catch (Exception e) {
e.printStackTrace();
error = true;
}
}
});
computations.start();
computations2.start();
computations.join();
computations2.join();
assertFalse(error);
} catch (Exception e) {
e.printStackTrace();
fail("Unknown exception");
}
}
/**
* Test update of a user that exist in the db
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment