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.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
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;

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

			}

			pstmt.close();
			rs.close();
			pstmt = null;
			rs = null;

			for (String resourcePath : sendFinalURIList)
			{

				if (Desktop.isDesktopSupported())
				{
					Desktop desktop = Desktop.getDesktop();
					try
					{
					 desktop.browse(new URI(resourcePath));
					} catch (Exception e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else
				{
					Runtime runtime = Runtime.getRuntime();
					try
					{
						runtime.exec("xdg-open " + resourcePath);
					} catch (IOException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				// String resourcePath = CommonConstants.TOMCAT_HOME + File.separator + "ibase" +File.separator + "dashboard" + File.separator + "mfg" +File.separator+"JSP"+File.separator+"ExportExample"+File.separator  +"ProductionVsBudgetReport_export.jsp";

				System.out.println("ProductionVsBudgetReportSchedule path =[" + 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());

			}
		}
	}

	// This method call for check null
	private String checkNull(String input)
	{

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

}
