Search in sources :

Example 6 with BadRequestException

use of io.crnk.core.exception.BadRequestException in project crnk-framework by crnk-project.

the class ActivitiRepositoryBase method checkFilter.

protected void checkFilter(Object resource, boolean applyAsDefault) {
    QuerySpec enforcementSpec = new QuerySpec((String) null);
    for (FilterSpec baseFilter : baseFilters) {
        enforcementSpec.addFilter(baseFilter);
        // apply as default if possible
        List<String> attributePath = baseFilter.getAttributePath();
        if (attributePath.size() == 1 && baseFilter.getOperator() == FilterOperator.EQ) {
            String attributeName = baseFilter.getAttributePath().get(0);
            Object actualValue = PropertyUtils.getProperty(resource, attributeName);
            Object expectedValue = baseFilter.getValue();
            if (actualValue == null) {
                PropertyUtils.setProperty(resource, attributeName, expectedValue);
            }
        }
    }
    if (enforcementSpec.apply(Arrays.asList(resource)).isEmpty()) {
        throw new BadRequestException("resource does not belong to this repository");
    }
}
Also used : BadRequestException(io.crnk.core.exception.BadRequestException) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec)

Aggregations

BadRequestException (io.crnk.core.exception.BadRequestException)6 ErrorData (io.crnk.core.engine.document.ErrorData)3 ErrorResponse (io.crnk.core.engine.error.ErrorResponse)3 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)3 Test (org.junit.Test)3 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)1 FilterSpec (io.crnk.core.queryspec.FilterSpec)1 QuerySpec (io.crnk.core.queryspec.QuerySpec)1 PersistenceExceptionMapper (io.crnk.jpa.internal.PersistenceExceptionMapper)1 PersistenceRollbackExceptionMapper (io.crnk.jpa.internal.PersistenceRollbackExceptionMapper)1 TransactionRollbackExceptionMapper (io.crnk.jpa.internal.TransactionRollbackExceptionMapper)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PersistenceException (javax.persistence.PersistenceException)1