All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.swconsultoria.mdfe.EventoMdfe Maven / Gradle / Ivy

The newest version!
package br.com.swconsultoria.mdfe;

import br.com.swconsultoria.mdfe.dom.ConfiguracoesMDFeInterface;
import br.com.swconsultoria.mdfe.exception.MdfeException;
import br.com.swconsultoria.mdfe.util.ConstantesMDFe;
import br.com.swconsultoria.mdfe.util.ObjetoMdfeUtil;
import br.com.swconsultoria.mdfe.util.WebServiceMdfeUtil;
import br.com.swconsultoria.mdfe.util.XmlMdfeUtil;
import br.com.swconsultoria.mdfe.wsdl.MDFeRecepcaoEvento.MDFeRecepcaoEventoStub;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.util.AXIOMUtil;

import javax.xml.bind.JAXBException;
import javax.xml.stream.XMLStreamException;
import java.rmi.RemoteException;

/**
 * @author Samuel Oliveira
 * @author john
 */
class EventoMdfe {

    static br.com.swconsultoria.mdfe.schema_300.evCancMDFe.TRetEvento cancelar(ConfiguracoesMDFeInterface configuracoes, br.com.swconsultoria.mdfe.schema_300.evCancMDFe.TEvento evento, boolean validar) throws MdfeException {
        try {
            String xml = XmlMdfeUtil.objectMdfeToXml(evento);
            xml = evento(configuracoes, xml, ConstantesMDFe.SERVICOS.CANCELAR, validar);

            return XmlMdfeUtil.xmlToObject(xml, br.com.swconsultoria.mdfe.schema_300.evCancMDFe.TRetEvento.class);

        } catch (JAXBException e) {
            throw new MdfeException(e.getMessage());
        }
    }

    static br.com.swconsultoria.mdfe.schema_300.evIncCondutorMDF.TRetEvento incluirCondutor(ConfiguracoesMDFeInterface configuracoes, br.com.swconsultoria.mdfe.schema_300.evIncCondutorMDF.TEvento evento, boolean validar) throws MdfeException {

        try {
            String xml = XmlMdfeUtil.objectMdfeToXml(evento);
            xml = evento(configuracoes, xml, ConstantesMDFe.SERVICOS.INCLUIR_CONDUTOR, validar);
            return XmlMdfeUtil.xmlToObject(xml, br.com.swconsultoria.mdfe.schema_300.evIncCondutorMDF.TRetEvento.class);
        } catch (JAXBException e) {
            throw new MdfeException(e.getMessage());
        }
    }

    static br.com.swconsultoria.mdfe.schema_300.evEncMDFe.TRetEvento encerrar(ConfiguracoesMDFeInterface configuracoes, br.com.swconsultoria.mdfe.schema_300.evEncMDFe.TEvento evento, boolean validar) throws MdfeException {
        try {
            String xml = XmlMdfeUtil.objectMdfeToXml(evento);
            xml = evento(configuracoes, xml, ConstantesMDFe.SERVICOS.ENCERRAR, validar);

            return XmlMdfeUtil.xmlToObject(xml, br.com.swconsultoria.mdfe.schema_300.evEncMDFe.TRetEvento.class);

        } catch (JAXBException e) {
            throw new MdfeException(e.getMessage());
        }
    }


    private static String evento(ConfiguracoesMDFeInterface configuracoes, String xml, String tipoEvento, boolean valida) throws MdfeException {

        try {

            xml = AssinaturaMdfe.assinar(configuracoes, xml, AssinaturaMdfe.EVENTO);

            if (valida) {
                String erros = ValidarMDFe.validaXml(configuracoes, xml, ConstantesMDFe.SERVICOS.EVENTO);

                if (!ObjetoMdfeUtil.isEmpty(erros)) {
                    throw new MdfeException("Erro Na Validação do Xml: " + erros);
                }
            }

            if (configuracoes.isLog()) {
                System.out.println("Xml EventoMdfe: " + xml);
            }

            OMElement ome = AXIOMUtil.stringToOM(xml);

            MDFeRecepcaoEventoStub.MdfeDadosMsg dadosMsg = new MDFeRecepcaoEventoStub.MdfeDadosMsg();
            dadosMsg.setExtraElement(ome);
            MDFeRecepcaoEventoStub.MdfeCabecMsg mdfeCabecMsg = new MDFeRecepcaoEventoStub.MdfeCabecMsg();
            mdfeCabecMsg.setCUF(String.valueOf(configuracoes.getEstado().getCodigoIbge()));
            mdfeCabecMsg.setVersaoDados(ConstantesMDFe.VERSAO.V3_00);

            MDFeRecepcaoEventoStub.MdfeCabecMsgE mdfeCabecMsgE = new MDFeRecepcaoEventoStub.MdfeCabecMsgE();
            mdfeCabecMsgE.setMdfeCabecMsg(mdfeCabecMsg);

            MDFeRecepcaoEventoStub stub = new MDFeRecepcaoEventoStub(WebServiceMdfeUtil.getUrl(configuracoes, ConstantesMDFe.MDFE, ConstantesMDFe.SERVICOS.EVENTO));

            return stub.mdfeRecepcaoEvento(dadosMsg, mdfeCabecMsgE).getExtraElement().toString();

        } catch (RemoteException | XMLStreamException e) {
            throw new MdfeException(e.getMessage());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy