/**
   * @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());
    }
  }
Exemple #2
0
  /**
   * This is an equivalent to a copy constructor. Creates a new copy of the given <code>obsToCopy
   * </code> with a null obs id
   *
   * @param obsToCopy The Obs that is going to be copied
   * @return a new Obs object with all the same attributes as the given obs
   */
  public static Obs newInstance(Obs obsToCopy) {
    Obs newObs =
        new Obs(
            obsToCopy.getPerson(),
            obsToCopy.getConcept(),
            obsToCopy.getObsDatetime(),
            obsToCopy.getLocation());

    newObs.setObsGroup(obsToCopy.getObsGroup());
    newObs.setAccessionNumber(obsToCopy.getAccessionNumber());
    newObs.setValueCoded(obsToCopy.getValueCoded());
    newObs.setValueDrug(obsToCopy.getValueDrug());
    newObs.setValueGroupId(obsToCopy.getValueGroupId());
    newObs.setValueDatetime(obsToCopy.getValueDatetime());
    newObs.setValueNumeric(obsToCopy.getValueNumeric());
    newObs.setValueModifier(obsToCopy.getValueModifier());
    newObs.setValueText(obsToCopy.getValueText());
    newObs.setComment(obsToCopy.getComment());
    newObs.setOrder(obsToCopy.getOrder());
    newObs.setEncounter(obsToCopy.getEncounter());
    newObs.setDateStarted(obsToCopy.getDateStarted());
    newObs.setDateStopped(obsToCopy.getDateStopped());
    newObs.setCreator(obsToCopy.getCreator());
    newObs.setDateCreated(obsToCopy.getDateCreated());
    newObs.setVoided(obsToCopy.getVoided());
    newObs.setVoidedBy(obsToCopy.getVoidedBy());
    newObs.setDateVoided(obsToCopy.getDateVoided());
    newObs.setVoidReason(obsToCopy.getVoidReason());

    newObs.setValueComplex(obsToCopy.getValueComplex());
    newObs.setComplexData(obsToCopy.getComplexData());

    if (obsToCopy.getGroupMembers() != null)
      for (Obs member : obsToCopy.getGroupMembers()) {
        // if the obs hasn't been saved yet, no need to duplicate it
        if (member.getObsId() == null) newObs.addGroupMember(member);
        else newObs.addGroupMember(Obs.newInstance(member));
      }

    return newObs;
  }