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.SquareException; import com.mkmeier.quickerbooks.iif.SquareToIif; import com.mkmeier.quickerbooks.parser.SquareParser; import com.mkmeier.quickerbooks.txn.SquareTxn; 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 = "ProcessSquare", urlPatterns = { "/ProcessSquare" }) public class ProcessSquare extends HttpServlet { private static final Logger logger = LogManager.getLogger(ProcessSquare.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 { if (!ServletFileUpload.isMultipartContent(request)) { showForm(request, response); } else { ServletFileUploader up = new ServletFileUploader(); up.parseRequest(request); File file = up.getFileMap().get("squareTxn"); int incomeAcctNum = Integer.parseInt(up.getFieldMap().get("incomeAcct")); int feeAcctNum = Integer.parseInt(up.getFieldMap().get("feeAcct")); int depositAcctNum = Integer.parseInt(up.getFieldMap().get("depositAcct")); QbAccount incomeAcct = getAccount(incomeAcctNum); QbAccount feeAcct = getAccount(feeAcctNum); QbAccount depositAcct = getAccount(depositAcctNum); SquareParser sp = new SquareParser(file); List<SquareTxn> txns; try { txns = sp.parse(); } catch (SquareException ex) { throw new ServletException(ex); } SquareToIif squareToIif = new SquareToIif(txns, incomeAcct, feeAcct, depositAcct); File iifFile = squareToIif.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(); // PrintWriter out = response.getWriter(); // for (SquareTxn txn : txns) { // out.println(txn.toString()); // } } } 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; } private void showForm(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { //Load Account Options Map<String, List<QbAccount>> accountLists = getAccountLists(); HttpSession session = request.getSession(true); session.setAttribute("lists", accountLists); RequestDispatcher rd = request.getRequestDispatcher("SquareProcessor.jsp"); rd.forward(request, response); } private Map<String, List<QbAccount>> getAccountLists() { //TODO LOTS of cleanup!! String incomeSQL = "SELECT " + "name, acct_num, acct_type " + "FROM account " + "WHERE " + "hidden = 0 " + "AND (acct_type = 'INC' OR acct_type = 'EQUITY' OR acct_type = 'EXINC') " + "ORDER BY acct_num"; String feeSQL = "SELECT " + "name, acct_num, acct_type " + "FROM account " + "WHERE " + "hidden = 0 " + "AND (acct_type = 'EXP' OR acct_type = 'EXEXP') " + "ORDER BY acct_num"; String depositSQL = "SELECT " + "name, acct_num, acct_type " + "FROM account " + "WHERE " + "hidden = 0 " + "AND (acct_type = 'BANK' OR acct_type = 'OASSET' OR acct_type = 'EQUITY') " + "ORDER BY acct_num"; Map<String, List<QbAccount>> accountLists = new HashMap<String, List<QbAccount>>(); accountLists.put("Income", getList(incomeSQL)); accountLists.put("Fee", getList(feeSQL)); accountLists.put("Deposit", getList(depositSQL)); 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; } // <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> }