001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.samples.daytrader.web.prims;
018    
019    import java.io.*;
020    import java.math.BigDecimal;
021    import javax.servlet.*;
022    import javax.servlet.http.*;
023    
024    import org.apache.geronimo.samples.daytrader.direct.*;
025    import org.apache.geronimo.samples.daytrader.util.*;
026    
027    import org.apache.geronimo.samples.daytrader.*;
028    /**
029     * 
030     * PingJDBCReadPrepStmt uses a prepared statement
031     * for database update. Statement parameters are set dynamically on each request.
032     * This primative uses {@link org.apache.geronimo.samples.daytrader.direct.TradeDirect} to set the price of a random stock
033     * (generated by {@link org.apache.geronimo.samples.daytrader.Trade_Config}) through the use of prepared statements.
034     * 
035     */
036    
037    public class PingJDBCWrite extends HttpServlet {
038    
039            private static String initTime;
040            private static int hitCount;
041      
042    
043    /**
044    * this is the main method of the servlet that will service all get requests.
045    * @param request HttpServletRequest
046    * @param responce HttpServletResponce
047    **/
048    public void doGet(HttpServletRequest req, HttpServletResponse res)
049            throws ServletException, IOException {
050    
051            String symbol = null;
052            BigDecimal newPrice;
053            StringBuffer output = new StringBuffer(100);
054            res.setContentType("text/html");
055            java.io.PrintWriter out = res.getWriter();
056    
057            try
058            {
059                    //get a random symbol to update and a random price.
060                    symbol = TradeConfig.rndSymbol();
061                    newPrice = TradeConfig.getRandomPriceChangeFactor();
062    
063                    //TradeJDBC makes use of prepared statements so I am going to reuse the existing code.
064                    TradeDirect trade = new TradeDirect(); 
065    
066                    //update the price of our symbol
067                    QuoteDataBean quoteData = null;
068                    int iter = TradeConfig.getPrimIterations();
069                    for (int ii = 0; ii < iter; ii++) {
070                            quoteData = trade.updateQuotePriceVolumeInt(symbol, newPrice, 100.0, false);
071                    }
072    
073                    //write the output
074                    output.append(
075                            "<html><head><title>Ping JDBC Write w/ Prepared Stmt.</title></head>"
076                                    + "<body><HR><FONT size=\"+2\" color=\"#000066\">Ping JDBC Write w/ Prep Stmt:</FONT><FONT size=\"-1\" color=\"#000066\"><HR>Init time : "
077                                    + initTime);
078                    hitCount++;
079                    output.append("<BR>Hit Count: " + hitCount);
080                    output.append("<HR>Update Information<BR>");
081                    output.append("<BR>" + quoteData.toHTML() + "<HR></FONT></BODY></HTML>");
082                    out.println(output.toString());
083    
084            }
085            catch (Exception e)
086            {
087                    Log.error(e, "PingJDBCWrite -- error updating quote for symbol", symbol);
088                    res.sendError(500, "PingJDBCWrite Exception caught: " + e.toString());
089            }
090    }                                 
091    /** 
092     * returns a string of information about the servlet
093     * @return info String: contains info about the servlet
094     **/
095    public String getServletInfo()
096    {
097            return "Basic JDBC Write using a prepared statment makes use of TradeJDBC code.";
098    }            
099    /**
100    * called when the class is loaded to initialize the servlet
101    * @param config ServletConfig:
102    **/
103    public void init(ServletConfig config) throws ServletException {
104            super.init(config);
105            initTime = new java.util.Date().toString();
106            hitCount = 0;
107    
108    }      
109    /**
110     * forwards post requests to the doGet method
111     * Creation date: (11/6/2000 10:52:39 AM)
112     * @param res javax.servlet.http.HttpServletRequest
113     * @param res2 javax.servlet.http.HttpServletResponse
114     */
115    public void doPost(HttpServletRequest req, HttpServletResponse res)
116            throws ServletException, IOException {
117            doGet(req, res);
118    }}