import DBcon.DataCon;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class ContractDetails extends HttpServlet
{

    public ContractDetails()
    {
    }

    public void service(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
        throws ServletException, IOException
    {
        String s = "";
        Connection connection = null;
        try
        {
            connection = (new DataCon()).getConnection(getServletContext());
            connection.setAutoCommit(false);
            boolean flag = false;
            String s1 = httpservletrequest.getParameter("TYPE");
            if(s1.equalsIgnoreCase("INSERT"))
            {
                PreparedStatement preparedstatement = connection.prepareStatement("INSERT INTO Con_Details values(?,?,?,?,?)");
                preparedstatement.setInt(1, Integer.parseInt(httpservletrequest.getParameter("TContractId")));
                preparedstatement.setInt(2, Integer.parseInt(httpservletrequest.getParameter("TCompanyId")));
                preparedstatement.setString(3, httpservletrequest.getParameter("TContractDesc"));
                preparedstatement.setString(4, httpservletrequest.getParameter("TConTask"));
                preparedstatement.setInt(5, Integer.parseInt(httpservletrequest.getParameter("TExecId")));
                preparedstatement.execute();
                preparedstatement = connection.prepareStatement("INSERT INTO Con_Status values(?,?,?)");
                preparedstatement.setInt(1, Integer.parseInt(httpservletrequest.getParameter("TContractId")));
                preparedstatement.setInt(2, Integer.parseInt(httpservletrequest.getParameter("TExecId")));
                preparedstatement.setString(3, "Not Finished");
                preparedstatement.execute();
                connection.commit();
                System.out.println("Record Inserted");
                s = "/inssuc.jsp";
            } else
            if(s1.equalsIgnoreCase("DELETE"))
            {
                PreparedStatement preparedstatement1 = connection.prepareStatement("DELETE From Con_Status where ContId=?");
                preparedstatement1.setInt(1, Integer.parseInt(httpservletrequest.getParameter("TContractId")));
                preparedstatement1.execute();
                preparedstatement1 = connection.prepareStatement("DELETE From Con_Details where ContId=?");
                preparedstatement1.setInt(1, Integer.parseInt(httpservletrequest.getParameter("TContractId")));
                preparedstatement1.execute();
                connection.commit();
                System.out.println("Record Deleted");
                s = "/delsuc.jsp";
            } else
            if(s1.equalsIgnoreCase("MODIFY"))
            {
                PreparedStatement preparedstatement2 = connection.prepareStatement("update Con_Details set Con_Desc=?,addrefno=?,Cont_Task=?,Exec_Id=?  where ContId=?");
                preparedstatement2.setInt(5, Integer.parseInt(httpservletrequest.getParameter("TContractId")));
                preparedstatement2.setString(1, httpservletrequest.getParameter("TContractDescription"));
                preparedstatement2.setInt(2, Integer.parseInt(httpservletrequest.getParameter("TCompanyId")));
                preparedstatement2.setString(3, httpservletrequest.getParameter("TConTask"));
                preparedstatement2.setInt(4, Integer.parseInt(httpservletrequest.getParameter("TExecId")));
                preparedstatement2.execute();
                preparedstatement2 = connection.prepareStatement("Update Con_Status set Exec_Id=?,status=? where ContId=?");
                preparedstatement2.setInt(1, Integer.parseInt(httpservletrequest.getParameter("TExecId")));
                preparedstatement2.setString(2, httpservletrequest.getParameter("TStatus"));
                preparedstatement2.setInt(3, Integer.parseInt(httpservletrequest.getParameter("TContractId")));
                preparedstatement2.execute();
                connection.commit();
                System.out.println("Record Updated");
                s = "/Updsuc.jsp";
            }
        }
        catch(Exception exception)
        {
            System.out.println(exception.toString());
            try
            {
                connection.rollback();
            }
            catch(Exception exception1) { }
            if(connection == null)
                s = "/Netfail.jsp";
            else
                s = "/opefail.jsp";
        }
        RequestDispatcher requestdispatcher = httpservletrequest.getRequestDispatcher(s);
        requestdispatcher.forward(httpservletrequest, httpservletresponse);
    }
}