diff --git a/pom.xml b/pom.xml index c299603fb..3a40cba9c 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 br.com.rjconsultores ModelWeb - 1.118.1 + 1.119.0 diff --git a/src/com/rjconsultores/ventaboletos/dao/RevenueConfigDAO.java b/src/com/rjconsultores/ventaboletos/dao/RevenueConfigDAO.java new file mode 100644 index 000000000..d2b271478 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/dao/RevenueConfigDAO.java @@ -0,0 +1,9 @@ +package com.rjconsultores.ventaboletos.dao; + +import com.rjconsultores.ventaboletos.entidad.RevenueConfig; + +public interface RevenueConfigDAO { + + public RevenueConfig obtenerRevenueConfigActivo(); + +} diff --git a/src/com/rjconsultores/ventaboletos/dao/hibernate/RevenueConfigHibernateDAO.java b/src/com/rjconsultores/ventaboletos/dao/hibernate/RevenueConfigHibernateDAO.java new file mode 100644 index 000000000..464f2f658 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/dao/hibernate/RevenueConfigHibernateDAO.java @@ -0,0 +1,39 @@ +package com.rjconsultores.ventaboletos.dao.hibernate; + +import java.util.List; + +import org.hibernate.Criteria; +import org.hibernate.SessionFactory; +import org.hibernate.criterion.Restrictions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Repository; + +import com.rjconsultores.ventaboletos.dao.RevenueConfigDAO; +import com.rjconsultores.ventaboletos.entidad.RevenueConfig; + +@Repository("revenueConfigDAO") +public class RevenueConfigHibernateDAO extends GenericHibernateDAO implements RevenueConfigDAO { + + @Autowired + public RevenueConfigHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) { + setSessionFactory(factory); + } + + @Override + @SuppressWarnings("unchecked") + public RevenueConfig obtenerRevenueConfigActivo() { + Criteria c = getSession().createCriteria(getPersistentClass()); + c.add(Restrictions.eq(ACTIVO, Boolean.TRUE)); + c.setMaxResults(1); + + List list = c.list(); + + if (list != null && !list.isEmpty()) { + return list.get(0); + } + + return null; + } + +} diff --git a/src/com/rjconsultores/ventaboletos/entidad/RevenueConfig.java b/src/com/rjconsultores/ventaboletos/entidad/RevenueConfig.java new file mode 100644 index 000000000..fc358d820 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/entidad/RevenueConfig.java @@ -0,0 +1,175 @@ +package com.rjconsultores.ventaboletos.entidad; + +import java.io.Serializable; +import java.util.Date; + +import javax.persistence.Basic; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.SequenceGenerator; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; + +@Entity +@SequenceGenerator(name = "REVENUE_CONFIG_SEQ", sequenceName = "REVENUE_CONFIG_SEQ", allocationSize = 1) +@Table(name = "REVENUE_CONFIG") +public class RevenueConfig implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + @Basic(optional = false) + @GeneratedValue(strategy = GenerationType.AUTO, generator = "REVENUE_CONFIG_SEQ") + @Column(name = "REVENUECONFIG_ID") + private Integer revenueconfigId; + + @Column(name = "INDUSACANALVENTA") + private Boolean indusacanalventa; + + @Column(name = "ACTIVO") + private Boolean activo; + + @Column(name = "FECMODIF") + @Temporal(TemporalType.TIMESTAMP) + private Date fecmodif; + + @Column(name = "USUARIO_ID") + private Integer usuarioId; + + @Column(name = "TOKEN") + private String token; + + @Column(name = "CONNECTOR_ID") + private String connectorId; + + @Column(name = "URL_PRICE") + private String urlPrice; + + @Column(name = "URL_PRICE_CANALVENDA") + private String urlPriceCanalVenda; + + @Column(name = "URL_MARKET") + private String urlMarket; + + @Column(name = "URL_SITE") + private String urlSite; + + @Column(name = "URL_ALERTA") + private String urlAlerta; + + @Column(name = "URL_GEOLOC") + private String urlGeoLoc; + + public Integer getRevenueconfigId() { + return revenueconfigId; + } + + public void setRevenueconfigId(Integer revenueconfigId) { + this.revenueconfigId = revenueconfigId; + } + + public Boolean getIndusacanalventa() { + return indusacanalventa; + } + + public void setIndusacanalventa(Boolean indusacanalventa) { + this.indusacanalventa = indusacanalventa; + } + + public Boolean getActivo() { + return activo; + } + + public void setActivo(Boolean activo) { + this.activo = activo; + } + + public Date getFecmodif() { + return fecmodif; + } + + public void setFecmodif(Date fecmodif) { + this.fecmodif = fecmodif; + } + + public Integer getUsuarioId() { + return usuarioId; + } + + public void setUsuarioId(Integer usuarioId) { + this.usuarioId = usuarioId; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + public String getConnectorId() { + return connectorId; + } + + public void setConnectorId(String connectorId) { + this.connectorId = connectorId; + } + + public String getUrlPrice() { + return urlPrice; + } + + public void setUrlPrice(String urlPrice) { + this.urlPrice = urlPrice; + } + + public String getUrlPriceCanalVenda() { + return urlPriceCanalVenda; + } + + public void setUrlPriceCanalVenda(String urlPriceCanalVenda) { + this.urlPriceCanalVenda = urlPriceCanalVenda; + } + + public String getUrlMarket() { + return urlMarket; + } + + public void setUrlMarket(String urlMarket) { + this.urlMarket = urlMarket; + } + + public String getUrlSite() { + return urlSite; + } + + public void setUrlSite(String urlSite) { + this.urlSite = urlSite; + } + + public String getUrlAlerta() { + return urlAlerta; + } + + public void setUrlAlerta(String urlAlerta) { + this.urlAlerta = urlAlerta; + } + + public String getUrlGeoLoc() { + return urlGeoLoc; + } + + public void setUrlGeoLoc(String urlGeoLoc) { + this.urlGeoLoc = urlGeoLoc; + } + + public static long getSerialversionuid() { + return serialVersionUID; + } + +} diff --git a/src/com/rjconsultores/ventaboletos/service/RevenueConfigService.java b/src/com/rjconsultores/ventaboletos/service/RevenueConfigService.java new file mode 100644 index 000000000..05b27c6f5 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/service/RevenueConfigService.java @@ -0,0 +1,13 @@ +package com.rjconsultores.ventaboletos.service; + +import com.rjconsultores.ventaboletos.entidad.RevenueConfig; +import com.rjconsultores.ventaboletos.exception.BusinessException; +import com.rjconsultores.ventaboletos.vo.revenue.RevenueGeoloc; + +public interface RevenueConfigService { + + public RevenueConfig obtenerRevenueConfigActivo(); + + public RevenueGeoloc pesquisarGeoloc(String endereco) throws BusinessException; + +} diff --git a/src/com/rjconsultores/ventaboletos/service/impl/RevenueConfigServiceImpl.java b/src/com/rjconsultores/ventaboletos/service/impl/RevenueConfigServiceImpl.java new file mode 100644 index 000000000..835c86063 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/service/impl/RevenueConfigServiceImpl.java @@ -0,0 +1,86 @@ +package com.rjconsultores.ventaboletos.service.impl; + +import java.lang.reflect.Type; +import java.util.List; + +import org.apache.commons.lang.StringUtils; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.json.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.zkoss.util.resource.Labels; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.rjconsultores.ventaboletos.dao.RevenueConfigDAO; +import com.rjconsultores.ventaboletos.entidad.RevenueConfig; +import com.rjconsultores.ventaboletos.exception.BusinessException; +import com.rjconsultores.ventaboletos.service.RevenueConfigService; +import com.rjconsultores.ventaboletos.vo.revenue.RevenueGeoloc; + +@Service("revenueConfigService") +public class RevenueConfigServiceImpl implements RevenueConfigService { + + private static Logger log = LogManager.getLogger(RevenueConfigService.class); + + @Autowired + private RevenueConfigDAO revenueConfigDAO; + + @Override + public RevenueConfig obtenerRevenueConfigActivo() { + return revenueConfigDAO.obtenerRevenueConfigActivo(); + } + + @Override + public RevenueGeoloc pesquisarGeoloc(String endereco) throws BusinessException { + RevenueGeoloc retorno = null; + try { + RevenueConfig revenueConfig = this.obtenerRevenueConfigActivo(); + verificarRevenueConfigGeoloc(revenueConfig); + + HttpPost request = new HttpPost(revenueConfig.getUrlGeoLoc()); + request.addHeader("token-access", revenueConfig.getToken()); + request.addHeader("connector-id", revenueConfig.getConnectorId()); + request.addHeader("Content-type", "application/json"); + + JSONObject payload = new JSONObject(); + payload.put("endereco", endereco); + HttpEntity entityPost = new StringEntity(payload.toString(), ContentType.APPLICATION_JSON); + request.setEntity(entityPost); + + CloseableHttpClient client = HttpClientBuilder.create().build(); + HttpResponse response = client.execute(request); + + if (response.getStatusLine().getStatusCode() == 200 && response.getEntity() != null) { + Type listType = new TypeToken>(){}.getType(); + String entity = EntityUtils.toString(response.getEntity()); + List retornos = new Gson().fromJson(entity, listType); + if (!retornos.isEmpty()) { + retorno = retornos.get(0); + } + } + } catch (BusinessException e) { + throw e; + } catch (Exception e) { + log.error(e.getMessage(), e); + throw new BusinessException(e.getMessage()); + } + return retorno; + } + + private void verificarRevenueConfigGeoloc(RevenueConfig revenueConfig) throws BusinessException { + if (revenueConfig == null || StringUtils.isBlank(revenueConfig.getUrlGeoLoc())) { + throw new BusinessException(Labels.getLabel("editarCatalogoDeParadaController.MSG.RevenueGeolocConfig.value")); + } + } + +} diff --git a/src/com/rjconsultores/ventaboletos/vo/revenue/RevenueGeoloc.java b/src/com/rjconsultores/ventaboletos/vo/revenue/RevenueGeoloc.java new file mode 100644 index 000000000..ea8468a07 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/vo/revenue/RevenueGeoloc.java @@ -0,0 +1,48 @@ +package com.rjconsultores.ventaboletos.vo.revenue; + +import java.math.BigDecimal; + +public class RevenueGeoloc { + + private String enderecoPesquisa; + private String enderecoCompleto; + private BigDecimal latitude; + private BigDecimal longitude; + + public String getEnderecoPesquisa() { + return enderecoPesquisa; + } + + public void setEnderecoPesquisa(String enderecoPesquisa) { + this.enderecoPesquisa = enderecoPesquisa; + } + + public String getEnderecoCompleto() { + return enderecoCompleto; + } + + public void setEnderecoCompleto(String enderecoCompleto) { + this.enderecoCompleto = enderecoCompleto; + } + + public BigDecimal getLatitude() { + return latitude; + } + + public void setLatitude(BigDecimal latitude) { + this.latitude = latitude; + } + + public BigDecimal getLongitude() { + return longitude; + } + + public void setLongitude(BigDecimal longitude) { + this.longitude = longitude; + } + + public boolean isLatitudeAndLongitude() { + return this.latitude != null && this.longitude != null; + } + +}