/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.tutorial.ear.servlet;

import org.jboss.tutorial.ear.bean.CalculatorLocal;

import javax.annotation.EJB;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.EJB;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * comment
 *
 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 */
public class CalculatorActionServlet extends HttpServlet
{
   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
           throws ServletException, IOException
   {
      doit(req, resp);
   }

   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
           throws ServletException, IOException
   {
      doit(req, resp);
   }

   /**
    * Simulate injection of Calculator EJB
    *
    * @throws ServletException
    */
   public void init() throws ServletException
   {
      super.init();
      try
      {
         InitialContext ctx = new InitialContext();

         // J2EE 1.5 has not yet defined exact XML <ejb-ref> syntax for EJB3
         CalculatorLocal calculator = (CalculatorLocal) ctx.lookup(CalculatorLocal.class.getName());
         setCalculator(calculator);
      }
      catch (NamingException e)
      {
         throw new RuntimeException(e);
      }

   }

   private CalculatorLocal calculator;

   /**
    * The @EJB annotation is similated.  Tomcat does not yet understand injection annotations.
    * @param calculator
    */
   @EJB(name = "calculator")
   public void setCalculator(CalculatorLocal calculator)
   {
      this.calculator = calculator;
   }

   protected void doit(HttpServletRequest req, HttpServletResponse resp)
           throws ServletException, IOException
   {
      int op1 = Integer.parseInt(req.getParameter("op1"));
      int op2 = Integer.parseInt(req.getParameter("op2"));

      String action = req.getParameter("action");

      int result = 0;
      if (action.equals("Add"))
         result = calculator.add(op1, op2);
      else if (action.equals("Subtract")) result = calculator.subtract(op1, op2);

      resp.setContentType("text/html");
      PrintWriter writer = resp.getWriter();
      writer.println("<html>");
      writer.println("<body>");
      writer.println("<h1>Answer: " + result + "</h1>");
      writer.println("</body");
      writer.println("</html>");
   }
}
