<!--#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/ResourceCreate/
'			CNCASPPageManager_ResourceCreateAcceptor.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 Charset
'	- Property ResourceGallery
'	- Property Resource
'	- Property URLBack2ResourceCreateLink
'	- Property Error
' ********************************************************************

Class CNCASPPageManager_ResourceCreateAcceptor

	' -- The Charset ASP page will be running under
	Public Charset

	' -- objects needed in the ASP page
	Public Resource
	Public ResourceGallery
	Public URLBack2ResourceCreateLink
	Public Error

	Private m_strGalleryGUID
	Private m_nScriptTimeOut
	Private m_bBuffer

	' ---- [Private] Class standard initialization routine
	Private Sub Class_Initialize()
		Set ResourceGallery = Nothing
		Set Resource = Nothing
		URLBack2ResourceCreateLink = "#"
		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 (ResourceGallery Is Nothing) Then
			Call CleanupRequestStream()
			Error = IDS_ERR_GALLERY_NOT_FOUND
			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_strGalleryGUID = GetGuidFromQueryString(IDS_QPARAM_GALLERY)
		Set ResourceGallery = AutoSession.Searches.GetByGUID( m_strGalleryGUID )
	End Sub

	' ---- [Private] Set up the MLS info this ASP page will be running under
	Private Sub SetupMLSInfo()
		Charset = GetCharacterSetForResource( ResourceGallery )
	End Sub

	' ---- [Private] Set up URL needed for links in the ASP page
	Private Sub SetupLinksURL()
		URLBack2ResourceCreateLink = "ResourceCreate.asp?" & IDS_QPARAM_GALLERY & "=" & m_strGalleryGUID
	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( ResourceGallery, 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


		' -- Create the ResourceGallery item from the local file
		Set Resource = ResourceGallery.CreateResource( strTempFilename )
		
		If (AutoSession.LastError.ID <> 0) Then
			Call AutoSession.RollbackAll()
			Error = IDS_ERR_RESOURCE_CREATE
			Exit Sub
		End If

		' -- Set content to Resource from local file and form submitted 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()
		
		' -- 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

%>