Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.mkmeier.quickerbooks; import com.mkmeier.mkm_core.database.DatabaseConnection; import com.mkmeier.mkm_core.utils.ServletFileUploader; import com.mkmeier.quickerbooks.exception.UwcuException; import com.mkmeier.quickerbooks.iif.UwcuToIif; import com.mkmeier.quickerbooks.parser.UwcuParser; import com.mkmeier.quickerbooks.txn.UwcuTxn; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /** * * @author Marcus */ @WebServlet(name = "ProcessUwcu", urlPatterns = { "/ProcessUwcu" }) public class ProcessUwcu extends HttpServlet { private static final Logger logger = LogManager.getLogger(ProcessUwcu.class); private static final String host = "jdbc:mysql://mysql.mdmeier.com"; private static final String db = "quickerbooks"; private static final String user = "quickerbooks"; private static final String password = "quickerbooks"; /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); if (!ServletFileUpload.isMultipartContent(request)) { showForm(request, response); } else { ServletFileUploader up = new ServletFileUploader(); up.parseRequest(request); File file = up.getFileMap().get("uwcuTxn"); int depositAcctNum = Integer.parseInt(up.getFieldMap().get("depositAcct")); int creditCardAcctNum = Integer.parseInt(up.getFieldMap().get("ccAcct")); QbAccount depositAcct = getAccount(depositAcctNum); QbAccount creditCardAcct = getAccount(creditCardAcctNum); UwcuParser uwcuParser = new UwcuParser(file); List<UwcuTxn> txns; try { txns = uwcuParser.parse(); } catch (UwcuException ex) { throw new ServletException(ex); } UwcuToIif uwcuToIif = new UwcuToIif(txns, depositAcct, creditCardAcct); File iifFile = uwcuToIif.getIifFile(); response.setHeader("Content-Disposition", "attachement; filename=\"iifFile.iif\""); ServletOutputStream out = response.getOutputStream(); FileInputStream in = new FileInputStream(iifFile); byte[] buffer = new byte[4096]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } in.close(); out.flush(); } } private void showForm(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { Map<String, List<QbAccount>> accountLists = getAccountLists(); HttpSession session = request.getSession(true); session.setAttribute("lists", accountLists); RequestDispatcher rd = request.getRequestDispatcher("UwcuProcessor.jsp"); rd.forward(request, response); } private Map<String, List<QbAccount>> getAccountLists() { String depositSQL = "SELECT " + "name, acct_num, acct_type " + "FROM account " + "WHERE " + "hidden = 0 " + "AND (acct_type = 'BANK') " + "ORDER BY acct_num"; String creditCardSQL = "SELECT " + "name, acct_num, acct_type " + "FROM account " + "WHERE " + "hidden = 0 " + "AND (acct_type = 'OASSET') " + "ORDER BY acct_num"; Map<String, List<QbAccount>> accountLists = new HashMap<String, List<QbAccount>>(); accountLists.put("Deposit", getList(depositSQL)); accountLists.put("CreditCard", getList(creditCardSQL)); return accountLists; } private List<QbAccount> getList(String sql) { List<QbAccount> accounts = new ArrayList<QbAccount>(); Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = DatabaseConnection.getDatabaseConnection(host, db, user, password); stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); while (rs.next()) { QbAccount acct = new QbAccount(); acct.setName(rs.getString("name")); acct.setAcctType(rs.getString("acct_type")); acct.setAcctNum(rs.getInt("acct_num")); accounts.add(acct); } } catch (SQLException ex) { ex.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } } catch (SQLException ex) { ex.printStackTrace(); } try { if (stmt != null) { stmt.close(); } } catch (SQLException ex) { ex.printStackTrace(); } try { if (conn != null) { conn.close(); } } catch (SQLException ex) { ex.printStackTrace(); } } return accounts; } private QbAccount getAccount(int acctNum) { String sql = "SELECT " + "name, acct_num, acct_type " + "FROM account "; List<QbAccount> acctList = getList(sql); for (QbAccount acct : acctList) { if (acct.getAcctNum() == acctNum) { return acct; } } return null; } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }