Program: 115033
Project: 115033
Buggy source:
public boolean saveStudyEnvRoles(HttpServletRequest request,UserAccountBean ubIn) throws Exception {
  UserAccount ub=userAccountDao.findByUserId(ubIn.getId());
  processSpecificStudyEnvUuid(request,ub);
  boolean studyUserRoleUpdated=false;
  int userActiveStudyId;
  if (ub.getActiveStudy() == null)   userActiveStudyId=0;
 else   userActiveStudyId=ub.getActiveStudy().getStudyId();
  ResponseEntity<StudyEnvironmentRoleDTO[]> userRoles=getUserRoles(request);
  for (  StudyEnvironmentRoleDTO role : userRoles.getBody()) {
    Study study=studyDao.findByStudyEnvUuid(role.getStudyEnvironmentUuid());
    if (study == null)     continue;
    Study parentStudy=study.getStudy();
    Study toUpdate=parentStudy == null ? study : study.getStudy();
    if (ub.getActiveStudy().getStudyId() == 0) {
      ub.setActiveStudy(toUpdate);
      userAccountDao.saveOrUpdate(ub);
    }
    UserAccount userAccount=new UserAccount();
    userAccount.setUserName(ub.getUserName());
    ArrayList<StudyUserRole> byUserAccount=studyUserRoleDao.findAllUserRolesByUserAccount(userAccount,study.getStudyId(),toUpdate.getStudyId());
    String rolename=role.getRoleName();
    String ocRole=getOCRole(rolename,false);
    if (byUserAccount.isEmpty()) {
      StudyUserRole studyUserRole=new StudyUserRole();
      StudyUserRoleId userRoleId=new StudyUserRoleId();
      studyUserRole.setId(userRoleId);
      userRoleId.setUserName(ub.getUserName());
      studyUserRole.setOwnerId(ub.getUserId());
      studyUserRole.setRoleName(ocRole);
      userRoleId.setStudyId(study.getStudyId());
      studyUserRole.setStatusId(org.akaza.openclinica.bean.core.Status.AVAILABLE.getId());
      studyUserRole.setDateCreated(new Date());
      studyUserRoleDao.saveOrUpdate(studyUserRole);
      if (userActiveStudyId == toUpdate.getStudyId())       studyUserRoleUpdated=true;
    }
 else {
      for (      StudyUserRole sur : byUserAccount) {
        if (sur.getRoleName().equals(ocRole))         continue;
 else {
          sur.setRoleName(ocRole);
          sur.setDateUpdated(new Date());
          StudyUserRole studyUserRole=studyUserRoleDao.saveOrUpdate(sur);
          if (userActiveStudyId == toUpdate.getStudyId())           studyUserRoleUpdated=true;
        }
      }
    }
  }
  if (ub.getActiveStudy().getStudyId() == 0) {
    throw new Exception("String_Node_Str");
  }
  if (studyUserRoleUpdated) {
    return true;
  }
 else   return false;
}

Fixed version (ground truth):
public boolean saveStudyEnvRoles(HttpServletRequest request,UserAccountBean ubIn) throws Exception {
  UserAccount ub=userAccountDao.findByUserId(ubIn.getId());
  processSpecificStudyEnvUuid(request,ub);
  boolean studyUserRoleUpdated=false;
  int userActiveStudyId;
  if (ub.getActiveStudy() == null)   userActiveStudyId=0;
 else   userActiveStudyId=ub.getActiveStudy().getStudyId();
  ResponseEntity<StudyEnvironmentRoleDTO[]> userRoles=getUserRoles(request);
  for (  StudyEnvironmentRoleDTO role : userRoles.getBody()) {
    Study study=studyDao.findByStudyEnvUuid(role.getStudyEnvironmentUuid());
    if (study == null)     continue;
    Study parentStudy=study.getStudy();
    Study toUpdate=parentStudy == null ? study : study.getStudy();
    if (ub.getActiveStudy() == null) {
      ub.setActiveStudy(toUpdate);
      userAccountDao.saveOrUpdate(ub);
    }
    UserAccount userAccount=new UserAccount();
    userAccount.setUserName(ub.getUserName());
    ArrayList<StudyUserRole> byUserAccount=studyUserRoleDao.findAllUserRolesByUserAccount(userAccount,study.getStudyId(),toUpdate.getStudyId());
    String rolename=role.getRoleName();
    String ocRole=getOCRole(rolename,false);
    if (byUserAccount.isEmpty()) {
      StudyUserRole studyUserRole=new StudyUserRole();
      StudyUserRoleId userRoleId=new StudyUserRoleId();
      studyUserRole.setId(userRoleId);
      userRoleId.setUserName(ub.getUserName());
      studyUserRole.setOwnerId(ub.getUserId());
      studyUserRole.setRoleName(ocRole);
      userRoleId.setStudyId(study.getStudyId());
      studyUserRole.setStatusId(org.akaza.openclinica.bean.core.Status.AVAILABLE.getId());
      studyUserRole.setDateCreated(new Date());
      studyUserRoleDao.saveOrUpdate(studyUserRole);
      if (userActiveStudyId == toUpdate.getStudyId())       studyUserRoleUpdated=true;
    }
 else {
      for (      StudyUserRole sur : byUserAccount) {
        if (sur.getRoleName().equals(ocRole))         continue;
 else {
          sur.setRoleName(ocRole);
          sur.setDateUpdated(new Date());
          StudyUserRole studyUserRole=studyUserRoleDao.saveOrUpdate(sur);
          if (userActiveStudyId == toUpdate.getStudyId())           studyUserRoleUpdated=true;
        }
      }
    }
  }
  if (ub.getActiveStudy().getStudyId() == 0) {
    throw new Exception("String_Node_Str");
  }
  if (studyUserRoleUpdated) {
    return true;
  }
 else   return false;
}