Creating XML file with Search for a given user in the table using forEach jpa java

0

Good morning !!

My personal doubts are as follows

I have two tables one Person and one Employee.

I'm making a for both tables to be able to create an Xml file, however since I'm not returning the results by Id and yes by the order of the for, my xml is being created because it is not pulling the correct data.

the job field is pulling that of another employee as shown below

Wrong

    2     Rafael     System analyst

Right

    2     Rafael     Editor

Below is the code to create Xml

    // Gerando Arquivo Xml Para Exportação.
private File GerarXmlFuncionarios() {

    List<FuncionarioModel> funcionariosModel = funcionarioRepository.todos();

    List<PessoaModel> pessoasModel = pessoaRepository.todas();

    // Nome do Elemento Raiz do Xml
    Element elementDados = new Element("funcionarios");

    Document documentoFuncionarios = new Document(elementDados);

    funcionariosModel.forEach(funcionario -> {
        pessoasModel.forEach(pessoa -> {
            // Campos dos Xml com os Seus Valores
            Element elementFuncionario = new Element("funcionario");
            elementFuncionario.addContent(new Element("codigo").setText(pessoa.getCodigo().toString()));
            elementFuncionario.addContent(new Element("nome").setText(pessoa.getNome()));
            elementFuncionario.addContent(new Element("cargo").setText(funcionario.getCargo()));

            elementDados.addContent(elementFuncionario);
        });
    });

    XMLOutputter xmlGerado = new XMLOutputter();
    xmlGerado.setFormat(Format.getPrettyFormat().setEncoding("ISO-8859-1"));

    try {

        // Gera o Nome do Arquivo 
        String nomeArquivo = "funcionarios_".concat(java.util.UUID.randomUUID().toString()).concat(".xml");

        // Caminho que o Arquivo Sera Salvo 
        File arquivo = new File("C:/Pasta/Sistema_Web/".concat(nomeArquivo));

        FileWriter fileWriter = new FileWriter(arquivo);

        xmlGerado.output(documentoFuncionarios, fileWriter);

        return arquivo;

    } catch (Exception ex) {

        ex.printStackTrace();
    }

    return null;
}

PersonRepository and OfficialRepository classes where I return my list of both classes

    public List<PessoaModel> todas() {
    TypedQuery<PessoaModel> query = manager.createQuery("from PessoaModel", PessoaModel.class);
    return query.getResultList();
}


    public List<FuncionarioModel> todos() {
    TypedQuery<FuncionarioModel> query = manager.createQuery("from FuncionarioModel", FuncionarioModel.class);
    return query.getResultList();
}

follow solution below     officialsModel.forEach (official -> {                     // Xml Fields with Your Values                     Element elementFuntionary = new Element ("official");                     element () () () () () () ().                     elementFuncionario.addContent (new Element ("name"). setText (oficial.getPessoaModel (). getNome ()));                     elementType.addContent (new Element ("title"). setText (official.getCargo ()));                     elementDados.addContent (elementFuncionario);         });

    
asked by anonymous 21.02.2017 / 15:16

2 answers

0

Since you do not have effective control of each element, I suggest that you query the database in an orderly way ( ORDER BY ) to ensure that the correct data moves in parallel.

Second thing is to do this interaction with the forEach of Java 8, but I do not know how it should be done, but with an interaction with the traditional for it would look something like this:

int funcionarioAdd = 0;
int pessoaAdd = 0;

for(List<FuncionarioModel> funcionario: funcionariosModel) {
 for(List<PessoaModel> pessoa: pessoasModel) {          
   if(funcionarioAdd == pessoaAdd++) {
    // Campos dos Xml com os Seus Valores
    Element elementFuncionario = new Element("funcionario");
    elementFuncionario.addContent(new Element("codigo").setText(pessoa.getCodigo()));
    elementFuncionario.addContent(new Element("nome").setText(pessoa.getNome()));
    elementFuncionario.addContent(new Element("cargo").setText(funcionario.getCargo()));
    elementDados.addContent(elementFuncionario);
    pessoaAdd = 0;
    break;
   }       
  }
 funcionarioAdd++;
}

Check the logic correctly and be careful because the chances of the data losing the ordering is very high in this type of procedure (sort without the keys that guarantee the consistency).

    
21.02.2017 / 16:35
0

Felipe and n3uRoQuiLa, Thank you again for your help.

I managed to solve it as below, after all I'm working object oriented was simpler than I thought.

        funcionariosModel.forEach(funcionario -> {
                // Campos dos Xml com os Seus Valores
                Element elementFuncionario = new Element("funcionario");
                elementFuncionario.addContent(new Element("codigo").setText(funcionario.getPessoaModel().getCodigo().toString()));
                elementFuncionario.addContent(new Element("nome").setText(funcionario.getPessoaModel().getNome()));
                elementFuncionario.addContent(new Element("cargo").setText(funcionario.getCargo()));
                elementDados.addContent(elementFuncionario);
    });

Once again thankful I hope one day to give back

    
21.02.2017 / 16:58