Commit 17d5871c4ea27d93ac75c48ad679b29266518fa8

Authored by andre.guimaraes
1 parent 0b0f5b22

Ajusta query de consulta unidades processo

A query não estava selecionando corretamente as unidades do processo, em
alguns momentos ela exibia unidades onde o processo estava fechado.
src/main/java/br/gov/ans/integracao/sei/dao/UnidadeDAO.java
1 1 package br.gov.ans.integracao.sei.dao;
2 2  
3 3 import java.util.List;
  4 +import java.util.function.Predicate;
  5 +import java.util.stream.Collectors;
4 6  
5 7 import javax.persistence.EntityManager;
6 8 import javax.persistence.PersistenceContext;
... ... @@ -8,26 +10,65 @@ import javax.persistence.PersistenceContextType;
8 10 import javax.persistence.Query;
9 11  
10 12 import br.gov.ans.integracao.sei.client.Unidade;
  13 +import br.gov.ans.integracao.sei.modelo.UnidadeTarefa;
11 14  
12 15 public class UnidadeDAO {
13 16 @PersistenceContext(unitName = "sei_pu", type = PersistenceContextType.EXTENDED)
14 17 private EntityManager em;
15 18  
  19 + private static final int ABERTURA_PROCESSO = 1;
  20 + private static final int CONCLUSAO_PROCESSO = 28;
  21 + private static final int REABERTURA_PROCESSO = 29;
  22 + private static final int ENVIO_PROCESSO = 32;
  23 + private static final int RECEBIMENTO_PROCESSO = 48;
  24 +
16 25 public List<Unidade> listarUnidadesProcesso(String idProcedimento){
17 26 StringBuilder sql = new StringBuilder("SELECT u.id_unidade idUnidade, u.sigla, u.descricao, u.sin_protocolo sinProtocolo, ");
18   - sql.append("u.sin_arquivamento sinArquivamento, u.sin_ouvidoria sinOuvidoria ");
  27 + sql.append("u.sin_arquivamento sinArquivamento, u.sin_ouvidoria sinOuvidoria, t.id_tarefa tarefa ");
19 28 sql.append("FROM protocolo p ");
20 29 sql.append("JOIN atividade a ON p.id_protocolo = a.id_protocolo ");
21 30 sql.append("JOIN unidade u ON a.id_unidade = u.id_unidade ");
22 31 sql.append("JOIN tarefa t ON a.id_tarefa = t.id_tarefa ");
23 32 sql.append("WHERE p.id_protocolo = :protocolo ");
24   - sql.append("and t.id_tarefa in (32, 29, 1) ");
25   - sql.append("group by u.id_unidade order by u.idx_unidade; ");
  33 + sql.append("and t.id_tarefa in (");
  34 + sql.append(ABERTURA_PROCESSO + ",");
  35 + sql.append(CONCLUSAO_PROCESSO + ",");
  36 + sql.append(REABERTURA_PROCESSO + ",");
  37 + sql.append(ENVIO_PROCESSO + ",");
  38 + sql.append(RECEBIMENTO_PROCESSO);
  39 + sql.append(") ");
  40 + sql.append("order by a.dth_abertura desc ");
26 41  
27   - Query query = em.createNativeQuery(sql.toString(), Unidade.class);
  42 + Query query = em.createNativeQuery(sql.toString(), UnidadeTarefa.class);
28 43  
29 44 query.setParameter("protocolo", idProcedimento);
30 45  
31   - return query.getResultList();
  46 + List<UnidadeTarefa> resultado = query.getResultList();
  47 +
  48 + List<Unidade> unidades = resultado.stream()
  49 + .filter(makeUntil(u -> u.getTarefa() == 28))
  50 + .distinct()
  51 + .filter(u -> u.getTarefa() != 28)
  52 + .collect(Collectors.toList());
  53 +
  54 + return unidades;
  55 + }
  56 +
  57 + public static <T> Predicate<T> makeUntil(final Predicate<T> predicate) {
  58 + return new Predicate<T>() {
  59 +
  60 + private boolean hasMatched;
  61 +
  62 + @Override
  63 + public boolean test(final T s) {
  64 + if (hasMatched) {
  65 + return false;
  66 + }
  67 + else {
  68 + hasMatched = predicate.test(s);
  69 + return true;
  70 + }
  71 + }
  72 + };
32 73 }
33 74 }
... ...
src/main/java/br/gov/ans/integracao/sei/modelo/UnidadeTarefa.java 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +package br.gov.ans.integracao.sei.modelo;
  2 +
  3 +import javax.persistence.Entity;
  4 +
  5 +import br.gov.ans.integracao.sei.client.Unidade;
  6 +
  7 +@Entity
  8 +public class UnidadeTarefa extends Unidade{
  9 +
  10 + private int tarefa;
  11 +
  12 + public int getTarefa() {
  13 + return tarefa;
  14 + }
  15 +
  16 + public void setTarefa(int tarefa) {
  17 + this.tarefa = tarefa;
  18 + }
  19 +}
0 20 \ No newline at end of file
... ...