-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from shipping7/cancelar-objeto-plp#79
Possibilita cancelar um objeto da PLP pelo SDK
- Loading branch information
Showing
14 changed files
with
488 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version=0.0.18-BETA | ||
version=0.0.19-BETA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/br/com/correios/api/postagem/CorreiosServicoPostagemAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package br.com.correios.api.postagem; | ||
|
||
import com.google.common.base.Optional; | ||
|
||
import br.com.correios.api.postagem.cliente.ClienteEmpresa; | ||
import br.com.correios.api.postagem.cliente.ContratoEmpresa; | ||
import br.com.correios.api.postagem.plp.DocumentoPlp; | ||
|
||
public interface CorreiosServicoPostagemAPI { | ||
|
||
Optional<ClienteEmpresa> buscaCliente(ContratoEmpresa contratoEmpresa); | ||
|
||
Optional<DocumentoPlp> buscaDocumentoPlp(Long plpId); | ||
|
||
Optional<DocumentoPlp> buscaDocumentoPlp(Long plpId, String etiqueta); | ||
|
||
boolean cancelaObjetoDaPlp(Long plpId, String numeroEtiqueta); | ||
} |
113 changes: 113 additions & 0 deletions
113
src/main/java/br/com/correios/api/postagem/SoapCorreiosServicoPostagemAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package br.com.correios.api.postagem; | ||
|
||
import static java.lang.String.format; | ||
|
||
import com.google.common.base.Optional; | ||
|
||
import br.com.correios.api.converter.Converter; | ||
import br.com.correios.api.exception.CorreiosServicoSoapException; | ||
import br.com.correios.api.postagem.cliente.ClienteEmpresa; | ||
import br.com.correios.api.postagem.cliente.ContratoEmpresa; | ||
import br.com.correios.api.postagem.exception.CorreiosPostagemAutenticacaoException; | ||
import br.com.correios.api.postagem.plp.DocumentoPlp; | ||
import br.com.correios.api.postagem.webservice.CorreiosClienteApi; | ||
import br.com.correios.api.postagem.xml.Correioslog; | ||
import br.com.correios.api.postagem.xml.XmlPlpParser; | ||
import br.com.correios.credentials.CorreiosCredenciais; | ||
import br.com.correios.webservice.postagem.AutenticacaoException; | ||
import br.com.correios.webservice.postagem.ClienteERP; | ||
import br.com.correios.webservice.postagem.Exception_Exception; | ||
import br.com.correios.webservice.postagem.SigepClienteException; | ||
|
||
class SoapCorreiosServicoPostagemAPI implements CorreiosServicoPostagemAPI { | ||
|
||
private final CorreiosClienteApi clienteApi; | ||
private final CorreiosCredenciais credenciais; | ||
private final Converter<ClienteERP, ClienteEmpresa> clienteEmpresaConverter; | ||
private final XmlPlpParser xmlPlpParser; | ||
private final Converter<Correioslog, Optional<DocumentoPlp>> documentoPlpConverter; | ||
|
||
SoapCorreiosServicoPostagemAPI(CorreiosCredenciais credenciais, | ||
CorreiosClienteApi clienteApi, | ||
Converter<ClienteERP, ClienteEmpresa> clienteEmpresaConverter, | ||
XmlPlpParser xmlPlpParser, | ||
Converter<Correioslog, Optional<DocumentoPlp>> documentoPlpConverter) { | ||
|
||
this.credenciais = credenciais; | ||
this.clienteApi = clienteApi; | ||
this.clienteEmpresaConverter = clienteEmpresaConverter; | ||
this.xmlPlpParser = xmlPlpParser; | ||
this.documentoPlpConverter = documentoPlpConverter; | ||
} | ||
|
||
@Override | ||
public Optional<ClienteEmpresa> buscaCliente(ContratoEmpresa contratoEmpresa) { | ||
try { | ||
ClienteERP clienteRetornadoDosCorreios = clienteApi.getCorreiosWebService().buscaCliente(contratoEmpresa.getContrato(), contratoEmpresa.getCartaoDePostagem(), credenciais.getUsuario(), credenciais.getSenha()); | ||
|
||
return Optional.fromNullable(clienteRetornadoDosCorreios) | ||
.transform(clienteEmpresaConverter::convert) | ||
.transform(Optional::of) | ||
.or(Optional.<ClienteEmpresa>absent()); | ||
|
||
} catch (AutenticacaoException e) { | ||
throw new CorreiosPostagemAutenticacaoException(format("Ocorreu um erro ao se autenticar nos correios com a seguinte credencial: %s", credenciais)); | ||
} catch (SigepClienteException e) { | ||
throw new CorreiosServicoSoapException(format("Ocorreu um erro ao chamar o servico com as informacoes de cliente %s", contratoEmpresa), e); | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<DocumentoPlp> buscaDocumentoPlp(Long plpId) { | ||
try { | ||
String xmlPlp = clienteApi.getCorreiosWebService().solicitaXmlPlp(plpId, credenciais.getUsuario(), credenciais.getSenha()); | ||
|
||
boolean xmlPlpDosCorreiosEstaValido = xmlPlp != null && !xmlPlp.isEmpty(); | ||
|
||
if (xmlPlpDosCorreiosEstaValido) { | ||
return xmlPlpParser.convert(xmlPlp) | ||
.transform(documentoPlpConverter::convert) | ||
.or(Optional.<DocumentoPlp>absent()); | ||
} | ||
} catch (AutenticacaoException e) { | ||
throw new CorreiosPostagemAutenticacaoException(format("Ocorreu um erro ao se autenticar nos correios com a seguinte credencial: %s", credenciais)); | ||
} catch (SigepClienteException e) { | ||
throw new CorreiosServicoSoapException(format("Ocorreu um erro ao chamar o servico com o PLP de id %d", plpId), e); | ||
} | ||
|
||
return Optional.absent(); | ||
} | ||
|
||
@Override | ||
public Optional<DocumentoPlp> buscaDocumentoPlp(Long plpId, String etiqueta) { | ||
try { | ||
String xmlPlp = clienteApi.getCorreiosWebService().solicitaPLP(plpId, etiqueta, credenciais.getUsuario(), credenciais.getSenha()); | ||
|
||
boolean xmlPlpDosCorreiosEstaValido = xmlPlp != null && !xmlPlp.isEmpty(); | ||
|
||
if (xmlPlpDosCorreiosEstaValido) { | ||
return xmlPlpParser.convert(xmlPlp) | ||
.transform(documentoPlpConverter::convert) | ||
.or(Optional.<DocumentoPlp>absent()); | ||
} | ||
} catch (AutenticacaoException e) { | ||
throw new CorreiosPostagemAutenticacaoException(format("Ocorreu um erro ao se autenticar nos correios com a seguinte credencial: %s", credenciais)); | ||
} catch (SigepClienteException e) { | ||
throw new CorreiosServicoSoapException(format("Ocorreu um erro ao chamar o servico com o PLP de id %d", plpId), e); | ||
} | ||
|
||
return Optional.absent(); | ||
} | ||
|
||
@Override | ||
public boolean cancelaObjetoDaPlp(Long plpId, String numeroEtiqueta) { | ||
try { | ||
return clienteApi.getCorreiosWebService().cancelarObjeto(plpId, numeroEtiqueta, credenciais.getUsuario(), credenciais.getSenha()); | ||
} catch (AutenticacaoException e) { | ||
throw new CorreiosPostagemAutenticacaoException(format("Ocorreu um erro ao se autenticar nos correios com a seguinte credencial: %s", credenciais)); | ||
} catch (Exception_Exception | SigepClienteException e) { | ||
throw new CorreiosServicoSoapException(format("Ocorreu um erro ao chamar o servico com o PLP de id %d, etiqueta %s", plpId, numeroEtiqueta), e); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/br/com/correios/api/postagem/exception/ObjetoPlpFoiPostadoException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package br.com.correios.api.postagem.exception; | ||
|
||
public class ObjetoPlpFoiPostadoException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = -3328326631056241491L; | ||
|
||
public ObjetoPlpFoiPostadoException(String mensagem, Exception exceptionCancelamento) { | ||
super(mensagem, exceptionCancelamento); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...br/com/correios/api/postagem/exception/ObjetoPlpInexistenteOuJaFoiCanceladoException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package br.com.correios.api.postagem.exception; | ||
|
||
public class ObjetoPlpInexistenteOuJaFoiCanceladoException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 8708529032418284246L; | ||
|
||
public ObjetoPlpInexistenteOuJaFoiCanceladoException(String mensagem, Exception exception) { | ||
super(mensagem, exception); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/br/com/correios/api/postagem/exception/ObjetoPlpJaFoiCanceladoException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package br.com.correios.api.postagem.exception; | ||
|
||
public class ObjetoPlpJaFoiCanceladoException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = -6940355031846418834L; | ||
|
||
public ObjetoPlpJaFoiCanceladoException(String mensagem, Exception exceptionCancelamento) { | ||
super(mensagem, exceptionCancelamento); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.