Imports EDSL.Entities
Imports EDSL.DAL
Imports System.IO

Partial Public Class CSV1
    Inherits BasePage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim objMainPage As New dbMainPage
        Dim objMainPageEntity As New entMainPage

        objMainPageEntity = objMainPage.GetPageDetails("EnquiryAlertsAdminEdit")
        Me.Title = ConfigurationManager.AppSettings("BrowserTitleCompanyName") & objMainPageEntity.PageTitle
        divContent.InnerHtml = objMainPageEntity.PageContent
        Me.MetaName = objMainPageEntity.PageMetaName
        Me.MetaContent = objMainPageEntity.PageMetaContent

        Call LoadData()
    End Sub
    Private Sub LoadData()
        Dim objMainVacancy As List(Of entMainProduct)

        objMainVacancy = GetAll()
        Dim strCSV As String = ""
        ' Build the excel header columns
        strCSV &= "Sector|Title|Description|Summary|Ref|Salary|SalaryText|Consultant|Location|Active|CreatedDate|VacancyLocation"

        strCSV &= vbCrLf
        For Each objVacancy In objMainVacancy
            With objVacancy
                Dim active As String = "No"
                If .Active = "1" Then
                    active = "Yes"
                End If

                Dim dbVacancy As New dbMainProduct
                .LocationList = dbVacancy.GetProductLocations(.ProductID)
                Dim locationNameList As String = ""
                For Each locationID In .LocationList
                    Dim dbLocation As New dbMainLocations
                    Dim location As New entMainLocations
                    location = dbLocation.GetLocationByID(Convert.ToInt32(locationID.ToString()))
                    locationNameList &= location.LocationValue & ","
                Next
                .ProductDesc1 = .ProductDesc1.Replace("<p>", "")
                .ProductDesc1 = .ProductDesc1.Replace("</p>", "")
                .ProductDesc1 = .ProductDesc1.Replace(vbCrLf, " ")
                .ProductDesc1 = .ProductDesc1.Replace("<br />", "")
                .ProductDesc1 = .ProductDesc1.Replace(vbCr, " ")
                .ProductDesc1 = .ProductDesc1.Replace(vbLf, " ")

                .ProductDesc2 = .ProductDesc2.Replace("<p>", "")
                .ProductDesc2 = .ProductDesc2.Replace("</p>", "")
                .ProductDesc2 = .ProductDesc2.Replace(vbCrLf, " ")
                .ProductDesc2 = .ProductDesc2.Replace("<br />", "")
                .ProductDesc2 = .ProductDesc2.Replace(vbCr, " ")
                .ProductDesc2 = .ProductDesc2.Replace(vbLf, " ")
                strCSV &= .SectorLabel & "|" & .ProductName & "|" & .ProductDesc1 & "|" & .ProductDesc2 & "|" & .ProductDesc3 & "|" & .Salary.ToString() & "|" & .SalaryText & "|" & .ConsultantName & "|" & .Location & "|" & active & "|" & .CreatedDate.ToString() & "|" & locationNameList & vbCrLf
            End With
        Next

        Dim strFilename As String = CreateTextFile(strCSV)
        If strFilename = "" Then
            divGetCSV.Visible = False
            divErrorPanel.InnerHtml = "Error creating CSV file"
        Else
            divGetCSV.Visible = True
            hypCSV.NavigateUrl = ConfigurationManager.AppSettings("TempFileURL") & strFilename
        End If
    End Sub
    Private Function GetAll() As List(Of entMainProduct)
        Dim objMain As New dbMainProduct
        Dim whereClause As String
        whereClause = " AND MainProduct.Active = 1 "
        Dim objMainProduct As List(Of entMainProduct) = objMain.GetAll(whereClause, "")
        Return objMainProduct
    End Function
    Protected Sub btnBack_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx?id=" & Request.QueryString("id"))
    End Sub
    Private Function CreateTextFile(ByVal strCSV As String) As String
        Try

            Dim strTempDirectory As String = ConfigurationManager.AppSettings("TempFilePath")
            Dim objFolder As New IO.DirectoryInfo(strTempDirectory)
            If objFolder.Exists = False Then
                Call MkDir(strTempDirectory)
            End If

            Dim clsMisc As New miscFunctions
            Dim strFileName As String = clsMisc.GetFilenameTimestamp & "VacancyCSVFile.csv"

            Dim objFile As New IO.FileInfo(strTempDirectory & strFileName)
            If objFile.Exists = True Then
                objFile.Delete()
            End If

            Dim oStreamWriter As StreamWriter
            oStreamWriter = File.CreateText(strTempDirectory & strFileName)
            oStreamWriter.Write(strCSV)
            oStreamWriter.Close()

            Return strFileName
        Catch ex As Exception
            Return ""
        End Try
    End Function
End Class