Option Compare Text
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.Data

Imports JF.PlmJobManager.JobServer
Imports JF.Tools 
Imports JF.PlmJobManager



' Imports JF.ScriptingEngine


'' Information for developer:
'' All Lines with mark "##" should be edit by the developer

'' # ---------------------------------------------------------------------------------------
'' # Documentation:
'' # Description  : ## please add the description for this script in this line
'' # Parameters   : ## /Para1=MyPara1 /Para2=MyPara2  

'' # History:
'' # Date         Person        ChangeInfo
'' # ##12.04.2007 ##Developer   createt
'' # ---------------------------------------------------------------------------------------

Namespace JS_ScriptingTemplate
    Public Class ScriptingEngineClass

        Public Function ScriptMain(ByVal JS_ScriptingInterface As ScriptingInterface.Methodes) As JF_Return_cls

            ' Required class for returning script results
            Dim MyScriptReturn As New JF_Return_cls
            Dim MyCallReturn As New JF_Return_cls
            Dim TextStr As String = ""
            Dim I As Integer
            Dim I2 As Integer

            '-- using "standard form" for printing script informations 
            Dim MyScriptOutput_frm As JobServer_ScriptRun_frm
            MyScriptOutput_frm = New JobServer_ScriptRun_frm(JS_ScriptingInterface.ScriptDef)
            MyScriptOutput_frm.Show()


            '-- printing Informations to the header textbox 
			MyScriptOutput_frm.InfoSysHeader.PrintInfoLine("Step - 1 Script started!")

            '-- handling Exeptions
            Try
                '-- parsing parameters 
                Dim Para1 As String = JS_ScriptingInterface.ParameterValueGet("Para1")
                Dim Para2 As String = JS_ScriptingInterface.ParameterValueGet("Para2")
                Dim Text_str As String = _
                 "Script started with parameters:" + vbCrLf + _
                 "[" + JS_ScriptingInterface.ParameterStr + "]" + vbCrLf + _
                 vbCrLf + _
                 "Para1=" + Para1 + vbCrLf + _
                 "Para2=" + Para2 + vbCrLf + _
                 vbCrLf + _
                 "..\10-JobLogArchiv :" + vbCrLf + _
                 "[" + JS_ScriptingInterface.ServerSettings.Dir_DataRoot_DP_10JobLogArchiv + "]"

                '-- printing informations to the protokoll textbox
                MyScriptOutput_frm.InfoSysProtocol.Print(Text_str)


                Select Case JS_ScriptingInterface.JobDefCurrJob.JobDef.JobServerScript_RunCondition

                    Case ScriptingDef.RunCondition_enum.Cond01_GenNewJob_Bevor_SelectObjectData

                        ' suche nach Files in einem bestimmten Verzeichniss:
                        Dim FileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
                        FileList = My.Computer.FileSystem.GetFiles(JS_ScriptingInterface.ServerSettings.Dir_DataRoot_DP_91JobControl, FileIO.SearchOption.SearchAllSubDirectories, "*.XML")

                        If FileList.Count > 0 Then
                            MyScriptOutput_frm.InfoSysProtocol.Print("List of Files to process:")
                            For Each FileItem_DPNE As String In FileList
                                MyScriptOutput_frm.InfoSysProtocol.Print(FileItem_DPNE)
                            Next
                            '-- Vorgaben fuer weiteres Verhalten einstellen: "JobPaket an den Client senden:Yes"
                            JS_ScriptingInterface.JobDefCurrJob.JobDef.JobControlRule.JobControlInterface.JobPaket_SendToJobClient = True
                        Else
                            MyScriptOutput_frm.InfoSysProtocol.Print("No Files to process:")
                            '-- Vorgaben fuer weiteres Verhalten einstellen: "JobPaket an den Client senden:No"
                            JS_ScriptingInterface.JobDefCurrJob.JobDef.JobControlRule.JobControlInterface.JobPaket_SendToJobClient = False
                        End If

                        '-- Aktion vor Selection der Objekt Daten:
                        With JS_ScriptingInterface.JobDefCurrJob.JobDef.JobFiles
                            '-- erstellt ein File der dem JobFiles hinzugef�gt wird
                            Dim fileToAdd_DPNE As String = Path.Combine(.JobDestRootDir_DP, "_MySpecialJobFile.txt")
                            File.WriteAllText(fileToAdd_DPNE, "TEST" + vbCrLf + fileToAdd_DPNE)
                            MyScriptOutput_frm.InfoSysProtocol.Print(fileToAdd_DPNE)
                        End With


                        '-- Vorgaben fuer weiteres Verhalten einstellen: "Keine Objekte selectieren lassen"
                        JS_ScriptingInterface.JobDefCurrJob.JobDef.JobControlRule.JobControlInterface.SelectObjects_ViaJobRule = False
                        '-- Vorgaben fuer weiteres Verhalten einstellen: "JobPaket an den Client senden"
                        JS_ScriptingInterface.JobDefCurrJob.JobDef.JobControlRule.JobControlInterface.JobPaket_SendToJobClient_AlsoWennObjektPaketEmty = False


                    Case ScriptingDef.RunCondition_enum.Cond02_GenNewJob_After_SelectObjectData
                        MyScriptOutput_frm.InfoSysProtocol.Print("Step:" + ScriptingDef.RunCondition_enum.Cond02_GenNewJob_After_SelectObjectData.ToString())
                        '-- processing JobPacke DataTable ---------------------
                        If JS_ScriptingInterface.JobDefCurrJob IsNot Nothing AndAlso _
                           JS_ScriptingInterface.JobDefCurrJob.ObjectPacket_DataTable IsNot Nothing Then
                            If JS_ScriptingInterface.JobDefCurrJob.ObjectPacket_DataTable.Rows.Count > 0 Then
                                ' Edit the the DataRow off the ObjectPacket_DataTable
                                MyScriptOutput_frm.InfoSysProtocol.Print("ObjectData found Processing Object Data:")
                                For Each DR As DataRow In JS_ScriptingInterface.JobDefCurrJob.ObjectPacket_DataTable.Rows
                                    MyScriptOutput_frm.InfoSysProtocol.PrintInfoLine("Details of:" + DR.Item("ObjectCliName").ToString)
                                    For Each Col As DataColumn In JS_ScriptingInterface.JobDefCurrJob.ObjectPacket_DataTable.Columns
                                        MyScriptOutput_frm.InfoSysProtocol.Print(("[" + Col.ColumnName + "]=").PadRight(35, "."c) + DR.Item(Col.ColumnName).ToString)
                                    Next
                                Next
                            End If
                            MyScriptReturn.ReturnDataTable = JS_ScriptingInterface.JobDefCurrJob.ObjectPacket_DataTable
                            MyScriptOutput_frm.InfoSysProtocol.Print("JS_ScriptingInterface.JobDefCurrJob.ObjectPacket_DataTable.Rows.Count=" + JS_ScriptingInterface.JobDefCurrJob.ObjectPacket_DataTable.Rows.Count.ToString)
                        Else
                            MyScriptOutput_frm.InfoSysProtocol.Print("ObjectData NOT found!!")
                        End If
                        MyScriptOutput_frm.Pause("click me and script will continue")

                    Case ScriptingDef.RunCondition_enum.Cond10_JobResults_Get
                End Select

                '-- processing DS of IR ---------------------
                '-- check/List if IR is in JobMgr DB
                Dim Ir_lcls As New Ir_cls
                Ir_lcls.ItemName = "000002"
                Ir_lcls.ObjectFromSiteID = "tceng"
                'Ir_lcls.Rev = "A"
                MyCallReturn = JS_ScriptingInterface.IR_GetDatsets(Ir_lcls)

                If MyCallReturn.ReturnDataTable.Rows.Count > 0 Then
                    ' List of all Datarows 
                    For Each DR As DataRow In MyCallReturn.ReturnDataTable.Rows
                        MyScriptOutput_frm.InfoSysProtocol.PrintInfoLine("Datasets of IR:" + DR.Item("IRName").ToString)
                        For Each Col As DataColumn In MyCallReturn.ReturnDataTable.Columns
                            MyScriptOutput_frm.InfoSysProtocol.Print(("[" + Col.ColumnName + "]=").PadRight(35, "."c) + DR.Item(Col.ColumnName).ToString)
                        Next
                    Next
                Else
                    MyScriptOutput_frm.InfoSysProtocol.Print("no datasets found for IR:" + Ir_lcls.ItemNameRev + " (Site:" + Ir_lcls.ObjectFromSiteID + ")")
                End If

                '-- starting the progress bar
                MyScriptOutput_frm.InfoSysProtocol.StatusBarProgressBarStart()

                Call MyScriptOutput_frm.InfoSysProtocol.Print_and_StatusBarSetCurrActionStatusInfo_Panel3("now waiting " + I.ToString + " [sec]")
                MyScriptOutput_frm.InfoSysHeader.Print("Step - 2 Script is in process")

                I2 = CInt("100")
            Catch ex As Exception
                MyScriptReturn.Ex = ex
                MyScriptReturn.ExUp = True
                MyScriptReturn.ExMsg = "ScriptTemplate"
                MyScriptOutput_frm.InfoSysProtocol.PrintEx(ex, MyScriptReturn.ExMsg)
                GoTo EndOfScript
            End Try

            '-- returning results 
            I = 0
            If I = 0 Then
                MyScriptReturn.ResultCode = 0
                MyScriptReturn.ResultMsg = "OK : this schript ends with no errors (this message generated with current script)"
            Else
                MyScriptReturn.ResultCode = 10
                MyScriptReturn.ResultMsg = "ERR: this schript ends with ERRORS! (this message generated with current script)"
            End If

            MyScriptReturn.ResultMsg += vbCrLf + JS_ScriptingInterface.ScriptDef.Script_DPNE

            '-- setting the progress bar to idel			
            MyScriptOutput_frm.InfoSysHeader.Print("Step - 3 Progressbar set idle")
            System.Threading.Thread.Sleep(1000)
            Call MyScriptOutput_frm.InfoSysProtocol.StatusBarProgressBarSetIdle()

EndOfScript:

            Call MyScriptOutput_frm.InfoSysProtocol.PrintInfoLine(MyScriptReturn.ResultMsg)

            TextStr = "Step - 4 Script will end after clicking the button"
            MyScriptOutput_frm.InfoSysHeader.Print(TextStr)
            MyScriptOutput_frm.InfoSysProtocol.Print_and_StatusBarSetCurrActionStatusInfo_Panel3(TextStr)
            MyScriptOutput_frm.Pause("click me and script will end")

            '-- closing the ScriptRun form 
            MyScriptOutput_frm.Close()
            MyScriptOutput_frm.Dispose()

            Return MyScriptReturn
        End Function
    End Class
End Namespace
