br.com.swconsultoria.mdfe.EnvioMdfe Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-mdfe Show documentation
Show all versions of java-mdfe Show documentation
API Java para consumo do WebService de MDFe
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.schema_300.enviMDFe.TEnviMDFe;
import br.com.swconsultoria.mdfe.schema_300.retEnviMDFe.TRetEnviMDFe;
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.MDFeRecepcao.MDFeRecepcaoStub;
import br.com.swconsultoria.mdfe.wsdl.MDFeRecepcao.MDFeRecepcaoStub.MdfeCabecMsg;
import br.com.swconsultoria.mdfe.wsdl.MDFeRecepcao.MDFeRecepcaoStub.MdfeCabecMsgE;
import br.com.swconsultoria.mdfe.wsdl.MDFeRecepcao.MDFeRecepcaoStub.MdfeDadosMsg;
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;
/**
* Classe Responsavel por Enviar o MDFe.
*
* @author Samuel Oliveira - [email protected] - www.samuelweb.com.br
* @author John
*/
class EnvioMdfe {
/**
* Metodo para Montar a Mdfe
*
* @param TEnviMDFe
* @return TEnviMDFe
* @throws MdfeException
*/
static TEnviMDFe montaMdfe(ConfiguracoesMDFeInterface config, TEnviMDFe enviMdfe, boolean validar) throws MdfeException {
try {
/**
* Assina o Xml
*/
String xml = AssinaturaMdfe.assinar(config, XmlMdfeUtil.objectMdfeToXml(enviMdfe), AssinaturaMdfe.MDFE);
/**
* Valida o Xml caso sejá selecionado True
*/
if (validar) {
String erros = ValidarMDFe.validaXml(config, xml, ConstantesMDFe.SERVICOS.ENVIO);
if (!ObjetoMdfeUtil.isEmpty(erros)) {
throw new MdfeException("Erro Na Validação do Xml: " + erros);
}
}
if (config.isLog()) {
System.out.println("MDFe Assinado: " + xml);
}
return XmlMdfeUtil.xmlToObject(xml, TEnviMDFe.class);
} catch (JAXBException e) {
throw new MdfeException(e.getMessage());
}
}
/**
* Metodo para Enviar a MDFe
*
* @param TEnviMDFe
* @return TRetEnviMDFe
* @throws MdfeException
*/
static TRetEnviMDFe enviarMdfe(ConfiguracoesMDFeInterface configuracoes, TEnviMDFe enviMdfe) throws MdfeException {
try {
OMElement ome = AXIOMUtil.stringToOM(XmlMdfeUtil.objectMdfeToXml(enviMdfe));
if (configuracoes.isLog()) {
System.out.println("MDFe para Envio: " + ome.toString());
}
MdfeDadosMsg dadosMsg = new MdfeDadosMsg();
dadosMsg.setExtraElement(ome);
MdfeCabecMsg mdfeCabecMsg = new MdfeCabecMsg();
/**
* Codigo do Estado.
*/
mdfeCabecMsg.setCUF(String.valueOf(configuracoes.getEstado().getCodigoIbge()));
/**
* Versao do XML
*/
mdfeCabecMsg.setVersaoDados(ConstantesMDFe.VERSAO.V3_00);
MdfeCabecMsgE mdfeCabecMsgE = new MdfeCabecMsgE();
mdfeCabecMsgE.setMdfeCabecMsg(mdfeCabecMsg);
MDFeRecepcaoStub stub = new MDFeRecepcaoStub(WebServiceMdfeUtil.getUrl(configuracoes, ConstantesMDFe.MDFE, ConstantesMDFe.SERVICOS.ENVIO));
return XmlMdfeUtil.xmlToObject(stub.mdfeRecepcaoLote(dadosMsg, mdfeCabecMsgE).getExtraElement().toString(), TRetEnviMDFe.class);
} catch (RemoteException | XMLStreamException | JAXBException e) {
throw new MdfeException(e.getMessage());
}
}
}