package ibase.dashboard.mfg.bean;
import ibase.scheduler.utility.interfaces.Schedule;
import ibase.system.config.ConnDriver;
import ibase.utility.CommonConstants;
import ibase.webitm.utility.ITMException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.awt.Desktop;
import java.net.URI;

public class HalolMFGReleaseSchedule implements Schedule
{
	public String schedule(HashMap map) throws Exception
	{
		System.out.println(schedule((String) map.get("PROCESS_NAME")));
		return schedule((String) map.get("PROCESS_NAME"));
	}

	public String schedule(String schedule) throws Exception
	{
		System.out.println("inside HalolMFGReleaseSchedule.........");
		this.runSchedule();
		return schedule;
	}

	public String schedulePriority(String arg0) throws Exception
	{
		return null;
	}

	private void runSchedule() throws ITMException
	{
		// Add new code on dated 10/01/2013
		String sql = "";
		ResultSet rs = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ConnDriver connDriver = new ConnDriver();
		String userID = "";
		String uri = "";
		String finalURI = "";
		List<String> sendFinalURIList = null;
		//add new variable on dated1/02/2013 
		String os = System.getProperty("os.name").toLowerCase();
		String browserPathForLinex = "";

		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			sendFinalURIList = new ArrayList<String>();
			//sql = " select user_id,uri from dashboard_comp where uri like '%ProductionVsBudgetReportExport.jsp%'";
			sql = "select user_id,uri from dashboard_comp where uri like '%ExportExample/GwtDashboardPage.html%'";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next())
			{
				userID    = rs.getString("user_id").trim();
				uri       = rs.getString("uri").trim();
				finalURI = CommonConstants.TOMCAT_HOME + uri +"&EMP_MAILCODE="+ userID;
				System.out.println("direct uri from dashboard_comp for HalolMFGReleaseSchedule =" + uri);
				System.out.println("User id is =" + userID);
				System.out.println("Final  url for HalolMFGReleaseSchedule =" + finalURI);
				sendFinalURIList.add(finalURI.trim());

			}

			pstmt.close();
			rs.close();
			pstmt = null;
			rs = null;
			if (os.indexOf("win") >= 0)
			{
				System.out.println("Start Window Section part execution for HalolMFGReleaseSchedule");
				if (Desktop.isDesktopSupported())
				{
					Desktop desktop = Desktop.getDesktop();
					for (String resourcePath : sendFinalURIList)
					{
						try
						{
							System.out.println("run on browser(IE) through window  machine");
							desktop.browse(new URI(resourcePath));
							Thread.sleep(90000);
							System.out.println("End run on browser through window ");
						} catch (Exception e)
						{
							e.printStackTrace();
						}
					}
					
				}
				System.out.println("End of Window Machine section part execution HalolMFGReleaseSchedule");
			}
			/* Ended Added new code for windows palform */

			if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0)
			{
				//browserPathForLinex ="/usr/lib/firefox/firefox";//this is my PC firefox
				browserPathForLinex = "/usr/bin/firefox";
				for (String resourcePath : sendFinalURIList)
				{
					callURL(browserPathForLinex, resourcePath); //call synchronized method         
					
				}
			}
			

		}// end try block

		catch (Exception e)
		{
			e.printStackTrace();

		} finally
		{
			try
			{
				if (conn != null)
				{
					if (rs != null)
						rs.close();
					rs = null;
					if (pstmt != null)
						pstmt.close();
					pstmt = null;
					conn.close();
					conn = null;
				}
				conn = null;
			} catch (Exception e)
			{
				e.printStackTrace();
				System.out.println("ibase.dashboard.mfg.bean: HalolMFGReleaseSchedule :runSchedule() " + e.getMessage());

			}
		}

	}

	synchronized void callURL(String browserPath, String resourcePath)
	{
		try
		{
			int second = (int) (System.currentTimeMillis() / 1000) % 60;
			System.out.println("View for Time when next URL are comming in HalolMFGReleaseSchedule===" + second);
			Runtime.getRuntime().exec(new String[]
			{ browserPath, resourcePath });
			Thread.sleep(90000);			
		} catch (Exception e)
		{
			System.out.println("ibase.dashboard.mfg.bean: HalolMFGReleaseSchedule() " + e.getMessage());
		}
	}
	// This method call for check null
	private String checkNull(String input)
	{
		if (input == null)
		{
			input = "";
		} else
		{
			input = input.trim();
		}
		return input;
	}

}
