Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
➖ removing the checker framework as it was not activated anyway, and …
Browse files Browse the repository at this point in the history
…if activated it failed compilation without any debug information

Signed-off-by: dseurotech <davide.salvador@eurotech.com>
dseurotech committed Apr 12, 2024
1 parent 117172d commit da1a068
Showing 5 changed files with 470 additions and 410 deletions.
4 changes: 0 additions & 4 deletions commons/pom.xml
Original file line number Diff line number Diff line change
@@ -149,10 +149,6 @@
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
</dependency>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
Original file line number Diff line number Diff line change
@@ -12,9 +12,35 @@
*******************************************************************************/
package org.eclipse.kapua.commons.jpa;

import com.google.common.base.MoreObjects;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

import javax.persistence.Embedded;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EntityType;

import org.apache.commons.lang.ArrayUtils;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.eclipse.kapua.KapuaEntityExistsException;
import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaErrorCodes;
@@ -39,34 +65,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.Embedded;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EntityType;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import com.google.common.base.MoreObjects;

public class KapuaEntityJpaRepository<E extends KapuaEntity, C extends E, L extends KapuaListResult<E>> implements KapuaEntityRepository<E, L> {

protected final Class<C> concreteClass;
protected final String entityName;
protected final Supplier<? extends L> listSupplier;
@@ -78,10 +80,13 @@ public class KapuaEntityJpaRepository<E extends KapuaEntity, C extends E, L exte
private static final String COMPARE_ERROR_MESSAGE = "Trying to compare a non-comparable value";

/**
* @param concreteClass The concrete class reifying a {@link KapuaEntity} entity, to be retrieved
* @param concreteClass
* The concrete class reifying a {@link KapuaEntity} entity, to be retrieved
* @param entityName
* @param listSupplier Generator of new, empty lists
* @param configuration Repo configuration, see {@link KapuaJpaRepositoryConfiguration} for configurable details
* @param listSupplier
* Generator of new, empty lists
* @param configuration
* Repo configuration, see {@link KapuaJpaRepositoryConfiguration} for configurable details
*/
public KapuaEntityJpaRepository(
Class<C> concreteClass,
@@ -328,19 +333,25 @@ protected E doDelete(EntityManager em, E entityToDelete) {
* <p>
* It can be invoked recursively (i.e. to handle {@link AttributePredicate}s of the {@link AndPredicate}.
*
* @param queryPredicate The {@link QueryPredicate} to handle.
* @param binds The {@link Map}&lg;{@link String}, {@link Object}&gt; of the query values.
* @param cb The JPA {@link CriteriaBuilder} of the {@link javax.persistence.Query}.
* @param userPermissionRoot The JPA {@link Root} of the {@link javax.persistence.Query}.
* @param entityType The JPA {@link EntityType} of the {@link javax.persistence.Query}.
* @param queryPredicate
* The {@link QueryPredicate} to handle.
* @param binds
* The {@link Map}&lg;{@link String}, {@link Object}&gt; of the query values.
* @param cb
* The JPA {@link CriteriaBuilder} of the {@link javax.persistence.Query}.
* @param userPermissionRoot
* The JPA {@link Root} of the {@link javax.persistence.Query}.
* @param entityType
* The JPA {@link EntityType} of the {@link javax.persistence.Query}.
* @return The handled {@link Predicate}
* @throws KapuaException If any problem occurs.
* @throws KapuaException
* If any problem occurs.
*/
private <E> Predicate handleKapuaQueryPredicates(@NonNull QueryPredicate queryPredicate,
@NonNull Map<ParameterExpression, Object> binds,
@NonNull CriteriaBuilder cb,
@NonNull Root<E> userPermissionRoot,
@NonNull EntityType<E> entityType)
private <E> Predicate handleKapuaQueryPredicates(QueryPredicate queryPredicate,
Map<ParameterExpression, Object> binds,
CriteriaBuilder cb,
Root<E> userPermissionRoot,
EntityType<E> entityType)
throws KapuaException {
Predicate predicate = null;
if (queryPredicate instanceof AttributePredicate) {
@@ -363,11 +374,11 @@ private <E> Predicate handleKapuaQueryPredicates(@NonNull QueryPredicate queryPr
return predicate;
}

private <E> Predicate handleAndPredicate(@NonNull AndPredicate andPredicate,
@NonNull Map<ParameterExpression, Object> binds,
@NonNull CriteriaBuilder cb,
@NonNull Root<E> entityRoot,
@NonNull EntityType<E> entityType)
private <E> Predicate handleAndPredicate(AndPredicate andPredicate,
Map<ParameterExpression, Object> binds,
CriteriaBuilder cb,
Root<E> entityRoot,
EntityType<E> entityType)
throws KapuaException {

Predicate[] jpaAndPredicates =
@@ -382,11 +393,11 @@ private <E> Predicate handleAndPredicate(@NonNull AndPredicate andPredicate,

}

private <E> Predicate handleOrPredicate(@NonNull OrPredicate orPredicate,
@NonNull Map<ParameterExpression, Object> binds,
@NonNull CriteriaBuilder cb,
@NonNull Root<E> entityRoot,
@NonNull EntityType<E> entityType)
private <E> Predicate handleOrPredicate(OrPredicate orPredicate,
Map<ParameterExpression, Object> binds,
CriteriaBuilder cb,
Root<E> entityRoot,
EntityType<E> entityType)
throws KapuaException {

Predicate[] jpaOrPredicates =
@@ -400,11 +411,11 @@ private <E> Predicate handleOrPredicate(@NonNull OrPredicate orPredicate,
return cb.or(jpaOrPredicates);
}

private <E> Predicate[] handlePredicate(@NonNull List<QueryPredicate> orPredicates,
@NonNull Map<ParameterExpression, Object> binds,
@NonNull CriteriaBuilder cb,
@NonNull Root<E> entityRoot,
@NonNull EntityType<E> entityType) throws KapuaException {
private <E> Predicate[] handlePredicate(List<QueryPredicate> orPredicates,
Map<ParameterExpression, Object> binds,
CriteriaBuilder cb,
Root<E> entityRoot,
EntityType<E> entityType) throws KapuaException {
Predicate[] jpaOrPredicates = new Predicate[orPredicates.size()];

for (int i = 0; i < orPredicates.size(); i++) {
@@ -414,11 +425,11 @@ private <E> Predicate[] handlePredicate(@NonNull List<QueryPredicate> orPredicat
return jpaOrPredicates;
}

private <E> Predicate handleAttributePredicate(@NonNull AttributePredicate<?> attrPred,
@NonNull Map<ParameterExpression, Object> binds,
@NonNull CriteriaBuilder cb,
@NonNull Root<E> entityRoot,
@NonNull EntityType<E> entityType)
private <E> Predicate handleAttributePredicate(AttributePredicate<?> attrPred,
Map<ParameterExpression, Object> binds,
CriteriaBuilder cb,
Root<E> entityRoot,
EntityType<E> entityType)
throws KapuaException {
Predicate expr;
String attrName = attrPred.getAttributeName();
@@ -468,84 +479,84 @@ private <E> Predicate handleAttributePredicate(@NonNull AttributePredicate<?> at
.replace(configuration.any, configuration.escape + configuration.any))
.orElse(null);
switch (attrPred.getOperator()) {
case LIKE:
ParameterExpression<String> pl = cb.parameter(String.class);
binds.put(pl, configuration.like + escapedAttributeValue + configuration.like);
expr = cb.like(extractAttribute(entityRoot, attrName), pl);
break;

case LIKE_IGNORE_CASE:
ParameterExpression<String> plci = cb.parameter(String.class);
binds.put(plci, configuration.like + escapedAttributeValue.toLowerCase() + configuration.like);
expr = cb.like(cb.lower(extractAttribute(entityRoot, attrName)), plci);
break;

case STARTS_WITH:
ParameterExpression<String> psw = cb.parameter(String.class);
binds.put(psw, escapedAttributeValue + configuration.like);
expr = cb.like(extractAttribute(entityRoot, attrName), psw);
break;

case STARTS_WITH_IGNORE_CASE:
ParameterExpression<String> pswci = cb.parameter(String.class);
binds.put(pswci, escapedAttributeValue.toLowerCase() + configuration.like);
expr = cb.like(cb.lower(extractAttribute(entityRoot, attrName)), pswci);
break;

case IS_NULL:
expr = cb.isNull(extractAttribute(entityRoot, attrName));
break;

case NOT_NULL:
expr = cb.isNotNull(extractAttribute(entityRoot, attrName));
break;

case NOT_EQUAL:
expr = cb.notEqual(extractAttribute(entityRoot, attrName), attributeValue);
break;

case GREATER_THAN:
if (attributeValue instanceof Comparable && ArrayUtils.contains(attribute.getJavaType().getInterfaces(), Comparable.class)) {
Comparable comparableAttrValue = (Comparable<?>) attributeValue;
Expression<? extends Comparable> comparableExpression = extractAttribute(entityRoot, attrName);
expr = cb.greaterThan(comparableExpression, comparableAttrValue);
} else {
throw new KapuaException(KapuaErrorCodes.ILLEGAL_ARGUMENT, COMPARE_ERROR_MESSAGE);
}
break;

case GREATER_THAN_OR_EQUAL:
if (attributeValue instanceof Comparable && ArrayUtils.contains(attribute.getJavaType().getInterfaces(), Comparable.class)) {
Expression<? extends Comparable> comparableExpression = extractAttribute(entityRoot, attrName);
Comparable comparableAttrValue = (Comparable<?>) attributeValue;
expr = cb.greaterThanOrEqualTo(comparableExpression, comparableAttrValue);
} else {
throw new KapuaException(KapuaErrorCodes.ILLEGAL_ARGUMENT, COMPARE_ERROR_MESSAGE);
}
break;

case LESS_THAN:
if (attributeValue instanceof Comparable && ArrayUtils.contains(attribute.getJavaType().getInterfaces(), Comparable.class)) {
Expression<? extends Comparable> comparableExpression = extractAttribute(entityRoot, attrName);
Comparable comparableAttrValue = (Comparable<?>) attributeValue;
expr = cb.lessThan(comparableExpression, comparableAttrValue);
} else {
throw new KapuaException(KapuaErrorCodes.ILLEGAL_ARGUMENT, COMPARE_ERROR_MESSAGE);
}
break;
case LESS_THAN_OR_EQUAL:
if (attributeValue instanceof Comparable && ArrayUtils.contains(attribute.getJavaType().getInterfaces(), Comparable.class)) {
Expression<? extends Comparable> comparableExpression = extractAttribute(entityRoot, attrName);
Comparable comparableAttrValue = (Comparable<?>) attributeValue;
expr = cb.lessThanOrEqualTo(comparableExpression, comparableAttrValue);
} else {
throw new KapuaException(KapuaErrorCodes.ILLEGAL_ARGUMENT, COMPARE_ERROR_MESSAGE);
}
break;

case EQUAL:
default:
expr = cb.equal(extractAttribute(entityRoot, attrName), attributeValue);
case LIKE:
ParameterExpression<String> pl = cb.parameter(String.class);
binds.put(pl, configuration.like + escapedAttributeValue + configuration.like);
expr = cb.like(extractAttribute(entityRoot, attrName), pl);
break;

case LIKE_IGNORE_CASE:
ParameterExpression<String> plci = cb.parameter(String.class);
binds.put(plci, configuration.like + escapedAttributeValue.toLowerCase() + configuration.like);
expr = cb.like(cb.lower(extractAttribute(entityRoot, attrName)), plci);
break;

case STARTS_WITH:
ParameterExpression<String> psw = cb.parameter(String.class);
binds.put(psw, escapedAttributeValue + configuration.like);
expr = cb.like(extractAttribute(entityRoot, attrName), psw);
break;

case STARTS_WITH_IGNORE_CASE:
ParameterExpression<String> pswci = cb.parameter(String.class);
binds.put(pswci, escapedAttributeValue.toLowerCase() + configuration.like);
expr = cb.like(cb.lower(extractAttribute(entityRoot, attrName)), pswci);
break;

case IS_NULL:
expr = cb.isNull(extractAttribute(entityRoot, attrName));
break;

case NOT_NULL:
expr = cb.isNotNull(extractAttribute(entityRoot, attrName));
break;

case NOT_EQUAL:
expr = cb.notEqual(extractAttribute(entityRoot, attrName), attributeValue);
break;

case GREATER_THAN:
if (attributeValue instanceof Comparable && ArrayUtils.contains(attribute.getJavaType().getInterfaces(), Comparable.class)) {
Comparable comparableAttrValue = (Comparable<?>) attributeValue;
Expression<? extends Comparable> comparableExpression = extractAttribute(entityRoot, attrName);
expr = cb.greaterThan(comparableExpression, comparableAttrValue);
} else {
throw new KapuaException(KapuaErrorCodes.ILLEGAL_ARGUMENT, COMPARE_ERROR_MESSAGE);
}
break;

case GREATER_THAN_OR_EQUAL:
if (attributeValue instanceof Comparable && ArrayUtils.contains(attribute.getJavaType().getInterfaces(), Comparable.class)) {
Expression<? extends Comparable> comparableExpression = extractAttribute(entityRoot, attrName);
Comparable comparableAttrValue = (Comparable<?>) attributeValue;
expr = cb.greaterThanOrEqualTo(comparableExpression, comparableAttrValue);
} else {
throw new KapuaException(KapuaErrorCodes.ILLEGAL_ARGUMENT, COMPARE_ERROR_MESSAGE);
}
break;

case LESS_THAN:
if (attributeValue instanceof Comparable && ArrayUtils.contains(attribute.getJavaType().getInterfaces(), Comparable.class)) {
Expression<? extends Comparable> comparableExpression = extractAttribute(entityRoot, attrName);
Comparable comparableAttrValue = (Comparable<?>) attributeValue;
expr = cb.lessThan(comparableExpression, comparableAttrValue);
} else {
throw new KapuaException(KapuaErrorCodes.ILLEGAL_ARGUMENT, COMPARE_ERROR_MESSAGE);
}
break;
case LESS_THAN_OR_EQUAL:
if (attributeValue instanceof Comparable && ArrayUtils.contains(attribute.getJavaType().getInterfaces(), Comparable.class)) {
Expression<? extends Comparable> comparableExpression = extractAttribute(entityRoot, attrName);
Comparable comparableAttrValue = (Comparable<?>) attributeValue;
expr = cb.lessThanOrEqualTo(comparableExpression, comparableAttrValue);
} else {
throw new KapuaException(KapuaErrorCodes.ILLEGAL_ARGUMENT, COMPARE_ERROR_MESSAGE);
}
break;

case EQUAL:
default:
expr = cb.equal(extractAttribute(entityRoot, attrName), attributeValue);
}
}
return expr;
@@ -558,12 +569,14 @@ private <E> Predicate handleAttributePredicate(@NonNull AttributePredicate<?> at
* <p>
* Filter predicates takes advantage of the dot notation to access {@link Embedded} attributes and nested {@link KapuaEntity}es.
*
* @param entityRoot The {@link Root} entity from which extract the attribute.
* @param attributeName The full attribute name.
* @param entityRoot
* The {@link Root} entity from which extract the attribute.
* @param attributeName
* The full attribute name.
* @return The {@link Path} expression that matches the given {@code attributeName} parameter.
* @since 1.0.0
*/
private <E, P> Path<P> extractAttribute(@NonNull Root<E> entityRoot, @NonNull String attributeName) {
private <E, P> Path<P> extractAttribute(Root<E> entityRoot, String attributeName) {
Path<P> expressionPath;
if (attributeName.contains(ATTRIBUTE_SEPARATOR)) {
String[] pathComponents = attributeName.split(ATTRIBUTE_SEPARATOR_ESCAPED);
@@ -582,11 +595,12 @@ private <E, P> Path<P> extractAttribute(@NonNull Root<E> entityRoot, @NonNull St
/**
* Check if the given {@link PersistenceException} is a SQL constraint violation error.
*
* @param persistenceException {@link PersistenceException} to check.
* @param persistenceException
* {@link PersistenceException} to check.
* @return {@code true} if it is a constraint validation error, {@code false} otherwise.
* @since 1.0.0
*/
public static boolean isInsertConstraintViolation(@NonNull PersistenceException persistenceException) {
public static boolean isInsertConstraintViolation(PersistenceException persistenceException) {
Throwable cause = persistenceException.getCause();
while (cause != null && !(cause instanceof SQLException)) {
cause = cause.getCause();
@@ -600,18 +614,18 @@ public static boolean isInsertConstraintViolation(@NonNull PersistenceException
return SQL_ERROR_CODE_CONSTRAINT_VIOLATION.equals(innerExc.getSQLState());
}

protected Optional<E> doFindByField(@NonNull TxContext txContext,
@NonNull KapuaId scopeId,
@NonNull String fieldName,
@NonNull Object fieldValue) {
protected Optional<E> doFindByField(TxContext txContext,
KapuaId scopeId,
String fieldName,
Object fieldValue) {
final List<C> result = doFindAllByField(txContext, scopeId, fieldName, fieldValue);
switch (result.size()) {
case 0:
return Optional.empty();
case 1:
return Optional.of(result.get(0));
default:
throw new NonUniqueResultException(String.format("Multiple %s results found for field %s with value %s", concreteClass.getName(), fieldName, fieldValue.toString()));
case 0:
return Optional.empty();
case 1:
return Optional.of(result.get(0));
default:
throw new NonUniqueResultException(String.format("Multiple %s results found for field %s with value %s", concreteClass.getName(), fieldName, fieldValue.toString()));
}
}

Large diffs are not rendered by default.

20 changes: 5 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -47,15 +47,14 @@
<console.maven.toolchain.jdk.version>8</console.maven.toolchain.jdk.version>

<!-- Dependencies versions -->
<camel.version>3.21.0</camel.version> <!-- latest 3.11.0 -->
<spring.version>5.3.23</spring.version> <!-- latest 5.3.8 -->
<spring-security.version>5.7.5</spring-security.version> <!-- latest 5.5.1 -->
<spring-boot.version>2.5.14</spring-boot.version> <!-- latest 2.5.2 --> <!-- 2.3.x is not fully supported by camel (will be on camel 3.4) -->
<camel.version>3.21.0</camel.version>
<spring.version>5.3.23</spring.version>
<spring-security.version>5.7.5</spring-security.version>
<spring-boot.version>2.5.14</spring-boot.version> <!-- 2.3.x is not fully supported by camel (will be on camel 3.4) -->

<aopalliance.version>1.0</aopalliance.version>
<artemis.version>2.31.2</artemis.version>
<assertj.version>3.2.0</assertj.version>
<checker-framework.version>3.15.0</checker-framework.version>
<commons-beanutils.version>1.9.4</commons-beanutils.version>
<commons-cli.version>1.4</commons-cli.version>
<commons-codec.version>1.15</commons-codec.version>
@@ -143,7 +142,7 @@
<maven-assembly-plugin.version>3.0.0</maven-assembly-plugin.version>
<maven-bundle-plugin.version>5.1.8</maven-bundle-plugin.version>
<maven-checkstyle-plugin.version>2.17</maven-checkstyle-plugin.version>
<maven-compiler-plugin.version>3.6.1</maven-compiler-plugin.version>
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
<maven-dependency-plugin.version>3.0.1</maven-dependency-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-enforcer-plugin.version>1.4.1</maven-enforcer-plugin.version>
@@ -1495,10 +1494,6 @@
<groupId>com.google.j2objc</groupId>
<artifactId>j2objc-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
@@ -1896,11 +1891,6 @@
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
<version>${checker-framework.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
Original file line number Diff line number Diff line change
@@ -12,7 +12,12 @@
*******************************************************************************/
package org.eclipse.kapua.service.authorization.access.shiro;

import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.inject.Inject;

import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.model.id.KapuaEid;
@@ -43,12 +48,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class GroupQueryHelperImpl implements GroupQueryHelper {

private final TxManager txManager;
private final AccessInfoFactory accessInfoFactory;
private final AccessInfoRepository accessInfoRepository;
@@ -146,7 +147,7 @@ private void handleKapuaQueryGroupPredicate(TxContext txContext, KapuaSession ka
}
}

private static boolean checkGroupPermission(@NonNull String domain, @NonNull List<Permission> groupPermissions, @NonNull Permission permission) {
private static boolean checkGroupPermission(String domain, List<Permission> groupPermissions, Permission permission) {
if ((permission.getDomain() == null || domain.equals(permission.getDomain())) &&
(permission.getAction() == null || Actions.read.equals(permission.getAction()))) {
if (permission.getGroupId() == null) {

0 comments on commit da1a068

Please sign in to comment.