@GET
  @Produces(MediaType.APPLICATION_JSON)
  @Path("obtenerTodosMensajes")
  public RespuestaMensajeDTO obtenerTodosMensajes() {

    RespuestaMensajeDTO respuestaMensajeDTO = new RespuestaMensajeDTO(0, "OK");
    try {

      List<Mensaje> mensajes = mensajeBeanLocal.encontrarTodos(Mensaje.class, "id", "ASC");

      for (Mensaje m : mensajes) {
        MensajeDTO mensajeDTO =
            new MensajeDTO(
                m.getId(),
                m.getUsrdesde(),
                m.getUsrdesde(),
                m.getTexto(),
                m.getStatus(),
                FORMATO_FECHA.format(m.getFecha()));
        respuestaMensajeDTO.getMensajes().add(mensajeDTO);
      }

    } catch (Exception e) {
      respuestaMensajeDTO.setCodigo(1);
      respuestaMensajeDTO.setMensaje("Hubo un error en el sistema");
      e.printStackTrace();
    }
    return respuestaMensajeDTO;
  }
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  @Path("mensajesRecibidosPorUsuario/{usuario}")
  public RespuestaMensajeDTO mensajesRecibidosPorUsuario(@PathParam("usuario") long usuario) {

    RespuestaMensajeDTO respuestaMensajeDTO = new RespuestaMensajeDTO(0, "OK");

    UsuarioDTO usuarioDTO = new UsuarioDTO();
    usuarioDTO.setCodigo(usuario);

    Client client = ClientBuilder.newClient();
    WebTarget targetMensaje = client.target(servicioObtenerUsuarioSesion);
    RespuestaSeguridadDTO resu =
        targetMensaje
            .request("application/json")
            .post(
                Entity.entity(usuarioDTO, MediaType.APPLICATION_JSON), RespuestaSeguridadDTO.class);

    System.out.println("RESU " + resu.getCodigo());
    System.out.println("RESU " + resu.getMensaje());

    if (resu.getCodigo() == 0) {

      try {

        List<Mensaje> mensajes = mensajeBeanLocal.mensajesRecibidosPorUsuario(usuario);

        for (Mensaje m : mensajes) {
          MensajeDTO mensajeDTO =
              new MensajeDTO(
                  m.getId(),
                  m.getUsrdesde(),
                  m.getUsrpara(),
                  m.getTexto(),
                  m.getStatus(),
                  FORMATO_FECHA.format(m.getFecha()));
          respuestaMensajeDTO.getMensajes().add(mensajeDTO);
        }

        if (mensajes.size() > 0) {

          // Inicio Otorga Puntos por Fidelizacion
          try {
            RegistrarPuntosDTO registrarPuntosDTO = new RegistrarPuntosDTO();
            registrarPuntosDTO.setCodigoUsuario(Long.toString(usuario));
            registrarPuntosDTO.setServicio("mensajesRecibidosPorUsuario");

            client = ClientBuilder.newClient();
            targetMensaje = client.target(servicioRegistrarServicio);
            RespuestaDTO resuDTO =
                targetMensaje
                    .request("application/json")
                    .post(
                        Entity.entity(registrarPuntosDTO, MediaType.APPLICATION_JSON),
                        RespuestaDTO.class);
          } catch (Exception exc) {
            System.out.println("Fidelizacion deshabilidado");
          }
          // Fin Otorga Puntos por Fidelizacion
        }

      } catch (Exception e) {

        respuestaMensajeDTO.setCodigo(1);
        respuestaMensajeDTO.setMensaje("Hubo un error en el sistema");
        e.printStackTrace();
      }
    } else {
      respuestaMensajeDTO.setCodigo(10);
      respuestaMensajeDTO.setMensaje(resu.getMensaje());
    }

    return respuestaMensajeDTO;
  }
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  @Path("encontrarMensajePorId/{cod}/{id}")
  public RespuestaMensajeDTO encontrarMensajePorId(
      @PathParam("cod") long codUsuario, @PathParam("id") long idMensaje) {

    RespuestaMensajeDTO respuestaMensajeDTO = new RespuestaMensajeDTO(0, "OK");

    UsuarioDTO usuarioDTO = new UsuarioDTO();
    usuarioDTO.setCodigo(codUsuario);

    Client client = ClientBuilder.newClient();
    WebTarget targetMensaje = client.target(servicioObtenerUsuarioSesion);
    RespuestaSeguridadDTO resu =
        targetMensaje
            .request("application/json")
            .post(
                Entity.entity(usuarioDTO, MediaType.APPLICATION_JSON), RespuestaSeguridadDTO.class);

    System.out.println("RESU " + resu.getCodigo());
    System.out.println("RESU " + resu.getMensaje());

    if (resu.getCodigo() == 0) {

      try {

        Mensaje m = mensajeBeanLocal.encontrarPorId(Mensaje.class, new Long(idMensaje));

        if (m != null) {
          MensajeDTO mensajeDTO =
              new MensajeDTO(
                  m.getId(),
                  m.getUsrdesde(),
                  m.getUsrpara(),
                  m.getTexto(),
                  m.getStatus(),
                  FORMATO_FECHA.format(m.getFecha()));
          respuestaMensajeDTO.getMensajes().add(mensajeDTO);

          // Inicio Otorga Puntos por Fidelizacion
          try {
            RegistrarPuntosDTO registrarPuntosDTO = new RegistrarPuntosDTO();
            registrarPuntosDTO.setCodigoUsuario(Long.toString(codUsuario));
            registrarPuntosDTO.setServicio("encontrarMensajePorId");

            client = ClientBuilder.newClient();
            targetMensaje = client.target(servicioRegistrarServicio);
            RespuestaDTO resuDTO =
                targetMensaje
                    .request("application/json")
                    .post(
                        Entity.entity(registrarPuntosDTO, MediaType.APPLICATION_JSON),
                        RespuestaDTO.class);
          } catch (Exception exc) {
            System.out.println("Fidelizacion deshabilidado");
          }
          // Fin Otorga Puntos por Fidelizacion

        } else {
          respuestaMensajeDTO.setCodigo(2);
          respuestaMensajeDTO.setMensaje("El Mensaje No existe en el sistema");
        }

      } catch (Exception e) {
        respuestaMensajeDTO.setCodigo(1);
        respuestaMensajeDTO.setMensaje("Hubo un error en el sistema");
        e.printStackTrace();
      }
    } else {

      respuestaMensajeDTO.setCodigo(10);
      respuestaMensajeDTO.setMensaje(resu.getMensaje());
    }

    return respuestaMensajeDTO;
  }