Imports BaseClasses.Utils
Imports BaseClasses.Data

Namespace Training2
    Public Class ExportFieldValue
        Inherits Training2.UI.BaseApplicationPage

#Region " Generated Code for Web Form Designer "
#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region
#End Region

        Public Sub New()
            MyBase.New()
        End Sub

        Public ReadOnly Property FieldId() As String
            Get
                Return Me.Decrypt(Me.Request.Params.Item("Field"))
            End Get
        End Property

        Public ReadOnly Property FileName() As String
            Get
                Return Me.Request.Params.Item("FileName")
            End Get
        End Property
        
        Public ReadOnly Property RecordId() As String
            Get
                Return Me.Decrypt(Me.Request.Params.Item("Record"))
            End Get
        End Property

        Public ReadOnly Property TableId() As String
            Get
                Dim errMsg As String
                Dim TYPE_FORMAT As String = "{0}.{1}{2},{3}"
                Dim tableName As String = Me.Request.Params.Item("Table")
                tableName = Me.Decrypt(tableName)
                tableName = tableName.Replace(" ", "_")

                ' Check if it is a table.
                Try
                    Dim tableType As String = String.Format(TYPE_FORMAT, "Training2.Business", tableName, "Table", "App_Code")
                    Type.GetType(tableType, True, True)
                    Return tableType
                Catch ex As Exception
                    errMsg = ex.Message
                End Try

                ' Check if it is a view.
                Try
                    Dim viewType As String = String.Format(TYPE_FORMAT, "Training2.Business", tableName, "View", "App_Code")
                    Type.GetType(viewType, True, True)
                    Return viewType
                Catch ex As Exception
                    errMsg = ex.Message
                End Try

                ' Check if it is a query.
                Try
                    Dim queryType As String = String.Format(TYPE_FORMAT, "Training2.Business", tableName, "Query", "App_Code")
                    Type.GetType(queryType, True, True)
                    Return queryType
                Catch ex As Exception
                    errMsg = ex.Message
                End Try

                ' If all failed return empty dtring.
                Return ""
            End Get
        End Property

        Public ReadOnly Property Offset() As Integer
            Get
                If Not IsNothing(Me.Request.Params.Item("Offset")) Then
                    Return CInt(Me.Request.Params.Item("Offset"))
                Else
                    Return 0
                End If
            End Get
        End Property
        Public ReadOnly Property ImageHeight() As Integer
	    Get
		If Not IsNothing(Me.Request.Params.Item("ImageHeight")) Then
			Return CInt(Me.Request.Params.Item("ImageHeight"))
		Else
		    Return 0
		End If
	    End Get
	End Property
	    Public ReadOnly Property ImageWidth() As Integer
	    Get
		If Not IsNothing(Me.Request.Params.Item("ImageWidth")) Then
			Return CInt(Me.Request.Params.Item("ImageWidth"))
		 Else
		    Return 0
		End If
	    End Get
        End Property
        Public ReadOnly Property ImagePercentSize() As Double
            Get
                If Not IsNothing(Me.Request.Params.Item("ImagePercentSize")) Then
                    Return CDbl(Me.Request.Params.Item("ImagePercentSize"))
                Else
                    Return 0.0
                End If
            End Get
        End Property
        ' Handler for the Load event.
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Validate the URL params
            If ((Len(BaseClasses.Utils.GetUrlParam(Me, "Table", True)) < 1) OrElse _
                (Len(BaseClasses.Utils.GetUrlParam(Me, "Record", True)) < 1) OrElse _
                (Len(BaseClasses.Utils.GetUrlParam(Me, "Field", True)) < 1) _
            ) Then
                Return
            End If
            Me.DataBind() 'Always call DataBind, even on PostBacks.
            Me.ExportData() 'Then export the data
        End Sub

        Protected Sub ExportData()
            Try
                Dim t As PrimaryKeyTable = BaseTable.CreateInstance(Me.TableId)
                Dim rec As IRecord = t.GetRecordData(Me.RecordId, False)
                If Not (Me.ImagePercentSize = 0.0) Or Not (Me.ImageHeight = 0 Or Me.ImageWidth = 0) Then
                    ' To display image with shrinking according to user specified height/width or ImagePercentSize
                    Dim fieldData As ColumnValue = MiscUtils.GetData(rec, _
                                             t.TableDefinition.ColumnList.GetByAnyName(Me.FieldId) _
                                           )
                    Dim binaryData() As Byte = MiscUtils.GetBinaryData(t.TableDefinition.ColumnList.GetByAnyName(Me.FieldId), _
                                                              fieldData)
                    Dim thumbNailSizeImage() As Byte = GetThumbNailSizeImage(binaryData)
                    Dim filName As String = MiscUtils.GetFileNameWithExtension(t.TableDefinition.ColumnList.GetByAnyName(Me.FieldId), _
                                          binaryData, _
                                        Nothing)
                    MiscUtils.SendToWriteResponse(Me.Response, _
                                       thumbNailSizeImage, _
                                      filName, _
                                      t.TableDefinition.ColumnList.GetByAnyName(Me.FieldId), _
                                         fieldData, _
                                       Me.Offset)
                Else
                'Calling ExportFieldData method without image shrinking.
                MiscUtils.ExportFieldData( _
                    Me.Response, _
                    rec, _
                    t.TableDefinition.ColumnList.GetByAnyName(Me.FieldId), _
                    FileName, _
                    Me.Offset)
                End If

            Catch ex As Exception
            End Try
        End Sub
        Protected Function GetThumbNailSizeImage(ByVal binaryData() As Byte) As Byte()
            Dim ThumbNailBinaryData() As Byte = Nothing
            Try
                Dim TempMemStream As New System.IO.MemoryStream(binaryData)
                Dim ImageObj As System.Drawing.Image
                Dim ThumbSizeImageObj As System.Drawing.Image
                ImageObj = System.Drawing.Image.FromStream(TempMemStream)
                Dim temHeight As Integer
                Dim temWidth As Integer
                temHeight = ImageObj.Height
                temWidth = ImageObj.Width
                Dim TempFileStream As New System.IO.MemoryStream
                'If Imagesize is less than 20*20 then return the original image
                If (temWidth < 20 And temHeight < 20) Then
                    Dim ImageBinaryData() As Byte
                    ImageObj.Save(TempFileStream, System.Drawing.Imaging.ImageFormat.Jpeg)

                    ImageBinaryData = New Byte(CType(TempFileStream.Length, Integer)) {}
                    ImageBinaryData = TempFileStream.ToArray()
                    Return ImageBinaryData
                End If
                Dim ImagePercentSize As Double = Me.ImagePercentSize
                ' If the ImagePercentSize is 0.2 then the actual percent calculation will result in generating
                ' temHeight and temWidth as Zero. When the height and width is zero, GetThumbnailImage() 
                ' will generate image of arbitrary size. Hence the image will not display as predicted, 
                ' ie, with size 0.2%. For this purpose, to maintain consistant image size, when ImagePercentSize 
                ' is less than 1, it is taken as actual percentage for eg. when ImagePercentSize is 0.2, it is considered as 20%
                ' and image will displayed with 20% of original size.
                If (Me.ImagePercentSize <= 1) Then
                    ImagePercentSize = Me.ImagePercentSize * 100
                End If
                'Actual percentage calculation. as ImagePercentSize value provided by client is a number not a percent
                ImagePercentSize = ImagePercentSize / 100
                temHeight = CInt(ImagePercentSize * temHeight)
                temWidth = CInt(ImagePercentSize * temWidth)
                'Create thumbnail size of the original Image ImageObj
                If (Me.ImagePercentSize = 0.0) Then
                    ThumbSizeImageObj = ImageObj.GetThumbnailImage(Me.ImageWidth, Me.ImageHeight, Nothing, IntPtr.Zero)
                Else
                    ThumbSizeImageObj = ImageObj.GetThumbnailImage(temWidth, temHeight, Nothing, IntPtr.Zero)
                End If
                ThumbSizeImageObj.Save(TempFileStream, System.Drawing.Imaging.ImageFormat.Jpeg)

                ThumbNailBinaryData = New Byte(CType(TempFileStream.Length, Integer)) {}
                ThumbNailBinaryData = TempFileStream.ToArray()
            Catch ex As Exception
            End Try
            Return ThumbNailBinaryData
        End Function
        'Since this is a "utility" page, do not log requests for it in the Session's Navigation History
        Protected Overrides Sub UpdateSessionNavigationHistory()
            'Do Nothing
        End Sub

    End Class
End Namespace


