/**
   * method to stream the byte array to response object
   *
   * @param fileContents
   * @param fileName
   * @param fileContentType
   * @param response
   * @throws Exception
   */
  protected void streamToResponse(
      org.kuali.kra.proposaldevelopment.bo.AttachmentDataSource attachmentDataSource,
      HttpServletResponse response)
      throws Exception {
    byte[] xbts = attachmentDataSource.getContent();
    ByteArrayOutputStream baos = null;
    try {
      baos = new ByteArrayOutputStream(xbts.length);
      baos.write(xbts);

      WebUtils.saveMimeOutputStreamAsFile(
          response,
          attachmentDataSource.getContentType(),
          baos,
          attachmentDataSource.getFileName());

    } finally {
      try {
        if (baos != null) {
          baos.close();
          baos = null;
        }
      } catch (IOException ioEx) {
        LOG.error("Error while downloading attachment");
        throw new RuntimeException("IOException occurred while downloading attachment", ioEx);
      }
    }
  }
示例#2
0
  /**
   * Handy method to stream the byte array to response object
   *
   * @param attachmentDataSource
   * @param response
   * @throws Exception
   */
  public void streamToResponse(
      AttachmentDataSource attachmentDataSource, HttpServletResponse response) throws Exception {
    byte[] xbts = attachmentDataSource.getContent();

    ByteArrayOutputStream baos = null;
    if (xbts != null)
      try {
        baos = new ByteArrayOutputStream(xbts.length);
        baos.write(xbts);

        WebUtils.saveMimeOutputStreamAsFile(
            response,
            attachmentDataSource.getContentType(),
            baos,
            attachmentDataSource.getFileName());
      } finally {
        try {
          if (baos != null) {
            baos.close();
            baos = null;
          }
        } catch (IOException ioEx) {
          LOG.warn(ioEx.getMessage(), ioEx);
        }
      }
  }
示例#3
0
 private void downloadFile(
     ActionForm form,
     HttpServletRequest request,
     HttpServletResponse response,
     byte[] bytesToDownload,
     String fileName,
     String contentType)
     throws Exception {
   ByteArrayOutputStream baos = null;
   try {
     baos = new ByteArrayOutputStream(bytesToDownload.length);
     baos.write(bytesToDownload);
     WebUtils.saveMimeOutputStreamAsFile(response, contentType, baos, fileName);
   } finally {
     try {
       if (baos != null) {
         baos.close();
         baos = null;
       }
     } catch (IOException ioEx) {
       LOG.warn(ioEx.getMessage(), ioEx);
     }
   }
 }
  /**
   * Runs the reports or dump selected by the user using the BudgetConstructionDocumentReportMode to
   * help determine the various objects needed to actually build the report data and render the
   * report.
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  public ActionForward performReportDump(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {

    ReportRunnerForm reportRunnerForm = (ReportRunnerForm) form;
    String principalName = GlobalVariables.getUserSession().getPerson().getPrincipalId();

    int selectIndex = this.getSelectedLine(request);
    String reportModeName =
        reportRunnerForm
            .getBudgetConstructionDocumentReportModes()
            .get(selectIndex)
            .getReportModeName();

    Collection reportSet = new ArrayList();
    String jasperFileName = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    switch (selectIndex) {
      case 0:
        {
          jasperFileName = "BudgetAccountObjectDetail";
          SpringContext.getBean(BudgetConstructionDocumentAccountObjectDetailReportService.class)
              .updateDocumentAccountObjectDetailReportTable(
                  principalName,
                  reportRunnerForm.getDocumentNumber(),
                  reportRunnerForm.getUniversityFiscalYear(),
                  reportRunnerForm.getChartOfAccountsCode(),
                  reportRunnerForm.getAccountNumber(),
                  reportRunnerForm.getSubAccountNumber());
          reportSet =
              SpringContext.getBean(
                      BudgetConstructionDocumentAccountObjectDetailReportService.class)
                  .buildReports(principalName);
          break;
        }

      case 1:
        {
          jasperFileName = "BudgetAccountSalaryDetail";
          reportSet =
              SpringContext.getBean(BudgetConstructionAccountSalaryDetailReportService.class)
                  .buildReports(
                      reportRunnerForm.getUniversityFiscalYear(),
                      reportRunnerForm.getChartOfAccountsCode(),
                      reportRunnerForm.getAccountNumber(),
                      reportRunnerForm.getSubAccountNumber());
          break;
        }

      case 2:
        {
          jasperFileName = "BudgetAccountMonthlyDetail";
          reportSet =
              SpringContext.getBean(BudgetConstructionAccountMonthlyDetailReportService.class)
                  .buildReports(
                      reportRunnerForm.getDocumentNumber(),
                      reportRunnerForm.getUniversityFiscalYear(),
                      reportRunnerForm.getChartOfAccountsCode(),
                      reportRunnerForm.getAccountNumber(),
                      reportRunnerForm.getSubAccountNumber());
          break;
        }
      case 3:
        {
          return new ActionForward(
              buildReportExportForwardURL(
                  reportRunnerForm,
                  mapping,
                  BudgetConstructionReportMode.ACCOUNT_EXPORT.reportModeName),
              true);
        }

      case 4:
        {
          return new ActionForward(
              buildReportExportForwardURL(
                  reportRunnerForm,
                  mapping,
                  BudgetConstructionReportMode.FUNDING_EXPORT.reportModeName),
              true);
        }

      case 5:
        {
          return new ActionForward(
              buildReportExportForwardURL(
                  reportRunnerForm,
                  mapping,
                  BudgetConstructionReportMode.MONTHLY_EXPORT.reportModeName),
              true);
        }
    }

    if (reportSet.isEmpty()) {
      List<String> messageList = new ArrayList<String>();
      messageList.add(BCConstants.Report.MSG_REPORT_NO_DATA);
      SpringContext.getBean(BudgetConstructionReportsServiceHelper.class)
          .generatePdf(messageList, baos);
      WebUtils.saveMimeOutputStreamAsFile(
          response,
          ReportGeneration.PDF_MIME_TYPE,
          baos,
          jasperFileName + ReportGeneration.PDF_FILE_EXTENSION);
    } else {
      ResourceBundle resourceBundle =
          ResourceBundle.getBundle(
              BCConstants.Report.REPORT_MESSAGES_CLASSPATH, Locale.getDefault());
      Map<String, Object> reportData = new HashMap<String, Object>();
      reportData.put(JRParameter.REPORT_RESOURCE_BUNDLE, resourceBundle);

      SpringContext.getBean(ReportGenerationService.class)
          .generateReportToOutputStream(
              reportData,
              reportSet,
              BCConstants.Report.REPORT_TEMPLATE_CLASSPATH + jasperFileName,
              baos);
      WebUtils.saveMimeOutputStreamAsFile(
          response,
          ReportGeneration.PDF_MIME_TYPE,
          baos,
          jasperFileName + ReportGeneration.PDF_FILE_EXTENSION);
    }
    return null;
  }
  /**
   * This service is used to print the report.
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  public ActionForward print(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    ContractsGrantsAwardBalancesReportLookupForm awardBalancesReportLookupForm =
        (ContractsGrantsAwardBalancesReportLookupForm) form;

    String methodToCall = findMethodToCall(form, request);
    if (methodToCall.equalsIgnoreCase(KRADConstants.SEARCH_METHOD)) {
      GlobalVariables.getUserSession().removeObjectsByPrefix(KRADConstants.SEARCH_METHOD);
    }

    Lookupable kualiLookupable = awardBalancesReportLookupForm.getLookupable();
    if (kualiLookupable == null) {
      throw new RuntimeException("Lookupable is null.");
    }

    List<ResultRow> resultTable = new ArrayList<ResultRow>();

    // validate search parameters
    kualiLookupable.validateSearchParameters(awardBalancesReportLookupForm.getFields());

    // this is for 200 limit. turn it off for report.
    boolean isUnbounded = true;

    List<ContractsGrantsAwardBalancesReport> displayList =
        (List<ContractsGrantsAwardBalancesReport>)
            kualiLookupable.performLookup(awardBalancesReportLookupForm, resultTable, isUnbounded);
    Object sortIndexObject =
        GlobalVariables.getUserSession().retrieveObject(ArConstants.SORT_INDEX_SESSION_KEY);

    if (ObjectUtils.isNull(sortIndexObject) || sortIndexObject.toString() == "0") {
      sortIndexObject = "0";
    }
    // get sort property
    String sortPropertyName =
        getFieldNameForSorting(
            Integer.parseInt(sortIndexObject.toString()), "ContractsGrantsAwardBalancesReport");

    // sort list
    sortReport(displayList, sortPropertyName);

    // check field is valid for subtotal
    boolean isFieldSubtotalRequired =
        CGConstants.ReportsConstants.awardBalancesReportSubtotalFieldsList.contains(
            sortPropertyName);
    Map<String, KualiDecimal> subTotalMap = new HashMap<String, KualiDecimal>();

    if (isFieldSubtotalRequired) {
      subTotalMap = buildSubTotalMap(displayList, sortPropertyName);
    }

    // build report
    ContractsGrantsReportDataHolder awardBalancesReportDataHolder =
        new ContractsGrantsReportDataHolder();
    List<ContractsGrantsAwardBalancesReportDetailDataHolder> details =
        awardBalancesReportDataHolder.getDetails();

    for (ContractsGrantsAwardBalancesReport awardBalancesReportEntry : displayList) {
      ContractsGrantsAwardBalancesReportDetailDataHolder reportDetail =
          new ContractsGrantsAwardBalancesReportDetailDataHolder();
      // set report data
      setReportDate(awardBalancesReportEntry, reportDetail);

      if (isFieldSubtotalRequired) {
        // set sortedFieldValue for grouping in the report
        reportDetail.setSortedFieldValue(
            getPropertyValue(awardBalancesReportEntry, sortPropertyName));
        reportDetail.setDisplaySubtotal(true);
        // set subTotal from subTotalMap
        reportDetail.setSubTotal(
            subTotalMap
                .get(getPropertyValue(awardBalancesReportEntry, sortPropertyName))
                .bigDecimalValue());
      } else {
        // set this to empty string for not displaying subtotal
        reportDetail.setDisplaySubtotal(false);
      }
      details.add(reportDetail);
    }
    awardBalancesReportDataHolder.setDetails(details);

    // build search criteria for report
    buildReportForSearchCriteia(
        awardBalancesReportDataHolder.getSearchCriteria(),
        awardBalancesReportLookupForm.getFieldsForLookup());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    String reportFileName =
        SpringContext.getBean(ContractsGrantsAwardBalancesReportService.class)
            .generateReport(awardBalancesReportDataHolder, baos);
    WebUtils.saveMimeOutputStreamAsFile(
        response,
        ReportGeneration.PDF_MIME_TYPE,
        baos,
        reportFileName + ReportGeneration.PDF_FILE_EXTENSION);
    return null;
  }
  /** Generates the Budget Report and returns pdf. */
  public ActionForward performReport(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    OrganizationReportSelectionForm organizationReportSelectionForm =
        (OrganizationReportSelectionForm) form;
    String principalId = GlobalVariables.getUserSession().getPerson().getPrincipalId();

    BudgetConstructionReportMode reportMode =
        BudgetConstructionReportMode.getBudgetConstructionReportModeByName(
            organizationReportSelectionForm.getReportMode());
    if (!storeCodeSelections(organizationReportSelectionForm, reportMode, principalId)) {
      return mapping.findForward(KFSConstants.MAPPING_BASIC);
    }

    // validate threshold settings if needed
    if (reportMode == BudgetConstructionReportMode.REASON_STATISTICS_REPORT
        || reportMode == BudgetConstructionReportMode.REASON_SUMMARY_REPORT
        || reportMode == BudgetConstructionReportMode.SALARY_SUMMARY_REPORT) {
      if (!this.validThresholdSettings(
          organizationReportSelectionForm.getBudgetConstructionReportThresholdSettings())) {
        return mapping.findForward(KFSConstants.MAPPING_BASIC);
      }
    }

    // for report exports foward to export action to display formatting screen
    if (reportMode.export) {
      String exportUrl = this.buildReportExportForwardURL(organizationReportSelectionForm, mapping);
      return new ActionForward(exportUrl, true);
    }

    // build report data and populate report objects for rendering
    Collection reportSet =
        buildReportData(
            reportMode,
            organizationReportSelectionForm.getUniversityFiscalYear(),
            principalId,
            organizationReportSelectionForm.isReportConsolidation(),
            organizationReportSelectionForm.getBudgetConstructionReportThresholdSettings());

    // build pdf and stream back
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // output the report or a message if empty
    if (reportSet.isEmpty()) {
      List<String> messageList = new ArrayList<String>();
      messageList.add(BCConstants.Report.MSG_REPORT_NO_DATA);
      SpringContext.getBean(BudgetConstructionReportsServiceHelper.class)
          .generatePdf(messageList, baos);
      WebUtils.saveMimeOutputStreamAsFile(
          response,
          ReportGeneration.PDF_MIME_TYPE,
          baos,
          reportMode.jasperFileName + ReportGeneration.PDF_FILE_EXTENSION);
    } else {
      ResourceBundle resourceBundle =
          ResourceBundle.getBundle(
              BCConstants.Report.REPORT_MESSAGES_CLASSPATH, Locale.getDefault());
      Map<String, Object> reportData = new HashMap<String, Object>();
      reportData.put(JRParameter.REPORT_RESOURCE_BUNDLE, resourceBundle);

      SpringContext.getBean(ReportGenerationService.class)
          .generateReportToOutputStream(
              reportData,
              reportSet,
              BCConstants.Report.REPORT_TEMPLATE_CLASSPATH + reportMode.jasperFileName,
              baos);
      WebUtils.saveMimeOutputStreamAsFile(
          response,
          ReportGeneration.PDF_MIME_TYPE,
          baos,
          reportMode.jasperFileName + ReportGeneration.PDF_FILE_EXTENSION);
    }
    return null;
  }