Search in sources :

Example 86 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project irida by phac-nml.

the class UserServiceImplTest method testCreateUserWithUnknownIntegrityConstraintViolation.

@Test(expected = DataIntegrityViolationException.class)
public void testCreateUserWithUnknownIntegrityConstraintViolation() {
    User u = new User();
    DataIntegrityViolationException integrityViolationException = new DataIntegrityViolationException("Duplicate");
    when(userRepository.save(any(User.class))).thenThrow(integrityViolationException);
    when(validator.validateValue(eq(User.class), eq("password"), any(String.class))).thenReturn(new HashSet<ConstraintViolation<User>>());
    userService.create(u);
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) ConstraintViolation(javax.validation.ConstraintViolation) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Example 87 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project irida by phac-nml.

the class UserServiceImplTest method testCreateUserWithUnknownIntegrityConstraintViolationName.

@Test(expected = EntityExistsException.class)
public void testCreateUserWithUnknownIntegrityConstraintViolationName() {
    User u = new User();
    ConstraintViolationException constraintViolationException = new ConstraintViolationException("Duplicate", null, "Not a very nicely formatted constraint violation name.");
    DataIntegrityViolationException integrityViolationException = new DataIntegrityViolationException("Duplicate", constraintViolationException);
    when(userRepository.save(any(User.class))).thenThrow(integrityViolationException);
    when(validator.validateValue(eq(User.class), eq("password"), any(String.class))).thenReturn(new HashSet<ConstraintViolation<User>>());
    userService.create(u);
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Example 88 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project irida by phac-nml.

the class GroupsController method editGroup.

/**
 * Submit changes to the {@link UserGroup}.
 *
 * @param userGroupId
 *            the group ID to edit.
 * @param name
 *            the new name of the group.
 * @param description
 *            the new description of the group.
 * @param principal
 *            the currently logged in user.
 * @param model
 *            the model to add attributes to.
 * @param locale
 *            the locale of the browser.
 * @return the route to the editing page on validation failure, or the
 *         details page on success.
 */
@RequestMapping(path = "/{userGroupId}/edit", method = RequestMethod.POST)
public String editGroup(@PathVariable final Long userGroupId, @RequestParam final String name, @RequestParam final String description, final Principal principal, final Model model, final Locale locale) {
    logger.debug("Editing group: [" + userGroupId + "]");
    final Map<String, String> errors = new HashMap<>();
    UserGroup group = userGroupService.read(userGroupId);
    try {
        group.setName(name);
        group.setDescription(description);
        userGroupService.update(group);
        return getDetailsPage(userGroupId, principal, model);
    } catch (final ConstraintViolationException e) {
        for (final ConstraintViolation<?> v : e.getConstraintViolations()) {
            errors.put(v.getPropertyPath().toString(), v.getMessage());
        }
    } catch (final EntityExistsException | DataIntegrityViolationException e) {
        errors.put("name", messageSource.getMessage("group.name.exists", null, locale));
    }
    model.addAttribute("errors", errors);
    model.addAttribute("group", userGroupService.read(userGroupId));
    model.addAttribute("given_name", name);
    model.addAttribute("given_description", description);
    return GROUPS_EDIT;
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException) DTUserGroup(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUserGroup) UserGroup(ca.corefacility.bioinformatics.irida.model.user.group.UserGroup) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 89 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project ma-core-public by infiniteautomation.

the class AbstractDwr method remove.

/**
 * Delete a VO
 * @param id
 * @return
 */
@DwrPermission(admin = true)
public ProcessResult remove(int id) {
    ProcessResult response = new ProcessResult();
    try {
        dao.delete(id);
    } catch (Exception e) {
        // Handle the exceptions.
        LOG.error(e);
        VO vo = dao.get(id);
        if (e instanceof DataIntegrityViolationException)
            response.addContextualMessage(vo.getName(), "table.edit.unableToDeleteDueToConstraints");
        else
            response.addContextualMessage(vo.getName(), "table.edit.unableToDelete", e.getMessage());
    }
    response.addData("id", id);
    return response;
}
Also used : ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) AbstractVO(com.serotonin.m2m2.vo.AbstractVO) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 90 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project ma-core-public by infiniteautomation.

the class AbstractRTDwr method remove.

@DwrPermission(admin = true)
@Override
public ProcessResult remove(int id) {
    ProcessResult response = new ProcessResult();
    try {
        runtimeManager.delete(id);
    } catch (Exception e) {
        // Handle the exceptions.
        LOG.error(e);
        // TODO Clean up and generify these messages to some central place
        if (e instanceof DataIntegrityViolationException)
            response.addMessage(this.keyName + "Errors", new TranslatableMessage("dsEdit.unableToDeleteDueToConstraints"));
        else
            response.addMessage(this.keyName + "Errors", new TranslatableMessage("dsEdit.unableToDelete"));
    }
    response.addData("id", id);
    return response;
}
Also used : ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Aggregations

DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)117 Test (org.junit.Test)26 HashMap (java.util.HashMap)12 BadRequestException (com.sequenceiq.cloudbreak.controller.BadRequestException)11 Transactional (org.springframework.transaction.annotation.Transactional)11 Test (org.junit.jupiter.api.Test)10 Transactional (javax.transaction.Transactional)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 User (ca.corefacility.bioinformatics.irida.model.user.User)8 SQLException (java.sql.SQLException)8 ConstraintViolation (javax.validation.ConstraintViolation)8 HashSet (java.util.HashSet)7 Locale (java.util.Locale)6 ConstraintViolationException (javax.validation.ConstraintViolationException)6 Date (java.util.Date)5 List (java.util.List)5 Set (java.util.Set)5 EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)4 ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)4 InvalidMetaException (com.netflix.metacat.common.server.connectors.exception.InvalidMetaException)4