0

I'm trying to make my object list "revogacaoData"

show the data as "name", "date", "login", but the return is always the memory space. How to interact to return this data?

This is the code

@GetMapping(value = { "/gerarCsv/{inicio}&{fim}" })
    @ResponseBody
    public ResponseEntity<?> gerarCsv(HttpServletRequest request,
            @PathVariable(value = "inicio", required = true) String inicio,
            @PathVariable(value = "fim", required = true) String fim) {
        try {
    
             
            Date dataInicio = new SimpleDateFormat("dd-MM-yyyy").parse(inicio);
            
            Date dataFim = new SimpleDateFormat("dd-MM-yyyy").parse(fim);
                    
            
            List<Object> revogacaoConsolidado = revogacao.consultarRevogacaoconsolidado(dataInicio, dataFim);
            
            File arquivo = new File("C:\\src\\main\\resources\\revogacao.csv");
            
        
            
            if(!arquivo.exists()) {
                arquivo.createNewFile();
            }
            
            FileWriter fw = new FileWriter(arquivo);
            BufferedWriter bw = new BufferedWriter(fw);
            
            bw.write("Date" + ";" + "login" + ";" + "nome");
        
        for(int i = 0 ; i < revogacaoConsolidado.size() ; i++) {
                
            System.out.println(revogacaoConsolidado.get(i));
                
                
                bw.newLine();
                bw.write( revogacaoConsolidado.get(0) + ";" + revogacaoConsolidado.get(1));
                    
                
            }
            
            System.out.println("Finished");
            bw.close();
            fw.close();
            
            logger.info("[Recurso:/admin/revogacao/revogacaoPorDataCSV, sessao:" + request.getSession().getId() + ", usuario:"
                    + request.getUserPrincipal().getName() + "]"
                    + "[Verificação de Revogações por data de processado CSV ]");

            return new ResponseEntity<Object>(revogacaoConsolidado, HttpStatus.OK);
        } catch (Exception e) {
            logger.info("[Recurso:/admin/revogacao/revogacaoPorDataCSV, sessao:" + request.getSession().getId() + ", usuario:"
                    + request.getUserPrincipal().getName() + "]"
                    + "[Verificação de Revogações por data de processado] error: " +e);
            return new ResponseEntity<String>("{\"msg\":\" Não foi possivel obter a lista consolidada}",
                    HttpStatus.EXPECTATION_FAILED);
        }
    }

And this is the return

[Ljava.lang.Object;@585e2103

I tried interate like this :

revogacaoConsolidado[i][1] 

but didn't work

I made some changes, created a model class where I can create the necessary toString. these were the changes.

Controller:

@GetMapping(value = { "/gerarCsv/{inicio}&{fim}" })
    @ResponseBody
    public ResponseEntity<?> gerarCsv(HttpServletRequest request,
            @PathVariable(value = "inicio", required = true) String inicio,
            @PathVariable(value = "fim", required = true) String fim) {
        try {
    
             
            Date dataInicio = new SimpleDateFormat("dd-MM-yyyy").parse(inicio);
            
            Date dataFim = new SimpleDateFormat("dd-MM-yyyy").parse(fim);
                    
            
            List<RevogacaoConsolidado> revogacaoConsolidado = revogacao.consultarRevogacaoconsolidado(dataInicio, dataFim);
            
    File arquivo =newFile("C:\\src\\\\main\\\\resources\\\\revogacao.csv");
            
        
            
            if(!arquivo.exists()) {
                arquivo.createNewFile();
            }
            
            FileWriter fw = new FileWriter(arquivo);
            BufferedWriter bw = new BufferedWriter(fw);
            
            bw.write("Data" + ";" + "login" + ";" + "nome");
        
            
        
            
        for(int i = 0 ; i < revogacaoConsolidado.size() ; i++) {
            
            
            
            String[] revog = new String[revogacaoConsolidado.size()];
                int index = 0;
                for(Object value : revogacaoConsolidado) {
                    revog[index] = String.valueOf(value);
                    index++;
                    
                    System.out.println("Data : " + revog[0].toString());
                }
            
                
                
                bw.newLine();
                
                bw.write( revog[0] + ";" + revogacaoConsolidado.get(1));
                    
                
            }
            
            System.out.println("Finished");
            bw.close();
            fw.close();
            
            logger.info("[Recurso:/admin/revogacao/revogacaoPorDataCSV, sessao:" + request.getSession().getId() + ", usuario:"
                    + request.getUserPrincipal().getName() + "]"
                    + "[Verificação de Revogações por data de processado CSV ]");

            return new ResponseEntity<Object>(revogacaoConsolidado, HttpStatus.OK);
        } catch (Exception e) {
            logger.info("[Recurso:/admin/revogacao/revogacaoPorDataCSV, sessao:" + request.getSession().getId() + ", usuario:"
                    + request.getUserPrincipal().getName() + "]"
                    + "[Verificação de Revogações por data de processado] error: " +e);
            return new ResponseEntity<String>("{\"msg\":\" Não foi possivel obter a lista consolidada}",
                    HttpStatus.EXPECTATION_FAILED);
        }
    }

Model:

package br.com.claro.tcc.web.portalsega.model;

import java.util.Date;

public class RevogacaoConsolidado {
private Date data_processo;
private String login;
private String nome;
private String eucesso;
private String erro;
private String nao_cadastrado;
private String ja_removido;
private String total;


  private String porcent_erro;
    
   //getters and setters
    
    
    @Override
    public String toString() {
    return "revogacaoConsolidado [data_processo=" + data_processo + ", login=" + login + ", nome=" + nome + ", eucesso="
            + eucesso + ", erro=" + erro + ", nao_cadastrado=" + nao_cadastrado + ", ja_removido=" + ja_removido
            + ", total=" + total + ", porcent_erro=" + porcent_erro + "]";
}



}

ConsultaRevogacaoConsolidada Service

@Transactional
    public List<RevogacaoConsolidado> consultarRevogacaoconsolidado(Date inicio, Date fim) {
        return revogacaoDAO.consultarRevogacaoconsolidado(inicio, fim);
        }

ConsultaRevogacaoConsolidada DAO

@Override
    public List<RevogacaoConsolidado> consultarRevogacaoconsolidado(Date inicio,Date fim) {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        
        String inicioData =dateFormat.format(inicio);
        String fimData =dateFormat.format(fim);
        
        Query query = sessionFactory.getCurrentSession()
                .createNativeQuery("select \r\n" + 
                        "to_char(a.dh_processado,'dd/mm/yyyy') as data_processo,\r\n" + 
                        "count(b.login) as login\r\n" + 
                        ",c.nome,\r\n" + 
                        "sum(case when e.nome = 'SUCESSO' then 1 else 0 end)  SUCESSO,\r\n" + 
                        "sum(case when e.nome = 'ERRO' then 1 else 0 end)  ERRO,\r\n" + 
                        "sum(case when e.nome = 'NAO CADASTRADO' then 1 else 0 end)  NAO_CADASTRADO,\r\n" + 
                        "sum(case when e.nome = 'JA REMOVIDO' then 1 else 0 end)  JA_REMOVIDO,\r\n" + 
                        "sum(case when e.nome = 'SUCESSO' then 1 else 0 end)  + \r\n" + 
                        "sum(case when e.nome = 'ERRO' then 1 else 0 end)  +\r\n" + 
                        "sum(case when e.nome = 'NAO CADASTRADO' then 1 else 0 end)  +\r\n" + 
                        "sum(case when e.nome = 'JA REMOVIDO' then 1 else 0 end)  TOTAL,\r\n" + 
                        "round((sum(case when e.nome = 'ERRO' then 1 else 0 end) / sum(case when e.nome = 'SUCESSO' then 1 end))*100,2) as porcentagem_erro\r\n" + 
                        "\r\n" + 
                        "from tb_revogacao_exec a\r\n" + 
                        "inner join tb_revogacao b on b.login = a.login\r\n" + 
                        "inner join tb_revogacao_processo c on c.id = a.id_processo\r\n" + 
                        "inner join tb_revogacao_sistema d on d.id = a.id_sistema\r\n" + 
                        "inner join tb_revogacao_status e on e.id = a.id_status\r\n" + 
                        "where a.dh_processado BETWEEN to_date(:inicio,'dd/MM/yyyy') \r\n" + 
                        "AND  to_date(:fim,'dd/MM/yyyy') \r\n" + 
                        "group by c.nome,to_char(a.dh_processado,'dd/mm/yyyy') "
                        + "order by to_char(a.dh_processado,'dd/mm/yyyy') desc");
    
        
        
        query.setParameter("inicio", inicioData);
        query.setParameter("fim",fimData);
        
        return query.getResultList();
        
    }
Willian Lima
  • 113
  • 5
  • Why did you have an List of object? If this list has instances of other objects, like Revogacao, have you override the toString method of this class? – sgtcortez Dec 02 '20 at 19:08
  • @MatheusRambo in reality, the list of objects receives data from my database, I tried to remodel the toString but without success tbm. – Willian Lima Dec 02 '20 at 19:11
  • @AlexRudenko Unfortunately not, I tried it the way this article mentions, even changing some toString settings, but it didn't work – Willian Lima Dec 02 '20 at 19:27
  • @WillianLima you will need to find a way to override the toString method, otherwise, it will not work. Please, share the consultarRevogacaoconsolidado with us. – sgtcortez Dec 02 '20 at 19:42
  • @MatheusRambo I edited the post with some more information and functionality – Willian Lima Dec 02 '20 at 19:54
  • I can believe that you dao method is working! How can you use a native query, and map to a java object without manual conversion? – sgtcortez Dec 02 '20 at 20:50
  • @MatheusRambo the method works, so much so that I use it to throw the dice on my front end ... what I don't know how to do is do the interaction inside this object to return the data instead of the memory space – Willian Lima Dec 02 '20 at 21:22

0 Answers0