diff --git a/backend/pom.xml b/backend/pom.xml index aee70d5..afcad5f 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -25,7 +25,33 @@ org.springframework.boot spring-boot-starter-web - + + com.querydsl + querydsl-core + 5.0.0 + + + com.querydsl + querydsl-jpa + 5.0.0 + jakarta + + + com.querydsl + querydsl-sql + 5.0.0 + + + com.querydsl + querydsl-sql-spring + 5.0.0 + + + com.querydsl + querydsl-apt + ${querydsl.version} + jakarta + org.postgresql postgresql diff --git a/backend/src/main/java/com/senac/gestaocurso/enterprise/BooleanBuilderUtil.java b/backend/src/main/java/com/senac/gestaocurso/enterprise/BooleanBuilderUtil.java new file mode 100644 index 0000000..1d0f495 --- /dev/null +++ b/backend/src/main/java/com/senac/gestaocurso/enterprise/BooleanBuilderUtil.java @@ -0,0 +1,125 @@ +package com.senac.gestaocurso.enterprise; + +import com.querydsl.core.BooleanBuilder; +import com.querydsl.core.types.Expression; +import com.querydsl.core.types.dsl.Expressions; +import com.querydsl.core.types.dsl.PathBuilder; + + +import java.lang.reflect.Field; +import java.time.LocalDate; + + +public class BooleanBuilderUtil { + + + public static BooleanBuilder buildPredicateFromFilter(String filter, Class classes) { + if (filter == null || filter.isEmpty()) { + return new BooleanBuilder(); + } + + + BooleanBuilder predicate = new BooleanBuilder(); + String[] parts = filter.split("\\+"); + + + if (parts.length == 3) { + try { + Field field = getFieldRecursively(classes, parts[0]); + field.setAccessible(true); + Class fieldType = field.getType(); + PathBuilder fieldPath = new PathBuilder<>(fieldType, field.getName()); + + + switch (parts[1].toLowerCase()) { + case "equal": + predicate.and(fieldPath.eq(Expressions.constant(parts[2]))); + break; + case "notEqual": + predicate.and(fieldPath.ne(Expressions.constant(parts[2]))); + break; + case "greater": + predicate.and(Expressions.booleanTemplate("{0} > {1}", fieldPath, getType(fieldType, parts[2]))); + break; + case "lesser": + predicate.and(Expressions.booleanTemplate("{0} < {1}", fieldPath, getType(fieldType, parts[2]))); + break; + case "greaterequal": + predicate.and(Expressions.booleanTemplate("{0} >= {1}", fieldPath, getType(fieldType, parts[2]))); + break; + case "lesserequal": + predicate.and(Expressions.booleanTemplate("{0} <= {1}", fieldPath, getType(fieldType, parts[2]))); + break; + case "like": + predicate.and(Expressions.booleanTemplate("{0} like '%'||{1}||'%'", fieldPath, Expressions.constant(parts[2]))); + break; + default: + throw new RuntimeException("Operador não suportado"); + } + + + } catch (NoSuchFieldException e) { + throw new RuntimeException("Campo não encontrado"); + } catch (Exception e) { + throw new RuntimeException("Acesso ilegal ao campo"); + } + } + + + if (parts.length == 4) { + try { + Field field = getFieldRecursively(classes, parts[0]); + field.setAccessible(true); + Class fieldType = field.getType(); + PathBuilder fieldPath = new PathBuilder<>(fieldType, field.getName()); + + + switch (parts[1].toLowerCase()) { + case "between": + predicate.and(Expressions.booleanTemplate("{0} >= {1} AND {0} <= {2}", fieldPath, getType(fieldType, parts[2]), getType(fieldType, parts[3]))); + break; + default: + throw new RuntimeException("Operador não suportado"); + } + + + } catch (NoSuchFieldException e) { + throw new RuntimeException("Campo não encontrado"); + } catch (Exception e) { + throw new RuntimeException("Acesso ilegal ao campo"); + } + } + + + return predicate; + } + + + + + public static Expression getType(Class fieldType, String part) { + if (fieldType == Integer.class || fieldType == int.class) { + return Expressions.constant(Integer.parseInt(part)); + } else if (fieldType == Double.class || fieldType == double.class) { + return Expressions.constant(Double.parseDouble(part)); + } else if (fieldType == LocalDate.class) { + return Expressions.constant(LocalDate.parse(part)); + } + return Expressions.constant(part); + } + + + private static Field getFieldRecursively(Class classes, String fieldName) throws NoSuchFieldException { + try { + return classes.getDeclaredField(fieldName); + } catch (NoSuchFieldException e) { + if (classes.getSuperclass() != null) { + return getFieldRecursively(classes.getSuperclass(), fieldName); + } else { + throw e; + } + } + } + + +} diff --git a/backend/src/main/java/com/senac/gestaocurso/enterprise/CustomQuerydslPredicateExecutor.java b/backend/src/main/java/com/senac/gestaocurso/enterprise/CustomQuerydslPredicateExecutor.java new file mode 100644 index 0000000..8ecb0f8 --- /dev/null +++ b/backend/src/main/java/com/senac/gestaocurso/enterprise/CustomQuerydslPredicateExecutor.java @@ -0,0 +1,24 @@ +package com.senac.gestaocurso.enterprise; + +import com.querydsl.core.BooleanBuilder; +import com.querydsl.core.types.Predicate; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.querydsl.QuerydslPredicateExecutor; + + +import java.util.List; + + +public interface CustomQuerydslPredicateExecutor extends QuerydslPredicateExecutor { + + + @Override + List findAll(Predicate predicate); + + + default Page findAll(String filter, Class entityType, Pageable pageable) { + BooleanBuilder booleanBuilder = BooleanBuilderUtil.buildPredicateFromFilter(filter, entityType); + return this.findAll(booleanBuilder, pageable); + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/senac/gestaocurso/repository/AulaRepository.java b/backend/src/main/java/com/senac/gestaocurso/repository/AulaRepository.java index 7acaad8..3db3ef6 100644 --- a/backend/src/main/java/com/senac/gestaocurso/repository/AulaRepository.java +++ b/backend/src/main/java/com/senac/gestaocurso/repository/AulaRepository.java @@ -1,9 +1,10 @@ package com.senac.gestaocurso.repository; +import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor; import com.senac.gestaocurso.models.Aula; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface AulaRepository extends JpaRepository { +public interface AulaRepository extends JpaRepository, CustomQuerydslPredicateExecutor { } diff --git a/backend/src/main/java/com/senac/gestaocurso/repository/AvaliacaoRepository.java b/backend/src/main/java/com/senac/gestaocurso/repository/AvaliacaoRepository.java index cf4e4d2..d47fb8f 100644 --- a/backend/src/main/java/com/senac/gestaocurso/repository/AvaliacaoRepository.java +++ b/backend/src/main/java/com/senac/gestaocurso/repository/AvaliacaoRepository.java @@ -1,9 +1,10 @@ package com.senac.gestaocurso.repository; +import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor; import com.senac.gestaocurso.models.Avaliacao; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface AvaliacaoRepository extends JpaRepository { +public interface AvaliacaoRepository extends JpaRepository, CustomQuerydslPredicateExecutor { } diff --git a/backend/src/main/java/com/senac/gestaocurso/repository/CargoRepository.java b/backend/src/main/java/com/senac/gestaocurso/repository/CargoRepository.java index fc66ef8..94eef0a 100644 --- a/backend/src/main/java/com/senac/gestaocurso/repository/CargoRepository.java +++ b/backend/src/main/java/com/senac/gestaocurso/repository/CargoRepository.java @@ -1,9 +1,10 @@ package com.senac.gestaocurso.repository; +import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor; import com.senac.gestaocurso.models.Cargo; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface CargoRepository extends JpaRepository { +public interface CargoRepository extends JpaRepository, CustomQuerydslPredicateExecutor { } diff --git a/backend/src/main/java/com/senac/gestaocurso/repository/CertifcacoesRepository.java b/backend/src/main/java/com/senac/gestaocurso/repository/CertifcacoesRepository.java index bfc0f04..a88de85 100644 --- a/backend/src/main/java/com/senac/gestaocurso/repository/CertifcacoesRepository.java +++ b/backend/src/main/java/com/senac/gestaocurso/repository/CertifcacoesRepository.java @@ -1,9 +1,10 @@ package com.senac.gestaocurso.repository; +import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor; import com.senac.gestaocurso.models.Certificacoes; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface CertifcacoesRepository extends JpaRepository{ +public interface CertifcacoesRepository extends JpaRepository, CustomQuerydslPredicateExecutor { } diff --git a/backend/src/main/java/com/senac/gestaocurso/repository/CursoRepository.java b/backend/src/main/java/com/senac/gestaocurso/repository/CursoRepository.java index 0cc20ec..75b2ac7 100644 --- a/backend/src/main/java/com/senac/gestaocurso/repository/CursoRepository.java +++ b/backend/src/main/java/com/senac/gestaocurso/repository/CursoRepository.java @@ -1,10 +1,11 @@ package com.senac.gestaocurso.repository; +import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor; import com.senac.gestaocurso.models.Curso; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface CursoRepository extends JpaRepository { +public interface CursoRepository extends JpaRepository, CustomQuerydslPredicateExecutor { } diff --git a/backend/src/main/java/com/senac/gestaocurso/repository/DadosBancariosRepository.java b/backend/src/main/java/com/senac/gestaocurso/repository/DadosBancariosRepository.java index 8351200..9952c64 100644 --- a/backend/src/main/java/com/senac/gestaocurso/repository/DadosBancariosRepository.java +++ b/backend/src/main/java/com/senac/gestaocurso/repository/DadosBancariosRepository.java @@ -1,9 +1,10 @@ package com.senac.gestaocurso.repository; +import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor; import com.senac.gestaocurso.models.DadosBancarios; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface DadosBancariosRepository extends JpaRepository { +public interface DadosBancariosRepository extends JpaRepository , CustomQuerydslPredicateExecutor { } diff --git a/backend/src/main/java/com/senac/gestaocurso/repository/DependentesRepository.java b/backend/src/main/java/com/senac/gestaocurso/repository/DependentesRepository.java index 2cbcafa..f6d471c 100644 --- a/backend/src/main/java/com/senac/gestaocurso/repository/DependentesRepository.java +++ b/backend/src/main/java/com/senac/gestaocurso/repository/DependentesRepository.java @@ -1,9 +1,10 @@ package com.senac.gestaocurso.repository; +import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor; import com.senac.gestaocurso.models.Dependentes; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface DependentesRepository extends JpaRepository { +public interface DependentesRepository extends JpaRepository, CustomQuerydslPredicateExecutor { } diff --git a/backend/src/main/java/com/senac/gestaocurso/repository/ExpAnteriorRepository.java b/backend/src/main/java/com/senac/gestaocurso/repository/ExpAnteriorRepository.java index d2c9db0..351672a 100644 --- a/backend/src/main/java/com/senac/gestaocurso/repository/ExpAnteriorRepository.java +++ b/backend/src/main/java/com/senac/gestaocurso/repository/ExpAnteriorRepository.java @@ -1,7 +1,8 @@ package com.senac.gestaocurso.repository; +import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor; import com.senac.gestaocurso.models.ExpAnterior; import org.springframework.data.jpa.repository.JpaRepository; -public interface ExpAnteriorRepository extends JpaRepository { +public interface ExpAnteriorRepository extends JpaRepository, CustomQuerydslPredicateExecutor { } diff --git a/backend/src/main/java/com/senac/gestaocurso/repository/FiliacaoRepository.java b/backend/src/main/java/com/senac/gestaocurso/repository/FiliacaoRepository.java index ca0d635..e8dc4b2 100644 --- a/backend/src/main/java/com/senac/gestaocurso/repository/FiliacaoRepository.java +++ b/backend/src/main/java/com/senac/gestaocurso/repository/FiliacaoRepository.java @@ -1,7 +1,8 @@ package com.senac.gestaocurso.repository; +import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor; import com.senac.gestaocurso.models.Filiacao; import org.springframework.data.jpa.repository.JpaRepository; -public interface FiliacaoRepository extends JpaRepository { +public interface FiliacaoRepository extends JpaRepository, CustomQuerydslPredicateExecutor { } diff --git a/backend/src/main/java/com/senac/gestaocurso/repository/FrequenciaRepository.java b/backend/src/main/java/com/senac/gestaocurso/repository/FrequenciaRepository.java index 8ce73e9..5586983 100644 --- a/backend/src/main/java/com/senac/gestaocurso/repository/FrequenciaRepository.java +++ b/backend/src/main/java/com/senac/gestaocurso/repository/FrequenciaRepository.java @@ -1,9 +1,10 @@ package com.senac.gestaocurso.repository; +import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor; import com.senac.gestaocurso.models.Frequencia; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface FrequenciaRepository extends JpaRepository { +public interface FrequenciaRepository extends JpaRepository, CustomQuerydslPredicateExecutor { } diff --git a/backend/src/main/java/com/senac/gestaocurso/repository/FuncionarioRepository.java b/backend/src/main/java/com/senac/gestaocurso/repository/FuncionarioRepository.java index e127e2e..c2118b7 100644 --- a/backend/src/main/java/com/senac/gestaocurso/repository/FuncionarioRepository.java +++ b/backend/src/main/java/com/senac/gestaocurso/repository/FuncionarioRepository.java @@ -1,11 +1,12 @@ package com.senac.gestaocurso.repository; +import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor; import com.senac.gestaocurso.models.Funcionario; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface FuncionarioRepository extends JpaRepository { +public interface FuncionarioRepository extends JpaRepository , CustomQuerydslPredicateExecutor { Funcionario findByCpf(String cpf); Funcionario findByRg(String rg); diff --git a/backend/src/main/java/com/senac/gestaocurso/repository/InscricaoRepository.java b/backend/src/main/java/com/senac/gestaocurso/repository/InscricaoRepository.java index c3b211c..393e9fd 100644 --- a/backend/src/main/java/com/senac/gestaocurso/repository/InscricaoRepository.java +++ b/backend/src/main/java/com/senac/gestaocurso/repository/InscricaoRepository.java @@ -1,9 +1,10 @@ package com.senac.gestaocurso.repository; +import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor; import com.senac.gestaocurso.models.Inscricao; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Service; @Service -public interface InscricaoRepository extends JpaRepository { +public interface InscricaoRepository extends JpaRepository, CustomQuerydslPredicateExecutor { } diff --git a/backend/src/main/java/com/senac/gestaocurso/repository/MateriaRepository.java b/backend/src/main/java/com/senac/gestaocurso/repository/MateriaRepository.java index f6915c1..8f2f228 100644 --- a/backend/src/main/java/com/senac/gestaocurso/repository/MateriaRepository.java +++ b/backend/src/main/java/com/senac/gestaocurso/repository/MateriaRepository.java @@ -1,9 +1,10 @@ package com.senac.gestaocurso.repository; +import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor; import com.senac.gestaocurso.models.Materia; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface MateriaRepository extends JpaRepository { +public interface MateriaRepository extends JpaRepository, CustomQuerydslPredicateExecutor { } diff --git a/backend/src/main/java/com/senac/gestaocurso/repository/TurmaRepository.java b/backend/src/main/java/com/senac/gestaocurso/repository/TurmaRepository.java index 3e69760..b8fb54c 100644 --- a/backend/src/main/java/com/senac/gestaocurso/repository/TurmaRepository.java +++ b/backend/src/main/java/com/senac/gestaocurso/repository/TurmaRepository.java @@ -1,9 +1,10 @@ package com.senac.gestaocurso.repository; +import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor; import com.senac.gestaocurso.models.Turma; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface TurmaRepository extends JpaRepository { +public interface TurmaRepository extends JpaRepository, CustomQuerydslPredicateExecutor { }