NotesItemWrapper

'*** Constants ***
Private Const ITEM_MAX_SIZE = 31000


Public Class NotesItemWrapper
	'declarations
	Private pDoc As NotesDocument
	Private pCurrentItem As NotesItem
	Private pItemNames As Variant
	Private pItemIndex As Integer

	'/**
	' * Constructor.
	' */
	Public Sub New(doc As NotesDocument, item_names As Variant, append_data As Boolean)
		'declarations
		Dim i As Integer

		'store the document
		Set Me.pDoc = doc

		'item_names must be an array of strings or a item
		Forall n In item_names
			If Typename(n)  "STRING" Then
				Error 9999, "The supplied item names but be a string or an array of strings."
			End If
		End Forall

		'make sure the item names are an array
		If Not Isarray(item_names) Then
			Dim v(0) As String
			v(0) = Cstr(item_names)
			Me.pItemNames = v
		Else
			'store the item names to use
			Me.pItemNames = item_names
		End If

		'get the first item to use
		If Not append_data Then
			'remove all the items with the supplied names and recreate the first one
			Forall n In Me.pItemNames
				Call Me.pDoc.RemoveItem(n)
			End Forall

			'recreate the first item
			Set Me.pCurrentItem = New NotesItem(Me.pDoc, Me.pItemNames(Me.pItemIndex), "")
		Else
			'find the first item to use
			For i=0 To Ubound(Me.pItemNames)
				'get the first item
				Set Me.pCurrentItem = Me.pDoc.GetFirstItem(Me.pItemNames(Me.pItemIndex))

				'is this item below the threshold
				If Lenb(Me.pCurrentItem.Text) < ITEM_MAX_SIZE Then
					'use this item
					Me.pItemIndex = i
					Exit For
				End If
			Next
		End If
	End Sub

	'/**
	' * Appends text to the item checking whether a new item
	' * is required to hold the text. If no more fields are
	' * available we return an Error 9999.
	' */
	Public Sub AppendText(data As String)
		'check whether there is space for the data
		If Me.pCheckSize(data) Then
			'it is ok - append
			Me.pCurrentItem.Text = Me.pCurrentItem.Text + data
		Else
			'raise error
			Error 9999, "Not enought space to hold the data."
		End If
	End Sub

	'/**
	' * Appends a multi-value item to the item(s). If no more space
	' * is available we return an Error 9999.
	' */
	Public Sub AppendToTextList(data As String)
		'check whether there is space for the data
		If Me.pCheckSize(data) Then
			'it is ok - append
			Call Me.pCurrentItem.AppendToTextList(data)
		Else
			'raise error
			Error 9999, "Not enought space to hold the data."
		End If
	End Sub

	'*****************************************************************************
	'* Private methods
	'*****************************************************************************

	'/**
	' * Checks the size of the current item. Returns True if the item
	' * can hold the supplied data.
	' */
	Private Function pCheckSize(data As String) As Boolean
		'declarations
		Dim size As Long

		'calculate the item size
		size = Lenb(Me.pCurrentItem.Text) + Lenb(data)
		'Print "Size is: " & size & " (index = " & Me.pItemIndex & ", " & Me.pCurrentItem.Name & ")"

		'decide what to do
		If size < ITEM_MAX_SIZE Then
			'item can hold the byte count
			pCheckSize = True
		Else
			'no can do - try and get the next item
			If Me.pItemIndex <= Ubound(Me.pItemNames) Then
				'get the next item
				Me.pItemIndex = Me.pItemIndex + 1
				Set Me.pCurrentItem = New NotesItem(Me.pDoc, Me.pItemNames(Me.pItemIndex), "")

				'return true
				pCheckSize = True
			Else
				'no more items
				pCheckSize = False
			End If
		End If
	End Function

End Class