@RequestMapping(value = "create", method = RequestMethod.GET) public ModelAndView initCreateFormTmp(Model model, HttpServletRequest request) throws Exception, ParseException { logger.debug("Crear un envio de ordenes de examen"); String urlValidacion = ""; try { urlValidacion = seguridadService.validarLogin(request); // si la url esta vacia significa que la validación del login fue exitosa if (urlValidacion.isEmpty()) urlValidacion = seguridadService.validarAutorizacionUsuario( request, ConstantsSecurity.SYSTEM_CODE, false); } catch (Exception e) { e.printStackTrace(); urlValidacion = "404"; } ModelAndView mav = new ModelAndView(); if (urlValidacion.isEmpty()) { mav.setViewName("tomaMx/sendOrders"); long idUsuario = seguridadService.obtenerIdUsuario(request); List<EntidadesAdtvas> entidadesAdtvases = entidadAdmonService.getAllEntidadesAdtvas(); List<TipoMx> tipoMxList = catalogosService.getTipoMuestra(); List<Laboratorio> laboratorioList; // Si es usuario a nivel central se cargan todas las unidades asociados al SILAIS y municipio if (seguridadService.esUsuarioNivelCentral(idUsuario, ConstantsSecurity.SYSTEM_CODE)) { laboratorioList = envioMxService.getAllLaboratorios(); } else { laboratorioList = envioMxService.getLaboratorios((int) idUsuario, ConstantsSecurity.SYSTEM_CODE); } mav.addObject("entidades", entidadesAdtvases); mav.addObject("tipoMuestra", tipoMxList); mav.addObject("laboratorios", laboratorioList); } else { mav.setViewName(urlValidacion); } return mav; }
@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(); } }