diff --git a/src/java/com/rjconsultores/ventaboletos/rest/LoginRS.java b/src/java/com/rjconsultores/ventaboletos/rest/LoginRS.java new file mode 100644 index 000000000..5024469b5 --- /dev/null +++ b/src/java/com/rjconsultores/ventaboletos/rest/LoginRS.java @@ -0,0 +1,27 @@ +package com.rjconsultores.ventaboletos.rest; + +import javax.annotation.security.RolesAllowed; +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import com.rjconsultores.ventaboletos.rest.bean.ParamsLogin; +import com.rjconsultores.ventaboletos.rest.service.LoginService; +import com.rjconsultores.ventaboletos.web.utilerias.spring.AppContext; + +@Path("/logar") +public class LoginRS { + + @POST + @Consumes({ MediaType.APPLICATION_JSON }) + @RolesAllowed("EMBARCADA") + @Produces({ MediaType.APPLICATION_JSON }) + public Response login(ParamsLogin params) { + LoginService service = (LoginService) AppContext.getApplicationContext().getBean("loginService"); + return Response.ok(service.logar(params), MediaType.APPLICATION_JSON).build(); + } + +} diff --git a/src/java/com/rjconsultores/ventaboletos/rest/bean/ParamsLogin.java b/src/java/com/rjconsultores/ventaboletos/rest/bean/ParamsLogin.java new file mode 100644 index 000000000..e6e8086d1 --- /dev/null +++ b/src/java/com/rjconsultores/ventaboletos/rest/bean/ParamsLogin.java @@ -0,0 +1,29 @@ +package com.rjconsultores.ventaboletos.rest.bean; + +import org.codehaus.jackson.annotate.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParamsLogin { + private String usuario; + private String senha; + + public ParamsLogin() { + + } + + public String getUsuario() { + return usuario; + } + + public void setUsuario(String usuario) { + this.usuario = usuario; + } + + public String getSenha() { + return senha; + } + + public void setSenha(String senha) { + this.senha = senha; + } +} diff --git a/src/java/com/rjconsultores/ventaboletos/rest/returns/LoginRetorno.java b/src/java/com/rjconsultores/ventaboletos/rest/returns/LoginRetorno.java new file mode 100644 index 000000000..e6d64977e --- /dev/null +++ b/src/java/com/rjconsultores/ventaboletos/rest/returns/LoginRetorno.java @@ -0,0 +1,66 @@ +package com.rjconsultores.ventaboletos.rest.returns; + +import java.util.ArrayList; +import java.util.List; + +public class LoginRetorno { + private String status; + private String mensagem; + + private Integer usuarioId; + private String nombusuario; + private Integer puntoventaId; + private List empresasId = new ArrayList(0); + + public LoginRetorno() { + + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getMensagem() { + return mensagem; + } + + public void setMensagem(String mensagem) { + this.mensagem = mensagem; + } + + public Integer getUsuarioId() { + return usuarioId; + } + + public void setUsuarioId(Integer usuarioId) { + this.usuarioId = usuarioId; + } + + public String getNombusuario() { + return nombusuario; + } + + public void setNombusuario(String nombusuario) { + this.nombusuario = nombusuario; + } + + public Integer getPuntoventaId() { + return puntoventaId; + } + + public void setPuntoventaId(Integer puntoventaId) { + this.puntoventaId = puntoventaId; + } + + public List getEmpresasId() { + return empresasId; + } + + public void setEmpresasId(List empresasId) { + this.empresasId = empresasId; + } +} diff --git a/src/java/com/rjconsultores/ventaboletos/rest/service/LoginService.java b/src/java/com/rjconsultores/ventaboletos/rest/service/LoginService.java new file mode 100644 index 000000000..5d1d2ae30 --- /dev/null +++ b/src/java/com/rjconsultores/ventaboletos/rest/service/LoginService.java @@ -0,0 +1,68 @@ +package com.rjconsultores.ventaboletos.rest.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import com.rjconsultores.ventaboletos.dao.UsuarioDAO; +import com.rjconsultores.ventaboletos.entidad.Usuario; +import com.rjconsultores.ventaboletos.entidad.UsuarioEmpresa; +import com.rjconsultores.ventaboletos.entidad.UsuarioUbicacion; +import com.rjconsultores.ventaboletos.rest.bean.ParamsLogin; +import com.rjconsultores.ventaboletos.rest.returns.LoginRetorno; +import com.rjconsultores.ventaboletos.service.UsuarioEmpresaService; +import com.rjconsultores.ventaboletos.service.UsuarioUbicacionService; + +@Service("loginService") +public class LoginService { + @Autowired + private UsuarioDAO usuarioDAO; + + @Autowired + private UsuarioUbicacionService usuarioUbicacionService; + + @Autowired + private UsuarioEmpresaService usuarioEmpresaService; + + @Transactional + public LoginRetorno logar(ParamsLogin params) { + LoginRetorno retorno = new LoginRetorno(); + retorno.setStatus("SUCCESS"); + + if (params.getUsuario() == null || params.getUsuario().isEmpty() || + params.getSenha() == null || params.getSenha().isEmpty()) { + retorno.setStatus("ERROR"); + retorno.setMensagem("Parametros invalidos"); + + return retorno; + } + + Usuario usuario = usuarioDAO.buscarPeloNomeSenha(params.getUsuario(), params.getSenha()); + + if (usuario == null) { + retorno.setStatus("ERROR"); + retorno.setMensagem("Usuario/Senha invalido"); + } else { + retorno.setUsuarioId(usuario.getUsuarioId()); + retorno.setNombusuario(usuario.getNombUsuarioCompleto()); + + List ubicacion = usuarioUbicacionService.buscarPorUsuario(usuario); + + if (ubicacion != null && !ubicacion.isEmpty()) { + retorno.setPuntoventaId(ubicacion.get(0).getPuntoVenta().getPuntoventaId()); + } + + List empresas = usuarioEmpresaService.obtenerPorUsuario(usuario); + + if (empresas != null) { + for (UsuarioEmpresa empresa : empresas) { + retorno.getEmpresasId().add(empresa.getEmpresa().getEmpresaId()); + } + } + } + + return retorno; + } +}