Пример #1
0
  @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();
    }
  }