<!--#include virtual="/NR/System/WBC/Internals/Main/WBCApp.inc"-->
<!-- #include virtual="/NR/System/WBC/Customizable/WBCFuncPagesConfig/MLSDisplayConfig/ResourceBrowserConfig.inc" -->
<!--#include virtual="/NR/System/WBC/Customizable/WBCFuncPagesConfig/UIStringConstants/ResMgntUIStrings.inc" -->
<!--#include virtual="/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/ResourceBrowser/Management/InternalProgrammingConstants.inc" -->
<!--#include virtual="/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/ResourceBrowser/Common/ResourceBrowserCommon.inc" -->
<!--#include virtual="/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/ResourceBrowser/Management/ResourceFileIO.inc" -->

<%
' ********************************************************************
' Copyright (C) 2001 Microsoft Corporation. All rights reserved.
'
'	IMPORTANT.  Please read the legal.txt file, located in the
'	"<CMS Install Directory>\Server\httpexec\WBC\Internals"
'	directory, governing the use of this internal file."
' ********************************************************************

' ********************************************************************
' File:		/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/ResourceBrowser/
'			Management/ResourceReplace/
'			CNCASPPageManager_ResourceReplaceAcceptor.inc
' Version: 	Microsoft Content Management Server
' Author:	Microsoft Corporation.
' Purpose:	Encapsulate the ASP logic associated with ResourceReplace.asp
'
' Implements the INCASPPageManager interface:
'	- Sub InitManager()
'	- Property Error
'
' Exposes the following methods/properties:
'	- Property ASPUtilsObject
'	- Property CodePage
'	- Property Charset
'	- Property Resource
'	- Property URLBack2ResourceReplaceLink
' ********************************************************************

Class CNCASPPageManager_ResourceReplaceAcceptor

	' -- MLS info
	Public ASPUtilsObject
	Public CodePage
	Public Charset

	' -- objects needed in the ASP page
	Public Resource
	Public URLBack2ResourceReplaceLink
	Public Error

	Private m_strResourceGUID
	Private m_nScriptTimeOut
	Private m_bBuffer

	' ---- [Private] Class standard initialization routine
	Private Sub Class_Initialize()
		Set ASPUtilsObject = Nothing
		Set Resource = Nothing
		URLBack2ResourceReplaceLink = "ResourceReplace.asp"
		Error = ""
	End Sub

	' ---- [Public] InitManager will take care of all programming logic in the ASP page
	Public Sub InitManager()
		' -- set up Gallery context and its associated MLS info
		Call SetupBrowseContext()
		If (Resource Is Nothing) Then
			Error = IDS_ERR_RESOURCE_NOT_FOUND
			Call CleanupRequestStream()
			Exit Sub
		End If
		Call SetupMLSInfo()

		' -- Set up URL needed for links in the ASP page
		Call SetupLinksURL()

		Call SetServerState()
		Call ReadAndSetResourceContent()
		Call RestoreServerState()
	End Sub

	' ---- [Private] Sets up the browsing context (Resource)
	Private Sub SetupBrowseContext()
		m_strResourceGUID = GetGuidFromQueryString(IDS_QPARAM_RESOURCE)
		Set Resource = AutoSession.Searches.GetByGUID( m_strResourceGUID )
	End Sub

	' ---- [Private] Set up the MLS info this ASP page will be running under
	Private Sub SetupMLSInfo()
		Set ASPUtilsObject = GetInitializedASPUtils()
		CodePage = GetCodePageForResource( Resource.Parent, ASPUtilsObject )
		Charset = GetCharacterSetForResource( Resource.Parent )
	End Sub

	' ---- [Private] Set up URL needed for links in the ASP page
	Private Sub SetupLinksURL()
		URLBack2ResourceReplaceLink = "ResourceReplace.asp?" & IDS_QPARAM_RESOURCE & "=" & m_strResourceGUID
	End Sub

	' ---- [Private] Set the server state to brace for big upload
	Sub SetServerState()
		m_nScriptTimeOut = Server.ScriptTimeOut
		m_bBuffer = Response.Buffer
		Server.ScriptTimeOut = 10000
		Response.Buffer = true
	End Sub

	' ---- [Private] restore Server to previous state
	Sub RestoreServerState
		Server.ScriptTimeOut = m_nScriptTimeOut
		Response.Buffer = m_bBuffer
	End Sub

	' ---- [Private] Read data from the form and set properties on Resource object
	Private Sub ReadAndSetResourceContent()
		' -- initialize Error to empty value
		Error = ""

		' -- there could be lots of places that can generate errors here
		On Error Resume Next
		Call AutoSession.ClearLastError()

		' -- set up the FormReader
		Dim pASPUtils, pFormReader
		Dim lCodePage
		Set pASPUtils = GetInitializedASPUtils()

		If (pASPUtils is Nothing) Then
			Error = IDS_ERR_INIT_FORMREADER
			Call CleanupRequestStream()
			Exit Sub
		End If


		lCodePage = GetCodePageForResource( Resource.Parent, pASPUtils )
		Set pFormReader = GetResMgntFormReaderSafe( False, lCodePage )
		If (Err.Number <> 0) Then
			Error = IDS_ERR_INIT_FORMREADER
			Call CleanupRequestStream()
			Exit Sub
		End If


		'Note: we only set this once and no need to reset to previous
		'      value when the upload is finished.  This is because
		'      ROM will stop give out error if we reset this flag after
		'      the upload
		pFormReader.UploadValidation = True


		' -- upload File content to a local file on server
		Dim strTempFilename
		strTempFilename = AcceptFileUpload( pFormReader, IDS_FORM_FILE_DATA )
		If (Err.Number <> 0) Then
			Error = IDS_ERR_FILE_UPLOAD
			Call CleanupRequestStream()
			Exit Sub
		End If


		' -- Set content to Resource from local file
		Call Resource.SetContentFromFile( strTempFilename )
		If (AutoSession.LastError.ID <> 0) Then
			Call AutoSession.RollbackAll()
			Error = IDS_ERR_RESOURCE_SETCONTENT
			Exit Sub
		End If


		' -- Set properties to Resource from submitted form values.
		' 	 Refer to TRACK #5053 as to why we need to do the empty string
		'	 check before setting DisplayName and Description
		Resource.Name = pFormReader.Files(IDS_FORM_FILE_DATA).FileName
		If pFormReader.Form(IDS_FORM_RESOURCE_DISPLAY_NAME) = "" Then
			Resource.DisplayName = ""
		Else
			Resource.DisplayName = pFormReader.Form(IDS_FORM_RESOURCE_DISPLAY_NAME)
		End if
		If pFormReader.Form(IDS_FORM_RESOURCE_DESCRIPTION) = "" Then
			Resource.Description = ""
		Else
			Resource.Description = pFormReader.Form(IDS_FORM_RESOURCE_DESCRIPTION)
		End if
		
		If (AutoSession.LastError.ID <> 0) Then
			Call AutoSession.RollbackAll()
			Error = IDS_ERR_RESOURCE_SETPROPS
			Exit Sub
		End if

		Call AutoSession.CommitAll()
		If (AutoSession.LastError.ID <> 0) Then
			Error = IDS_ERR_RESOURCE_SETPROPS
			Call CleanupRequestStream()
		End if

		' -- finally, clean up the temporary file
		Dim pFSO
		Set pFSO = CreateObject( "Scripting.FileSystemObject" )
		Call pFSO.DeleteFile( strTempFilename )
		If (Err.Number <> 0) Then
			Error = IDS_ERR_FILE_CLEANUP
			Exit Sub
		End If

	End Sub


End Class

%>