package com.webreference.autocomplete; import java.io.IOException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Iterator; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class AutocompleteServlet extends HttpServlet { private static final long serialVersionUID = 9111106498492817985L; private ServletContext context; private HashMap funds = new HashMap(); public void init(ServletConfig config) throws ServletException { super.init(config); this.context = config.getServletContext(); funds.put("DIG", new Fund("DIG", "Oil & Gas ETF Fund", BigDecimal.valueOf(90.45), new GregorianCalendar(2008, 1, 24))); funds.put("SAA", new Fund("SAA", "Small Cap ETF Fund", BigDecimal.valueOf(33.67), new GregorianCalendar(2008, 1, 24))); funds.put("SSO", new Fund("SSO", "S&P 500 index fund", BigDecimal.valueOf(44.00), new GregorianCalendar(2008, 1, 25))); funds.put("SIRI", new Fund("SIRI", "Sirius Satellite Radio", BigDecimal.valueOf(23.10), new GregorianCalendar(2008, 1, 25))); funds.put("SSCC", new Fund("SSCC", "Smurfit Corp.", BigDecimal.valueOf(119.40), new GregorianCalendar(2008, 1, 24))); funds.put("DELL", new Fund("DELL", "Dell Inc.", BigDecimal.valueOf(37.01), new GregorianCalendar(2008, 1, 24))); funds.put("DIS", new Fund("DIS", "Walt Disney Inc.", BigDecimal.valueOf(62.21), new GregorianCalendar(2008, 1, 25))); funds.put("DSCM", new Fund("DSCM", "Drugstore.com Inc.", BigDecimal.valueOf(5.50), new GregorianCalendar(2008, 1, 25))); funds.put("PFE", new Fund("PFE", "Pfizer Inc.", BigDecimal.valueOf(100.00), new GregorianCalendar(2008, 1, 25))); funds.put("PFG", new Fund("PFG", "Principal Financial Group Inc.", BigDecimal.valueOf(12.56), new GregorianCalendar(2008, 1, 24))); } protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String action = req.getParameter("action"); String searchString = req.getParameter("searchString"); JSONArray matches = null; if ("complete".equals(action)) { if ( searchString != null && !searchString.equals("") ) { searchString = searchString.trim().toLowerCase(); try { JSONObject JsonFund = null; Iterator it = funds.keySet().iterator(); matches = new JSONArray(); while (it.hasNext()) { String symbol = it.next(); Fund fund = funds.get(symbol); if ( searchStringMatches(fund, searchString) ) { JsonFund = new JSONObject(); JsonFund.put( "symbol", symbol ); JsonFund.put( "name", fund.getName() ); JsonFund.put( "price", fund.getPrice().doubleValue() ); JsonFund.put( "date", fund.getJsonDate() ); matches.put( JsonFund ); } } if (matches.length() > 0) { resp.setContentType("text/plain"); resp.setHeader("Cache-Control", "no-cache"); resp.getWriter().write( matches.toString(2) ); } } catch( JSONException e) { //enclose the error message in quotes for the eval() function. resp.getWriter().write( "\"" + e.getMessage() + "\"" ); } } else { //nothing to show resp.setStatus(HttpServletResponse.SC_NO_CONTENT); } } else if ("lookupbyname".equals(action)) { searchString = req.getParameter("searchString").trim().toLowerCase(); Iterator it = funds.keySet().iterator(); ArrayList fundsArray = new ArrayList(); while (it.hasNext()) { String symbol = it.next(); Fund fund = funds.get(symbol); if ( searchStringMatches(fund, searchString) ) { fundsArray.add(fund); } } if (fundsArray.size() > 0) { req.setAttribute("funds", fundsArray); } context.getRequestDispatcher("/Funds.jsp").forward(req, resp); } else if ("lookup".equals(action)) { searchString = req.getParameter("symbol").trim().toUpperCase(); // put the target fund in the request scope to display if ( (searchString != null) && funds.containsKey(searchString) ) { req.setAttribute("fund", funds.get(searchString)); } context.getRequestDispatcher("/Fund.jsp").forward(req, resp); } } private boolean searchStringMatches( Fund fund, String searchString ) { return ( fund.getSymbol().toLowerCase().startsWith(searchString) || fund.getName() .toLowerCase().startsWith(searchString) ); } }