/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.dashboard.util;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ProcessadorDeArquivos {

    private File diretorio;
    private ExcellHelper helper;

    public static void main(String[] args) throws Exception {
        System.out.println("Executando ProcessadorDeArquivos");
        System.out.println("uma forma facil de jogar xmls malucos em um excell profissional!!");

        //Aqui fiz a alteração para passar o diretório e o arquivo a gerar
        ProcessadorDeArquivos processador = new ProcessadorDeArquivos("C:\\temp\\");
        ExcellHelper helper = new ExcellHelper("Excel-Out");
        processador.processarArquivos(helper);

        //gera o excell com todos os aquivos
        helper.geraArquivo();

    }

    private void processarArquivos(ExcellHelper helper) throws ParserConfigurationException, SAXException, IOException {
        File[] arquivos = this.diretorio.listFiles();
        for (int i = 0; i < arquivos.length; i++) {
            File file = arquivos[i];

            //legal validar se o arquivo é um xml e esta no formato adequado para o processo mas isso vc ve se é importante fazer
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(file);

            //aqui vc manda o conteudo para o helper no metodo de criar a linha
            //Passa pelo nó "infNFe" capturando os detalhes da nf     
            NodeList nList = doc.getElementsByTagName("infNFe");
            for (int n1 = 0; n1 < nList.getLength(); n1++) {
                Node node = nList.item(n1);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) node;

                    //Passa pelo nó "prod" capturando cada registro (detalhes da nf)
                    NodeList nList2 = element.getElementsByTagName("prod");
                    for (int j = 0; j < nList2.getLength(); j++) {
                        Node prod = nList2.item(j);
                        if (prod.getNodeType() == Node.ELEMENT_NODE) {
                            Element product = (Element) prod;
                            helper.criaLinha(element, product);
                        }
                    }
                }
            }
        }
    }//terminou de processar todos os arquivos

    public ProcessadorDeArquivos(String pathFiles) throws Exception {
        this.diretorio = new File(pathFiles);
        if (diretorio.isDirectory()) {
            System.out.println("total de arquivos para importar:" + diretorio.listFiles().length);
        } else {
            throw new Exception("O parametro deve ser um diretorio!!");
        }
    }
}
