From 63cd4b349bc372f8f90966bbfb2d72578240781b Mon Sep 17 00:00:00 2001 From: pedro-hos Date: Sun, 19 Nov 2017 23:57:35 -0200 Subject: [PATCH] =?UTF-8?q?vers=C3=A3o=202.0-Alpha1=20e=20#38=20Filtro=20p?= =?UTF-8?q?ara=20Leis=20na=20tela=20de=20autores?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../temis/model/repositories/impl/Leis.java | 35 +++++++++++++++++-- .../temis/resources/LeiResource.java | 4 +-- .../temis/resources/impl/LeiResourceImpl.java | 8 ++--- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 4dd9ae5..9259fd3 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.sjcdigital.temis temis-api war - 2.0.0-Alpha1 + 2.0-Alpha1 temis-api diff --git a/src/main/java/com/sjcdigital/temis/model/repositories/impl/Leis.java b/src/main/java/com/sjcdigital/temis/model/repositories/impl/Leis.java index a41c64f..0f15ca4 100644 --- a/src/main/java/com/sjcdigital/temis/model/repositories/impl/Leis.java +++ b/src/main/java/com/sjcdigital/temis/model/repositories/impl/Leis.java @@ -63,7 +63,8 @@ public List doAutor(final Long idAutor, int total, int pg) { // Sem ser por autor - public List filtraPaginado(final Long idSituacao, final Long idClasse, final Long idTipo, final Integer ano, final int total, final int pg) { + public List filtraPaginado( final Long idSituacao, final Long idClasse, final Long idTipo, final Integer ano, final Long idAutor, + final int total, final int pg) { StringBuilder sql = new StringBuilder("SELECT lei FROM Lei lei "); boolean and = false; @@ -100,6 +101,15 @@ public List filtraPaginado(final Long idSituacao, final Long idClasse, fina } + if(Objects.nonNull(idAutor)) { + adicionaWhereSeNecessario(sql, where); + adicionaAndSeNecessario(sql, and); + and = true; + where = true; + sql.append("lei.autor.id = :idAutor "); + + } + TypedQuery query = em.createQuery(sql.toString(), Lei.class); if(Objects.nonNull(idTipo)) { @@ -118,6 +128,10 @@ public List filtraPaginado(final Long idSituacao, final Long idClasse, fina query.setParameter("ano", ano); } + if(Objects.nonNull(idAutor)) { + query.setParameter("idAutor", idAutor); + } + query.setFirstResult(pg * total); query.setMaxResults(total); @@ -164,9 +178,24 @@ public List contaLeisPorClasse() { return query.getResultList(); } - public Anos anos() { - TypedQuery query = em.createQuery("SELECT DISTINCT new com.sjcdigital.temis.model.dto.Ano(lei.ano) FROM Lei lei ORDER BY lei.ano", Ano.class); + public Anos anos(final Long idAutor) { + StringBuilder sql = new StringBuilder("SELECT DISTINCT new com.sjcdigital.temis.model.dto.Ano(lei.ano) FROM Lei lei "); + + if(Objects.nonNull(idAutor)) { + sql.append("WHERE lei.autor.id = :idAutor "); + + } + + sql.append("ORDER BY lei.ano"); + + TypedQuery query = em.createQuery(sql.toString(), Ano.class); + + if(Objects.nonNull(idAutor)) { + query.setParameter("idAutor", idAutor); + } + List anos = query.getResultList(); + return new Anos(anos); } diff --git a/src/main/java/com/sjcdigital/temis/resources/LeiResource.java b/src/main/java/com/sjcdigital/temis/resources/LeiResource.java index 66dedb9..6b3f4a0 100644 --- a/src/main/java/com/sjcdigital/temis/resources/LeiResource.java +++ b/src/main/java/com/sjcdigital/temis/resources/LeiResource.java @@ -29,7 +29,7 @@ public interface LeiResource { @GET @Path("/filtra") Response filtraPaginado( @QueryParam("idSituacao") Long idSituacao, @QueryParam("idClasse") Long idClasse, - @QueryParam("idTipo") Long idTipo, @QueryParam("ano") Integer ano, + @QueryParam("idTipo") Long idTipo, @QueryParam("ano") Integer ano, @QueryParam("idAutor") Long idAutor, @QueryParam("total") int total, @QueryParam("pg") int pg ); @GET @@ -38,7 +38,7 @@ Response filtraPaginado( @QueryParam("idSituacao") Long idSituacao, @QueryParam( @GET @Path("/anos") - Response buscaAnos(); + Response buscaAnos(@QueryParam("idAutor") Long idAutor); @PUT @Path("/{id}/vota") diff --git a/src/main/java/com/sjcdigital/temis/resources/impl/LeiResourceImpl.java b/src/main/java/com/sjcdigital/temis/resources/impl/LeiResourceImpl.java index 0e7cc45..41b5f2b 100644 --- a/src/main/java/com/sjcdigital/temis/resources/impl/LeiResourceImpl.java +++ b/src/main/java/com/sjcdigital/temis/resources/impl/LeiResourceImpl.java @@ -94,14 +94,14 @@ public Response graficos() { } @Override - public Response filtraPaginado(Long idSituacao, Long idClasse, Long idTipo, Integer ano, int total, int pg) { - List leisFiltrada = RESTUtils.lanca404SeNulo(leis.filtraPaginado(idSituacao, idClasse, idTipo, ano, total, pg)); + public Response filtraPaginado(Long idSituacao, Long idClasse, Long idTipo, Integer ano, Long idAutor, int total, int pg) { + List leisFiltrada = RESTUtils.lanca404SeNulo(leis.filtraPaginado(idSituacao, idClasse, idTipo, ano, idAutor, total, pg)); return Response.ok().entity(leisFiltrada).build(); } @Override - public Response buscaAnos() { - return Response.ok().entity(leis.anos()).build(); + public Response buscaAnos(final Long idAutor) { + return Response.ok().entity(leis.anos(idAutor)).build(); } }