
Event(OnBeforeJobStart)

	Dim fso, DM, target
	Set fso = CreateObject("Scripting.FileSystemObject")
	If fso.FileExists(MASTER_INPUT+".ddf") Then fso.DeleteFile(MASTER_INPUT+".ddf",True)

End Event


InputDatasource(my_MASTER_DataSource)

	ConnectionString = "Provider = mrOleDB.Provider.2; _
						Data Source=mrCsvDsc; _
						Location = "+MASTER_INPUT+"_paired.csv; _
						Initial Catalog = "+MASTER_INPUT+".mdd"

End InputDatasource


OutputDatasource(my_DDF_OutputDataSource)

	ConnectionString = "Provider=mrOleDB.Provider.2; _
						Data Source=mrDataFileDsc; _
						mr Init Overwrite=1; _
						Location="+MASTER_INPUT+".ddf"
	MetadataOutputName=MASTER_INPUT+".mdd"

End OutputDatasource


Event(OnJobStart)

    'Line separator const.
    Const adCR = 13, adCRLF = -1, adLF = 10

	Set dmgrGlobal.CaseData = CreateObject("ADODB.Stream")

	dmgrGlobal.CaseData.CharSet = "utf-8"
	Select Case CRLF
		Case "CR"
			dmgrGlobal.CaseData.LineSeparator = adCR
		Case "CRLF"
			dmgrGlobal.CaseData.LineSeparator = adCRLF
		Case "LF"
			dmgrGlobal.CaseData.LineSeparator = adLF
		Case Else
			Debug.Log("Unknown CRLF.")
	End Select
	dmgrGlobal.CaseData.Open()
	dmgrGlobal.CaseData.LoadFromFile(MASTER_INPUT+"_datastore.csv")

	Set dmgrGlobal.Columns = dmgrGlobal.CaseData.ReadText(-2)

	'Set decimal separator
	Dim oExcel
	dmgrJob.GlobalVariables.Add("DecimalSeparator")
	dmgrJob.GlobalVariables.DecimalSeparator = "."
	Set oExcel = createobject("Excel.Application")
	if oExcel.DecimalSeparator="," Then dmgrJob.GlobalVariables.DecimalSeparator = ","

	dmgrGlobal.Add("vars_failed")
	dmgrGlobal.Add("var_failed")
    dmgrGlobal.vars_failed = ""
    
End Event


Event(OnNextCase)

	const MaxValue = 2147483647
	const MinValue = -2147483648 

	Dim i, oA, oA_value
	Dim columns, col, data
	Dim gSplit, gName, gElement, gField
	Dim response

	data = dmgrGlobal.CaseData.ReadText(-2)
	data = Split(data, ",")
	columns = Split(dmgrGlobal.Columns, ",")

	i = 0
	For Each col in columns
		If col.Find("{") <> -1 Then
			gSplit = col.Split(".")
			gField = gSplit[1]
			gSplit = gSplit[0].Split("[{")
			gName = gSplit[0]
			gElement = gSplit[1].Replace("}]", "")
			Set oA = dmgrJob.Questions[gName].Item[gElement].Item[gField]
		Else
			Set oA = dmgrJob.Questions[col]
		End If
		Select Case oA.QuestionDataType
			Case 1
				If data[i] = "NULL" Then
					oA = NULL
				Else
					oA_value=data[i]
					if dmgrJob.GlobalVariables.DecimalSeparator = "," Then
						oA_value = Replace(CText(oA_value),".",",")
					End If
					If CDouble(oA_value) > MaxValue or CDouble(oA_value) < MinValue then
						Debug.Log("Value: " + CText(oA_value) + " for variable: '" + oA.label + "' exceeds limit for variable of type Long. Dropping set variable")
						If dmgrGlobal.var_failed <> oA.label Then
							dmgrGlobal.vars_failed = dmgrGlobal.vars_failed + oA.label + ", "
							dmgrGlobal.var_failed = oA.label
						End If
					Else
						oA = oA_value
					End If
				End If
			Case 6, 7
				If data[i] = "NULL" Then
					oA = NULL
				Else
					oA_value=data[i]
					if dmgrJob.GlobalVariables.DecimalSeparator = "," Then
						oA_value = Replace(CText(oA_value),".",",")
					End If
					oA = oA_value
				End If
			Case 2
				oA = data[i].Replace(">_>_>", ",")
				If oA = "" Then oA = NULL
			Case 3
				Select Case oA.Validation.MaxValue
					Case 1
						If data[i] <> NULL Then oA = cCategorical(data[i])
					Case Else
						If data[i] <> ";" Then
							For Each response in Split(data[i], ";")
								If response <> "" Then oA = oA + cCategorical(response)
							Next
						End If
				End Select
			Case 5
				If data[i] = "" Then
					oA = Null
				Else
                   Dim parts
                   parts = data[i].Split(".")
                   oA = cDate(parts[0])
				End If
		End Select
		i = i + 1
	Next

End Event

Event (OnJobEnd)

	If dmgrGlobal.vars_failed <> "" Then	
        Debug.Log("**************************************************************************************************")
        Debug.Log("Please note: ")
        Debug.Log("For one or more cases a value exceeded the limit for Dimensions variable type 'Long'.")
        Debug.Log("For those cases setting the variable were skipped.")
        Debug.Log("The variables affected are: " + dmgrGlobal.vars_failed)
        Debug.Log("If you need those variables then change DataType from 'mr.Long' to 'mr.Double' by editing 'create_mdd [].mrs' for affected variables")
        Debug.Log("")
	End If        

End Event

