Transform javax.persistence.Query to ArrayList

1

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

I need to return an object of type Query but can not have all result values.

I want to delete some results based on some tests, the code is below. I have to return an object of type Query because it is a generic class and I can not 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;
    }
    
asked by anonymous 13.11.2014 / 03:24

1 answer

1

I think returning a javax.persistence.Query does not seem like a good idea, if I understand your case right. Nothing would prevent the caller from invoking methods such as setParameter , getResultList , setMaxResults or executeUpdate on Query returned and messing with the query or at least not having the desired result, since the query has already been made and the results are already ready, they just need to be filtered.

In this case, the code that you have already results in 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 (besides changing the return type accordingly), you put this:

return new Filterable<>(listaSituacoesEmpresasContratadas);

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

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

    
13.11.2014 / 03:48