Program: 114997
Project: 114997
Buggy source:
/** 
 * @api {post} /pages/auth/api/v1/studies/ Create a study
 * @apiName createNewStudy
 * @apiPermission Authenticate using api-key. admin
 * @apiVersion 3.8.0
 * @apiParam {String} uniqueProtococlId Study unique study ID.
 * @apiParam {String} briefTitle Brief Title .
 * @apiParam {String} principalInvestigator Principal Investigator Name.
 * @apiParam {Integer} expectedTotalEnrollment Expected Total Enrollment number
 * @apiParam {String} sponsor Sponsor name.
 * @apiParam {String} studyType 'Interventional' or ' Observational'
 * @apiParam {String} status 'Available' or 'Design'
 * @apiParam {String} briefSummary Study Summary
 * @apiParam {Date} startDate Start date
 * @apiParam {Array} assignUserRoles Assign Users to Roles for this Study.
 * @apiGroup Study
 * @apiHeader {String} api_key Users unique access-key.
 * @apiDescription This API is to create a New Study in OC.All the fields are required fields and can't be left blank. You need to provide your Api-key to be connected.
 * @apiParamExample {json} Request-Example:{ "briefTitle": "Study Study ID Name", "principalInvestigator": "Principal Investigator Name", "expectedTotalEnrollment": "10", "sponsor": "Sponsor Name", "studyType": "Interventional", "status": "available", "assignUserRoles": [ { "username": "usera", "role": "Data Manager" }, { "username": "userb", "role": "Study Director" }, { "username": "userc", "role": "Data Specialist" }, { "username": "userd", "role": "Monitor" }, { "username": "usere", "role": "Data Entry Person" } ], "uniqueStudyID": "Study Study ID", "briefSummary": "Study Summary", "startDate": "2011-11-11" }
 * @apiErrorExample {json} Error-Response:HTTP/1.1 400 Bad Request { "message": "VALIDATION FAILED", "status": "available", "principalInvestigator": "Principal Investigator Name", "expectedTotalEnrollment": "10", "sponsor": "Sponsor Name", "studyType": "Interventional", "errors": [ {"field": "UniqueStudyId","resource": "Study Object","code": "Unique Study Id exist in the System"} ], "startDate": "2011-11-11", "assignUserRoles": [ {"username": "usera","role": "Data Manager"}, {"username": "userb","role": "Study Director"}, {"username": "userc","role": "Data Specialist"} ], "uniqueStudyID": "Study Study ID", "briefTitle": "Study Study ID", "briefSummary": "Study Summary", "studyOid": null }
 * @apiSuccessExample {json} Success-Response:HTTP/1.1 200 OK { "message": "SUCCESS", "uniqueStudyID": "Study Study ID", "studyOid": "S_STUDYPRO", }
 */
@RequestMapping(value="String_Node_Str",method=RequestMethod.PUT) public ResponseEntity<Object> changeStudyStatus(@RequestBody HashMap<String,Object> requestDTO,@PathVariable("String_Node_Str") String studyEnvUuid,HttpServletRequest request){
  ResponseEntity response=null;
  ArrayList<ErrorObject> errorObjects=new ArrayList<ErrorObject>();
  StudyDTO studyDTO=new StudyDTO();
  Locale locale=new Locale("String_Node_Str");
  request.getSession().setAttribute(LocaleResolver.getLocaleSessionAttributeName(),locale);
  ResourceBundleProvider.updateLocale(locale);
  UserAccountBean ub=(UserAccountBean)request.getSession().getAttribute("String_Node_Str");
  if (ub == null) {
    logger.error("String_Node_Str");
    return new ResponseEntity<Object>("String_Node_Str",HttpStatus.FORBIDDEN);
  }
  StudyDAO studyDAO=new StudyDAO(dataSource);
  StudyBean currentPublicStudy=studyDAO.findByStudyEnvUuid(studyEnvUuid);
  String tenantSchema=currentPublicStudy.getSchemaName();
  CoreResources.setRequestSchema(request,tenantSchema);
  StudyBean currentStudy=studyDAO.findByStudyEnvUuid(studyEnvUuid);
  if (currentPublicStudy == null || currentStudy == null) {
    ErrorObject errorObject=createErrorObject("String_Node_Str","String_Node_Str","String_Node_Str");
    errorObjects.add(errorObject);
    studyDTO.setErrors(errorObjects);
    studyDTO.setMessage(validation_failed_message);
    return new ResponseEntity(studyDTO,org.springframework.http.HttpStatus.BAD_REQUEST);
  }
  if (ub != null) {
    CoreResources.setRequestSchema(request,"String_Node_Str");
    studyBuildService.updateStudyUserRoles(request,studyBuildService.getUserAccountObject(ub),ub.getActiveStudyId());
    UserAccountDAO userAccountDAO=new UserAccountDAO(dataSource);
    ub=(UserAccountBean)userAccountDAO.findByUserUuid(ub.getUserUuid());
    if (!roleValidForStatusChange(ub,currentPublicStudy,1)) {
      logger.error("String_Node_Str");
      return new ResponseEntity<Object>("String_Node_Str",HttpStatus.FORBIDDEN);
    }
  }
  CoreResources.setRequestSchema(request,tenantSchema);
  Status status=getStatus((String)requestDTO.get("String_Node_Str"));
  if (status == null) {
    ErrorObject errorObject=createErrorObject("String_Node_Str","String_Node_Str","String_Node_Str");
    errorObjects.add(errorObject);
    studyDTO.setErrors(errorObjects);
    studyDTO.setMessage(validation_failed_message);
    return new ResponseEntity(studyDTO,org.springframework.http.HttpStatus.BAD_REQUEST);
  }
 else   if (!status.equals(Status.PENDING) && !status.equals(Status.AVAILABLE) && !status.equals(Status.FROZEN)&& !status.equals(Status.LOCKED)) {
    ErrorObject errorObject=createErrorObject("String_Node_Str","String_Node_Str","String_Node_Str");
    errorObjects.add(errorObject);
    studyDTO.setErrors(errorObjects);
    studyDTO.setMessage(validation_failed_message);
    return new ResponseEntity(studyDTO,org.springframework.http.HttpStatus.BAD_REQUEST);
  }
  currentStudy.setOldStatus(currentStudy.getStatus());
  currentStudy.setStatus(status);
  studyDAO.updateStudyStatus(currentStudy);
  ArrayList siteList=(ArrayList)studyDAO.findAllByParent(currentStudy.getId());
  if (siteList.size() > 0) {
    studyDAO.updateSitesStatus(currentStudy);
  }
  CoreResources.setRequestSchema(request,"String_Node_Str");
  currentPublicStudy.setOldStatus(currentPublicStudy.getStatus());
  currentPublicStudy.setStatus(status);
  studyDAO.updateStudyStatus(currentPublicStudy);
  ArrayList publicSiteList=(ArrayList)studyDAO.findAllByParent(currentPublicStudy.getId());
  if (publicSiteList.size() > 0) {
    studyDAO.updateSitesStatus(currentPublicStudy);
  }
  StudyEnvStatusDTO studyEnvStatusDTO=new StudyEnvStatusDTO();
  studyEnvStatusDTO.setStudyEnvUuid(currentPublicStudy.getStudyEnvUuid());
  studyEnvStatusDTO.setStatus(currentPublicStudy.getStatus().getName());
  ArrayList updatedPublicSiteList=(ArrayList)studyDAO.findAllByParent(currentPublicStudy.getId());
  for (  StudyBean site : (ArrayList<StudyBean>)updatedPublicSiteList) {
    SiteStatusDTO siteStatusDTO=new SiteStatusDTO();
    siteStatusDTO.setSiteUuid(site.getStudyEnvSiteUuid());
    siteStatusDTO.setStatus(site.getStatus().getName());
    studyEnvStatusDTO.getSiteStatuses().add(siteStatusDTO);
  }
  return new ResponseEntity(studyEnvStatusDTO,org.springframework.http.HttpStatus.OK);
}

Fixed version (ground truth):
/** 
 * @api {post} /pages/auth/api/v1/studies/ Create a study
 * @apiName createNewStudy
 * @apiPermission Authenticate using api-key. admin
 * @apiVersion 3.8.0
 * @apiParam {String} uniqueProtococlId Study unique study ID.
 * @apiParam {String} briefTitle Brief Title .
 * @apiParam {String} principalInvestigator Principal Investigator Name.
 * @apiParam {Integer} expectedTotalEnrollment Expected Total Enrollment number
 * @apiParam {String} sponsor Sponsor name.
 * @apiParam {String} studyType 'Interventional' or ' Observational'
 * @apiParam {String} status 'Available' or 'Design'
 * @apiParam {String} briefSummary Study Summary
 * @apiParam {Date} startDate Start date
 * @apiParam {Array} assignUserRoles Assign Users to Roles for this Study.
 * @apiGroup Study
 * @apiHeader {String} api_key Users unique access-key.
 * @apiDescription This API is to create a New Study in OC.All the fields are required fields and can't be left blank. You need to provide your Api-key to be connected.
 * @apiParamExample {json} Request-Example:{ "briefTitle": "Study Study ID Name", "principalInvestigator": "Principal Investigator Name", "expectedTotalEnrollment": "10", "sponsor": "Sponsor Name", "studyType": "Interventional", "status": "available", "assignUserRoles": [ { "username": "usera", "role": "Data Manager" }, { "username": "userb", "role": "Study Director" }, { "username": "userc", "role": "Data Specialist" }, { "username": "userd", "role": "Monitor" }, { "username": "usere", "role": "Data Entry Person" } ], "uniqueStudyID": "Study Study ID", "briefSummary": "Study Summary", "startDate": "2011-11-11" }
 * @apiErrorExample {json} Error-Response:HTTP/1.1 400 Bad Request { "message": "VALIDATION FAILED", "status": "available", "principalInvestigator": "Principal Investigator Name", "expectedTotalEnrollment": "10", "sponsor": "Sponsor Name", "studyType": "Interventional", "errors": [ {"field": "UniqueStudyId","resource": "Study Object","code": "Unique Study Id exist in the System"} ], "startDate": "2011-11-11", "assignUserRoles": [ {"username": "usera","role": "Data Manager"}, {"username": "userb","role": "Study Director"}, {"username": "userc","role": "Data Specialist"} ], "uniqueStudyID": "Study Study ID", "briefTitle": "Study Study ID", "briefSummary": "Study Summary", "studyOid": null }
 * @apiSuccessExample {json} Success-Response:HTTP/1.1 200 OK { "message": "SUCCESS", "uniqueStudyID": "Study Study ID", "studyOid": "S_STUDYPRO", }
 */
@RequestMapping(value="String_Node_Str",method=RequestMethod.PUT) public ResponseEntity<Object> changeStudyStatus(@RequestBody HashMap<String,Object> requestDTO,@PathVariable("String_Node_Str") String studyEnvUuid,HttpServletRequest request){
  ResponseEntity response=null;
  ArrayList<ErrorObject> errorObjects=new ArrayList<ErrorObject>();
  StudyDTO studyDTO=new StudyDTO();
  Locale locale=new Locale("String_Node_Str");
  request.getSession().setAttribute(LocaleResolver.getLocaleSessionAttributeName(),locale);
  ResourceBundleProvider.updateLocale(locale);
  UserAccountBean ub=(UserAccountBean)request.getSession().getAttribute("String_Node_Str");
  if (ub == null) {
    logger.error("String_Node_Str");
    return new ResponseEntity<Object>("String_Node_Str",HttpStatus.FORBIDDEN);
  }
  StudyDAO studyDAO=new StudyDAO(dataSource);
  StudyBean currentPublicStudy=studyDAO.findByStudyEnvUuid(studyEnvUuid);
  String tenantSchema=currentPublicStudy.getSchemaName();
  CoreResources.setRequestSchema(request,tenantSchema);
  StudyBean currentStudy=studyDAO.findByStudyEnvUuid(studyEnvUuid);
  if (currentPublicStudy == null || currentStudy == null) {
    ErrorObject errorObject=createErrorObject("String_Node_Str","String_Node_Str","String_Node_Str");
    errorObjects.add(errorObject);
    studyDTO.setErrors(errorObjects);
    studyDTO.setMessage(validation_failed_message);
    return new ResponseEntity(studyDTO,org.springframework.http.HttpStatus.BAD_REQUEST);
  }
  if (ub != null) {
    CoreResources.setRequestSchema(request,"String_Node_Str");
    studyBuildService.updateStudyUserRoles(request,studyBuildService.getUserAccountObject(ub),ub.getActiveStudyId());
    UserAccountDAO userAccountDAO=new UserAccountDAO(dataSource);
    if (StringUtils.isEmpty(ub.getUserUuid()))     ub=(UserAccountBean)userAccountDAO.findByUserName(ub.getName());
 else     ub=(UserAccountBean)userAccountDAO.findByUserUuid(ub.getUserUuid());
    if (!roleValidForStatusChange(ub,currentPublicStudy,1)) {
      logger.error("String_Node_Str");
      return new ResponseEntity<Object>("String_Node_Str",HttpStatus.FORBIDDEN);
    }
  }
  CoreResources.setRequestSchema(request,tenantSchema);
  Status status=getStatus((String)requestDTO.get("String_Node_Str"));
  if (status == null) {
    ErrorObject errorObject=createErrorObject("String_Node_Str","String_Node_Str","String_Node_Str");
    errorObjects.add(errorObject);
    studyDTO.setErrors(errorObjects);
    studyDTO.setMessage(validation_failed_message);
    return new ResponseEntity(studyDTO,org.springframework.http.HttpStatus.BAD_REQUEST);
  }
 else   if (!status.equals(Status.PENDING) && !status.equals(Status.AVAILABLE) && !status.equals(Status.FROZEN)&& !status.equals(Status.LOCKED)) {
    ErrorObject errorObject=createErrorObject("String_Node_Str","String_Node_Str","String_Node_Str");
    errorObjects.add(errorObject);
    studyDTO.setErrors(errorObjects);
    studyDTO.setMessage(validation_failed_message);
    return new ResponseEntity(studyDTO,org.springframework.http.HttpStatus.BAD_REQUEST);
  }
  currentStudy.setOldStatus(currentStudy.getStatus());
  currentStudy.setStatus(status);
  studyDAO.updateStudyStatus(currentStudy);
  ArrayList siteList=(ArrayList)studyDAO.findAllByParent(currentStudy.getId());
  if (siteList.size() > 0) {
    studyDAO.updateSitesStatus(currentStudy);
  }
  CoreResources.setRequestSchema(request,"String_Node_Str");
  currentPublicStudy.setOldStatus(currentPublicStudy.getStatus());
  currentPublicStudy.setStatus(status);
  studyDAO.updateStudyStatus(currentPublicStudy);
  ArrayList publicSiteList=(ArrayList)studyDAO.findAllByParent(currentPublicStudy.getId());
  if (publicSiteList.size() > 0) {
    studyDAO.updateSitesStatus(currentPublicStudy);
  }
  StudyEnvStatusDTO studyEnvStatusDTO=new StudyEnvStatusDTO();
  studyEnvStatusDTO.setStudyEnvUuid(currentPublicStudy.getStudyEnvUuid());
  studyEnvStatusDTO.setStatus(currentPublicStudy.getStatus().getName());
  ArrayList updatedPublicSiteList=(ArrayList)studyDAO.findAllByParent(currentPublicStudy.getId());
  for (  StudyBean site : (ArrayList<StudyBean>)updatedPublicSiteList) {
    SiteStatusDTO siteStatusDTO=new SiteStatusDTO();
    siteStatusDTO.setSiteUuid(site.getStudyEnvSiteUuid());
    siteStatusDTO.setStatus(site.getStatus().getName());
    studyEnvStatusDTO.getSiteStatuses().add(siteStatusDTO);
  }
  return new ResponseEntity(studyEnvStatusDTO,org.springframework.http.HttpStatus.OK);
}