110 lines
3.6 KiB
Java
110 lines
3.6 KiB
Java
package com.rjconsultores.ventaboletos.service.impl;
|
||
|
||
import java.util.Date;
|
||
import java.util.List;
|
||
|
||
import org.codehaus.jettison.json.JSONObject;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
|
||
import com.rjconsultores.ventaboletos.dao.ConstanteDAO;
|
||
import com.rjconsultores.ventaboletos.dao.SapDAO;
|
||
import com.rjconsultores.ventaboletos.entidad.Constante;
|
||
import com.rjconsultores.ventaboletos.entidad.Empresa;
|
||
import com.rjconsultores.ventaboletos.entidad.FechamentoCntcorrente;
|
||
import com.rjconsultores.ventaboletos.rest.IntegracaoSapRest;
|
||
import com.rjconsultores.ventaboletos.service.SapService;
|
||
import com.rjconsultores.ventaboletos.vo.integracao.FechamentoCntCorrenteVO;
|
||
|
||
@Service("sapService")
|
||
public class SapServiceImpl implements SapService{
|
||
|
||
private static final String INTEGRACAO_SAP_URL = "INTEGRACAO_SAP_URL";
|
||
private static final String INTEGRACAO_SAP_CREDENCIAL = "INTEGRACAO_SAP_CREDENCIAL";
|
||
|
||
@Autowired
|
||
private SapDAO sapDAO;
|
||
|
||
@Autowired
|
||
private ConstanteDAO constanteDAO;
|
||
|
||
@Override
|
||
public List<FechamentoCntcorrente> obtenerTodos() {
|
||
return sapDAO.obtenerTodos();
|
||
}
|
||
|
||
@Override
|
||
public FechamentoCntcorrente obtenerID(Long id) {
|
||
return sapDAO.obtenerID(id);
|
||
}
|
||
|
||
@Override
|
||
@Transactional
|
||
public FechamentoCntcorrente suscribir(FechamentoCntcorrente entidad) {
|
||
return sapDAO.suscribir(entidad);
|
||
}
|
||
|
||
@Override
|
||
@Transactional
|
||
public FechamentoCntcorrente actualizacion(FechamentoCntcorrente entidad) {
|
||
return sapDAO.actualizacion(entidad);
|
||
}
|
||
|
||
@Override
|
||
@Transactional
|
||
public void borrar(FechamentoCntcorrente entidad) {
|
||
sapDAO.borrar(entidad);
|
||
|
||
}
|
||
|
||
@Override
|
||
public List<FechamentoCntCorrenteVO> obtenerTodosParaRemessa(Empresa empresa, Date dataDe, Date dataAte, Boolean reenviar) {
|
||
return sapDAO.obtenerTodosParaRemessa(empresa, dataDe, dataAte, reenviar);
|
||
}
|
||
|
||
@Override
|
||
public int remessa(List<FechamentoCntCorrenteVO> fechamentos) throws Exception {
|
||
try {
|
||
int contador = 0;
|
||
IntegracaoSapRest integracaoSapRest = IntegracaoSapRest.getInstance();
|
||
String credenciais = buscaConstante(INTEGRACAO_SAP_CREDENCIAL);
|
||
String url = buscaConstante(INTEGRACAO_SAP_URL);
|
||
|
||
for (FechamentoCntCorrenteVO fechamentoCntcorrente : fechamentos) {
|
||
if( fechamentoCntcorrente.isEnviar()) {
|
||
JSONObject integrado = integracaoSapRest.enviarIntegracaoSap(fechamentoCntcorrente, url, credenciais);
|
||
|
||
if(integrado == null) {
|
||
throw new RuntimeException("Erro no envio/retorno da requisicao para integra<72><61>o SAP");
|
||
}else if(integrado.getString("Integration").equals("S")) {
|
||
FechamentoCntcorrente registro = obtenerID(fechamentoCntcorrente.getFechamentocntcorrenteId());
|
||
registro.setIntegradoSap(true);
|
||
suscribir(registro);
|
||
contador++;
|
||
}else if(integrado.getString("Integration").equals("E")) {
|
||
throw new RuntimeException("Retorno enviado pelo SAP: \r\n"+integrado.getString("Message"));
|
||
}
|
||
}
|
||
}
|
||
|
||
return contador;
|
||
}catch(RuntimeException re){
|
||
throw re;
|
||
}catch(Exception e){
|
||
throw e;
|
||
}
|
||
}
|
||
|
||
private String buscaConstante(String constanteString) {
|
||
Constante constante = constanteDAO.buscarPorNomeConstante(constanteString);
|
||
|
||
if( constante == null || constante.getValorconstante() == null || constante.getValorconstante().isEmpty() ) {
|
||
throw new RuntimeException("Constante "+constanteString+" n<>o cadastrada");
|
||
}
|
||
|
||
return constante.getValorconstante();
|
||
}
|
||
|
||
}
|