예제 #1
0
  /**
   * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
   *     javax.servlet.http.HttpServletResponse)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    String obsId = request.getParameter("obsId");
    String view = request.getParameter("view");
    String viewType = request.getParameter("viewType");

    HttpSession session = request.getSession();

    if (obsId == null || obsId.length() == 0) {
      session.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.null");
      return;
    }
    if (!Context.hasPrivilege(OpenmrsConstants.PRIV_VIEW_OBS)) {
      session.setAttribute(
          WebConstants.OPENMRS_ERROR_ATTR, "Privilege required: " + OpenmrsConstants.PRIV_VIEW_OBS);
      session.setAttribute(
          WebConstants.OPENMRS_LOGIN_REDIRECT_HTTPSESSION_ATTR,
          request.getRequestURI() + "?" + request.getQueryString());
      response.sendRedirect(request.getContextPath() + "/login.htm");
      return;
    }

    Obs complexObs = Context.getObsService().getComplexObs(Integer.valueOf(obsId), view);
    ComplexData cd = complexObs.getComplexData();
    Object data = cd.getData();

    if ("download".equals(viewType)) {
      response.setHeader("Content-Disposition", "attachment; filename=" + cd.getTitle());
      response.setHeader("Pragma", "no-cache");
    }

    if (data instanceof byte[]) {
      ByteArrayInputStream stream = new ByteArrayInputStream((byte[]) data);
      OpenmrsUtil.copyFile(stream, response.getOutputStream());
    } else if (RenderedImage.class.isAssignableFrom(data.getClass())) {
      RenderedImage img = (RenderedImage) data;
      String[] parts = cd.getTitle().split(".");
      String extension = "jpg"; // default extension
      if (parts.length > 0) {
        extension = parts[parts.length - 1];
      }

      ImageIO.write(img, extension, response.getOutputStream());
    } else if (InputStream.class.isAssignableFrom(data.getClass())) {
      InputStream stream = (InputStream) data;
      OpenmrsUtil.copyFile(stream, response.getOutputStream());
      stream.close();
    } else {
      throw new ServletException(
          "Couldn't serialize complex obs data for obsId=" + obsId + " of type " + data.getClass());
    }
  }
  @Test
  @Verifies(
      value = "shouldSetTheValueComplexOfObsIfConceptIsComplex",
      method = "createObs(Concept concept, Object value, Date datetime, String accessionNumber)")
  public void createObs_shouldSetTheValueComplexOfObsIfConceptIsComplex() {

    ComplexData complexData = new ComplexData("test", null);
    Concept c = new Concept();
    ConceptDatatype cd = new ConceptDatatype();
    cd.setUuid(HtmlFormEntryConstants.COMPLEX_UUID);
    c.setDatatype(cd);
    Obs o = HtmlFormEntryUtil.createObs(c, complexData, null, null);
    Assert.assertEquals(o.getValueComplex(), complexData.getTitle());
  }