• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wmixvideo / nfe / #6669

09 May 2016 12:13PM UTC coverage: 79.328% (-0.04%) from 79.364%
#6669

push

travis-ci

jefperito
Ajustado javadoc para HTML Strict

4651 of 5863 relevant lines covered (79.33%)

50.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/com/fincatto/nfe310/webservices/WSLoteEnvio.java
1
package com.fincatto.nfe310.webservices;
2

3
import com.fincatto.nfe310.NFeConfig;
4
import com.fincatto.nfe310.assinatura.AssinaturaDigital;
5
import com.fincatto.nfe310.classes.NFAutorizador31;
6
import com.fincatto.nfe310.classes.NFModelo;
7
import com.fincatto.nfe310.classes.lote.envio.NFLoteEnvio;
8
import com.fincatto.nfe310.classes.lote.envio.NFLoteEnvioRetorno;
9
import com.fincatto.nfe310.classes.nota.NFNota;
10
import com.fincatto.nfe310.classes.nota.NFNotaInfo;
11
import com.fincatto.nfe310.classes.nota.NFNotaInfoSuplementar;
12
import com.fincatto.nfe310.parsers.NotaParser;
13
import com.fincatto.nfe310.persister.NFPersister;
14
import com.fincatto.nfe310.utils.NFGeraChave;
15
import com.fincatto.nfe310.utils.NFGeraQRCode;
16
import com.fincatto.nfe310.validadores.xsd.XMLValidador;
17
import com.fincatto.nfe310.webservices.gerado.NfeAutorizacaoStub;
18
import com.fincatto.nfe310.webservices.gerado.NfeAutorizacaoStub.NfeAutorizacaoLoteResult;
19
import com.fincatto.nfe310.webservices.gerado.NfeAutorizacaoStub.NfeCabecMsg;
20
import com.fincatto.nfe310.webservices.gerado.NfeAutorizacaoStub.NfeCabecMsgE;
21
import com.fincatto.nfe310.webservices.gerado.NfeAutorizacaoStub.NfeDadosMsg;
22
import org.apache.axiom.om.OMElement;
23
import org.apache.axiom.om.util.AXIOMUtil;
24
import org.slf4j.Logger;
25
import org.slf4j.LoggerFactory;
26

27
import javax.xml.stream.XMLStreamException;
28
import java.util.Iterator;
29

30
class WSLoteEnvio {
31

32
    private static final String NFE_ELEMENTO = "NFe";
33
    private static final Logger LOGGER = LoggerFactory.getLogger(WSLoteEnvio.class);
×
34
    private final NFeConfig config;
35

36
    WSLoteEnvio(final NFeConfig config) {
×
37
        this.config = config;
×
38
    }
×
39

40
    NFLoteEnvioRetorno enviaLoteAssinado(final String loteAssinadoXml) throws Exception {
41
        XMLValidador.validaLote(loteAssinadoXml);
×
42
        return this.comunicaLote(loteAssinadoXml, NFModelo.NFE);
×
43
    }
44

45
    NFLoteEnvioRetorno enviaLote(final NFLoteEnvio lote) throws Exception {
46
        // adiciona a chave e o dv antes de assinar
47
        for (final NFNota nota : lote.getNotas()) {
×
48
            final NFGeraChave geraChave = new NFGeraChave(nota);
×
49
            final NFNotaInfo notaInfo = nota.getInfo();
×
50
            notaInfo.setIdentificador(geraChave.getChaveAcesso());
×
51
            notaInfo.getIdentificacao().setDigitoVerificador(geraChave.getDV());
×
52
        }
×
53

54
        // valida o lote gerado (ainda nao assinado)
55
        final String loteNaoAssinado = lote.toString();
×
56
        XMLValidador.validaLote(loteNaoAssinado);
×
57

58
        // assina o lote
59
        final String documentoAssinado = new AssinaturaDigital(this.config).assinarDocumento(loteNaoAssinado);
×
60
        final NFLoteEnvio loteAssinado = new NotaParser().loteParaObjeto(documentoAssinado);
×
61

62
        // verifica se nao tem NFCe junto com NFe no lote e gera qrcode (apos assinar mesmo, eh assim)
63
        int qtdNF = 0, qtdNFC = 0;
×
64
        for (final NFNota nota : loteAssinado.getNotas()) {
×
65
            switch (nota.getInfo().getIdentificacao().getModelo()) {
×
66
                case NFE:
67
                    qtdNF++;
×
68
                    break;
×
69
                case NFCE:
70
                    final NFGeraQRCode geraQRCode = new NFGeraQRCode(nota, this.config);
×
71
                    nota.setInfoSuplementar(new NFNotaInfoSuplementar());
×
72
                    nota.getInfoSuplementar().setQrCode(geraQRCode.getQRCode());
×
73
                    qtdNFC++;
×
74
                    break;
×
75
                default:
76
                    throw new IllegalArgumentException(String.format("Modelo de nota desconhecida: %s", nota.getInfo().getIdentificacao().getModelo()));
×
77
            }
78
        }
×
79

80
        // verifica se todas as notas do lote sao do mesmo modelo
81
        if ((qtdNF > 0) && (qtdNFC > 0)) {
×
82
            throw new IllegalArgumentException("Lote contendo notas de modelos diferentes!");
×
83
        }
84

85
        // guarda o modelo das notas
86
        final NFModelo modelo = qtdNFC > 0 ? NFModelo.NFCE : NFModelo.NFE;
×
87

88
        // comunica o lote
89
        return this.comunicaLote(loteAssinado.toString(), modelo);
×
90
    }
91

92
    private NFLoteEnvioRetorno comunicaLote(final String loteAssinadoXml, final NFModelo modelo) throws Exception {
93
        final OMElement omElement = this.nfeToOMElement(loteAssinadoXml);
×
94

95
        final NfeDadosMsg dados = new NfeDadosMsg();
×
96
        dados.setExtraElement(omElement);
×
97

98
        final NfeCabecMsgE cabecalhoSOAP = this.getCabecalhoSOAP();
×
99
        WSLoteEnvio.LOGGER.debug(omElement.toString());
×
100

101
        final NFAutorizador31 autorizador = NFAutorizador31.valueOfCodigoUF(this.config.getCUF());
×
102
        final String endpoint = NFModelo.NFE.equals(modelo) ? autorizador.getNfeAutorizacao(this.config.getAmbiente()) : autorizador.getNfceAutorizacao(this.config.getAmbiente());
×
103
        if (endpoint == null) {
×
104
            throw new IllegalArgumentException("Nao foi possivel encontrar URL para Autorizacao " + modelo.name() + ", autorizador " + autorizador.name());
×
105
        }
106

107
        final NfeAutorizacaoLoteResult autorizacaoLoteResult = new NfeAutorizacaoStub(endpoint).nfeAutorizacaoLote(dados, cabecalhoSOAP);
×
108
        final NFLoteEnvioRetorno loteEnvioRetorno = new NFPersister().read(NFLoteEnvioRetorno.class, autorizacaoLoteResult.getExtraElement().toString());
×
109
        WSLoteEnvio.LOGGER.info(loteEnvioRetorno.toString());
×
110
        return loteEnvioRetorno;
×
111
    }
112

113
    private NfeCabecMsgE getCabecalhoSOAP() {
114
        final NfeCabecMsg cabecalho = new NfeCabecMsg();
×
115
        cabecalho.setCUF(this.config.getCUF().getCodigoIbge());
×
116
        cabecalho.setVersaoDados(NFeConfig.VERSAO_NFE);
×
117
        final NfeCabecMsgE cabecalhoSOAP = new NfeCabecMsgE();
×
118
        cabecalhoSOAP.setNfeCabecMsg(cabecalho);
×
119
        return cabecalhoSOAP;
×
120
    }
121

122
    private OMElement nfeToOMElement(final String documento) throws XMLStreamException {
123
        final OMElement ome = AXIOMUtil.stringToOM(documento);
×
124
        final Iterator<?> children = ome.getChildrenWithLocalName(WSLoteEnvio.NFE_ELEMENTO);
×
125
        while (children.hasNext()) {
×
126
            final OMElement omElement = (OMElement) children.next();
×
127
            if ((omElement != null) && (WSLoteEnvio.NFE_ELEMENTO.equals(omElement.getLocalName()))) {
×
128
                omElement.addAttribute("xmlns", NFeConfig.NFE_NAMESPACE, null);
×
129
            }
130
        }
×
131
        return ome;
×
132
    }
133
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc