Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Data.Common
Imports System.Data
Imports System.Collections.Generic
Imports System.Xml
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.CommonEntities.Library.Enumerations

Partial Class Admin_CharterContentManagement_ContentCreation
    Inherits AdminPage

    Private _cmd As DbCommand
    Private _db As Database

    Private ReadOnly Property UnAuthorizedUserRedirectPath() As String
        Get
            Select Case Environment.MachineName.ToLower
                Case "kstlmcc2p01"
                    '#######
                    'PROD WEB
                    '#######
                    Return "http://www.Charter.com/Visitors/Home.aspx"
                Case "kstlmcc2p02"
                    '#######
                    'PROD WEB 
                    '#######
                    Return "http://www.Charter.com/Visitors/Home.aspx"
                Case "kstlmweb66"
                    '#######
                    'UAT WEB 
                    '#######
                    Return "http://cc2uatx.chartercom.com/Visitors/Home.aspx"
                Case "kstlmweb67"
                    '#######
                    'UAT WEB 
                    '#######
                    Return "http://cc2uatx.chartercom.com/Visitors/Home.aspx"
                Case "kstlmcc2p03"
                    '#######
                    'PROD APP 
                    '#######
                    Return "http://cc2prda"
                Case "kstlmcc2p04"
                    '#######
                    'PROD APP 
                    '#######
                    Return "http://cc2prda"
                Case "kstlmweb68"
                    '#######
                    'UAT APP 
                    '#######
                    Return "http://cc2uata"
                Case "kstlmweb69"
                    '#######
                    'UAT APP
                    '#######
                    Return "http://cc2uata"
                Case "kstlmweb65"
                    '#######
                    'DEV WEB AND APP
                    '#######
                    Return "http://KSTLMWEB65:8080"
                Case "kstlintcncegenm"
                    '#######
                    'DEV WEB AND APP
                    '#######
                    Return "http://localhost/bl.remotinghost/login.aspx"
                Case Else
                    '#####################################
                    'THIS SHOULD ONLY BE LOCALHOST STUFF HERE.
                    '#####################################
                    Return "http://www.Charter.com/Visitors/Home.aspx"
            End Select
        End Get
    End Property
    Private Property DB() As Database
        Get
            Return _db
        End Get
        Set(ByVal value As Database)
            _db = value
        End Set
    End Property
    Private Property CMD() As DbCommand
        Get
            Return _cmd
        End Get
        Set(ByVal value As DbCommand)
            _cmd = value
        End Set
    End Property
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.DB = DatabaseFactory.CreateDatabase("Charter.Website.WS.CommerceEngineConnectionString")
    End Sub



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Session("user") Is Nothing Then
                '#####################################
                'SESSION EXPIRED, SEND TO MAIN PAGE
                '#####################################
                Response.Redirect(Me.UnAuthorizedUserRedirectPath())
            End If



            '#########################################################################
            'RIGHT NOW ONLY SITE ADMINS SHOULD BE USING THIS PAGE SO ALLOW ONLY THEM
            '#########################################################################
            Select Case True
                Case Me.IsInRole("ContentAdmin")
                    '#####################################
                    'CONTINUE ON AND GIVE ACCESS TO PAGE
                    '#####################################
                Case Else
                    Response.Redirect(Me.UnAuthorizedUserRedirectPath())
            End Select



            '########################
            'CLEAR INITIAL VALUES
            '########################
            lblMessage.Text = ""
            lblMessage.ForeColor = Drawing.Color.Black



            txtTemplateID.Focus()

        Catch ex As Exception
            lblMessage.Text = ex.Message.ToString()
            lblMessage.ForeColor = Drawing.Color.Red
        End Try
    End Sub





    'Protected Sub ddlTemplateID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTemplateID.SelectedIndexChanged



    'End Sub




    'Protected Sub ddlUserControlID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlUserControlID.SelectedIndexChanged
    '    Dim ownertypeid As Int32 = Int32.Parse(ddlOwnerTypeID.SelectedItem.Value)
    '    Dim usercontrolname As String = Me.ddlUserControlID.SelectedItem.Value

    '    Dim corgTA As DALTableAdapters.Content_OrganizationTableAdapter = New DALTableAdapters.Content_OrganizationTableAdapter()
    '    Dim corgTable As DAL.Content_OrganizationDataTable = corgTA.GetData(ownertypeid, usercontrolname)

    '    Me.ddlOwnerID.Items.Clear()
    '    For Each owneridrow As DAL.Content_OrganizationRow In corgTable
    '        Me.ddlOwnerID.Items.Add(New ListItem(owneridrow.ownerid, owneridrow.ownerid))
    '    Next
    'End Sub

    'Protected Sub ddlOwnerTypeID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlOwnerTypeID.SelectedIndexChanged
    '    Dim ownertypeid As Int32 = Int32.Parse(ddlOwnerTypeID.SelectedItem.Value)
    '    Dim usercontrolname As String = Me.ddlUserControlID.SelectedItem.Value

    '    Dim corgTA As DALTableAdapters.Content_OrganizationTableAdapter = New DALTableAdapters.Content_OrganizationTableAdapter()
    '    Dim corgTable As DAL.Content_OrganizationDataTable = corgTA.GetData(ownertypeid, usercontrolname)

    '    Me.ddlOwnerID.Items.Clear()
    '    For Each owneridrow As DAL.Content_OrganizationRow In corgTable
    '        Me.ddlOwnerID.Items.Add(New ListItem(owneridrow.ownerid, owneridrow.ownerid))
    '    Next
    'End Sub


    Protected Sub btnCreateContentRecord_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateContentRecord.Click
        Try

            Dim lTemplateID As String = txtTemplateID.Text.ToString().Trim().ToLower()
            Dim lUserControlID As String = txtUserControlID.Text.ToString().Trim().ToLower()
            Dim lLocaleID As Integer
            If txtLocaleID.Text.ToString().Trim() <> "" Then
                lLocaleID = CType(txtLocaleID.Text.ToString().Trim().ToLower(), Integer)
            End If

            Dim lOwnerID As String = txtOwnerID.Text.ToString().Trim().ToLower()

            Dim outOwnerTypeID As New OwnerType
            Select Case txtOwnerTypeID.Text.ToString().Trim()
                Case "1"
                    outOwnerTypeID = OwnerType.Bundle
                Case "2"
                    outOwnerTypeID = OwnerType.ServicePlan
                Case "3"
                    outOwnerTypeID = OwnerType.Other
                Case "4"
                    outOwnerTypeID = OwnerType.Feature
                Case "5"
                    outOwnerTypeID = OwnerType.CharterMessages
                Case Else
                    outOwnerTypeID = OwnerType.Other
            End Select

            Dim lRegionID As Integer
            If txtRegionID.Text.ToString().Trim() <> "" Then
                lRegionID = CType(txtRegionID.Text.ToString().Trim().ToLower(), Integer)
            End If

            Dim lAreaID As String = txtAreaID.Text.ToString().Trim().ToLower()
            Dim lBillingStationID As Integer
            If txtBillingStationID.Text.ToString().Trim() <> "" Then
                lBillingStationID = CType(txtBillingStationID.Text.ToString().Trim().ToLower(), Integer)
            End If
            Dim lSourceFTACode As String = txtSourceFTACode.Text.ToString().Trim().ToLower()

            Dim lContentID As Integer
            If txtContentID.Text.ToString().Trim() <> "" Then
                lContentID = CType(txtContentID.Text.ToString().Trim().ToLower(), Integer)
            End If

            Dim lContentVersionCreateByUserID As String
            If Not (Session("userinfo") Is Nothing) Then
                Dim lUserInfo As CMSUserToken = Session("userinfo")
                If Not (lUserInfo Is Nothing) Then
                    lContentVersionCreateByUserID = lUserInfo._userid
                End If
            End If


            Dim lContentText As String = FCKeditor.Value.Trim()
            If lContentText.Trim() = "" Then

            End If
            Dim lContentName As String = txtContentName.Text.ToString().Trim().ToLower()


            '############################
            'CREATE THE CONTENT RECORD.
            '############################
            Call AddContentItem(lTemplateID, lUserControlID, lLocaleID, lOwnerID, outOwnerTypeID, lRegionID, lAreaID, _
                lBillingStationID, lSourceFTACode, lContentID, lContentVersionCreateByUserID, lContentText, lContentName)

        Catch ex As Exception
            lblMessage.Text = ex.Message.ToString()
            lblMessage.ForeColor = Drawing.Color.Red
        End Try


    End Sub


    Private Sub AddContentItem(ByVal lTemplateID As String, ByVal lUserControlID As String, ByVal lLocaleID As Integer, ByVal lOwnerID As String, _
        ByVal lOwnerTypeID As OwnerType, ByVal lRegionID As Integer, ByVal lAreaID As String, ByVal lBillingStationID As Integer, _
        ByVal lSourceFTACode As String, ByVal lContentID As Integer, ByVal lContentVersionCreateByUserID As String, ByVal lContentText As String, _
        ByVal lContentName As String)


        Dim returnCode1 As Integer
        Dim returnCode2 As Integer
        Try
            CMD = DB.GetStoredProcCommand("[CharterDotCom_CMS].[content].[CREATE_CONTENT_AND_CONTENT_VERSION]")


            If lTemplateID <> Nothing And lTemplateID.Trim() <> "" Then
                DB.AddInParameter(CMD, "@TEMPLATENAME", DbType.String, lTemplateID.Trim())
            End If
            If lUserControlID <> Nothing And lUserControlID.Trim() <> "" Then
                DB.AddInParameter(CMD, "@USERCONTROLID", DbType.String, lUserControlID.Trim())
            End If
            If lLocaleID > 0 Then
                DB.AddInParameter(CMD, "@LOCALEID", DbType.Int32, lLocaleID)
            End If
            If lOwnerID <> Nothing And lOwnerID.Trim() <> "" Then
                DB.AddInParameter(CMD, "@OWNERID", DbType.String, lOwnerID.Trim())
            End If

            DB.AddInParameter(CMD, "@OWNERTYPEID", DbType.Int32, lOwnerTypeID)

            If lRegionID > 0 Then
                DB.AddInParameter(CMD, "@REGION_ID", DbType.Int32, lRegionID)
            End If
            If lAreaID <> Nothing And lAreaID.Trim() <> "" Then
                DB.AddInParameter(CMD, "@AREA_ID", DbType.String, lAreaID.Trim())
            End If
            If lBillingStationID > 0 Then
                DB.AddInParameter(CMD, "@BILLING_STATION_ID", DbType.Int32, lBillingStationID)
            End If
            If lSourceFTACode <> Nothing And lSourceFTACode.Trim() <> "" Then
                DB.AddInParameter(CMD, "@SOURCE_FTA_CODE", DbType.String, lSourceFTACode.Trim())
            End If
            If lContentID > 0 Then
                DB.AddInParameter(CMD, "@CONTENTID", DbType.Int32, lContentID)
            End If
            If lContentVersionCreateByUserID > 0 Then
                DB.AddInParameter(CMD, "@CONTENT_VERSION_CREATEBY_USERID", DbType.Int32, lContentVersionCreateByUserID)
            End If
            If lContentText <> Nothing And lContentText.Trim() <> "" Then
                DB.AddInParameter(CMD, "@CONTENTTEXT", DbType.String, lContentText.Trim())
            End If
            If lContentName <> Nothing And lContentName.Trim() <> "" Then
                DB.AddInParameter(CMD, "@CONTENTNAME", DbType.String, lContentName.Trim())
            End If
            DB.AddInParameter(CMD, "@CONTENT_VERSION_CREATEDATE", DbType.DateTime, Now)



            '################################
            'MAKE THE CALL TO THE DB
            '################################
            returnCode1 = DB.ExecuteScalar(CMD) 'THE RETURN VALUE IS THE CONTENT ID

            'If returnCode = 0 Then
            CMD = DB.GetStoredProcCommand("[CharterDotCom].[content].[CREATE_CONTENT_AND_CONTENT_VERSION]")

            If lTemplateID <> Nothing And lTemplateID.Trim() <> "" Then
                DB.AddInParameter(CMD, "@TEMPLATENAME", DbType.String, lTemplateID.Trim())
            End If
            If lUserControlID <> Nothing And lUserControlID.Trim() <> "" Then
                DB.AddInParameter(CMD, "@USERCONTROLID", DbType.String, lUserControlID.Trim())
            End If
            If lLocaleID > 0 Then
                DB.AddInParameter(CMD, "@LOCALEID", DbType.Int32, lLocaleID)
            End If
            If lOwnerID <> Nothing And lOwnerID.Trim() <> "" Then
                DB.AddInParameter(CMD, "@OWNERID", DbType.String, lOwnerID.Trim())
            End If

            DB.AddInParameter(CMD, "@OWNERTYPEID", DbType.Int32, lOwnerTypeID)

            If lRegionID > 0 Then
                DB.AddInParameter(CMD, "@REGION_ID", DbType.Int32, lRegionID)
            End If
            If lAreaID <> Nothing And lAreaID.Trim() <> "" Then
                DB.AddInParameter(CMD, "@AREA_ID", DbType.String, lAreaID.Trim())
            End If
            If lBillingStationID > 0 Then
                DB.AddInParameter(CMD, "@BILLING_STATION_ID", DbType.Int32, lBillingStationID)
            End If
            If lSourceFTACode <> Nothing And lSourceFTACode.Trim() <> "" Then
                DB.AddInParameter(CMD, "@SOURCE_FTA_CODE", DbType.String, lSourceFTACode.Trim())
            End If
            If lContentID > 0 Then
                DB.AddInParameter(CMD, "@CONTENTID", DbType.Int32, lContentID)
            End If
            If lContentVersionCreateByUserID > 0 Then
                DB.AddInParameter(CMD, "@CONTENT_VERSION_CREATEBY_USERID", DbType.Int32, lContentVersionCreateByUserID)
            End If
            If lContentText <> Nothing And lContentText.Trim() <> "" Then
                DB.AddInParameter(CMD, "@CONTENTTEXT", DbType.String, lContentText.Trim())
            End If
            If lContentName <> Nothing And lContentName.Trim() <> "" Then
                DB.AddInParameter(CMD, "@CONTENTNAME", DbType.String, lContentName.Trim())
            End If
            DB.AddInParameter(CMD, "@CONTENT_VERSION_CREATEDATE", DbType.DateTime, Now)



            returnCode2 = DB.ExecuteScalar(CMD) 'THE RETURN VALUE IS THE CONTENT ID
            'If returnCode = 0 Then
            '    '#############################################
            '    'ALL IS GOOD, MESSAGE BACK TO USER AS SUCH
            '    '#############################################

            'Else
            '    '#####################################################################################################################
            '    'THERE IS A PROBLEM WHICH MAY INVOLVE A FAILURE IN ONE OF THE TWO LARGE TRANSACTIONS NEEDED TO CREATE CONTENT RECORDS.
            '    '#####################################################################################################################
            'End If
            'End If

            lblMessage.Text = "Content Record Created Successfully.  Live DB Content ID: " & returnCode1 & " CMS DB Content ID: " & returnCode2
            lblMessage.ForeColor = Drawing.Color.Green
        Catch ex As Exception
            lblMessage.Text = ex.Message.ToString()
            lblMessage.ForeColor = Drawing.Color.Red
        Finally
            If Not CMD Is Nothing Then
                CMD.Dispose()
            End If
        End Try



    End Sub



End Class
