Transform javax.persistence.Query to Arraylist

Asked

Viewed 205 times

1

It is possible to transform a javax.persistence.Query in ArrayList.

I need to return an object of the type Query but it cannot have all the values of the result.

I want to eliminate some results based on some tests, the code is below. I have to return an object of the type Query because it is a generic class and I cannot modify SQL.

        ...
        Query consulta = entityManager.createQuery(builder.toString());
        Query queryRetornoQuery;
        Set<String> chaveParametros = parametros.keySet();

        if (chaveParametros != null) {
            for (String parametro : chaveParametros) {
                consulta.setParameter(parametro, parametros.get(parametro));
            }
        }

        RelatorioSituacaoEmpresasContratadasTO empresasContratadasTO = null;
        List<RelatorioSituacaoEmpresasContratadas> listaSituacoes =
                consulta.getResultList();
        for (RelatorioSituacaoEmpresasContratadas relatorioSituacaoEmpresasContratadas : listaSituacoes) {
            empresasContratadasTO =
                    ConversorEntidadeTransferObject
                            .converterRelatorioSituacaoEmpresasContratadas(relatorioSituacaoEmpresasContratadas);

            /* O contrato está inativo? */
            if (hoje.compareTo(empresasContratadasTO
                    .getDataInicioContratoEmpresa()) < 0
                    || hoje.compareTo(empresasContratadasTO
                            .getDataFimContratoEmpresa()) > 0) {
                empresasContratadasTO
                        .setStatusContatoEmpresa(Constantes.CODIGO_STATUS_CONTRATO_EMPRESA_INATIVO);
                empresasContratadasTO
                        .setDescricaoStatusContratoEmpresa(Constantes.DESCRICAO_STATUS_CONTRATO_EMPRESA_INATIVO);
            }

            listaSituacoesEmpresasContratadas.add(empresasContratadasTO);
        }
        // No lugar dessa lista abaixo quero retornar um objeto Query
        return listaSituacoesEmpresasContratadas;
    }

1 answer

1

I think return one javax.persistence.Query doesn’t seem like a good idea, if I understood your case correctly. Nothing would stop the caller from invoking methods like setParameter, getResultList, setMaxResults or executeUpdate in the Query returned and mess with the query or at least not have the desired result, because in fact the query has already been made and the results are ready, they just need to be filtered.

In this case, the code you have already brings as a result a ArrayList with the results. If your problem is to allow the application of arbitrary filters on top of the results, I would do something like this (java 8):

public class Filterable<E> {
    private final List<E> results;

    public Filterable(List<E> results) {
        this.results = results;
    }

    public Filterable<E> keepIf(Predicate<? super E> predicate) {
        results.removeIf(predicate.negate());
        return this;
    }

    public Filterable<E> removeIf(Predicate<? super E> predicate) {
        results.removeIf(predicate);
        return this;
    }

    public List<E> getResult() {
        // Retorna uma cópia, para que modificações na lista retornada não baguncem os filtros e vice-versa.
        return new ArrayList<>(results);
    }
}

And at the end of your method (in addition to changing the type of return according), you put this:

return new Filterable<>(listaSituacoesEmpresasContratadas);

If you are using a version of Java earlier than 8, you will also need to create an interface analogous to Predicate and create a private method to implement the removeIf of the list.

Or, if you prefer, it is possible to return to ArrayList the way it is and just call the removeIf whenever you want to filter it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.