Example usage for org.springframework.dao DataIntegrityViolationException getMostSpecificCause

List of usage examples for org.springframework.dao DataIntegrityViolationException getMostSpecificCause

Introduction

In this page you can find the example usage for org.springframework.dao DataIntegrityViolationException getMostSpecificCause.

Prototype

public Throwable getMostSpecificCause() 

Source Link

Document

Retrieve the most specific cause of this exception, that is, either the innermost cause (root cause) or this exception itself.

Usage

From source file:edu.isi.misd.scanner.network.registry.web.controller.StudyManagementPolicyController.java

@RequestMapping(value = ENTITY_PATH, method = RequestMethod.PUT, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
public @ResponseBody StudyManagementPolicy updateStudyManagementPolicy(
        @RequestHeader(HEADER_LOGIN_NAME) String loginName, @PathVariable(ID_URL_PATH_VAR) Integer id,
        @RequestBody StudyManagementPolicy studyManagementPolicy) {
    // find the requested resource
    StudyManagementPolicy foundStudyManagementPolicy = studyManagementPolicyRepository.findOne(id);
    // if the ID is not found then throw a ResourceNotFoundException (404)
    if (foundStudyManagementPolicy == null) {
        throw new ResourceNotFoundException(id);
    }// ww w .ja  v  a  2  s .  c o m
    // if the ID in the request body is null, use the ID parsed from the URL
    // if the ID is found in the request body but does not match the ID in 
    // the current data, then throw a ConflictException (409)
    Integer updateID = studyManagementPolicy.getStudyPolicyId();
    if (updateID == null) {
        studyManagementPolicy.setStudyPolicyId(id);
    } else if (!studyManagementPolicy.getStudyPolicyId()
            .equals(foundStudyManagementPolicy.getStudyPolicyId())) {
        throw new ConflictException(studyManagementPolicy.getStudyPolicyId(),
                foundStudyManagementPolicy.getStudyPolicyId());
    }
    // check that the user can perform the update
    if (!registryService.userCanManageStudy(loginName, studyManagementPolicy.getStudy().getStudyId())) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_STUDY_MANAGEMENT_ROLE_REQUIRED);
    }
    try {
        studyManagementPolicyRepository.save(studyManagementPolicy);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return studyManagementPolicyRepository.findOne(studyManagementPolicy.getStudyPolicyId());
}

From source file:edu.isi.misd.scanner.network.registry.web.controller.StudyPolicyStatementController.java

@RequestMapping(value = BASE_PATH, method = RequestMethod.POST, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
@ResponseStatus(value = HttpStatus.CREATED)
public @ResponseBody StudyPolicyStatement createStudyPolicyStatement(
        @RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @RequestBody StudyPolicyStatement studyPolicyStatement) {
    // first, check that the requested Study association is valid
    Assert.notNull(studyPolicyStatement.getStudy(), nullVariableMsg(Study.class.getSimpleName()));
    Integer studyId = studyPolicyStatement.getStudy().getStudyId();
    Study study = studyRepository.findOne(studyId);
    if (study == null) {
        throw new BadRequestException(Study.class.getSimpleName(), studyId);
    }//from  w  w w .  j  av  a 2  s .com
    // check that the user can perform the create
    if (!registryService.userCanManageStudy(loginName, study.getStudyId())) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_STUDY_MANAGEMENT_ROLE_REQUIRED);
    }
    studyPolicyStatement.setPolicyOriginator(scannerUserRepository.findByUserName(loginName));
    try {
        studyPolicyStatementRepository.save(studyPolicyStatement);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return studyPolicyStatementRepository.findOne(studyPolicyStatement.getStudyPolicyStatementId());
}

From source file:edu.isi.misd.scanner.network.registry.web.controller.StudyPolicyStatementController.java

@RequestMapping(value = ENTITY_PATH, method = RequestMethod.PUT, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
public @ResponseBody StudyPolicyStatement updateStudyPolicyStatement(
        @RequestHeader(HEADER_LOGIN_NAME) String loginName, @PathVariable(ID_URL_PATH_VAR) Integer id,
        @RequestBody StudyPolicyStatement studyPolicyStatement) {
    // find the requested resource
    StudyPolicyStatement foundStudyPolicyStatement = studyPolicyStatementRepository.findOne(id);
    // if the ID is not found then throw a ResourceNotFoundException (404)
    if (foundStudyPolicyStatement == null) {
        throw new ResourceNotFoundException(id);
    }/*from w  w  w .j ava  2s  .  c  o m*/
    // if the ID in the request body is null, use the ID parsed from the URL
    // if the ID is found in the request body but does not match the ID in 
    // the current data, then throw a ConflictException (409)
    Integer updateID = studyPolicyStatement.getStudyPolicyStatementId();
    if (updateID == null) {
        studyPolicyStatement.setStudyPolicyStatementId(id);
    } else if (!studyPolicyStatement.getStudyPolicyStatementId()
            .equals(foundStudyPolicyStatement.getStudyPolicyStatementId())) {
        throw new ConflictException(studyPolicyStatement.getStudyPolicyStatementId(),
                foundStudyPolicyStatement.getStudyPolicyStatementId());
    }
    // check that the user can perform the update
    if (!registryService.userCanManageStudy(loginName, studyPolicyStatement.getStudy().getStudyId())) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_STUDY_MANAGEMENT_ROLE_REQUIRED);
    }

    try {
        studyPolicyStatementRepository.save(studyPolicyStatement);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return studyPolicyStatementRepository.findOne(studyPolicyStatement.getStudyPolicyStatementId());
}

From source file:edu.isi.misd.scanner.network.registry.web.controller.StudyRequestedSiteController.java

@RequestMapping(value = BASE_PATH, method = RequestMethod.POST, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
@ResponseStatus(value = HttpStatus.CREATED)
public @ResponseBody StudyRequestedSite createStudyRequestedSite(
        @RequestHeader(HEADER_LOGIN_NAME) String loginName, @RequestBody StudyRequestedSite site) {
    // first, check that the requested Study association is valid
    Assert.notNull(site.getStudy(), nullVariableMsg(Study.class.getSimpleName()));
    Integer studyId = site.getStudy().getStudyId();
    Study study = studyRepository.findOne(studyId);
    if (study == null) {
        throw new BadRequestException(Study.class.getSimpleName(), studyId);
    }//from w  ww.  ja  va 2 s  . c  om
    // check that the user can perform the create
    if (!registryService.userCanManageStudy(loginName, study.getStudyId())) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_STUDY_MANAGEMENT_ROLE_REQUIRED);
    }
    try {
        studyRequestedSiteRepository.save(site);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return studyRequestedSiteRepository.findOne(site.getStudyRequestedSiteId());
}

From source file:edu.isi.misd.scanner.network.registry.web.controller.StudyRequestedSiteController.java

@RequestMapping(value = ENTITY_PATH, method = RequestMethod.PUT, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
public @ResponseBody StudyRequestedSite updateStudyRequestedSite(
        @RequestHeader(HEADER_LOGIN_NAME) String loginName, @PathVariable(ID_URL_PATH_VAR) Integer id,
        @RequestBody StudyRequestedSite site) {
    // find the requested resource
    StudyRequestedSite foundStudyRequestedSite = studyRequestedSiteRepository.findOne(id);
    // if the ID is not found then throw a ResourceNotFoundException (404)
    if (foundStudyRequestedSite == null) {
        throw new ResourceNotFoundException(id);
    }// w  ww.j a  va  2 s  .  com
    // if the ID in the request body is null, use the ID parsed from the URL
    // if the ID is found in the request body but does not match the ID in 
    // the current data, then throw a ConflictException (409)
    Integer updateID = site.getStudyRequestedSiteId();
    if (updateID == null) {
        site.setStudyRequestedSiteId(id);
    } else if (!site.getStudyRequestedSiteId().equals(foundStudyRequestedSite.getStudyRequestedSiteId())) {
        throw new ConflictException(site.getStudyRequestedSiteId(),
                foundStudyRequestedSite.getStudyRequestedSiteId());
    }
    // check that the user can perform the update
    if (!registryService.userCanManageStudy(loginName, site.getStudy().getStudyId())) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_STUDY_MANAGEMENT_ROLE_REQUIRED);
    }

    try {
        studyRequestedSiteRepository.save(site);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return studyRequestedSiteRepository.findOne(site.getStudyRequestedSiteId());
}

From source file:edu.isi.misd.scanner.network.registry.web.controller.StudyRoleController.java

@RequestMapping(value = BASE_PATH, method = RequestMethod.POST, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
@ResponseStatus(value = HttpStatus.CREATED)
public @ResponseBody StudyRole createStudyRole(@RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @RequestBody StudyRole studyRole) {
    // first, check that the requested Study association is valid
    Assert.notNull(studyRole.getStudy(), nullVariableMsg(Study.class.getSimpleName()));
    Integer studyId = studyRole.getStudy().getStudyId();
    Study study = studyRepository.findOne(studyId);
    if (study == null) {
        throw new BadRequestException(Study.class.getSimpleName(), studyId);
    }//from www  . java  2 s  . c  om
    // check that the user can perform the create
    if (!registryService.userCanManageStudy(loginName, study.getStudyId())) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_STUDY_MANAGEMENT_ROLE_REQUIRED);
    }
    try {
        studyRoleRepository.save(studyRole);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return studyRoleRepository.findOne(studyRole.getRoleId());
}

From source file:edu.isi.misd.scanner.network.registry.web.controller.StudyRoleController.java

@RequestMapping(value = ENTITY_PATH, method = RequestMethod.PUT, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
public @ResponseBody StudyRole updateStudyRole(@RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @PathVariable(ID_URL_PATH_VAR) Integer id, @RequestBody StudyRole studyRole) {
    // find the requested resource
    StudyRole foundStudyRole = studyRoleRepository.findOne(id);
    // if the ID is not found then throw a ResourceNotFoundException (404)
    if (foundStudyRole == null) {
        throw new ResourceNotFoundException(id);
    }/*from  w w w  . ja va 2s  .  c  o  m*/
    // if the ID in the request body is null, use the ID parsed from the URL
    // if the ID is found in the request body but does not match the ID in 
    // the current data, then throw a ConflictException (409)
    Integer updateID = studyRole.getRoleId();
    if (updateID == null) {
        studyRole.setRoleId(id);
    } else if (!studyRole.getRoleId().equals(foundStudyRole.getRoleId())) {
        throw new ConflictException(studyRole.getRoleId(), foundStudyRole.getRoleId());
    }
    // check that the user can perform the update
    if (!registryService.userCanManageStudy(loginName, studyRole.getStudy().getStudyId())) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_STUDY_MANAGEMENT_ROLE_REQUIRED);
    }
    try {
        studyRoleRepository.save(studyRole);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return studyRoleRepository.findOne(studyRole.getRoleId());
}

From source file:edu.isi.misd.scanner.network.registry.web.controller.ToolLibraryController.java

@RequestMapping(value = BASE_PATH, method = RequestMethod.POST, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
@ResponseStatus(value = HttpStatus.CREATED)
public @ResponseBody ToolLibrary createToolLibrary(@RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @RequestBody ToolLibrary library) {
    // check that the user can perform the create
    if (!registryService.userIsSuperuser(loginName)) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_SUPERUSER_ROLE_REQUIRED);
    }/*from  w w  w  .j  a  va  2s .  c om*/
    try {
        registryService.saveToolLibrary(library);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        // throwing nested MostSpecificCause should probably be replaced
        // eventually for privacy reasons, but for now it is good for debugging
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return toolLibraryRepository.findOne(library.getLibraryId());
}

From source file:edu.isi.misd.scanner.network.registry.web.controller.ToolLibraryController.java

@RequestMapping(value = ENTITY_PATH, method = RequestMethod.PUT, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
public @ResponseBody ToolLibrary updateToolLibrary(@RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @PathVariable(ID_URL_PATH_VAR) Integer id, @RequestBody ToolLibrary library) {
    // find the requested resource
    ToolLibrary toolLib = toolLibraryRepository.findOne(id);
    // if the ID is not found then throw a ResourceNotFoundException (404)
    if (toolLib == null) {
        throw new ResourceNotFoundException(id);
    }//from  w  w w.j a va  2 s .  c  o m
    // if the ID in the request body is null, use the ID parsed from the URL
    // if the ID is found in the request body but does not match the ID in 
    // the current data, then throw a ConflictException (409)
    Integer updateID = library.getLibraryId();
    if (updateID == null) {
        library.setLibraryId(id);
    } else if (!library.getLibraryId().equals(toolLib.getLibraryId())) {
        throw new ConflictException(library.getLibraryId(), toolLib.getLibraryId());
    }
    // check that the user can perform the update
    if (!registryService.userIsSuperuser(loginName)) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_SUPERUSER_ROLE_REQUIRED);
    }
    try {
        registryService.saveToolLibrary(library);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return toolLibraryRepository.findOne(library.getLibraryId());
}

From source file:edu.isi.misd.scanner.network.registry.web.controller.UserRoleController.java

@RequestMapping(value = BASE_PATH, method = RequestMethod.POST, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
@ResponseStatus(value = HttpStatus.CREATED)
public @ResponseBody UserRole createUserRole(@RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @RequestBody UserRole userRole) {
    // first, check that the requested StudyRole association is valid
    Assert.notNull(userRole.getStudyRole(), nullVariableMsg(StudyRole.class.getSimpleName()));
    Integer roleId = userRole.getStudyRole().getRoleId();
    StudyRole studyRole = studyRoleRepository.findOne(roleId);
    if (studyRole == null) {
        throw new BadRequestException(StudyRole.class.getSimpleName(), roleId);
    }// w  ww  . j  a  v a2  s .  c  om
    // check that the user can perform the create
    if (!registryService.userCanManageStudy(loginName, studyRole.getStudy().getStudyId())) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_STUDY_MANAGEMENT_ROLE_REQUIRED);
    }
    try {
        userRoleRepository.save(userRole);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return userRoleRepository.findOne(userRole.getUserRoleId());
}