package gov.maine.node.util;

import java.sql.*;
import oracle.jdbc.driver.*;
//
import org.apache.axis.AxisFault;
import gov.epa.cdx.axis.fault.FaultErrorCode;
import gov.epa.cdx.axis.fault.FaultHandler;

public class WebServicesTracking 
{
  String trackingId;
  String status;
  String userId;
  String serviceId;

  public WebServicesTracking(String m_userId, String m_serviceId, String m_status,
                            Connection dbConnection) throws AxisFault
  {
    try
    {
       String sql = "SELECT service_tracking_seq.nextval snext from dual";
       PreparedStatement ps = dbConnection.prepareStatement(sql);
       ResultSet rs = ps.executeQuery();
       if (!rs.next())
       {
         throw new  Exception("No sequence Id in database");
       }           
       else
       {
         trackingId = rs.getString("snext");
         userId = m_userId;
         serviceId = m_serviceId;
         status = m_status;     
         rs.close();
         ps.close();
       }
    }
    catch (Exception e)
    {
       AxisFault fault = FaultHandler.getFault("Server", "Tracking is Unavailable", 
         FaultErrorCode.INTERNAL_ERROR);
       throw fault;
    }

  }

  public WebServicesTracking(String m_trackingId, String m_userId, String m_serviceId, 
                            String m_status)
  {
     trackingId = m_trackingId;
     userId = m_userId;
     serviceId = m_serviceId;
     status = m_status;
  }
  
  public String getTrackingId()
  {
    return trackingId;
  }

  public void setUserId(String m_userId)
  {
    userId = m_userId;
  }


  public String getUserId ()
  {
    return userId;
  }
  
  public String getServiceId() 
  {
  
   return serviceId;
  }

 public void setServiceId(String m_serviceId)
  {
   serviceId=m_serviceId;
  }

  public void setTrackingStatus(String m_status)
  {
    status = m_status;
  }

  public String getTrackingStatus()
  {
    return status;
  }


  public void writeToDB(Connection dbConnection) throws AxisFault
     {
       try
       {      
         /* write to incoming_notification table */
          String  sql = "INSERT INTO  service_tracking_log (service_tracking_id, user_id,service_id,status,timestamp) "+
                     "values (?,?,?,?,sysdate)";
           PreparedStatement ps = dbConnection.prepareStatement(sql);
           ps.setString(1,trackingId);
           ps.setString(2,userId);
           ps.setString(3,serviceId);
           ps.setString(4,status);
           ps.execute();
           dbConnection.commit();
           ps.close();
       }
       catch (Exception e)
       {
         AxisFault fault = FaultHandler.getFault("Server", "Tracking is Unavailable", 
           FaultErrorCode.INTERNAL_ERROR);
         throw fault;
       }

     }

  
}