﻿
Partial Class order_centre_DownloadJob
    Inherits System.Web.UI.Page

    Private _webAdapter As New WebWS()
    Public ReadOnly Property WebAdapter() As WebWS
        Get
            Return _webAdapter
        End Get
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim downloadId As String = ""
        If Not Request.QueryString("downloadId") Is Nothing Then
            downloadId = Request.QueryString("downloadId")
		Else
			If Not Request.QueryString("id") Is Nothing Then
				downloadId = Request.QueryString("id")
			End If
        End If
		

        If Not downloadId.Equals("") Then
            Dim _webWS As New WebWS()
            Dim _jobId As String = _webWS.GetJobIdFromDownloadId(downloadId)
            Dim _sourceBasePath As String = ConfigurationManager.AppSettings("JobFilesPath") + "\" + _jobId
            Dim _destinationPath As String = Request.PhysicalApplicationPath.ToString() + "order_centre\downloads\" + downloadId.ToString() + ".zip"            


  	    Dim _forceNewDownload As Boolean = False
            If Not Request.QueryString("force") Is Nothing Then
                _forceNewDownload = True
            End If
            If _forceNewDownload Then
                If System.IO.File.Exists(_destinationPath) Then
                    System.IO.File.Delete(_destinationPath)
                End If
            End If

            'Response.Write(_destinationPath)
			If (_jobId.Length > 1) Then
				Dim _zipResults As String = "OK"
				If Not System.IO.File.Exists(_destinationPath) Then
					Try
						_zipResults = com.racadtech.Framework.Util.IO.Zip.ZipFiles(_sourceBasePath, _destinationPath, 0)
                        Response.Write("ZipResults: " + _zipResults + "<br />")
                        'Response.Write("Source: " + _sourceBasePath + "<br />")
					Catch ex As Exception
						Response.Write("Ex:" + ex.Message + "<br />")
					End Try
				End If

				If Not System.IO.File.Exists(_destinationPath) Then
					' File might of been uploaded from print driver via. " File -> Print ",
					' In this case we should attempt to copy this file from the print driver temp location.
					Dim _pdTempUploadDirectory As String = "L:\Sites\services.racadtech.com\PrintIt\Temp"
					Dim _jobDS As New Data.DataSet
					_jobDS = WebAdapter.GetFilesByJobId(Integer.Parse(_jobId))
                    Dim _fileName As String = _jobDS.Tables(0).Rows(0).Item("FileName")
					If System.IO.File.Exists(_pdTempUploadDirectory + "\" + _fileName) Then
						System.IO.Directory.CreateDirectory(_sourceBasePath)
						System.IO.File.Copy(_pdTempUploadDirectory + "\" + _fileName, _sourceBasePath + "\" + _fileName)
						com.racadtech.Framework.Util.IO.Zip.ZipFiles(_sourceBasePath, _destinationPath)
					Else
						Response.Write("----- [Pending] ----- <br />")
					End If
                End If
			End If

            ' Second attempt at trying to find zip file. ;)
            If Not System.IO.File.Exists(_destinationPath) Then
                Response.Write("Your job has not been processed yet. <br /><br />Please check back shortly to see if your job has completed.  <br />If you have not been able to download your job after a couple hours, please contact support for further assistance.")
				'Response.Write(_sourceBasePath + "<br />")
				'Response.Write(_destinationPath + "<br />")
				'Response.Write(_zipResults + "<br />")
            Else
                Response.ContentType = "application/zip"
                Response.Redirect("~/order_centre/downloads/" + downloadId.ToString() + ".zip")
                Response.End()
            End If
        Else
            Response.Write("Invalid Request")
        End If
    End Sub
End Class
