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.ScannerUserController.java

@RequestMapping(value = ENTITY_PATH, method = RequestMethod.PUT, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
public @ResponseBody ScannerUser updateScannerUser(@RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @PathVariable(ID_URL_PATH_VAR) Integer id, @RequestBody ScannerUser user) {
    // get the user record for the current user
    ScannerUser loggedInUser = scannerUserRepository.findByUserName(loginName);
    if (loggedInUser == null) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_UNKNOWN_USER_NAME);
    }// w  ww  .  ja va  2s .c om
    // find the requested resource
    ScannerUser foundUser = scannerUserRepository.findOne(id);
    // if the ID is not found then throw a ResourceNotFoundException (404)
    if (foundUser == null) {
        throw new ResourceNotFoundException(id);
    }
    // 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 = user.getUserId();
    if (updateID == null) {
        user.setUserId(id);
    } else if (!user.getUserId().equals(foundUser.getUserId())) {
        throw new ConflictException(user.getUserId(), foundUser.getUserId());
    }
    // check that the current user can edit this record        
    if ((!loginName.equalsIgnoreCase(foundUser.getUserName()) && (!loggedInUser.getIsSuperuser()))) {
        throw new ForbiddenException(loginName, "Unable to edit a user record for another user. "
                + RegistryServiceConstants.MSG_SUPERUSER_ROLE_REQUIRED);
    }
    // check if a non-superuser is trying to set the superuser flag
    if ((!loggedInUser.getIsSuperuser()) && (user.getIsSuperuser())) {
        throw new ForbiddenException(loginName,
                "Unable to set the superuser flag. " + RegistryServiceConstants.MSG_SUPERUSER_ROLE_REQUIRED);
    }
    try {
        scannerUserRepository.save(user);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return scannerUserRepository.findOne(user.getUserId());
}

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

@RequestMapping(value = BASE_PATH, method = RequestMethod.POST, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
@ResponseStatus(value = HttpStatus.CREATED)
public @ResponseBody Site createSite(@RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @RequestParam Map<String, String> paramMap, @RequestBody Site site) {
    if (!registryService.userIsSuperuser(loginName)) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_SUPERUSER_ROLE_REQUIRED);
    }//from w w  w  .j  a v  a2 s  . com
    Map<String, String> params = validateParameterMap(paramMap, REQUEST_PARAM_USER_NAME);
    String userName = params.get(REQUEST_PARAM_USER_NAME);

    ScannerUser user = null;
    if (userName != null) {
        user = scannerUserRepository.findByUserName(userName);
        if (user == null) {
            throw new BadRequestException(
                    String.format(RegistryServiceConstants.MSG_INVALID_PARAMETER_VALUE, userName) + " "
                            + RegistryServiceConstants.MSG_UNKNOWN_USER_NAME);
        }
    }
    try {
        registryService.createSite(site, user);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return siteRepository.findOne(site.getSiteId());
}

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

@RequestMapping(value = ENTITY_PATH, method = RequestMethod.PUT, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
public @ResponseBody Site updateSite(@RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @PathVariable(ID_URL_PATH_VAR) Integer id, @RequestBody Site site) {
    // find the requested resource
    Site foundSite = siteRepository.findOne(id);
    // if the ID is not found then throw a ResourceNotFoundException (404)
    if (foundSite == null) {
        throw new ResourceNotFoundException(id);
    }// w w  w  .  ja v a 2 s . c  om
    // 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.getSiteId();
    if (updateID == null) {
        site.setSiteId(id);
    } else if (!site.getSiteId().equals(foundSite.getSiteId())) {
        throw new ConflictException(site.getSiteId(), foundSite.getSiteId());
    }

    // check that the user can perform the update
    if (!registryService.userCanManageSite(loginName, site.getSiteId())) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_SITE_MANAGEMENT_ROLE_REQUIRED);
    }
    try {
        siteRepository.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 siteRepository.findOne(site.getSiteId());
}

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

@RequestMapping(value = BASE_PATH, method = RequestMethod.POST, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
@ResponseStatus(value = HttpStatus.CREATED)
public @ResponseBody SitePolicy createSitePolicy(@RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @RequestBody SitePolicy sitePolicy) {
    // first, check that the requested Site association is valid
    Assert.notNull(sitePolicy.getSite(), nullVariableMsg(Site.class.getSimpleName()));

    // check that the user can perform the create
    if (!registryService.userCanManageSite(loginName, sitePolicy.getSite().getSiteId())) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_SITE_MANAGEMENT_ROLE_REQUIRED);
    }// www  .  j a v a 2s  . c o  m
    try {
        sitePolicyRepository.save(sitePolicy);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return sitePolicyRepository.findOne(sitePolicy.getSitePolicyId());
}

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

@RequestMapping(value = ENTITY_PATH, method = RequestMethod.PUT, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
public @ResponseBody SitePolicy updateSitePolicy(@RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @PathVariable(ID_URL_PATH_VAR) Integer id, @RequestBody SitePolicy sitePolicy) {
    // find the requested resource
    SitePolicy foundSitePolicy = sitePolicyRepository.findOne(id);
    // if the ID is not found then throw a ResourceNotFoundException (404)
    if (foundSitePolicy == null) {
        throw new ResourceNotFoundException(id);
    }// ww w  . j  a v  a2  s.  c om
    // 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 = sitePolicy.getSitePolicyId();
    if (updateID == null) {
        sitePolicy.setSitePolicyId(id);
    } else if (!sitePolicy.getSitePolicyId().equals(foundSitePolicy.getSitePolicyId())) {
        throw new ConflictException(sitePolicy.getSitePolicyId(), foundSitePolicy.getSitePolicyId());
    }
    // check that the user can perform the update
    if (!registryService.userCanManageSite(loginName, sitePolicy.getSite().getSiteId())) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_SITE_MANAGEMENT_ROLE_REQUIRED);
    }
    try {
        sitePolicyRepository.save(sitePolicy);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return sitePolicyRepository.findOne(sitePolicy.getSitePolicyId());
}

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

@RequestMapping(value = BASE_PATH, method = RequestMethod.POST, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
@ResponseStatus(value = HttpStatus.CREATED)
public @ResponseBody StandardRole createStandardRole(@RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @RequestBody StandardRole standardRole) {
    // 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 v  a2s  .  c  o  m*/
    try {
        standardRoleRepository.save(standardRole);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return standardRoleRepository.findOne(standardRole.getStandardRoleId());
}

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

@RequestMapping(value = ENTITY_PATH, method = RequestMethod.PUT, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
public @ResponseBody StandardRole updateStandardRole(@RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @PathVariable(ID_URL_PATH_VAR) Integer id, @RequestBody StandardRole standardRole) {
    // find the requested resource
    StandardRole foundStandardRole = standardRoleRepository.findOne(id);
    // if the ID is not found then throw a ResourceNotFoundException (404)
    if (foundStandardRole == null) {
        throw new ResourceNotFoundException(id);
    }//from  w  ww  .ja  v a2  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 = standardRole.getStandardRoleId();
    if (updateID == null) {
        standardRole.setStandardRoleId(id);
    } else if (!standardRole.getStandardRoleId().equals(foundStandardRole.getStandardRoleId())) {
        throw new ConflictException(standardRole.getStandardRoleId(), foundStandardRole.getStandardRoleId());
    }
    // check that the user can perform the update
    if (!registryService.userIsSuperuser(loginName)) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_SUPERUSER_ROLE_REQUIRED);
    }
    try {
        standardRoleRepository.save(standardRole);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return standardRoleRepository.findOne(standardRole.getStandardRoleId());
}

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

@RequestMapping(value = BASE_PATH, method = RequestMethod.POST, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
@ResponseStatus(value = HttpStatus.CREATED)
public @ResponseBody Study createStudy(@RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @RequestBody Study study) {//from  www .j  a v  a2s. co m
    // get the user record for the current user
    ScannerUser loggedInUser = scannerUserRepository.findByUserName(loginName);
    if (loggedInUser == null) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_UNKNOWN_USER_NAME);
    }
    study.setStudyOwner(loggedInUser);
    try {
        registryService.createStudy(study);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return studyRepository.findOne(study.getStudyId());
}

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

@RequestMapping(value = ENTITY_PATH, method = RequestMethod.PUT, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
public @ResponseBody Study updateStudy(@RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @PathVariable(ID_URL_PATH_VAR) Integer id, @RequestBody Study study) {
    // find the requested resource
    Study foundStudy = studyRepository.findOne(id);
    // if the ID is not found then throw a ResourceNotFoundException (404)
    if (foundStudy == null) {
        throw new ResourceNotFoundException(id);
    }/*from w  ww .j  a  v  a  2  s .  c om*/
    // 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 = study.getStudyId();
    if (updateID == null) {
        study.setStudyId(id);
    } else if (!study.getStudyId().equals(foundStudy.getStudyId())) {
        throw new ConflictException(study.getStudyId(), foundStudy.getStudyId());
    }
    // check that the user can perform the update
    if (!registryService.userCanManageStudy(loginName, study.getStudyId())) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_STUDY_MANAGEMENT_ROLE_REQUIRED);
    }
    // ensure that the studyOwner is populated correctly. If it is absent
    // in the update request, use the value of the existing study. Otherwise
    // look up the the specified studyOwner to see if it is a valid user
    if (study.getStudyOwner() == null) {
        study.setStudyOwner(foundStudy.getStudyOwner());
    } else {
        ScannerUser owner = scannerUserRepository.findOne(study.getStudyOwner().getUserId());
        if (owner != null) {
            study.setStudyOwner(owner);
        } else {
            throw new BadRequestException(String.format(RegistryServiceConstants.MSG_INVALID_PARAMETER_VALUE,
                    study.getStudyOwner().getUserId()) + " " + RegistryServiceConstants.MSG_UNKNOWN_USER_NAME);
        }
    }
    // perform the update
    try {
        registryService.updateStudy(study);
    } catch (DataIntegrityViolationException e) {
        log.warn(e);
        throw new ConflictException(e.getMostSpecificCause());
    }
    // force the re-query to ensure a complete result view if updated
    return studyRepository.findOne(study.getStudyId());
}

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

@RequestMapping(value = BASE_PATH, method = RequestMethod.POST, consumes = HEADER_JSON_MEDIA_TYPE, produces = HEADER_JSON_MEDIA_TYPE)
@ResponseStatus(value = HttpStatus.CREATED)
public @ResponseBody StudyManagementPolicy createStudyManagementPolicy(
        @RequestHeader(HEADER_LOGIN_NAME) String loginName,
        @RequestBody StudyManagementPolicy studyManagementPolicy) {
    // first, check that the requested Study association is valid
    Assert.notNull(studyManagementPolicy.getStudy(), nullVariableMsg(Study.class.getSimpleName()));

    // check that the user can perform the create
    if (!registryService.userCanManageStudy(loginName, studyManagementPolicy.getStudy().getStudyId())) {
        throw new ForbiddenException(loginName, RegistryServiceConstants.MSG_STUDY_MANAGEMENT_ROLE_REQUIRED);
    }// www .j  a  v  a 2s . c om
    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());
}