Search in sources :

Example 11 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException in project openmrs-core by openmrs.

the class FormServiceImpl method saveFormResource.

/**
 * @see org.openmrs.api.FormService#saveFormResource(org.openmrs.FormResource)
 */
@Override
public FormResource saveFormResource(FormResource formResource) throws APIException {
    if (formResource == null) {
        return null;
    }
    // If a form resource with same name exists, replace it with current value
    FormResource toPersist = formResource;
    FormResource original = Context.getFormService().getFormResource(formResource.getForm(), formResource.getName());
    if (original != null) {
        original.setName(formResource.getName());
        original.setValue(formResource.getValue());
        original.setDatatypeClassname(formResource.getDatatypeClassname());
        original.setDatatypeConfig(formResource.getDatatypeConfig());
        original.setPreferredHandlerClassname(formResource.getPreferredHandlerClassname());
        toPersist = original;
    }
    try {
        CustomDatatypeUtil.saveIfDirty(toPersist);
    } catch (ConstraintViolationException ex) {
        throw new InvalidFileTypeException(ex.getMessage(), ex);
    }
    return dao.saveFormResource(toPersist);
}
Also used : InvalidFileTypeException(org.openmrs.api.InvalidFileTypeException) FormResource(org.openmrs.FormResource) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException)

Example 12 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException in project herd by FINRAOS.

the class HerdErrorInformationExceptionHandlerTest method testConstraintViolationException.

@Test
public void testConstraintViolationException() throws Exception {
    validateErrorInformation(exceptionHandler.handlePersistenceException(getPersistenceException(new ConstraintViolationException(MESSAGE, null, "testConstraint")), new MockHttpServletResponse()), HttpStatus.BAD_REQUEST, false);
    validateErrorInformation(exceptionHandler.handlePersistenceException(getPersistenceException(new SQLException(MESSAGE, HerdErrorInformationExceptionHandler.POSTGRES_SQL_STATE_CODE_FOREIGN_KEY_VIOLATION, 0)), new MockHttpServletResponse()), HttpStatus.BAD_REQUEST, false);
}
Also used : SQLException(java.sql.SQLException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) AbstractServiceTest(org.finra.herd.service.AbstractServiceTest)

Example 13 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException in project crnk-framework by crnk-project.

the class HibernateConstraintViolationExceptionMapper method fromErrorResponse.

@Override
public ConstraintViolationException fromErrorResponse(ErrorResponse errorResponse) {
    Iterable<ErrorData> errors = errorResponse.getErrors();
    ErrorData error = errors.iterator().next();
    String msg = error.getDetail();
    String constraintName = error.getCode();
    return new ConstraintViolationException(msg, null, constraintName);
}
Also used : ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ErrorData(io.crnk.core.engine.document.ErrorData)

Example 14 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException in project candlepin by candlepin.

the class RdbmsExceptionTranslator method isConstraintViolationDuplicateEntry.

public boolean isConstraintViolationDuplicateEntry(PersistenceException pe) {
    log.debug("Translating {}", pe);
    if (pe.getCause() != null && pe.getCause() instanceof ConstraintViolationException) {
        ConstraintViolationException cve = (ConstraintViolationException) pe.getCause();
        log.info("ConstraintViolationException error code:" + cve.getErrorCode());
        // http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
        if (cve.getSQLState() != null && cve.getSQLState().equals("23000") && cve.getErrorCode() == 1062) {
            return true;
        }
        // https://www.postgresql.org/docs/8.3/static/errcodes-appendix.html
        if (cve.getSQLState() != null && cve.getSQLState().equals("23505")) {
            return true;
        }
    // TODO add Oracle
    }
    return false;
}
Also used : ConstraintViolationException(org.hibernate.exception.ConstraintViolationException)

Example 15 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException in project candlepin by candlepin.

the class OwnerResource method deleteOwner.

/**
 * Removes an Owner
 *
 * @httpcode 404
 * @httpcode 200
 */
@DELETE
@Path("/{owner_key}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(notes = "Removes an Owner", value = "Delete Owner")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found") })
public void deleteOwner(@PathParam("owner_key") String ownerKey, @QueryParam("revoke") @DefaultValue("true") boolean revoke, @QueryParam("force") @DefaultValue("false") boolean force) {
    Owner owner = findOwnerByKey(ownerKey);
    Event event = eventFactory.ownerDeleted(owner);
    if (!force && consumerCurator.doesShareConsumerExist(owner)) {
        throw new BadRequestException(i18n.tr("owner \"{0}\" cannot be deleted while there is a share consumer. " + "You may use \"force\" to bypass.", owner.getKey()));
    }
    try {
        ownerManager.cleanupAndDelete(owner, revoke);
    } catch (PersistenceException e) {
        if (e.getCause() instanceof ConstraintViolationException) {
            throw new ConflictException(e.getMessage(), e);
        } else {
            throw e;
        }
    }
    sink.queueEvent(event);
}
Also used : Owner(org.candlepin.model.Owner) ConflictException(org.candlepin.common.exceptions.ConflictException) PersistenceException(javax.persistence.PersistenceException) Event(org.candlepin.audit.Event) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)38 Test (org.junit.Test)16 PersistenceException (javax.persistence.PersistenceException)12 Session (org.hibernate.Session)11 SQLException (java.sql.SQLException)7 RObject (com.evolveum.midpoint.repo.sql.data.common.RObject)4 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)4 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)4 User (ca.corefacility.bioinformatics.irida.model.user.User)3 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)3 Connection (java.sql.Connection)3 AbstractManyToManyAssociationClassTest (org.hibernate.test.manytomanyassociationclass.AbstractManyToManyAssociationClassTest)3 ResultModels (eu.bcvsolutions.idm.core.api.dto.ResultModels)2 DefaultErrorModel (eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel)2 ErrorModel (eu.bcvsolutions.idm.core.api.exception.ErrorModel)2 ErrorData (io.crnk.core.engine.document.ErrorData)2 PreparedStatement (java.sql.PreparedStatement)2