Public Class Migrate
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
     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

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
      WriteLog(">>> Startup <<<")
      If Request.QueryString("code") Is Nothing Then
        WriteLog("Missing argument.")
        Exit Sub
      End If
      If Request.QueryString("code") <> "sasko" Then
        WriteLog("Missing value.")
        Exit Sub
      End If

      ' Create the document servers.
      WriteLog("Creating ExtantDocumentServer...")
      Dim ExtantServer As IDocumentServer = App.GetDocumentServer(App.DocumentServerType.ExtantServer)

      WriteLog("Creating SppsDocumentServer...")
      Dim SppsServer As IDocumentServer = App.GetDocumentServer(App.DocumentServerType.SppsServer)

      ' Migrate.
      WriteLog("Starting migration...")
      MigrateFolder(ExtantServer, Nothing, "", SppsServer, Nothing)

    Catch ex As Exception
      WriteLog("Exception thrown: " & ex.ToString())
    Finally
      WriteLog(">>> Finish <<<")
    End Try
  End Sub


  Private Sub MigrateFolder( _
   ByVal sourceServer As IDocumentServer, _
   ByVal sourceFolderUrl As String, _
   ByVal sourceFolderName As String, _
   ByVal targetServer As IDocumentServer, _
   ByVal targetFolderParentUrl As String)

    WriteLog(String.Format("Migrating [{0}][{1}] --> [{2}]", sourceFolderUrl, sourceFolderName, targetFolderParentUrl))

    ' If this is not a toplevel source folder, create the target folder.
    Dim TargetFolderUrl As String
    If (sourceFolderUrl Is Nothing) OrElse (sourceFolderUrl.Length = 0) Then
      TargetFolderUrl = String.Empty
    Else
      WriteLog(String.Format("-- Creating folder [{0}].", sourceFolderUrl))
      Try
        TargetFolderUrl = targetServer.CreateFolder(targetFolderParentUrl, sourceFolderName)
        WriteLog(String.Format("-- [{0}] created.", TargetFolderUrl))
      Catch ex As Exception
        WriteLog(String.Format("-- Folder NOT created, error [{0}].", ex.ToString()))
        Exit Sub
      End Try
    End If

    ' Get a list of documents and create them in the target folder.
    Dim SourceDocuments As DataTable = sourceServer.GetDocuments(sourceFolderUrl)
    Dim SourceDocumentRow As DataRow, SourceStream As System.IO.Stream, MimeType As String, TargetDocumentUrl As String
    For Each SourceDocumentRow In SourceDocuments.Rows
      WriteLog(String.Format("---- Migrating document [{0}].", SourceDocumentRow("Url").ToString()))
      Try
        SourceStream = sourceServer.GetDocumentContent(SourceDocumentRow("Url").ToString(), MimeType)
        TargetDocumentUrl = targetServer.CreateDocument( _
         TargetFolderUrl, SourceDocumentRow("Author").ToString(), SourceDocumentRow("Description").ToString(), _
         CType(SourceDocumentRow("IssueDate"), DateTime), CType(SourceDocumentRow("MasterNumber"), Integer), _
         CType(SourceDocumentRow("SupplementNumber"), Integer), MimeType, _
         SourceDocumentRow("FileTitle").ToString(), SourceStream)
        WriteLog(String.Format("---- Document migrated [{0}].", TargetDocumentUrl))
      Catch ex As Exception
        WriteLog(String.Format("---- Document NOT migrated, error [{0}].", ex.ToString()))
      Finally
        If Not SourceStream Is Nothing Then
          SourceStream.Close()
        End If
      End Try
    Next

    ' Get a list of subfolders and migrate the calling itself recursively.
    Dim SourceSubfolders As DataTable = sourceServer.GetFolders(sourceFolderUrl)
    Dim SourceFolderRow As DataRow
    For Each SourceFolderRow In SourceSubfolders.Rows
      MigrateFolder(sourceServer, SourceFolderRow("Url").ToString(), SourceFolderRow("Name").ToString(), targetServer, TargetFolderUrl)
    Next

  End Sub

  Private Sub WriteLog(ByVal text As String)
    Response.Write(System.DateTime.Now.ToString())
    Response.Write(" " & Server.HtmlEncode(text) & "
") Response.Flush() End Sub End Class