Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
XML
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Reflection;

{
  /// <summary>
  /// ClassAutosysRun is the base class for generating other applications
  /// which are spawned from Autosys and require the use
  /// of the AutosysUtility to schedule and log their actions
  /// </summary>
  public abstract class ClassAutosysRun
  {
    /// <summary>
    /// Component name
    /// </summary>
    protected string _Component = "AutosysRun";
    /// <summary>
    /// getter
    /// </summary>
    public string Component
    {
      get
      {
        return _Component;
      }
    }

    /// <summary>
    /// Autosys command line arguments
    /// </summary>
    protected string[] _args;

    /// <summary>
    /// Utility application log
    /// </summary>
    protected ClassLog _Logger;

    ///  <summary>
    ///  Autosys box identifier
    ///  </summary>
    ///  the following are all parameters that must be passed into the program
    protected string _BoxName;
    public string BoxName
    {
      get
      {
        return _BoxName;
      }
      set
      {
        _BoxName = String.IsNullOrEmpty(value) ? String.Empty : value;
      }
    }

    ///  <summary>
    ///  Autosys workflow identifier
    ///  </summary>
    protected string _AutosysName;
    public string AutosysName
    {
      get
      {
        return _AutosysName;
      }
      set
      {
        _AutosysName = String.IsNullOrEmpty(value) ? String.Empty : value;
      }
    }

    /// <summary>
    /// Default Log Configuration
    /// </summary>
    protected string _LogDefault;
    /// <summary>
    /// getter/setter
    /// </summary>
    public string LogDefault
    {
      get
      {
        return _LogDefault;
      }
      set
      {
        if (!String.IsNullOrEmpty(value))
        {
          _LogDefault = value;
          //Get the default log configuration information from the registry
          _objUtility.GetRegSetting(
            ref _LogDefault,
            out _LogServer,
            out _LogDB,
            out _LogUser,
            out _LogPassword);
        }
        else _LogDefault = String.Empty;
      }
    }

    ///  <summary>
    ///  Logging server
    ///  </summary>
    protected string _LogServer;
    ///  <summary>
    ///  Logging database
    ///  </summary>
    protected string _LogDB;
    ///  <summary>
    ///  Logging user signon
    ///  </summary>
    ///  variables
    protected string _LogUser;
    ///  <summary>
    ///  Logging password
    ///  </summary>
    protected string _LogPassword;

    /// <summary>
    /// Database instance default label
    /// </summary>
    protected string _DBDefault;
    /// <summary>
    /// getter/setter
    /// </summary>
    public string DBDefault
    {
      get
      {
        return DBDefault;
      }
      set
      {
      if (!String.IsNullOrEmpty(value))
      {
        _DBDefault = value;
        //Get the default database configuration information from the registry
        _objUtility.GetRegSetting(
          ref _DBDefault,
          out _DBServer,
          out _DBName,
          out _DBUser,
          out _DBPassword);
      }
      else _DBDefault = String.Empty;
      }
    }
    ///  <summary>
    ///  SQL Server name
    ///  </summary>
    protected string _DBServer;

    /// <summary>
    /// Database name
    /// </summary>
    protected string _DBName;
    ///  <summary>
    ///  User for SQL Server authentication
    ///  </summary>
    protected string _DBUser;
    ///  <summary>
    ///  Password for SQL Server authentication
    ///  </summary>
    protected string _DBPassword;

    /// <summary>
    /// Autosys schedule identifier
    /// </summary>
    protected int _lSchedID = 0;

    ///  <summary>
    ///  Run result code
    ///  </summary>
    protected int _rc;

    ///  <summary>
    ///  Log message
    ///  </summary>
    protected System.Text.StringBuilder _Message;

    ///  <summary>
    ///  Autosys helper utility
    ///  </summary>
    ///  global objects
    protected ClassAutosysUtility _objUtility;

    /// <summary>
    /// CONSTRUCTOR
    /// </summary>
    protected ClassAutosysRun()
    {
      //ALWAYS SET RESULT CODE FIRST
      //Set informational code
      _rc = 0;

      _AutosysName = String.Empty;
      _BoxName = String.Empty;
      _LogDefault = String.Empty;
      _LogServer = String.Empty;
      _LogUser = String.Empty;
      _LogPassword = String.Empty;

      _DBDefault = String.Empty;
      _DBServer = String.Empty;
      _DBUser = String.Empty;
      _DBPassword = String.Empty;
      _Message = new System.Text.StringBuilder("");
      _Component = String.Format("{0}",
        Assembly.GetEntryAssembly().GetName().Name);
      _Logger = new ClassLog(_Component);
      _objUtility = new ClassAutosysUtility();
    }

    ///  <summary>
    ///  CONSTRUCTOR
    ///  </summary>
    protected ClassAutosysRun(
        string BoxName_A,
        string AutosysName_A,
        string LogDefault_A,
        string DBDefault_A) : this()
    {
      BoxName = BoxName_A;
      AutosysName = AutosysName_A;
      //  get the database information from the registry for the Log Server
      LogDefault = LogDefault_A;
      DBDefault = DBDefault_A;
    }

    /// <summary>
    /// PEN default for Parse, Execute, and Notify method
    /// </summary>
    /// <param name="args"></param>
    /// <returns>ErrorLevel</returns>
    public virtual int PEN( ref string[] args )
    {
      int ErrorLevel = 0;
      try
      {
        if (args.Length == 0)
        {
          _rc = 2;
          _Logger.LogEntry(_Component, "No command arguments from Autosys.");
          return _rc;
        }

        if (String.IsNullOrEmpty(args[0]))
        {
          _rc = 2;
          _Logger.LogEntry(_Component, "First command argument from Autosys empty.");
          return _rc;
        }

        if (Parse(args[0]))
        {
          if (args.Length > 1) Parse(ref args);
          Execute(out ErrorLevel);
          switch (ErrorLevel)
          {
            case (0): SendNotifications(_Component + " executed successfully."); break;
            case (1): SendNotifications(_Component + " executed with warnings"); break;
            case (2): SendNotifications(_Component + " executed with errors"); break;
            default:
              SendNotifications(
                _Component + " executed at ErrorLevel " + ErrorLevel.ToString()); break;
          }
        }
        else
        {
          SendNotifications(_Component + " command parsing error.");
        }

      }
      catch (Exception ex)
      {
        ErrorLevel = 2;
      }

      return ErrorLevel;

    }

    public abstract void Execute(out int RunCode_A);

    /// <summary>
    /// Execute process to automate and log remote package execution
    /// check the registry for each execution
    /// </summary>
    protected void Execute()
    {
      //Always set the result code before logging
      _rc = 0;
      _lSchedID = 0;

      if (String.IsNullOrEmpty(_LogServer))
      {
        _rc = 2;
        _Message.Length = 0;
        _Message.Append("Could not locate all DB data for log server ");
        if (!String.IsNullOrEmpty(_LogDefault))
          _Message.Append(_LogDefault);
        else
          _Message.Append("not specified.");

        _Logger.LogEntry("Execute", _Message.ToString());
        return;
      }

      try
      {
        // Variant type is being returned by AssignSchedID which has to be converted to object type
        // TODO Verify variant conversion to object type check
        _lSchedID = _objUtility.AssignSchedID(
          ref _BoxName,
          ref _AutosysName,
          ref _LogServer,
          ref _LogDB,
          ref _LogUser,
          ref _LogPassword);

        if (_lSchedID == 0)
        {
          _rc = 2;
          _Logger.LogEntry(
            _Component,
            "Unable to assign a scheduler ID.", EventLogEntryType.Error);
        }
      }
      catch (Exception ex)
      {
        _rc = 2;
        _Logger.LogException(ref ex);
      }
    }

    public virtual bool Parse(string Command_A, int NumParams)
    {
      if (String.IsNullOrEmpty(Command_A))
      {
        _Logger.LogEntry("Parse", "Command null or empty.");
        _rc = 2;
        return false;
      }

      _args = Command_A.Split(new Char[] { ';' });

      if (_args.Length < NumParams)
      {
        _Logger.LogEntry("Parse",
          "Invalid minimum number of parameters on command " + Command_A);

        _rc = 2;
        return false;
      }

      BoxName = _args[0];
      AutosysName = _args[1];
      LogDefault = _args[2];

      return true;
    }

    public virtual bool Parse(string Command_A)
    {
      if (!Parse(Command_A, 3)) return false;
      return true;
    }

    public virtual bool Parse(ref string[] args)
    {
      return true;
    }

    protected void LogIt(ref string Text_A, int rc_A)
    {
      int rc = _rc;
      _rc = rc_A;
      LogIt(ref Text_A);
      _rc = rc;
    }

    /// <summary>
    /// LogIt simplifies logging for _LogDefault configuration
    /// </summary>
    /// <param name="Text_A"></param>
    protected void LogIt(ref string Text_A)
    {
      bool Logged = false;

      if (!String.IsNullOrEmpty(_LogDefault))
      {
        //Attempt to log
        _objUtility.Logger_LogIt(
          ref _BoxName,
          ref _AutosysName,
          ref _LogServer,
          ref _LogDB,
          ref _LogUser,
          ref _LogPassword,
          ref Text_A,
          ref _rc,
          out Logged);
      }

      //Fallback code if primary logging to database is not available
      if (!Logged) _Logger.LogEntry(_Component,
        String.Format("Logger_LogIt failure for result {0} {1}", _rc, Text_A));
    }

    /// <summary>
    /// SendNotifications result to log server and email recips
    /// </summary>
    /// <param name="Text_A"></param>
    public void SendNotifications(string Text_A)
    {
      if(!String.IsNullOrEmpty(Text_A))
        LogIt(ref Text_A);

      bool Emailed = false;

      if (!String.IsNullOrEmpty(_LogDefault))
      {
        _objUtility.Email_It(
          ref _BoxName,
          ref _AutosysName,
          ref _LogServer,
          ref _LogDB,
          ref _LogUser,
          ref _LogPassword,
          out Emailed);
      }

      if (!Emailed)
      {
        //Store result code
        int rc = _rc;
        //Set error type
        _rc = 2;
        try
        {
          //Log the event
          string stext = "Email_It failure, check Application log.";
          LogIt(ref stext);
        }
        catch (Exception e)
        {
          _Logger.LogException(ref e);
        }
        //restore result code
        _rc = rc;
        _Logger.LogEntry(_Component, "Email_It failure.");

      }

    }
  }
}
Posted
Comments
Sandeep Mewara 30-Jun-11 12:53pm    
Not clear. Can you elaborate and add what is the issue?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900