private String tomaMxToJson(List<DaTomaMx> tomaMxList) {
    String jsonResponse = "";
    Map<Integer, Object> mapResponse = new HashMap<Integer, Object>();
    Integer indice = 0;
    for (DaTomaMx tomaMx : tomaMxList) {
      Map<String, String> map = new HashMap<String, String>();
      map.put("idTomaMx", tomaMx.getIdTomaMx());
      // map.put("fechaHoraOrden",DateToString(orden.getFechaHSolicitud(),"dd/MM/yyyy hh:mm:ss a"));
      map.put(
          "fechaTomaMx", DateUtil.DateToString(tomaMx.getFechaHTomaMx(), "dd/MM/yyyy hh:mm:ss a"));
      map.put("estadoMx", tomaMx.getEstadoMx().getValor());
      map.put("codSilais", tomaMx.getIdNotificacion().getCodSilaisAtencion().getNombre());
      map.put("codUnidadSalud", tomaMx.getIdNotificacion().getCodUnidadAtencion().getNombre());
      map.put(
          "separadaMx",
          (tomaMx.getMxSeparada() != null ? (tomaMx.getMxSeparada() ? "Si" : "No") : ""));
      map.put("tipoMuestra", tomaMx.getCodTipoMx().getNombre());

      if (tomaMx.getIdNotificacion().getUrgente() != null) {
        map.put("urgente", tomaMx.getIdNotificacion().getUrgente().getValor());
      } else {
        map.put("urgente", "--");
      }

      // map.put("tipoExamen",orden.getCodDx().getNombre());
      // Si hay fecha de inicio de sintomas se muestra
      Date fechaInicioSintomas =
          tomaMx
              .getIdNotificacion()
              .getFechaInicioSintomas(); // envioMxService.getFechaInicioSintomas(tomaMx.getIdNotificacion().getIdNotificacion());
      if (fechaInicioSintomas != null)
        map.put("fechaInicioSintomas", DateUtil.DateToString(fechaInicioSintomas, "dd/MM/yyyy"));
      else map.put("fechaInicioSintomas", " ");

      // hospitalizado
      String[] arrayHosp = {"13", "17", "11", "16", "10", "12"};
      boolean hosp = false;

      if (tomaMx.getCodUnidadAtencion() != null) {
        int h =
            Arrays.binarySearch(
                arrayHosp, String.valueOf(tomaMx.getCodUnidadAtencion().getTipoUnidad()));
        hosp = h > 0;
      }

      if (hosp) {
        map.put("hospitalizado", messageSource.getMessage("lbl.yes", null, null));
      } else {
        map.put("hospitalizado", messageSource.getMessage("lbl.no", null, null));
      }

      // Si hay persona
      if (tomaMx.getIdNotificacion().getPersona() != null) {
        /// se obtiene el nombre de la persona asociada a la ficha
        String nombreCompleto = "";
        nombreCompleto = tomaMx.getIdNotificacion().getPersona().getPrimerNombre();
        if (tomaMx.getIdNotificacion().getPersona().getSegundoNombre() != null)
          nombreCompleto =
              nombreCompleto + " " + tomaMx.getIdNotificacion().getPersona().getSegundoNombre();
        nombreCompleto =
            nombreCompleto + " " + tomaMx.getIdNotificacion().getPersona().getPrimerApellido();
        if (tomaMx.getIdNotificacion().getPersona().getSegundoApellido() != null)
          nombreCompleto =
              nombreCompleto + " " + tomaMx.getIdNotificacion().getPersona().getSegundoApellido();
        map.put("persona", nombreCompleto);
        // Se calcula la edad
        int edad = calcularEdadAnios(tomaMx.getIdNotificacion().getPersona().getFechaNacimiento());
        map.put("edad", String.valueOf(edad));
        // se obtiene el sexo
        map.put("sexo", tomaMx.getIdNotificacion().getPersona().getSexo().getValor());
        if (edad > 12 && tomaMx.getIdNotificacion().getPersona().isSexoFemenino()) {
          map.put(
              "embarazada",
              envioMxService.estaEmbarazada(tomaMx.getIdNotificacion().getIdNotificacion()));
        } else map.put("embarazada", "--");
      } else {
        map.put("persona", " ");
        map.put("edad", " ");
        map.put("sexo", " ");
        map.put("embarazada", "--");
      }
      // se arma estructura de diagnósticos o estudios
      List<DaSolicitudDx> solicitudDxList =
          envioMxService.getSolicitudesDxByIdTomaMx(tomaMx.getIdTomaMx());
      Map<Integer, Object> mapSolicitudesList = new HashMap<Integer, Object>();
      Map<String, String> mapSolicitud = new HashMap<String, String>();
      if (solicitudDxList.size() > 0) {
        int subIndice = 0;
        for (DaSolicitudDx solicitudDx : solicitudDxList) {
          mapSolicitud.put("nombre", solicitudDx.getCodDx().getNombre());
          mapSolicitud.put("tipo", "Rutina");
          mapSolicitud.put(
              "fechaSolicitud",
              DateUtil.DateToString(solicitudDx.getFechaHSolicitud(), "dd/MM/yyyy hh:mm:ss a"));
          subIndice++;
          mapSolicitudesList.put(subIndice, mapSolicitud);
          mapSolicitud = new HashMap<String, String>();
        }
        map.put("solicitudes", new Gson().toJson(mapSolicitudesList));
      } else {
        List<DaSolicitudEstudio> solicitudEstudios =
            envioMxService.getSolicitudesEstudioByIdTomaMx(tomaMx.getIdTomaMx());
        int subIndice = 0;
        for (DaSolicitudEstudio solicitudEstudio : solicitudEstudios) {
          mapSolicitud.put("nombre", solicitudEstudio.getTipoEstudio().getNombre());
          mapSolicitud.put("tipo", "Estudio");
          mapSolicitud.put(
              "fechaSolicitud",
              DateUtil.DateToString(
                  solicitudEstudio.getFechaHSolicitud(), "dd/MM/yyyy hh:mm:ss a"));
          subIndice++;
          mapSolicitudesList.put(subIndice, mapSolicitud);
          mapSolicitud = new HashMap<String, String>();
        }
        map.put("solicitudes", new Gson().toJson(mapSolicitudesList));
      }

      mapResponse.put(indice, map);
      indice++;
    }
    jsonResponse = new Gson().toJson(mapResponse);
    UnicodeEscaper escaper = UnicodeEscaper.above(127);
    return escaper.translate(jsonResponse);
  }
  @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();
    }
  }