Capture PRINT statements to reuse LS Agent in XPages - Part 1/3


Option Public
Option Declare

Public Class ResultHandler
    Private out As NotesStream 'This is where the result goes
    Private m_fieldName As String 'The field name for the result
    Private m_noPrintMessage as String 'The message if the agent didn't print anything
    Private s As NotesSession
    Private doc As NotesDocument 'The document handed over by XPages
    
    Public Sub New
        Set me.s = New NotesSession
        Set me.doc = s.Documentcontext
        me.m_fieldName = "AgentPrintOutputResult"
        me.m_noPrintMessage = "[NoPrintOutput]"
    End Sub
    
    Public Sub prt(msg As String)
        If out Is Nothing Then
            Set out = s.Createstream()
        End if
        Call out.Writetext(msg, EOL_PLATFORM)
    End Sub
    
    Public Property Set fieldName As String
        me.m_fieldName = fieldName
    End Property

    Public Property Set noPrintMessage As String
        me.m_noPrintMessage = noPrintMessage
    End Property
    
    Public Sub save
        Dim mimeFlag As Boolean
        Dim mimeEntry As NotesMIMEEntity
        
        'We need to make sure we don't have the item in the document
        If me.doc.Hasitem(me.m_fieldName) Then
            Call me.doc.Removeitem(me.m_fieldName)
        End If
        
        'Set mime conversion to false - we want native
        mimeFlag = me.s.Convertmime
        me.s.Convertmime = False

        'There might not be a print output
        If me.out Is Nothing then
            Call me.agentDidNotPrintAnything()
        End If

        
        'Now write out
        me.out.Position = 0 'Make sure we write everything
        Set mimeEntry = doc.Createmimeentity(me.m_fieldName)
        Call mimeEntry.Setcontentfromtext(me.out,"text/plain;charset=UTF-8" ,ENC_NONE)
        Call me.doc.Closemimeentities(true, me.m_fieldName)
        
        'Close stream
        Call me.out.Close()
        Set me.out = Nothing 'If we call it again in prt it gets reinitialized
        
        'Restore mime conversion to what it was
        me.s.Convertmime = mimeFlag
    End Sub

    Private Sub agentDidNotPrintAnything
        Set out = s.Createstream()
        Call out.Writetext(me.m_noPrintMessage, EOL_PLATFORM)
    End Sub
    
End Class

%REM
    Sample agent use
%END REM

Option Public
Option Declare

Use "AgentSupport"

Dim result As ResultHandler

Sub Initialize
    Set result = New ResultHandler
    
    result.prt "Some message"
    result.prt "<h1> a header </h1>"
    
    Call result.save()
    
End Sub
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...