/****************************************************************************
 *
 *	%Q%
 *
 *	All rights reserved.  Unpublished, all rights reserved under
 *	copyright law and international treaty.  Use of a copyright notice
 *	is precautionary only and does not imply publication or disclosure.
 *	This software contains confidential information and trade secrets of
 *	Micro Focus International Ltd, and no part of this program or
 *	publication may be reproduced, transmitted, transcribed, stored in
 *	a retrieval system, or translated into any language or computer
 *	language, in any form or by any means, electronic, mechanical,
 *	magnetic, optical, chemical, manual or otherwise, without the prior
 *	written permission of:
 *
 *	Micro Focus International Ltd,
 *	The Lawn, Old Bath Road, Newbury, Berkshire, RG14 1QN, UK.
 *	(01635) 32646
 *
 *	RESTRICTED RIGHTS LEGEND
 *	Use, duplication, or disclosure by the Government is subject to
 *	restrictions as set forth in subparagraph (c)(1)(ii) of the Rights
 *	in Technical Data and Computer Software clause at DFARS 52.227-7013
 *	or subparagraphs (c)(1) and (2) of Commercial Computer Software -
 *	Restricted Rights at 48 CFR 52.227-19, as applicable.  Contact
 *	Micro Focus International Ltd, 701 East Middlefield Road,
 *	Mountain View, California 94043, USA.
 *
 *	%Y%
 *
 *	Source Module %M%	Sccs Identification %I%
 *
 *	Last Modified on %E% at %U%
 *
 *      sccsid %W%
 *
 ****************************************************************************/

package com.mypackage.JMapServ ;

import java.io.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.rmi.RemoteException;
import java.util.Date;

/**
 * Servlet that is used to process the incoming requests from the JSP, invoke an instance of the Stateless EJB, and forward
 * the response back to the JSP.
 */
public class MapDemoServlet extends HttpServlet {

	private JMapServ mr = null;
	private JMapServHome mHome = null;

     public void init() throws ServletException {}

	/**
	 * Process incoming HTTP GET requests
	 *
	 * @param req Object that encapsulates the request to the servlet
	 * @param res Object that encapsulates the response from the servlet
	 * @throws javax.servlet.ServletException This exception is thrown to indicate a servlet problem.
	 * @throws java.io.IOException Signals that an I/O exception of some sort has occurred.
	 */
	public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		performTask(req, res);
	}

	/**
	 * Process incoming HTTP POST requests
	 *
	 * @param req Object that encapsulates the request to the servlet
	 * @param res Object that encapsulates the response from the servlet
	 * @throws javax.servlet.ServletException This exception is thrown to indicate a servlet problem.
	 * @throws java.io.IOException Signals that an I/O exception of some sort has occurred.
	 */
	public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		performTask(req, res);
	}

	/**
	 * Main service method for BasicCalculatorServlet
	 *
	 * @param req Object that encapsulates the request to the servlet
	 * @param res Object that encapsulates the response from the servlet
	 * @throws javax.servlet.ServletException This exception is thrown to indicate a servlet problem.
	 * @throws java.io.IOException Signals that an I/O exception of some sort has occurred.
	 */
	public void performTask (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

		String lnk_b_title = "";
		String lnk_b_type = "";
   		String lnk_b_author = "";
        String lnk_b_stockno = "";
        String lnk_b_retail = "";
		String lnk_b_onhand = "";
        String lnk_b_sold = "";
		String lnk_file_status = "";
        String message = "";
        String operation = "?";


        HttpSession   session = req.getSession(true);
        /* just to debug... */
       //session.setMaxInactiveInterval(20);

	    lnk_b_title = req.getParameter("lnk_b_title");
        lnk_b_type = req.getParameter("lnk_b_type");
        lnk_b_author = req.getParameter("lnk_b_author");
        lnk_b_stockno = req.getParameter("lnk_b_stockno");
        lnk_b_retail = req.getParameter("lnk_b_retail");
        lnk_b_onhand = req.getParameter("lnk_b_onhand");
        lnk_b_sold = req.getParameter("lnk_b_sold");
        lnk_file_status = req.getParameter("lnk_file_status");
        lnk_b_onhand = req.getParameter("lnk_b_onhand");
        operation = req.getParameter("operation");


		try {
             mr = SessionMonitor.getClientRequest(session);
            if (mr == null)
            {
 	          InitialContext initCtx = new InitialContext();
              Object objRef = initCtx.lookup("java:comp/env/ejb/JMapServEJB");
              mHome = (JMapServHome) javax.rmi.PortableRemoteObject.narrow(objRef, com.mypackage.JMapServ.JMapServHome.class);
              mr = mHome.create();
              SessionMonitor.setClientRequest(session, mr);
              System.out.println("Successfully created EJB");
           }
		   else
		   {
              mr = SessionMonitor.getClientRequest(session);
              System.out.println("Successfully created EJB");
           }

           if(operation.equals("Add")) {
                  System.out.println("operation is add");
                  lnk_file_status = mr.Add(lnk_b_title, lnk_b_type,lnk_b_author,
                                           lnk_b_stockno, (new Integer(lnk_b_retail)).intValue(),
                                           (new Integer(lnk_b_onhand)).intValue(), (new Integer(lnk_b_sold)).intValue());
            }
            if(operation.equals("Next")) {
                  System.out.println("operation is next");
                  java.util.ArrayList a = mr.Next(lnk_b_stockno);
                  if(a.size() == 8)
                  {
                    lnk_file_status = (String) a.get(0);
                    lnk_b_title = (String) a.get(1);
                    lnk_b_type = (String) a.get(2);
                    lnk_b_author = (String) a.get(3);
                    lnk_b_stockno = (String) a.get(4);
                    lnk_b_retail = ((Integer) a.get(5)).toString();
                    lnk_b_onhand = ((Integer) a.get(6)).toString();
                    lnk_b_sold = ((Integer) a.get(7)).toString();
                  }
                  else {
                    message = "Incomplete/No output";
                  }
            }
            if(operation.equals("Read")) {
              System.out.println("operation is Read");
              com.mypackage.JMapServ.Lnk_b_details det ;
              det = new com.mypackage.JMapServ.Lnk_b_details();
              det.setLnk_b_title(lnk_b_title);
              det.setLnk_b_author(lnk_b_author);
              det.setLnk_b_stockno(lnk_b_stockno);
              java.util.ArrayList a = mr.Read(det);
              if(a.size() == 8)
              {
                lnk_file_status = (String) a.get(0);
                lnk_b_title = (String) a.get(1);
                lnk_b_type = (String) a.get(2);
                lnk_b_author = (String) a.get(3);
                lnk_b_stockno = (String) a.get(4);
                lnk_b_retail = ((Integer) a.get(5)).toString();
                lnk_b_onhand = ((Integer) a.get(6)).toString();
                lnk_b_sold = ((Integer) a.get(7)).toString();
              }
              else {
                message = "Incomplete/No output";
              }
            }
            if(operation.equals("Delete")) {
              System.out.println("operation is Delete");
              com.mypackage.JMapServ.Lnk_b_details det ;
              det = new com.mypackage.JMapServ.Lnk_b_details();
              det.setLnk_b_title(lnk_b_title);
              det.setLnk_b_author(lnk_b_author);
              det.setLnk_b_stockno(lnk_b_stockno);
              lnk_file_status = mr.Delete(det);
            }

        } catch (Exception e) {
			e.printStackTrace();
			throw new ServletException(e);
		}
		MapservJspBean mapservJspBean = new MapservJspBean();
		mapservJspBean.setLnk_b_title(lnk_b_title);
        mapservJspBean.setLnk_b_type(lnk_b_type);
        mapservJspBean.setLnk_b_author(lnk_b_author);
        mapservJspBean.setLnk_b_stockno(lnk_b_stockno);
        mapservJspBean.setLnk_b_retail(lnk_b_retail);
        mapservJspBean.setLnk_b_onhand(lnk_b_onhand);
        mapservJspBean.setLnk_b_sold(lnk_b_sold);
        mapservJspBean.setLnk_file_status(lnk_file_status);
   		mapservJspBean.setOperation(operation);
		mapservJspBean.setMessage(message);
        mapservJspBean.setSct( (new Date(session.getCreationTime())).toString());
        mapservJspBean.setSlt( (new Date(session.getLastAccessedTime())).toString());
        mapservJspBean.setSii( (new Integer(session.getMaxInactiveInterval())).toString());

		session.setAttribute("MapservJspBean", mapservJspBean);
		RequestDispatcher rd = getServletContext( ).getRequestDispatcher("/MapDemo.jsp" );
		rd.forward(req, res);
	}

    public void destroy() {}
}

/** End of source module %M% **/
