@Transactional(readOnly = true)
 public UserDetails loadUserByUsername(String usuarioId) throws UsernameNotFoundException {
   Usuario u = usuarioService.getUsuarioByUsuario(usuarioId);
   if (u == null) {
     throw new UsernameNotFoundException("Usuario inexistente");
   }
   return new org.springframework.security.core.userdetails.User(
       u.getUsuarioId(), u.getPassword(), true, true, true, true, getGrantedAuthorities(u));
 }
  /**
   * Agrega una notificación de tipo Rotavirus
   *
   * @param json con los datos de la ficha
   * @param request con datos de autenticación
   * @return DaNotificacion agregada
   * @throws Exception
   */
  public DaNotificacion guardarNotificacion(String json, HttpServletRequest request)
      throws Exception {

    logger.debug("Guardando Notificacion");
    DaNotificacion noti = new DaNotificacion();
    Integer personaId = null;
    Integer codSilaisAtencion = null;
    Integer codUnidadAtencion = null;
    String urgente = null;
    JsonObject jObjectJson = new Gson().fromJson(json, JsonObject.class);
    if (jObjectJson.get("personaId") != null
        && !jObjectJson.get("personaId").getAsString().isEmpty()) {
      personaId = jObjectJson.get("personaId").getAsInt();
    }
    if (jObjectJson.get("codSilaisAtencion") != null
        && !jObjectJson.get("codSilaisAtencion").getAsString().isEmpty()) {
      codSilaisAtencion = jObjectJson.get("codSilaisAtencion").getAsInt();
    }
    if (jObjectJson.get("codUnidadAtencion") != null
        && !jObjectJson.get("codUnidadAtencion").getAsString().isEmpty()) {
      codUnidadAtencion = jObjectJson.get("codUnidadAtencion").getAsInt();
    }
    if (jObjectJson.get("urgente") != null && !jObjectJson.get("urgente").getAsString().isEmpty()) {
      urgente = jObjectJson.get("urgente").getAsString();
    }

    if (personaId != null) {
      SisPersona persona = personaService.getPersona(personaId);
      noti.setPersona(persona);
      noti.setFechaRegistro(new Timestamp(new Date().getTime()));
      noti.setCodSilaisAtencion(entidadAdmonService.getSilaisByCodigo(codSilaisAtencion));
      noti.setCodUnidadAtencion(unidadesService.getUnidadByCodigo(codUnidadAtencion));
      long idUsuario = seguridadService.obtenerIdUsuario(request);
      noti.setUsuarioRegistro(usuarioService.getUsuarioById((int) idUsuario));
      noti.setCodTipoNotificacion(catalogoService.getTipoNotificacion("TPNOTI|RTV"));
      noti.setMunicipioResidencia(persona.getMunicipioResidencia());
      noti.setDireccionResidencia(persona.getDireccionResidencia());
      noti.setUrgente(catalogoService.getRespuesta(urgente));

      daNotificacionService.addNotification(noti);
      return noti;
    } else {
      throw new Exception();
    }
  }
  @RequestMapping(
      value = "agregarEnvioOrdenes",
      method = RequestMethod.POST,
      consumes = MediaType.APPLICATION_JSON_VALUE)
  protected void agregarEnvioOrdenes(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String json = "";
    String resultado = "";
    String strOrdenes = "";
    String idEnvio = "";
    int cantOrdenes = 0;
    int cantOrdenesProc = 0;
    try {
      BufferedReader br =
          new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF8"));
      json = br.readLine();
      // Recuperando Json enviado desde el cliente
      JsonObject jsonpObject = new Gson().fromJson(json, JsonObject.class);
      strOrdenes = jsonpObject.get("ordenes").toString();
      cantOrdenes = jsonpObject.get("cantOrdenes").getAsInt();
      String nombreTransporta = jsonpObject.get("nombreTransporta").getAsString();
      Float temperaturaTermo = jsonpObject.get("temperaturaTermo").getAsFloat();
      String laboratorioProcedencia = jsonpObject.get("laboratorioProcedencia").getAsString();

      long idUsuario = seguridadService.obtenerIdUsuario(request);
      Usuarios usuario = usuarioService.getUsuarioById((int) idUsuario);
      // Se obtiene estado enviado a laboratorio
      Laboratorio labDestino = envioMxService.getLaboratorio(laboratorioProcedencia);

      DaEnvioMx envioOrden = new DaEnvioMx();

      envioOrden.setUsarioRegistro(usuario);
      envioOrden.setFechaHoraEnvio(new Timestamp(new Date().getTime()));
      envioOrden.setNombreTransporta(nombreTransporta);
      envioOrden.setTemperaturaTermo(temperaturaTermo);
      // envioOrden.setTiempoEspera(CalcularDiferenciaHorasFechas());
      envioOrden.setLaboratorioDestino(labDestino);

      EstadoMx estadoMx = catalogosService.getEstadoMx("ESTDMX|ENV");

      try {
        idEnvio = envioMxService.addEnvioOrden(envioOrden);
      } catch (Exception ex) {
        resultado = messageSource.getMessage("msg.sending.error.add", null, null);
        resultado = resultado + ". \n " + ex.getMessage();
        ex.printStackTrace();
      }
      if (!idEnvio.isEmpty()) {
        envioOrden.setIdEnvio(idEnvio);

        JsonObject jObjectOrdenes = new Gson().fromJson(strOrdenes, JsonObject.class);
        for (int i = 0; i < cantOrdenes; i++) {
          String idSoli = jObjectOrdenes.get(String.valueOf(i)).getAsString();
          DaTomaMx tomaMxUpd = tomaMxService.getTomaMxById(idSoli);
          tomaMxUpd.setEnvio(envioOrden);
          tomaMxUpd.setEstadoMx(estadoMx);
          tomaMxService.updateTomaMx(tomaMxUpd);
          List<DaSolicitudDx> solicitudDxList =
              tomaMxService.getRutinasByIdMX(tomaMxUpd.getIdTomaMx());
          for (DaSolicitudDx solicitudDx : solicitudDxList) {
            solicitudDx.setLabProcesa(envioOrden.getLaboratorioDestino());
            tomaMxService.updateSolicitudDx(solicitudDx);
          }
          cantOrdenesProc++;
        }
      }
    } catch (Exception ex) {
      logger.error(ex.getMessage(), ex);
      ex.printStackTrace();
      resultado = messageSource.getMessage("msg.sending.error", null, null);
      resultado = resultado + ". \n " + ex.getMessage();

    } finally {
      Map<String, String> map = new HashMap<String, String>();
      map.put("idEnvio", idEnvio);
      map.put("cantOrdenes", String.valueOf(cantOrdenes));
      map.put("cantOrdenesProc", String.valueOf(cantOrdenesProc));
      map.put("mensaje", resultado);
      map.put("ordenes", strOrdenes);
      String jsonResponse = new Gson().toJson(map);
      response.getOutputStream().write(jsonResponse.getBytes());
      response.getOutputStream().close();
    }
  }