@RequestMapping(value = "/download", method = RequestMethod.GET)
  public void downloadStatement(HttpSession session, HttpServletResponse response, Model model)
      throws IOException {
    String uniqId = session.getAttribute("uniqueid").toString();
    StatementGenerator.statementbyuniqid(uniqId, session);
    ExternalUser extUser = databaseConnector.getExternalUserByUniqId(uniqId);
    TempTransactions transactionObj = new TempTransactions();
    transactionObj.setBalance(extUser.getBalance());
    logger.info("Current Balance" + transactionObj.getBalance());
    float amount = transactionObj.getTransactionAmount();
    float currentBalance = transactionObj.getBalance();
    logger.info("balance :", currentBalance);
    logger.info("account number ", transactionObj.getAccountno());
    // credit amount from current account balance

    transactionObj.setUniqId(uniqId);
    transactionObj.setDescription("transferred amount: " + amount);
    transactionObj.setTransactionType("tranfer");
    transactionObj.setBalance(currentBalance - amount);

    extUser.setBalance(currentBalance - amount);

    Transactions temp = new Transactions();
    temp.setBalance(transactionObj.getBalance());
    model.addAttribute("debitOp", temp);
    model.addAttribute("creditOp", temp);
    model.addAttribute("checkAccBal", temp.getBalance());
    model.addAttribute("transferOp", transactionObj);
    model.addAttribute("paymerchantOp", transactionObj);
    List<Transactions> obj = displaytransaction(session);
    model.addAttribute("transactionOp", obj);
    // get absolute path of the application
    ServletContext context = session.getServletContext();

    String realContextPath = context.getRealPath("/");
    String fullpath = realContextPath + "/statement/" + uniqId + "_statement.pdf";
    //  System.out.println("aPath = " +realContextPath);
    // String filePath="Statement.pdf";
    // construct the complete absolute path of the file
    //  String fullPath = realContextPath+filePath;
    System.out.println(fullpath);

    File downloadFile = new File(fullpath);
    FileInputStream inputStream = new FileInputStream(downloadFile);

    // get MIME type of the file
    String mimeType = context.getMimeType(fullpath);
    if (mimeType == null) {
      // set to binary type if MIME mapping not found
      mimeType = "application/pdf";
    }
    System.out.println("MIME type: " + mimeType);

    // set content attributes for the response
    response.setContentType(mimeType);
    response.setContentLength((int) downloadFile.length());

    // set headers for the response
    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
    response.setHeader(headerKey, headerValue);

    OutputStream outStream = response.getOutputStream();

    byte[] buffer = new byte[4096];
    int bytesRead = -1;

    // write bytes read from the input stream into the output stream
    while ((bytesRead = inputStream.read(buffer)) != -1) {
      outStream.write(buffer, 0, bytesRead);
    }

    inputStream.close();
    outStream.close();

    //
    //					//ServletOutputStream out = response.getOutputStream();
    //					FileOutputStream fos = new FileOutputStream(downloadFile);
    //						System.out.println("Adding " + downloadFile.getName());
    //
    //						// Get the file
    //						FileInputStream fis = null;
    //						try {
    //							fis = new FileInputStream(downloadFile);
    //
    //						} catch (FileNotFoundException fnfe) {
    //							// If the file does not exists, write an error entry instead of
    //							// file
    //							// contents
    //							fos.write(("ERROR could not find file " + downloadFile.getName())
    //									.getBytes());
    //							fos.close();
    //							System.out.println("Couldfind file "
    //									+ downloadFile.getAbsolutePath());
    //						}
    //
    //						BufferedInputStream fif = new BufferedInputStream(fis);
    //
    //						// Write the contents of the file
    //						int data = 0;
    //						while ((data = fif.read()) != -1) {
    //							fos.write(data);
    //						}
    //						fif.close();
    //
    //						fos.close();
    System.out.println("Finished Downloading file " + downloadFile.getName());
    return;
    // return "redirect:extUserHomePage";
  }
  @RequestMapping(value = "/pay_merchant", method = RequestMethod.POST)
  public String paymerchantPageAction(
      @ModelAttribute("paymerchantOp") TempTransactions transactionObj,
      Model model,
      HttpSession session)
      throws IOException {
    logger.info("Inside pay merchant op POST");

    /** To display user profile */
    UserInfo UI = new UserInfo();
    DatabaseConnectors dbcon = new DatabaseConnectors();
    UI = dbcon.getUserInfoByUniqId((String) session.getAttribute("uniqueid"));
    String uniqueID = (String) session.getAttribute("uniqueid");
    ExternalUser extUser = databaseConnector.getExternalUserByUniqId(uniqueID);
    String utype = null;
    String str1 = (String) session.getAttribute("uniqueid");
    System.out.println(str1);
    String str2 = str1.substring(0, 2);

    if (str2.equals("ei")) {
      utype = "Single User";
    } else if (str2.equals("em")) {
      utype = "Merchant";
    } else if (str2.equals("ir")) {
      utype = "Internal User";
    } else if (str2.equals("im")) {
      utype = "Manager";
    } else if (str2.equals("admin")) {
      utype = "Administrator";
    }

    model.addAttribute("firstName", UI.getFirstName());
    model.addAttribute("lastName", UI.getLastName());
    model.addAttribute("Username", UI.getUsername());
    model.addAttribute("email", UI.getEmailId());
    model.addAttribute("accountno", extUser.getAccountno());
    model.addAttribute("streetAddress", UI.getAddress());
    model.addAttribute("city", UI.getCity());
    model.addAttribute("state", UI.getState());
    model.addAttribute("country", UI.getCountry());
    model.addAttribute("zip", UI.getZipcode());
    model.addAttribute("contactNo", UI.getContactNo());
    model.addAttribute("userType", utype);

    logger.info("Current user" + uniqueID);

    MultipartFile fileGot = transactionObj.getMpFile();
    String file_name = fileGot.getOriginalFilename();

    ServletContext context = session.getServletContext();
    String realContextPath = context.getRealPath("/");

    //	            String certpath = realContextPath+"/certificates/"+fileGot.getOriginalFilename();
    String certpath = realContextPath + "/certificates/" + uniqueID + "_cert.pem";

    String temp__2_2 = realContextPath + "/certificates";
    File temp_1_2 = new File(temp__2_2);
    if (!temp_1_2.exists()) temp_1_2.mkdirs();

    File convFile = new File(certpath);
    convFile.createNewFile();
    FileOutputStream fos = new FileOutputStream(convFile);
    fos.write(fileGot.getBytes());
    fos.close();

    if (pkiGringott.verifyCertificate(uniqueID, session) == false) {
      logger.info(certpath);
      logger.info("uniqId is:" + uniqueID);
      logger.info("Certificate verification is failed");
      model.addAttribute("message", "Certificate verification failed");

      Transactions transObj_1 = new Transactions();
      //					logger.info("Ext User"+extUser);
      // transObj.setBalance(extUser.getBalance());

      TempTransactions temp_1 = new TempTransactions();
      temp_1.setBalance(transactionObj.getBalance());

      model.addAttribute("debitOp", transObj_1);
      model.addAttribute("creditOp", transObj_1);
      model.addAttribute("checkAccBal", transObj_1.getBalance());
      model.addAttribute("savingAccBal", "500");
      model.addAttribute("transferOp", temp_1);
      model.addAttribute("paymerchantOp", temp_1);
      model.addAttribute("transactionOp", temp_1);
      model.addAttribute("UpdateProfile", new UserInfo());
      List<Transactions> obj_1 = displaytransaction(session);
      if (obj_1 == null) {
        model.addAttribute("transactionOp", null);
      } else {
        model.addAttribute("transactionOp", obj_1);
      }

      return "extUserHomePage";
    } else {
      logger.info("uniqId is:" + uniqueID);
      logger.info("Certificate verified");
    }

    MultipartFile pkf = transactionObj.getPkFile();

    String pkf_name = pkf.getOriginalFilename();
    logger.info(pkf_name);

    //				String pkpath = realContextPath+"/privatekeys/"+pkf.getOriginalFilename();
    String pkpath = realContextPath + "/privatekeys/" + uniqueID + "_private.key";

    String temp_1 = realContextPath + "/privatekeys";
    File temp_1_1 = new File(temp_1);
    if (!temp_1_1.exists()) temp_1_1.mkdirs();

    File convFile_1 = new File(pkpath);
    convFile_1.createNewFile();
    FileOutputStream fos_1 = new FileOutputStream(convFile_1);
    fos_1.write(pkf.getBytes());
    fos_1.close();

    if (pkiGringott.verifyPrivateKey(uniqueID, session) == false) {
      logger.info(pkpath);
      logger.info("uniqId is:" + uniqueID);
      logger.info("private key verification is failed");
      model.addAttribute("message", "private key verification failed");

      Transactions transObj_2 = new Transactions();
      //					logger.info("Ext User"+extUser);
      // transObj.setBalance(extUser.getBalance());

      TempTransactions temp_2 = new TempTransactions();
      temp_2.setBalance(transactionObj.getBalance());

      model.addAttribute("debitOp", transObj_2);
      model.addAttribute("creditOp", transObj_2);
      model.addAttribute("checkAccBal", transObj_2.getBalance());
      // model.addAttribute("savingAccBal", "500" );
      model.addAttribute("transferOp", temp_2);
      model.addAttribute("paymerchantOp", temp_2);
      model.addAttribute("transactionOp", temp_2);
      model.addAttribute("UpdateProfile", new UserInfo());
      List<Transactions> obj_2 = displaytransaction(session);
      if (obj_2 == null) {
        model.addAttribute("transactionOp", null);
      } else {
        model.addAttribute("transactionOp", obj_2);
      }

      return "extUserHomePage";
    } else {
      logger.info("uniqId is:" + uniqueID);
      logger.info("Private key verified");
    }

    transactionObj.setBalance(extUser.getBalance());
    logger.info("Current Balance" + transactionObj.getBalance());
    float amount = transactionObj.getTransactionAmount();
    float currentBalance = transactionObj.getBalance();
    logger.info("balance :", currentBalance);
    logger.info("account number ", transactionObj.getAccountno());
    // credit amount from current account balance

    ExternalUser merch = databaseConnector.getExternalUserByAccNum(transactionObj.getAccountno());
    if (merch == null) {
      model.addAttribute("message", "Account number is invalid");
      Transactions transObj_2 = new Transactions();
      //					logger.info("Ext User"+extUser);
      // transObj.setBalance(extUser.getBalance());

      TempTransactions temp_2 = new TempTransactions();
      temp_2.setBalance(transactionObj.getBalance());

      model.addAttribute("debitOp", transObj_2);
      model.addAttribute("creditOp", transObj_2);
      model.addAttribute("checkAccBal", transObj_2.getBalance());
      // model.addAttribute("savingAccBal", "500" );
      model.addAttribute("transferOp", temp_2);
      model.addAttribute("paymerchantOp", temp_2);
      model.addAttribute("transactionOp", temp_2);
      model.addAttribute("UpdateProfile", new UserInfo());
      List<Transactions> obj_2 = displaytransaction(session);
      if (obj_2 == null) {
        model.addAttribute("transactionOp", null);
      } else {
        model.addAttribute("transactionOp", obj_2);
      }

      return "extUserHomePage";
    }

    transactionObj.setUniqId(uniqueID);
    transactionObj.setDescription("transferred amount: " + amount);
    transactionObj.setTransactionType("tranfer");
    transactionObj.setBalance(currentBalance - amount);

    // extUser.setBalance(currentBalance-amount);
    // databaseConnector.updateExternalUser(extUser);
    databaseConnector.saveTempTransaction(transactionObj);

    Transactions temp = new Transactions();
    temp.setBalance(transactionObj.getBalance());
    model.addAttribute("debitOp", temp);
    model.addAttribute("creditOp", temp);
    model.addAttribute("checkAccBal", temp.getBalance());
    model.addAttribute("transferOp", transactionObj);
    model.addAttribute("paymerchantOp", transactionObj);
    model.addAttribute("UpdateProfile", new UserInfo());
    List<Transactions> obj = displaytransaction(session);
    if (obj == null) {
      model.addAttribute("transactionOp", null);
    } else {
      model.addAttribute("transactionOp", obj);
    }

    logger.info("Leaving transfer money POST");
    return "extUserHomePage";
  }