コード例 #1
0
ファイル: PDFController.java プロジェクト: chasethenag420/sbs
  @RequestMapping(value = "/bankStatement", method = RequestMethod.GET)
  public ModelAndView getCreditForm(HttpServletRequest request) {

    String username = (String) request.getSession().getAttribute("username");
    if (username == null || username.equals("")) {
      ModelAndView modelView = new ModelAndView();
      modelView.addObject("error", "User does not exit. Enter valid username");

      modelView.setViewName("login");
      return modelView;
    }
    Users user = userDao.getUserByUserName(username);
    List<String> accountNumbers = new ArrayList<String>();
    for (Account account : accountService.getAccounts(user.getUserId())) {
      accountNumbers.add("" + account.getAccountNumber());
    }
    //

    // logs debug message
    if (logger.isDebugEnabled()) {
      logger.debug("bank statement Screen is executed!");
    }
    ModelAndView modelView = new ModelAndView();

    modelView.addObject("form", new TransactionForm());
    modelView.addObject("accounts", accountNumbers);
    modelView.setViewName("bankStatement");
    return modelView;
  }
コード例 #2
0
ファイル: PDFController.java プロジェクト: chasethenag420/sbs
  @RequestMapping(value = "/downloadPDF")
  public ModelAndView downloadPDF(
      @Valid @ModelAttribute("form") TransactionForm form,
      BindingResult result,
      HttpServletRequest request,
      HttpServletResponse response)
      throws IOException {

    String username = (String) request.getSession().getAttribute("username");
    Users user = userDao.getUserByUserName(username);

    // convert the string dates
    transactionInputValidator.setRequest(request);
    transactionInputValidator.validate(form, result);

    if (result.hasErrors()) {
      ModelAndView modelView = new ModelAndView();
      modelView.addObject("form", form);
      if (logger.isDebugEnabled()) {
        logger.debug(
            "************************************************8error while creating download");
      }

      List<String> accountNumbers = new ArrayList<String>();
      for (Account account : accountService.getAccounts(user.getUserId())) {
        accountNumbers.add("" + account.getAccountNumber());
      }
      modelView.addObject("form", form);
      modelView.addObject("accounts", accountNumbers);
      modelView.setViewName("bankStatement");
      return modelView;
    } else {
      List<Transactions> transactions =
          transactionDao.getTransactionsBetweenDates(
              Integer.parseInt(form.getToAccount()), form.getFromDate(), form.getToDate());
      final ServletContext servletContext = request.getSession().getServletContext();
      final File tempDirectory =
          (File) servletContext.getAttribute("javax.servlet.context.tempdir");
      final String temperotyFilePath = tempDirectory.getAbsolutePath();

      String fileName = "bankstatement.pdf";
      response.setContentType("application/pdf");
      response.setHeader("Content-disposition", "attachment; filename=" + fileName);

      try {

        CreatePDF.createPDF(
            temperotyFilePath + "\\" + fileName, transactions, form.getToAccount(), user);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos = convertPDFToByteArrayOutputStream(temperotyFilePath + "\\" + fileName);
        OutputStream os = response.getOutputStream();
        baos.writeTo(os);
        os.flush();
      } catch (Exception e1) {
        e1.printStackTrace();
        ModelAndView modelView = new ModelAndView();
        modelView.addObject(
            "successfulMessage", "Successful! PDF is created successful for transactions.");
        modelView.setViewName("successful");
        return modelView;
      }

      ModelAndView modelView = new ModelAndView();
      modelView.addObject(
          "successfulMessage", "Successful! PDF is created successful for transactions.");
      modelView.setViewName("successful");
      return modelView;
    }
  }