Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Collections.Generic
Imports JF.PlmJobManager
Imports JF.PlmJobManager.JobServer
Imports JF.Tools
Imports JF.InfoSys

'' --------------------------------------------------------------------------------------------------------
'' Script for Importing Data via TaskSheduler:
''  Date         Person        ChangeInfo
''  12.04.2007   J.Feuerstein  createt
''  29.08.2007   J.Feuerstein  #Doc: add Parameter /SiteList
''  07.03.2013   J.Feuerstein  #Doc: add Parameter /SyncSiteData 
''  07.11.2015   J.Feuerstein  #Doc: add Parameter /Methode=#MethodeType#
''  19.04.2016   J.Feuerstein  #Doc: add Parameter /ImpUpdCond=#ImpUpdCond#
''  07.03.2013   J.Feuerstein  #Doc: add Parameter /SyncSiteData --> Obsolate since 07.11.2015 #toDo Replace with /Methode=
''  06.12.2016   J.Feuerstein  #Doc: add Parameter /Methode=Sync_NeedUpdateAndEnd
''  23.03.2017   J.Feuerstein  #Doc: add Parameter /EndOfProcessDoWait_msec=
''  20.03.2018   S.Gueth       #Doc: add Task Sheduler Infom Sys Object, removed scripting form
'' --------------------------------------------------------------------------------------------------------
'' 
'' #Doc: Parameter usage:
'' 
'' /SiteList=TcSite1;TCSite2      #Doc: Defines Sites to process  
'' -----------------------
'' /Methode=ImportUpdate          #Doc: Defines to run ImportUpdate controled by LastMod Date
''                                      |-> works like:'Sites/Data Import'->Button:'Import.Site.Data'"
'' /Methode=Sync_Items            #Doc: Defines to Sync TC-JobMgr via Item Data
''                                      |-> works like:'Sites/Data Import'->Button:'Sync.Site.Data'->10 - Sync.Site [Items.Only]"
'' /Methode=Sync_Ir               #Doc: Defines to Sync TC-JobMgr via ItemRevision Data
''                                      |-> works like:'Sites/Data Import'->Button:'Sync.Site.Data'->20 - Sync.Site [Ir.Only]"
'' /Methode=Sync_Ir_ImportUpdate  #Doc: Defines to Sync TC-JobMgr via ItemRevision Data and 
''                                      |-> works like:'Sites/Data Import'->Button:'Sync.Site.Data'->21 - Sync.Site [Ir + Import.Site.Data]"
'' /Methode=Sync_All_ImportUpdate #Doc: Defines to Sync TC-JobMgr via Item + IR Data and runs ImportUpdate 
''                                      |-> works like:'Sites/Data Import'->Button:'Sync.Site.Data'->30 - Sync.Site [Items/Ir.Only + Import.Site.Data]"
'' /Methode=Sync_NeedUpdateAndEnd #Doc: Defines to refresh JM.DB Data that has O.Need.Upd flag and finisch Import process
''                                      |-> works like:'Sites/Data Import'->Button:'Sync.Site.Data'->40 - Sync.Site [Items/Ir.Only + Import.Site.Data]"
'' -----------------------
'' /ImpUpdCond=LastModDate        #Doc: Defines to Use LastModDate for Import Update Condition [Default]
''                                      |-> works like:'Sites/Data Import'->Button:'Import.Site.Data'"
'' /ImpUpdCond=ReleaseDate        #Doc: Defines to Use ReleaseDate for Import Update Condition
''                                      |-> works like:'Sites/Data Import'->Button:'Import Extended'->Import.Site.Data 'control by ReleaseDate'" 
'' /ImpUpdStartDeltaTime=00.00.00 #Doc: Defines timeSpann that will be substracted from "IContr:Ir.ModDateBegin" or "IContr:Ir.ReleaseDateBegin"
''
'' -----------------------
'' /EndOfProcessDoWait_msec=1000  #Doc: Defines im msec how long to wait after 'Import' is done (Default=0 no wait) (#New: 3.1309;23.03.2017;J.Fes)
''                                #Exa: /EndOfProcessDoWait_msec=10000 ' wait 10 sec after end of import action

Namespace JS_ScriptingDataImport

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

			'-- #Doc: Mapp info Sys to TaskShedulerInfoSysGet
			Dim InfoSys_Obj as JF_InfoSys = JS_ScriptingInterface.TaskShedulerInfoSysGet()
			'-- #Doc: ScriptResult_r required Object for returning script results
			Dim ScriptReturn_r As New JF_Return_cls
			Dim ImportResult_Obj As New JF_Return_cls

			Dim SyncSiteData_Do As Boolean = False
			Dim SiteList As String()
			Dim ImportControlSettings_Obj As New ImportControlSettings_cls
			Dim ImportMethode_para As String
			Dim ImpUpdCond_para As String
			Dim ImpUpdStartDeltaTime_para As New TimeSpan
			' #New: 3.1309;23.03.2017;J.Fes
			Dim EndOfProcessDoWait_msec As Integer

			Try

				' InfoSys_Obj.PrintInfoLine("SiteImport")
				' InfoSys_Obj.Print(JS_ScriptingInterface.ScriptDef.cmdPara)

				'-- #Step: Get Parameters
				ImportMethode_para = JS_ScriptingInterface.ParameterValueGet("Methode").Trim
				ImpUpdCond_para = JS_ScriptingInterface.ParameterValueGet("ImpUpdCond").Trim

				SiteList = JS_ScriptingInterface.ParameterValueGet("SiteList").Split(New Char() {","c, ";"c})
				If JS_ScriptingInterface.ParameterValueGet("ImpUpdStartDeltaTime") <> "" Then
					ImportControlSettings_Obj.ImpUpdStartDeltaTime = TimeSpan.Parse(JS_ScriptingInterface.ParameterValueGet("ImpUpdStartDeltaTime"))
				End If

				If JS_ScriptingInterface.ParameterValueGet("EndOfProcessDoWait_msec") <> "" Then
					EndOfProcessDoWait_msec = CTypeTryDefault(Of Integer)(JS_ScriptingInterface.ParameterValueGet("EndOfProcessDoWait_msec"), -1)
				End If


				Select Case ImportMethode_para

					Case "ImportUpdate"
						ImportControlSettings_Obj.ImportMethode = ImportMethode_enum.ImportUpdate

					Case "Sync_Items"
						ImportControlSettings_Obj.ImportMethode = ImportMethode_enum.Sync_Items

					Case "Sync_Ir"
						ImportControlSettings_Obj.ImportMethode = ImportMethode_enum.Sync_Ir

					' ApplDokuIntern: 3.1291;06.12.2016;J.Fes;#New: Methode Sync_Ir_ImportUpdate
					Case "Sync_Ir_ImportUpdate"
						ImportControlSettings_Obj.ImportMethode = ImportMethode_enum.Sync_Ir_ImportUpdate

					Case "Sync_All_ImportUpdate"
						ImportControlSettings_Obj.ImportMethode = ImportMethode_enum.Sync_All_ImportUpdate

					' ApplDokuIntern: 3.1291;06.12.2016;J.Fes;#New: Methode SyncSiteData_NeedUpdateRunAndStop
					Case "Sync_NeedUpdateAndEnd"
						ImportControlSettings_Obj.ImportMethode = ImportMethode_enum.Sync_NeedUpdateRunAndStop

					Case Else
						Throw New System.Exception(String.Format("ERR: Paramter /Methode={0} value invalid or is missing", ImportMethode_para))
				End Select

				Select Case ImpUpdCond_para.Trim

					Case "", "LastModDate"
						ImportControlSettings_Obj.ImpUpdCond = ImpUpdCond_enum.LastModDate

					Case "ReleaseDate"
						ImportControlSettings_Obj.ImpUpdCond = ImpUpdCond_enum.ReleaseDate

					Case Else
						Throw New System.Exception(String.Format("ERR: Paramter /ImpUpdCond={0} value invalid or is missing", ImpUpdCond_para))
				End Select

				For Each SiteId As String In SiteList
					ImportResult_Obj.ResetToDefaultValues()
					' InfoSys_Obj.Print("Import IRs and Datasets of site: " + SiteId)
					ImportResult_Obj = JS_ScriptingInterface.Irs_and_Datasets_ImportFromTcDB(InfoSys_Obj, SiteId, ImportControlSettings_Obj)
					' InfoSys_Obj.Print(ImportResult_Obj.ResultMsgEvaluate)
					ScriptReturn_r.ResultMsgAdd("Result import update data of site: " + SiteId)
					ScriptReturn_r.ResultMsgAdd(ImportResult_Obj.ResultMsgEvaluate)
				Next

			Catch ex As Exception
				ScriptReturn_r.Ex = ex
				ScriptReturn_r.ExMsg = "Exception in Script:" + JS_ScriptingInterface.ScriptDef.NameDescription
				ScriptReturn_r.ResultMsgAdd(JF.Tools.JF_ErrHandler_cls.MyAppl_ExToStr(JF_ErrHandler_cls.Appl_ErrMsg_Enum.err0001_NotSpec, ex, "TaskShedule:S:Dataimport_Tc_DataSetsAndIrs.vb Task:" + JS_ScriptingInterface.ScriptDef.NameDescription))
				InfoSys_Obj.PrintEx(ex, "Dataimport_Tc_DataSetsAndIrs.vb")
			End Try

			' Call InfoSys_Obj.Print(ScriptReturn_r.ResultMsgEvaluate)
			'-- #Step: Wait n sec before end
			If EndOfProcessDoWait_msec > 0 Then
				InfoSys_Obj.PrintObject_and_StatusBarSetCurrActionStatusInfo_Panel3("wait", (EndOfProcessDoWait_msec / 1000).ToString("#.#") + " sec")
				System.Threading.Thread.Sleep(EndOfProcessDoWait_msec)
			End If

			Return ScriptReturn_r
		End Function
	End Class
End Namespace




