Ejemplo n.º 1
0
  /**
   * Expects the following servlet params: type,format,session,start,end,criterion,groupByCriterion
   *
   * <p>Type, format, criterion, and groupByCriterion are taken from the enums: ReportType,
   * ReportFormat, and ReportingCriterion. GroupByCriterion can also have the value "None". Start
   * and end are in milliseconds. Session is a session id string.
   */
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    User user = this.getUserFromSession(Param.session.getRaw(req));
    if (user == null) {
      throw new RuntimeException("User was null");
    }

    /* Parse all params
     */
    final String reportType = Param.type.get(req);
    final ReportFormat format = ReportFormat.valueOf(Param.format.get(req));
    final long start = Long.parseLong(Param.start.get(req));
    final long end = Long.parseLong(Param.end.get(req));
    final Period period = new Period(start, end);
    final ReportingCriterion criterion = ReportingCriterion.valueOf(Param.criterion.get(req));
    final Units displayUnits = Units.getDefaultDisplayUnits();

    ReportingCriterion groupByCriterion = null;
    // GroupByCriterion can optionally have value "None"; check for it
    String groupByParam = req.getParameter(Param.groupByCriterion.name());
    if (groupByParam != null && !groupByParam.equalsIgnoreCase("NONE")) {
      groupByCriterion = ReportingCriterion.valueOf(Param.groupByCriterion.get(req));
    }
    LOG.info(
        String.format(
            "Params: type:%s format:%s period:%s" + "criterion:%s groupBy:%s",
            reportType, format, period, criterion, groupByCriterion));

    /* Set servlet response content type, etc, based upon report format.
     */
    setContentTypeHeader(res, format, Param.type.get(req));

    /* Generate the report and send it thru the OutputStream
     */
    if (user.isSystemAdmin()) {
      // Generate report of all accounts
      ReportGenerator.getInstance()
          .generateReport(
              reportType,
              format,
              period,
              criterion,
              groupByCriterion,
              displayUnits,
              res.getOutputStream(),
              null);
    } else if (user.isAccountAdmin()) {
      String accountId;
      try {
        accountId = user.getAccount().getAccountNumber();
      } catch (AuthException aex) {
        throw new RuntimeException("Auth failed");
      }
      // Generate report of this account only
      ReportGenerator.getInstance()
          .generateReport(
              reportType,
              format,
              period,
              criterion,
              groupByCriterion,
              displayUnits,
              res.getOutputStream(),
              accountId);
    } else {
      throw new RuntimeException("Only admins and account owners can generate reports");
    }
  }