<%
Option Explicit

'
' Comments
' In staging mode all URLs are surrounded with <NCOMPASSSTAGINGSERVER></NCOMPASSSTAGINGSERVER> tags.
'

Dim nWriteCount
nWriteCount = 0
Response.Buffer = true

'---- Set up some Global Variables
Dim g_AutoSession
Dim g_Server
Dim g_ThisChannel
Dim g_ThisGallery

'======'
' MAIN '
'======'
	Err.Clear
	On Error Resume Next

	Server.ScriptTimeout = 60 * 60 * 10	'Set script timeout to be 10 hours

	CheckErrorStatus("Main")

	Set g_AutoSession = Server.CreateObject("ResolutionObjectModel.AutoSession3")

	CheckErrorStatus("Main")

	g_AutoSession.AuthenticateUsingToken

	CheckErrorStatus("Main - Trying to Authenticate.")

	Response.ContentType = "text/xml"

	WriteLn("<?xml version=""1.0""?>")	' Write the nice CDF header stuff on top.
	WriteLn("<SITEMAP version=""1.0"">")

	Set g_Server = g_AutoSession.Server

	CheckErrorStatus("Main - Trying to get server object from g_AutoSession")

	Set g_ThisChannel = g_AutoSession.RootChannel

	CheckErrorStatus("Main - Trying to get root channel from g_AutoSession")

	If Not g_ThisChannel Is Empty Then
		Call PrintChannel(g_ThisChannel, 0)
	End If

	Set g_ThisGallery = g_AutoSession.RootResourceGallery

	CheckErrorStatus("Main - Trying to get root resource gallery from g_AutoSession")

	If Not g_ThisGallery Is Nothing Then
		Call PrintResourceGallery(g_ThisGallery, 0)
	End If

	WriteLn("</SITEMAP>")

	Indent(0)

'========='
' end MAIN'
'========='

'========='
'Functions'
'========='

'-----------------------------------------------------------------
'HomePort Utilities

'--- Pretty-Print an Error Message and stop processing
Sub PrintError(strFunctionName)
	WriteLn( "<P>Sitemap.asp: Err in Function " & strFunctionName & "<BR>" )
	WriteLn( "Err Number = " & Err.Number & "<BR>" )
	WriteLn( "Err Desc = " & Err.Description & "</P>" )
	Response.End
End Sub

Sub CheckErrorStatus(functionname)
	If Err.Number <> 0 Then
		PrintError(functionname)
	End If
End Sub

'---- Write out the given String
Sub Write( Str )
	Response.Write( Str )
	if not Response.IsClientConnected then
		Response.End
	end if

	nWriteCount = nWriteCount + 1
	if nWriteCount > 50 then
		nWriteCount = 0
		Response.Flush
	end if
End Sub

'---- Write out the given string with a CrLF at the end
Sub WriteLn( Str )
	Response.Write( Str & vbCrLf )
	if not Response.IsClientConnected then
		Response.End
	end if

	nWriteCount = nWriteCount + 1
	if nWriteCount > 50 then
		nWriteCount = 0
		Response.Flush
	end if
End Sub

'---- Create an indent of the specified level by writing out spaces
Sub Indent(i)
	Dim s
	s = String(i*3, " ")
	Response.Write s

	if not Response.IsClientConnected then
		Response.End
	end if

	nWriteCount = nWriteCount + 1
	if nWriteCount > 50 then
		nWriteCount = 0
		Response.Flush
	end if
End Sub

Sub PrintChannel(theChannel, nIndentDepth)
	Err.Clear
	On Error Resume Next

	Dim SubChannels
	Dim Postings
	Dim Child
	Dim i

	Indent(nIndentDepth)
	WriteLn("<CHANNEL>")

	Call Write_Channel_HREF(theChannel, nIndentDepth+1)
	Call Write_Channel_LASTMOD(theChannel, nIndentDepth+1)
	Call Write_Channel_GUID(theChannel, nIndentDepth+1)
	Call Write_Channel_Title(theChannel, nIndentDepth+1)

	Set SubChannels = theChannel.VisibleSubchannels 			'Print out all the user-subscribed subchannels

	If Not IsEmpty(SubChannels) Then
		If SubChannels.Count > 0 Then
			Call SubChannels.SortBy("Name")
			Call SubChannels.SortBy("EffectiveDate")
			For i=0 to SubChannels.Count - 1
				Set Child = SubChannels(i)
				Call PrintChannel(Child, nIndentDepth+1)
			Next
		End If
	End If

	CheckErrorStatus("PrintChannel")

	Set Postings = theChannel.Postings

	CheckErrorStatus("PrintChannel")

	If Not IsEmpty(Postings) Then
		If Postings.Count > 0 Then
			Call Postings.SortBy("Name")
			Call Postings.SortBy("EffectiveDate")
			For i=0 to Postings.Count - 1
				Set Child = Postings(i)
				Call PrintPageView(Child, nIndentDepth+1)
			Next
		End If
	End If

	CheckErrorStatus("PrintChannel")

	Indent(nIndentDepth)
	WriteLn("</CHANNEL>")

	CheckErrorStatus("PrintChannel")
End Sub

'============================================================================
Sub PrintPageView(thePosting, nIndentDepth)
	Err.Clear
	On Error Resume Next

	CheckErrorStatus("PrintPageView")

	Call Indent(nIndentDepth)
	Call WriteLn("<ITEM>")

	Call Write_Posting_TITLE(thePosting, nIndentDepth+1)
	Call Write_Posting_LASTMOD(thePosting, nIndentDepth+1)
	Call Write_Posting_HREF(thePosting, nIndentDepth+1)
	Call Write_Posting_GUID(thePosting, nIndentDepth+1)

	CheckErrorStatus("PrintPageView")

	Call Indent(nIndentDepth)
	Call WriteLn("</ITEM>")

	CheckErrorStatus("PrintPageView")
End Sub


Sub PrintResourceGallery(theResourceGallery, nIndentDepth)
	Err.Clear
	On Error Resume Next

	Dim SubGallery
	Dim SubGalleries
	Dim ResourceGalleryItem
	Dim ResourceGalleryItems
	Dim Child
	Dim i

	Indent(nIndentDepth)
	WriteLn("<RESOURCEGALLERY>")
	Call WriteTitle(theResourceGallery, nIndentDepth+1)
	Call WriteLn("<LASTMOD>" + ConvertDateToISO(theResourceGallery.LastModifiedDate) + "</LASTMOD>")
	Call WriteLn("<GUID>" + theResourceGallery.GUID  + "</GUID>")

	Set SubGalleries = theResourceGallery.ResourceGalleries

	If Not IsEmpty(SubGalleries) Then
		If SubGalleries.Count > 0 Then
			For i=0 to SubGalleries.Count - 1
				Set Child = SubGalleries(i)
				Call PrintResourceGallery(Child, nIndentDepth+1)
			Next
		End If
	End If

	CheckErrorStatus("PrintResourceGallery")

	Set ResourceGalleryItems = theResourceGallery.Resources

	CheckErrorStatus("PrintResourceGallery")

	If Not IsEmpty(ResourceGalleryItems) Then
		If ResourceGalleryItems.Count > 0 Then
			For i=0 to ResourceGalleryItems.Count - 1
				Set ResourceGalleryItem = ResourceGalleryItems(i)
				Call PrintResourceGalleryItem(ResourceGalleryItem, nIndentDepth+1)
			Next
		End If
	End If

	CheckErrorStatus("PrintResourceGallery")

	Indent(nIndentDepth)
	WriteLn("</RESOURCEGALLERY>")

	CheckErrorStatus("PrintResourceGallery")
End Sub

Sub PrintResourceGalleryItem(theResourceGalleryItem, nIndentDepth)

	Err.Clear
	On Error Resume Next

	CheckErrorStatus("PrintResourceGalleryItem")

	Call Indent(nIndentDepth)

	CheckErrorStatus("PrintResourceGalleryItem")

	Call WriteLn("<RESOURCEGALLERYITEM>")

	CheckErrorStatus("PrintResourceGalleryItem")

	Call WriteTitle(theResourceGalleryItem, nIndentDepth+1)

	CheckErrorStatus("PrintResourceGalleryItem")

	Call WriteLn("<LASTMOD>" + ConvertDateToISO(theResourceGalleryItem.LastModifiedDate) + "</LASTMOD>")
	Call WriteLn("<GUID>" + theResourceGalleryItem.GUID  + "</GUID>")
	Call WriteLn("<HREF>" + Server.URLEncode(theResourceGalleryItem.URL) + "</HREF>")

	Call Indent(nIndentDepth)

	CheckErrorStatus("PrintResourceGalleryItem")

	Call WriteLn("</RESOURCEGALLERYITEM>")

	CheckErrorStatus("PrintResourceGalleryItem")
End Sub

'============================================================================

Sub WriteTitle(theObject, nIndentDepth)
	Dim strTitle

	Err.Clear
	On Error Resume Next

	strTitle = theObject.Name

	CheckErrorStatus("WriteTitle")

	strTitle = Server.URLEncode(strTitle)'what are the consequences of changing HTMLEncode to URLEncode?

	CheckErrorStatus("WriteTitle")

	Indent(nIndentDepth)
	WriteLn("<TITLE>" & strTitle & "</TITLE>")

	CheckErrorStatus("WriteTitle")

End Sub

'============================================================================

Sub Write_Channel_Title(theChannel, nIndentDepth)
	Err.Clear
	On Error Resume Next

	CheckErrorStatus("Write_Channel_Title")

	Indent(nIndentDepth)

	WriteLn("<TITLE>" & Server.URLEncode(theChannel.Name) & "</TITLE>")

	CheckErrorStatus("Write_Channel_Title")
End Sub

Sub Write_Channel_HREF(theChannel, nIndentDepth)
	Err.Clear
	On Error Resume Next

	CheckErrorStatus("Write_Channel_HREF")

	Indent(nIndentDepth)

	WriteLn("<HREF>" & Server.URLEncode(theChannel.URL) & "</HREF>")

	CheckErrorStatus("Write_Channel_HREF")
End Sub

Sub Write_Channel_LASTMOD(theChannel, nIndentDepth)
	Err.Clear
	On Error Resume Next

	CheckErrorStatus("Write_Channel_LASTMOD")

	Indent(nIndentDepth)

	WriteLn("<LASTMOD>" & ConvertDateToISO(theChannel.LastModifiedDate) & "</LASTMOD>")

	CheckErrorStatus("Write_Channel_LASTMOD")
End Sub

Sub Write_Channel_GUID(theChannel, nIndentDepth)
	Err.Clear
	On Error Resume Next

	CheckErrorStatus("Write_Channel_GUID")

	Indent(nIndentDepth)

	WriteLn("<GUID>" & theChannel.GUID & "</GUID>")

	CheckErrorStatus("Write_Channel_GUID")
End Sub

'============================================================================

Sub Write_Posting_Title(thePosting, nIndentDepth)
	Err.Clear
	On Error Resume Next

	CheckErrorStatus("Write_Posting_Title")

	Indent(nIndentDepth)

	WriteLn("<TITLE>" & Server.URLEncode(thePosting.Name) & "</TITLE>")

	CheckErrorStatus("Write_Posting_Title")
End Sub

Sub Write_Posting_HREF(thePosting, nIndentDepth)
	Err.Clear
	On Error Resume Next

	CheckErrorStatus("Write_Posting_HREF")

	Indent(nIndentDepth)

	WriteLn("<HREF>" & Server.URLEncode(thePosting.URL) & "</HREF>")

	CheckErrorStatus("Write_Posting_HREF")
End Sub

Sub Write_Posting_LASTMOD(thePosting, nIndentDepth)
	Err.Clear
	On Error Resume Next

	CheckErrorStatus("Write_Posting_LASTMOD")

	Indent(nIndentDepth)

	WriteLn("<LASTMOD>" & ConvertDateToISO(thePosting.LastModifiedDate) & "</LASTMOD>")

	CheckErrorStatus("Write_Posting_LASTMOD")
End Sub

Sub Write_Posting_GUID(thePosting, nIndentDepth)
	Err.Clear
	On Error Resume Next

	CheckErrorStatus("Write_Posting_GUID")

	Indent(nIndentDepth)

	WriteLn("<GUID>" & thePosting.GUID & "</GUID>")

	CheckErrorStatus("Write_Posting_GUID")
End Sub

'============================================================================
' various html utils
'============================================================================

Function TwoDigitString(str)
	' takes a string that is assumed to be either 1 or 2 chars long.
	' If it is one char long then "0" is prepended to it.

	If Len(str) = 1 Then str = "0" & str
	TwoDigitString = str
End Function

Function ConvertDateToISO(SomeDate)
	' Converts to ISO 8601:1988
	' ex: The date "14 February 1993, 13:10:30" is represented as "19930214T131030" or "1993-02-14T13:10:30"

	Dim strISO
	strISO = Year(SomeDate) & "-" & TwoDigitString(Month(SomeDate)) & "-" & TwoDigitString(Day(SomeDate)) _
		& "T" & TwoDigitString(Hour(SomeDate)) & ":" & TwoDigitString(Minute(SomeDate)) & ":" & TwoDigitString(Second(SomeDate))

	ConvertDateToISO = strISO
End Function

%>