Rev 106 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 106 | espaco | 1 | package br.com.ec.domain.dto.anvisa; |
| 2 | |||
| 3 | import java.io.StringWriter; |
||
| 4 | import java.util.Date; |
||
| 5 | |||
| 6 | import javax.xml.bind.JAXBContext; |
||
| 7 | import javax.xml.bind.JAXBException; |
||
| 8 | import javax.xml.bind.Marshaller; |
||
| 9 | import javax.xml.bind.PropertyException; |
||
| 10 | import javax.xml.bind.annotation.XmlRootElement; |
||
| 11 | import javax.xml.transform.stream.StreamResult; |
||
| 12 | |||
| 195 | espaco | 13 | import br.com.ec.core.util.DataUtils; |
| 14 | import br.com.ec.core.util.VerificadorUtil; |
||
| 106 | espaco | 15 | import br.com.ec.domain.model.TransmissaoSngpc; |
| 16 | |||
| 17 | @XmlRootElement(name="mensagemSNGPC", namespace="urn:sngpc-schema") |
||
| 18 | public class MensagemSNGPC { |
||
| 19 | |||
| 20 | private Cabecalho cabecalho; |
||
| 21 | private Corpo corpo; |
||
| 22 | |||
| 23 | public Cabecalho getCabecalho() { |
||
| 24 | return cabecalho; |
||
| 25 | } |
||
| 26 | public void setCabecalho(Cabecalho cabecalho) { |
||
| 27 | this.cabecalho = cabecalho; |
||
| 28 | } |
||
| 29 | |||
| 30 | public Corpo getCorpo() { |
||
| 31 | return corpo; |
||
| 32 | } |
||
| 33 | public void setCorpo(Corpo corpo) { |
||
| 34 | this.corpo = corpo; |
||
| 35 | } |
||
| 36 | |||
| 37 | public String gerarXmlTransmissao(TransmissaoSngpc transmissaoSngpc) { |
||
| 38 | MensagemSNGPC mensagemSNGPC = new MensagemSNGPC(); |
||
| 39 | Cabecalho cabecalho = new Cabecalho(); |
||
| 40 | cabecalho.setCnpjEmissor("24884351000170"); |
||
| 41 | cabecalho.setCpfTransmissor("05864264423"); |
||
| 42 | |||
| 43 | Date dataInicial = transmissaoSngpc.getDataInicial(); |
||
| 44 | if (VerificadorUtil.naoEstaNulo(dataInicial)) { |
||
| 45 | cabecalho.setDataInicio(DataUtils.converterDataParaStringNoFormato(dataInicial, "yyyy-MM-dd")); //2018-07-02 |
||
| 46 | } |
||
| 47 | Date dataFinal = transmissaoSngpc.getDataFinal(); |
||
| 48 | if (VerificadorUtil.naoEstaNulo(dataFinal)) { |
||
| 49 | cabecalho.setDataFim(DataUtils.converterDataParaStringNoFormato(dataFinal, "yyyy-MM-dd")); //2018-07-04 |
||
| 50 | } |
||
| 51 | mensagemSNGPC.setCabecalho(cabecalho); |
||
| 52 | |||
| 53 | Corpo corpo = new Corpo(); |
||
| 54 | corpo.setMedicamentos(new Medicamentos()); |
||
| 55 | corpo.setInsumos(new Insumos()); |
||
| 56 | mensagemSNGPC.setCorpo(corpo); |
||
| 57 | |||
| 58 | final StringWriter out = new StringWriter(); |
||
| 59 | JAXBContext context = null; |
||
| 60 | try { |
||
| 61 | context = JAXBContext.newInstance(mensagemSNGPC.getClass()); |
||
| 62 | Marshaller marshaller = context.createMarshaller(); |
||
| 63 | marshaller.setProperty("jaxb.encoding", "ISO-8859-1"); |
||
| 64 | marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); |
||
| 65 | marshaller.marshal(mensagemSNGPC, new StreamResult(out)); |
||
| 66 | } catch (PropertyException e) { |
||
| 67 | e.printStackTrace(); |
||
| 68 | } catch (JAXBException e) { |
||
| 69 | e.printStackTrace(); |
||
| 70 | } |
||
| 71 | return out.toString().replace("ns2:", "").replace(":ns2", ""); |
||
| 72 | } |
||
| 73 | |||
| 74 | public static void main(String[] args) { |
||
| 75 | MensagemSNGPC mensagemSNGPC = new MensagemSNGPC(); |
||
| 76 | Cabecalho cabecalho = new Cabecalho(); |
||
| 77 | cabecalho.setCnpjEmissor("24884351000170"); |
||
| 78 | cabecalho.setCpfTransmissor("05864264423"); |
||
| 79 | cabecalho.setDataInicio("2018-07-02"); |
||
| 80 | cabecalho.setDataFim("2018-07-04"); |
||
| 81 | mensagemSNGPC.setCabecalho(cabecalho); |
||
| 82 | |||
| 83 | Corpo corpo = new Corpo(); |
||
| 84 | /* |
||
| 85 | Medicamentos medicamentos = new Medicamentos(); |
||
| 86 | EntradaMedicamentos entradaMedicamentos = new EntradaMedicamentos(); |
||
| 87 | MedicamentoEntrada medicamentoEntrada = new MedicamentoEntrada(); |
||
| 88 | MedicamentoEntrada medicamentoEntrada2 = new MedicamentoEntrada(); |
||
| 89 | ArrayList<MedicamentoEntrada> medicamentosEntrada = new ArrayList<MedicamentoEntrada>(); |
||
| 90 | medicamentosEntrada.add(medicamentoEntrada); |
||
| 91 | medicamentosEntrada.add(medicamentoEntrada2); |
||
| 92 | entradaMedicamentos.setMedicamentoEntrada(medicamentosEntrada); |
||
| 93 | medicamentos.setEntradaMedicamentos(entradaMedicamentos); |
||
| 94 | corpo.setMedicamentos(medicamentos); |
||
| 95 | */ |
||
| 96 | |||
| 97 | corpo.setMedicamentos(new Medicamentos()); |
||
| 98 | corpo.setInsumos(new Insumos()); |
||
| 99 | mensagemSNGPC.setCorpo(corpo); |
||
| 100 | |||
| 101 | final StringWriter out = new StringWriter(); |
||
| 102 | JAXBContext context = null; |
||
| 103 | try { |
||
| 104 | context = JAXBContext.newInstance(mensagemSNGPC.getClass()); |
||
| 105 | Marshaller marshaller = context.createMarshaller(); |
||
| 106 | marshaller.setProperty("jaxb.encoding", "ISO-8859-1"); |
||
| 107 | marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); |
||
| 108 | marshaller.marshal(mensagemSNGPC, new StreamResult(out)); |
||
| 109 | } catch (PropertyException e) { |
||
| 110 | e.printStackTrace(); |
||
| 111 | } catch (JAXBException e) { |
||
| 112 | e.printStackTrace(); |
||
| 113 | } |
||
| 114 | System.out.println(out.toString()); |
||
| 115 | |||
| 116 | /* |
||
| 117 | MensagemSNGPC mensagemSNGPC = new MensagemSNGPC(); |
||
| 118 | Cabecalho cabecalho = new Cabecalho(); |
||
| 119 | cabecalho.setCnpjEmissor("24884351000170"); |
||
| 120 | mensagemSNGPC.setCabecalho(cabecalho); |
||
| 121 | |||
| 122 | try { |
||
| 123 | JAXBContext context = JAXBContext.newInstance("br.com.caelum"); |
||
| 124 | Marshaller marshaller = context.createMarshaller(); |
||
| 125 | JAXBElement<MensagemSNGPC> element = new ObjectFactory().createCarro(mensagemSNGPC); |
||
| 126 | marshaller.marshal(element, System.out); |
||
| 127 | |||
| 128 | context = JAXBContext.newInstance("mensagemSNGPC"); |
||
| 129 | Marshaller m = context.createMarshaller(); |
||
| 130 | m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); |
||
| 131 | m.marshal(mensagemSNGPC, System.out); |
||
| 132 | } catch (JAXBException e) { |
||
| 133 | e.printStackTrace(); |
||
| 134 | } |
||
| 135 | */ |
||
| 136 | } |
||
| 137 | |||
| 138 | } |