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 ProductionVsBudgetReportSchedule 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 ProductionVsBudgetReportSchedule.........");
		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/ProductionVsBudgetReportExport.jsp%'";
			
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next())
			{
				userID   = checkNull(rs.getString("user_id").trim());
				uri      = checkNull(rs.getString("uri"));
				finalURI = CommonConstants.TOMCAT_HOME+uri+"&userID="+userID;				
				System.out.println("direct uri from table ="+ uri);
				System.out.println("User id is =" + userID);
				System.out.println("Final  url  appended user id in URL =" + finalURI);
				sendFinalURIList.add(finalURI);	

			}

			pstmt.close();
			rs.close();
			pstmt = null;
			rs = null;
			
			//This URL comming from table as dashboard_comp as multiple types
			//CommonConstants.TOMCAT_HOME+ibase/dashboard/mfg/jsp/ProductionVsBudgetReport.jsp?geoLocation=105,110,117,130 &unitForm=TAB,CAP,V01,A01&userID=E04111
	
			/*	if (os.indexOf( "win" ) >= 0) 
				{
				  
					 for (String resourcePath : sendFinalURIList)
					    {
			         
						Runtime.getRuntime().exec( "rundll32 url.dll,FileProtocolHandler"+ resourcePath);
						Thread.sleep(25000);
					  }
			    } */
			/*Added new code for windows palform*/
			
			 if (os.indexOf("win") >= 0)
			  {
			  System.out.println("Start Window Section part execution");
			    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(120000);
					System.out.println("End run on browser through window ");
		          }
		          catch (Exception e)
		          {
		            e.printStackTrace();
		          }
		        }
		          //Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler" + resourcePath);
		          //Thread.sleep(25000L);
		        }
				System.out.println("End of Window Machine section part execution");
		      }
			 /* 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         
			        //String[] b  = {browserPath, resourcePath};
			        }
		         } 
				
			// end forloop

		}// 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("error inside when scheduler invoke ProductionVsBudgetExport.jsp " + 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==="+second); 	 
		     Runtime.getRuntime().exec(new String[] {browserPath, resourcePath});
             Thread.sleep(120000); 
             System.out.println("In Linex12"); 
		     }catch(Exception e){System.out.println("Exception Inside callURL by synchronization method  "+e.getMessage());}  
		   }  
		  
	
	// This method call for check null
	private String checkNull(String input)
	{

		if (input == null)
		{
			input = "";
		} else
		{
			input = input.trim();
		}
		return input;
	}

}
